undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
419
test/jdk/java/security/spec/ECCBasic.java
Normal file
419
test/jdk/java/security/spec/ECCBasic.java
Normal file
|
|
@ -0,0 +1,419 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 4794996
|
||||
* @summary Ensure basic functionality of these new ECC classes.
|
||||
* @author Valerie Peng
|
||||
*/
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.security.*;
|
||||
import java.security.spec.*;
|
||||
|
||||
public class ECCBasic {
|
||||
private static final BigInteger ZERO = BigInteger.ZERO;
|
||||
private static final BigInteger ONE = BigInteger.ONE;
|
||||
private static final BigInteger TEN = BigInteger.TEN;
|
||||
private static final ECFieldFp FP = new ECFieldFp(TEN);
|
||||
private static final int F2M_M = 4;
|
||||
private static final int[] F2M_KS;
|
||||
static {
|
||||
F2M_KS = new int[1];
|
||||
F2M_KS[0] = 1;
|
||||
}
|
||||
private static final BigInteger F2M_RP = BigInteger.valueOf(19);
|
||||
private static final ECFieldF2m F2M = new ECFieldF2m(F2M_M, F2M_KS);
|
||||
private static final EllipticCurve CURVE = new EllipticCurve
|
||||
(new ECFieldFp(TEN), ONE, ONE);
|
||||
private static final ECPoint POINT = new ECPoint(ONE, TEN);
|
||||
private static final BigInteger ORDER = ONE;
|
||||
private static final int COFACTOR = 3;
|
||||
private static final ECParameterSpec PARAMS = new ECParameterSpec
|
||||
(CURVE, POINT, ORDER, COFACTOR);
|
||||
private static final ECGenParameterSpec GENPARAMS =
|
||||
new ECGenParameterSpec("prime192v1");
|
||||
private static final ECPrivateKeySpec PRIV_KEY = new ECPrivateKeySpec
|
||||
(TEN, PARAMS);
|
||||
private static final ECPublicKeySpec PUB_KEY = new ECPublicKeySpec
|
||||
(POINT, PARAMS);
|
||||
|
||||
private static void testECFieldFp() throws Exception {
|
||||
System.out.println("Testing ECFieldFp(BigInteger)");
|
||||
try {
|
||||
new ECFieldFp(ZERO);
|
||||
throw new Exception("...should throw IAE");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("...expected IAE thrown");
|
||||
}
|
||||
try {
|
||||
new ECFieldFp(null);
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
if (TEN.equals(FP.getP()) == false) {
|
||||
throw new Exception("...error in getP()");
|
||||
}
|
||||
if (FP.getFieldSize() != TEN.bitLength()) {
|
||||
throw new Exception("...error in getFieldSize()");
|
||||
}
|
||||
}
|
||||
private static void testECFieldF2m() throws Exception {
|
||||
// invalid parameters
|
||||
int mBad = 0;
|
||||
int[] ksBad;
|
||||
for (int i=0; i<7; i++) {
|
||||
System.out.print("Testing ECFieldF2m");
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("(int)");
|
||||
new ECFieldF2m(mBad);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("(int, BigInteger)#1");
|
||||
new ECFieldF2m(mBad, F2M_RP);
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("(int, BigInteger)#2");
|
||||
new ECFieldF2m(mBad, F2M_RP);
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("(int, int[])#1");
|
||||
new ECFieldF2m(mBad, F2M_KS);
|
||||
break;
|
||||
case 4:
|
||||
ksBad = new int[2];
|
||||
System.out.println("(int, int[])#2");
|
||||
new ECFieldF2m(F2M_M, ksBad);
|
||||
break;
|
||||
case 5:
|
||||
ksBad = new int[1];
|
||||
ksBad[0] = 5;
|
||||
System.out.println("(int, int[])#3");
|
||||
new ECFieldF2m(F2M_M, ksBad);
|
||||
break;
|
||||
case 6:
|
||||
ksBad = new int[3];
|
||||
ksBad[0] = 1;
|
||||
ksBad[1] = 2;
|
||||
ksBad[2] = 3;
|
||||
System.out.println("(int, int[])#4");
|
||||
new ECFieldF2m(F2M_M, ksBad);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw IAE");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("...expected IAE thrown");
|
||||
}
|
||||
}
|
||||
for (int i=0; i<2; i++) {
|
||||
System.out.print("Testing ECFieldF2m");
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("(int, BigInteger)");
|
||||
new ECFieldF2m(F2M_M, (BigInteger) null);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("(int, int[])#1");
|
||||
new ECFieldF2m(F2M_M, (int[]) null);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
}
|
||||
if (F2M_RP.compareTo(F2M.getReductionPolynomial()) != 0) {
|
||||
throw new Exception("...error in getReductionPolynomial()");
|
||||
}
|
||||
ECFieldF2m field = new ECFieldF2m(F2M_M, F2M_RP);
|
||||
if (!(Arrays.equals(F2M_KS,
|
||||
field.getMidTermsOfReductionPolynomial()))) {
|
||||
throw new Exception("...error in getMidTermsOfReductionPolynomial()");
|
||||
}
|
||||
if (field.getFieldSize() != F2M_M) {
|
||||
throw new Exception("...error in getFieldSize()");
|
||||
}
|
||||
}
|
||||
private static void testECParameterSpec() throws Exception {
|
||||
System.out.println("Testing ECParameterSpec(...)");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("with zero order");
|
||||
new ECParameterSpec(CURVE, POINT, ZERO, COFACTOR);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("with negative cofactor");
|
||||
new ECParameterSpec(CURVE, POINT, ORDER, -1);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw IAE");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("...expected IAE thrown");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("with null curve");
|
||||
new ECParameterSpec(null, POINT, ORDER, COFACTOR);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("with null generator");
|
||||
new ECParameterSpec(CURVE, null, ORDER, COFACTOR);
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("with null order");
|
||||
new ECParameterSpec(CURVE, POINT, null, COFACTOR);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
}
|
||||
if (!(CURVE.equals(PARAMS.getCurve()))) {
|
||||
throw new Exception("...error in getCurve()");
|
||||
}
|
||||
if (!(POINT.equals(PARAMS.getGenerator()))) {
|
||||
throw new Exception("...error in getGenerator()");
|
||||
}
|
||||
if (!(ORDER.equals(PARAMS.getOrder()))) {
|
||||
throw new Exception("...error in getOrder()");
|
||||
}
|
||||
if (COFACTOR != PARAMS.getCofactor()) {
|
||||
throw new Exception("...error in getCofactor()");
|
||||
}
|
||||
}
|
||||
private static void testECGenParameterSpec() throws Exception {
|
||||
System.out.println("Testing ECGenParameterSpec(String)");
|
||||
try {
|
||||
new ECGenParameterSpec(null);
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
if (!("prime192v1".equals(GENPARAMS.getName()))) {
|
||||
throw new Exception("...error in getName()");
|
||||
}
|
||||
}
|
||||
private static void testECPoint() throws Exception {
|
||||
System.out.println("Testing ECParameterSpec(...)");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("with null x-coordinate");
|
||||
new ECPoint(null, TEN);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("with null y-coordinate");
|
||||
new ECPoint(ONE, null);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
}
|
||||
if (!(ONE.equals(POINT.getAffineX()))) {
|
||||
throw new Exception("...error in getAffineX()");
|
||||
}
|
||||
if (!(TEN.equals(POINT.getAffineY()))) {
|
||||
throw new Exception("...error in getAffineY()");
|
||||
}
|
||||
}
|
||||
private static void testECPublicKeySpec() throws Exception {
|
||||
System.out.println("Testing ECPublicKeySpec(...)");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("with null public value");
|
||||
new ECPublicKeySpec(null, PARAMS);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("with null params");
|
||||
new ECPublicKeySpec(POINT, null);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
}
|
||||
if (!(POINT.equals(PUB_KEY.getW()))) {
|
||||
throw new Exception("...error in getW()");
|
||||
}
|
||||
if (!(PARAMS.equals(PUB_KEY.getParams()))) {
|
||||
throw new Exception("...error in getParams()");
|
||||
}
|
||||
}
|
||||
private static void testECPrivateKeySpec() throws Exception {
|
||||
System.out.println("Testing ECPrivateKeySpec(...)");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
System.out.println("with null private value");
|
||||
new ECPrivateKeySpec(null, PARAMS);
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("with null param");
|
||||
new ECPrivateKeySpec(ONE, null);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
}
|
||||
if (!(TEN.equals(PRIV_KEY.getS()))) {
|
||||
throw new Exception("...error in getS()");
|
||||
}
|
||||
if (!(PARAMS.equals(PRIV_KEY.getParams()))) {
|
||||
throw new Exception("...error in getParams()");
|
||||
}
|
||||
}
|
||||
private static void testEllipticCurve() throws Exception {
|
||||
System.out.println("Testing EllipticCurve(...)");
|
||||
for (int j = 0; j < 2; j++) {
|
||||
BigInteger a = ONE;
|
||||
BigInteger b = ONE;
|
||||
if (j == 0) { // test a out of range
|
||||
System.out.println("with a's value out of range");
|
||||
a = BigInteger.valueOf(20);
|
||||
} else { // test b out of range
|
||||
System.out.println("with b's value out of range");
|
||||
b = BigInteger.valueOf(20);
|
||||
}
|
||||
System.out.println(">> over ECFieldFp");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
new EllipticCurve(FP, a, b);
|
||||
break;
|
||||
case 1:
|
||||
new EllipticCurve(FP, a, b, null);
|
||||
break;
|
||||
case 2:
|
||||
new EllipticCurve(FP, a, b, new byte[8]);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw IAE");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("...expected IAE thrown for #" + (i+1));
|
||||
}
|
||||
}
|
||||
System.out.println(">> over ECFieldF2m");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
new EllipticCurve(F2M, a, b);
|
||||
break;
|
||||
case 1:
|
||||
new EllipticCurve(F2M, a, b, null);
|
||||
break;
|
||||
case 2:
|
||||
new EllipticCurve(F2M, a, b, new byte[8]);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw IAE");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
System.out.println("...expected IAE thrown for #" + (i+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
try {
|
||||
switch (i) {
|
||||
case 0:
|
||||
new EllipticCurve(null, ONE, TEN);
|
||||
break;
|
||||
case 1:
|
||||
new EllipticCurve(FP, null, TEN);
|
||||
break;
|
||||
case 2:
|
||||
new EllipticCurve(FP, ONE, null);
|
||||
break;
|
||||
case 3:
|
||||
new EllipticCurve(null, ONE, TEN, null);
|
||||
break;
|
||||
case 4:
|
||||
new EllipticCurve(FP, null, TEN, null);
|
||||
break;
|
||||
case 5:
|
||||
new EllipticCurve(FP, ONE, null, null);
|
||||
break;
|
||||
}
|
||||
throw new Exception("...should throw NPE");
|
||||
} catch (NullPointerException npe) {
|
||||
System.out.println("...expected NPE thrown");
|
||||
}
|
||||
}
|
||||
if (!(FP.equals(CURVE.getField()))) {
|
||||
throw new Exception("...error in getField()");
|
||||
}
|
||||
if (!(ONE.equals(CURVE.getA()))) {
|
||||
throw new Exception("...error in getA()");
|
||||
}
|
||||
if (!(ONE.equals(CURVE.getB()))) {
|
||||
throw new Exception("...error in getB()");
|
||||
}
|
||||
if (!(CURVE.equals(new EllipticCurve(FP, ONE, ONE, null)))) {
|
||||
throw new Exception("...error in equals()");
|
||||
}
|
||||
}
|
||||
private static void testAllHashCode() throws Exception {
|
||||
// make sure no unexpected exception when hashCode() is called.
|
||||
FP.hashCode();
|
||||
F2M.hashCode();
|
||||
GENPARAMS.hashCode();
|
||||
PARAMS.hashCode();
|
||||
POINT.hashCode();
|
||||
PRIV_KEY.hashCode();
|
||||
PUB_KEY.hashCode();
|
||||
CURVE.hashCode();
|
||||
}
|
||||
public static void main(String[] argv) throws Exception {
|
||||
testECFieldFp();
|
||||
testECFieldF2m();
|
||||
testECParameterSpec();
|
||||
testECGenParameterSpec();
|
||||
testECPoint();
|
||||
testECPrivateKeySpec();
|
||||
testECPublicKeySpec();
|
||||
testEllipticCurve();
|
||||
testAllHashCode();
|
||||
System.out.println("All Test Passed");
|
||||
}
|
||||
}
|
||||
84
test/jdk/java/security/spec/EllipticCurveMatch.java
Normal file
84
test/jdk/java/security/spec/EllipticCurveMatch.java
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 6738532
|
||||
* @summary Check EllipticCurve.equals() does not compare seed value of curve.
|
||||
* @author Mike StJohns
|
||||
* @key randomness
|
||||
*/
|
||||
|
||||
import java.security.spec.*;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
public class EllipticCurveMatch {
|
||||
static String primeP256 =
|
||||
"0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF";
|
||||
static String aP256 =
|
||||
"0FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC";
|
||||
static String bP256 =
|
||||
"05AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B";
|
||||
static String seedP256 =
|
||||
"0C49D360886E704936A6678E1139D26B7819F7E90";
|
||||
|
||||
private static EllipticCurve addSeedToCurve(EllipticCurve curve)
|
||||
{
|
||||
Random rand = new Random();
|
||||
byte[] seed = new byte[12];
|
||||
rand.nextBytes(seed);
|
||||
|
||||
return new EllipticCurve (curve.getField(), curve.getA(), curve.getB(),
|
||||
seed);
|
||||
}
|
||||
|
||||
private static EllipticCurve getP256Curve()
|
||||
{
|
||||
ECFieldFp field = new ECFieldFp(new BigInteger (primeP256,16));
|
||||
BigInteger a = new BigInteger (aP256, 16);
|
||||
BigInteger b = new BigInteger (bP256, 16);
|
||||
|
||||
return new EllipticCurve (field, a, b);
|
||||
}
|
||||
|
||||
public static void main (String[] argv) throws Exception {
|
||||
EllipticCurve firstCurve = getP256Curve();
|
||||
EllipticCurve secondCurve = addSeedToCurve(firstCurve);
|
||||
EllipticCurve thirdCurve = addSeedToCurve(firstCurve);
|
||||
|
||||
if (!firstCurve.equals(firstCurve))
|
||||
throw new Exception("Original curve doesn't equal itself");
|
||||
|
||||
if (!firstCurve.equals(secondCurve))
|
||||
throw new Exception ("Original curve doesn't equal seeded curve");
|
||||
|
||||
if (!secondCurve.equals(secondCurve))
|
||||
throw new Exception ("Seeded curve doesn't equal itself");
|
||||
|
||||
if (!secondCurve.equals(thirdCurve))
|
||||
throw new Exception ("Seeded curve doesn't equal differently " +
|
||||
"seeded curve");
|
||||
System.out.println("Curve equals test passed");
|
||||
}
|
||||
}
|
||||
117
test/jdk/java/security/spec/IsAssignableFromOrder.java
Normal file
117
test/jdk/java/security/spec/IsAssignableFromOrder.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import sun.security.util.CurveDB;
|
||||
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.DESKeySpec;
|
||||
import javax.crypto.spec.DESedeKeySpec;
|
||||
import javax.crypto.spec.DHParameterSpec;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.OAEPParameterSpec;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import javax.crypto.spec.PBEParameterSpec;
|
||||
import javax.crypto.spec.RC2ParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.AlgorithmParameters;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.security.spec.DSAParameterSpec;
|
||||
import java.security.spec.ECGenParameterSpec;
|
||||
import java.security.spec.ECParameterSpec;
|
||||
import java.security.spec.KeySpec;
|
||||
import java.security.spec.PSSParameterSpec;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8279800
|
||||
* @modules java.base/sun.security.util
|
||||
* @summary isAssignableFrom checks in AlgorithmParametersSpi.engineGetParameterSpec appear to be backwards
|
||||
*/
|
||||
|
||||
public class IsAssignableFromOrder {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// AlgorithmParameters
|
||||
testAlgSpec("AES", new IvParameterSpec(new byte[16]));
|
||||
testAlgSpec("ChaCha20-Poly1305", new IvParameterSpec(new byte[12]));
|
||||
testAlgSpec("DiffieHellman", new DHParameterSpec(BigInteger.ONE, BigInteger.TWO));
|
||||
testAlgSpec("GCM", new GCMParameterSpec(96, new byte[16]));
|
||||
testAlgSpec("OAEP", OAEPParameterSpec.DEFAULT);
|
||||
testAlgSpec("PBEWithSHA1AndDESede", new PBEParameterSpec(
|
||||
"saltsalt".getBytes(StandardCharsets.UTF_8), 10000));
|
||||
testAlgSpec("PBEWithHmacSHA256AndAES_256", new PBEParameterSpec(
|
||||
"saltsalt".getBytes(StandardCharsets.UTF_8), 10000,
|
||||
new IvParameterSpec(new byte[16])));
|
||||
testAlgSpec("RC2", new RC2ParameterSpec(256, new byte[32]));
|
||||
testAlgSpec("DSA", new DSAParameterSpec(
|
||||
BigInteger.ONE, BigInteger.TWO, BigInteger.TEN));
|
||||
testAlgSpec("RSASSA-PSS", PSSParameterSpec.DEFAULT);
|
||||
testAlgSpec("EC", new ECGenParameterSpec("secp256r1"));
|
||||
testAlgSpec("EC", CurveDB.lookup("secp256r1"),
|
||||
ECParameterSpec.class, AlgorithmParameterSpec.class);
|
||||
|
||||
// SecretKeyFactory
|
||||
var spec = new PBEKeySpec(
|
||||
"password".toCharArray(),
|
||||
"saltsalt".getBytes(StandardCharsets.UTF_8),
|
||||
10000,
|
||||
32);
|
||||
|
||||
testKeySpec("PBE", spec, PBEKeySpec.class);
|
||||
testKeySpec("PBEWithHmacSHA256AndAES_256", spec, PBEKeySpec.class);
|
||||
testKeySpec("PBKDF2WithHmacSHA1", spec, PBEKeySpec.class);
|
||||
|
||||
testKeySpec("DES", new SecretKeySpec(new byte[8], "DES"), DESKeySpec.class);
|
||||
testKeySpec("DESede", new SecretKeySpec(new byte[24], "DESede"), DESedeKeySpec.class);
|
||||
}
|
||||
|
||||
|
||||
static void testAlgSpec(String algorithm, AlgorithmParameterSpec spec,
|
||||
Class<? extends AlgorithmParameterSpec>... classes) throws Exception {
|
||||
System.out.println(algorithm);
|
||||
var ap1 = AlgorithmParameters.getInstance(algorithm);
|
||||
ap1.init(spec);
|
||||
var ap2 = AlgorithmParameters.getInstance(algorithm);
|
||||
ap2.init(ap1.getEncoded());
|
||||
if (classes.length == 0) {
|
||||
classes = new Class[]{spec.getClass(), AlgorithmParameterSpec.class};
|
||||
}
|
||||
for (var c : classes) {
|
||||
ap1.getParameterSpec(c);
|
||||
ap2.getParameterSpec(c);
|
||||
}
|
||||
}
|
||||
|
||||
static void testKeySpec(String algorithm, KeySpec spec, Class<?> clazz)
|
||||
throws Exception {
|
||||
System.out.println(algorithm);
|
||||
var kf = SecretKeyFactory.getInstance(algorithm);
|
||||
var key = kf.generateSecret(spec);
|
||||
kf.getKeySpec(key, KeySpec.class);
|
||||
kf.getKeySpec(key, clazz);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8047223
|
||||
* @summary Add algorithm parameter to PKCS8EncodedKeySpec class
|
||||
*/
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
import javax.crypto.EncryptedPrivateKeyInfo;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
|
||||
public class Algorithm {
|
||||
|
||||
private static String PKCS8PrivateKey =
|
||||
"MIICoTAbBgkqhkiG9w0BBQMwDgQIqQMPwbNEhOgCAggABIICgCwRkeLXVGdO7S1h\n" +
|
||||
"FAFUiwj1HCzqYFF2x9+FzjlXNwEWecZsor5eoKQlTtJ9dsPajQ/wFgY76lkXDQXE\n" +
|
||||
"hdm8ndWFgCwqFBshmAp4TOvO9GlaAloDTnLMUg715D5FujiElcV7vqIY2V/7uB21\n" +
|
||||
"YRanKUa21sZAFJGj6Hom1+5+k0Q7Xi4kHgt+ZIPNLwrNFPWVovbTJdScZuJaDp6m\n" +
|
||||
"Q1DJUIQOzthV11VI+MU/v5SSKhj/uCaxizazEi5lgdmR7rRGgMz2YipOIjXIsKgu\n" +
|
||||
"jKX5LYFAZ8nYq1hy8Q1JPR5VPuWMFqeyofO/teXJb8gI/4TC1ZoED8hXj07jpJqG\n" +
|
||||
"2NVO1Dwqab31qSAjfjBkSYHKun63BvZPq2mT+frJF1YzvQhCDnWN1zbMKFNTZJfd\n" +
|
||||
"cUaecH/fgNKwKpeKGgX7UlWxo26/lS8pBiJ5ihtbyFfMUBtlwEN5uOHqVFOeZp1Z\n" +
|
||||
"DwCc0o1JA7yOcazA2TtNT9pc58tFZ8pEeyLj7ZchOgv06N0hZJsI6AiwII4ljd+K\n" +
|
||||
"4WKvs/xiSZU3tcHaWzqlf+6/M5kC3Pihm9GhZbKBmvrZYiKyTlJEeVI3pFRNSqbE\n" +
|
||||
"nZUJgkmgzNT/ZfM2WsUJm03Rq0eNCU/FDscIZnCWSA6Bf/DJDQWmhMhg2QmTGzQM\n" +
|
||||
"hw/vy77q7jxV67s36HGxxR1oe8uoZ2zugBBxHWEdqyQyrVwZXJukdjrc2S7pvMln\n" +
|
||||
"/VSleEf91MEcDhztyhPSqlX+H95vMnVmh5oY2gwY+P0oD5Eki6/9K+BHfuqgtS4S\n" +
|
||||
"LIna1iSyLr17pRO1lmNtvuCMwmUjeI8w3JhLmxxx//bl/WCAekqj3nMplrJHZ7xd\n" +
|
||||
"6k0Stxo=";
|
||||
|
||||
private static String keyAlg = "RSA";
|
||||
private static String password = "password";
|
||||
|
||||
/*
|
||||
* This test checks that a PKCS8EncodedKeySpec is properly constructed
|
||||
* from an encrypted private key and that the key algorithm name can be
|
||||
* retrieved as expected.
|
||||
*/
|
||||
public static void main(String[] argv) throws Exception {
|
||||
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
|
||||
Base64.getMimeDecoder().decode(PKCS8PrivateKey));
|
||||
PBEKeySpec pks = new PBEKeySpec(password.toCharArray());
|
||||
SecretKeyFactory skf = SecretKeyFactory.getInstance(epki.getAlgName());
|
||||
SecretKey sk = skf.generateSecret(pks);
|
||||
PKCS8EncodedKeySpec keySpec = epki.getKeySpec(sk);
|
||||
|
||||
// Get the key algorithm and make sure it's what we expect
|
||||
String alg = keySpec.getAlgorithm();
|
||||
if (!alg.equals(keyAlg)) {
|
||||
throw new Exception("Expected: " + keyAlg + ", Got: " + alg);
|
||||
}
|
||||
|
||||
System.out.println("Test passed");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8351113
|
||||
* @summary check for negative offset
|
||||
* @library /test/lib
|
||||
*/
|
||||
|
||||
import jdk.test.lib.Utils;
|
||||
import javax.crypto.spec.RC2ParameterSpec;
|
||||
|
||||
public class InvalidArrayIndex {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Utils.runAndCheckException(() -> new RC2ParameterSpec(0, new byte[20],
|
||||
Integer.MIN_VALUE), ArrayIndexOutOfBoundsException.class);
|
||||
}
|
||||
}
|
||||
69
test/jdk/java/security/spec/TestNamedParameterSpec.java
Normal file
69
test/jdk/java/security/spec/TestNamedParameterSpec.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8345057
|
||||
* @summary Test the existence of constants inside the
|
||||
* java.security.spec.NamedParameterSpec class.
|
||||
* @run main TestNamedParameterSpec
|
||||
*/
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.security.spec.NamedParameterSpec;
|
||||
import java.util.Arrays;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class TestNamedParameterSpec {
|
||||
|
||||
// names of the static fields in NamedParameterSpec class
|
||||
private static String[] EXPECTED = {
|
||||
"ED25519", "ED448", "X25519", "X448",
|
||||
"ML_DSA_44", "ML_DSA_65", "ML_DSA_87",
|
||||
"ML_KEM_512", "ML_KEM_768", "ML_KEM_1024",
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Arrays.sort(EXPECTED);
|
||||
var actual = getSortedConstNames(NamedParameterSpec.class);
|
||||
// both arrays should be sorted before comparison
|
||||
if (Arrays.compare(EXPECTED, actual) != 0) {
|
||||
System.out.println("expected: " + Arrays.toString(EXPECTED));
|
||||
System.out.println("actual: " + Arrays.toString(actual));
|
||||
throw new RuntimeException("Name check failed");
|
||||
}
|
||||
System.out.println("Test Passed");
|
||||
}
|
||||
|
||||
public static String[] getSortedConstNames(Class<?> clazz) {
|
||||
TreeSet<String> names = new TreeSet<String>();
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
int mods = field.getModifiers();
|
||||
if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
|
||||
&& Modifier.isFinal(mods)){
|
||||
names.add(field.getName());
|
||||
}
|
||||
}
|
||||
return names.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue