8265138: Simplify DerUtils::checkAlg

Reviewed-by: xuelei
This commit is contained in:
Weijun Wang 2021-04-13 16:56:36 +00:00
parent c7975113a0
commit 9cd5400d9b
3 changed files with 36 additions and 34 deletions
test
jdk/sun/security
lib/jdk/test/lib/security

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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
@ -236,8 +236,8 @@ public class ParamsPreferences {
checkAlg(data, "110c110110", certAlg);
if (certAlg == PBES2) {
checkAlg(data, "110c11011100", PBKDF2WithHmacSHA1);
checkAlg(data, "110c1101110130", args[i++]);
checkAlg(data, "110c11011110", args[i++]);
checkAlg(data, "110c1101110130", (KnownOIDs)args[i++]);
checkAlg(data, "110c11011110", (KnownOIDs)args[i++]);
checkInt(data, "110c110111011", (int) args[i++]);
} else {
checkInt(data, "110c1101111", (int) args[i++]);
@ -249,8 +249,8 @@ public class ParamsPreferences {
checkAlg(data, "110c010c01000", keyAlg);
if (keyAlg == PBES2) {
checkAlg(data, "110c010c0100100", PBKDF2WithHmacSHA1);
checkAlg(data, "110c010c010010130", args[i++]);
checkAlg(data, "110c010c0100110", args[i++]);
checkAlg(data, "110c010c010010130", (KnownOIDs)args[i++]);
checkAlg(data, "110c010c0100110", (KnownOIDs)args[i++]);
checkInt(data, "110c010c01001011", (int) args[i++]);
} else {
checkInt(data, "110c010c010011", (int) args[i++]);

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, 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
@ -74,18 +74,18 @@ public class GenerateAll {
@DataProvider(name = "eddsa")
public Object[][] eddsaData() {
return new Object[][]{
{"eddsa", null, "ed25519"},
{"eddsa", "eddsa", "ed25519"},
{"eddsa", "ed25519", "ed25519"},
{"eddsa", null, Ed25519},
{"eddsa", "eddsa", Ed25519},
{"eddsa", "ed25519", Ed25519},
{"eddsa", "ed448", null},
{"ed25519", null, "ed25519"},
{"ed25519", "eddsa", "ed25519"},
{"ed25519", "ed25519", "ed25519"},
{"ed25519", null, Ed25519},
{"ed25519", "eddsa", Ed25519},
{"ed25519", "ed25519", Ed25519},
{"ed25519", "ed448", null},
{"ed448", null, "ed448"},
{"ed448", "eddsa", "ed448"},
{"ed448", null, Ed448},
{"ed448", "eddsa", Ed448},
{"ed448", "ed25519", null},
{"ed448", "ed448", "ed448"},
{"ed448", "ed448", Ed448},
};
}
@ -96,7 +96,7 @@ public class GenerateAll {
* @param expected expected algorithm of generated signature
*/
@Test(dataProvider = "eddsa")
public void eddsaTest(String keyAlg, String sigAlg, String expected)
public void eddsaTest(String keyAlg, String sigAlg, KnownOIDs expected)
throws Exception {
String alias = keyAlg + "-" + sigAlg;
OutputAnalyzer oa = kt0("-genkeypair -alias " + alias
@ -177,19 +177,22 @@ public class GenerateAll {
sigAlg = SignatureUtil.getDefaultSigAlgForKey(pk);
}
KnownOIDs sigOID = KnownOIDs.findMatch(sigAlg);
KnownOIDs keyOID = KnownOIDs.findMatch(keyAlg);
byte[] crt = read(alias + ".self");
DerUtils.checkAlg(crt, "020", sigAlg); // tbsCertificate.signature
DerUtils.checkAlg(crt, "0600", keyAlg); // tbsCertificate.subjectPublicKeyInfo.algorithm
DerUtils.checkAlg(crt, "020", sigOID); // tbsCertificate.signature
DerUtils.checkAlg(crt, "0600", keyOID); // tbsCertificate.subjectPublicKeyInfo.algorithm
assertEquals(
DerUtils.innerDerValue(crt, "02"), // tbsCertificate.signature
DerUtils.innerDerValue(crt, "1")); // signatureAlgorithm
byte[] req = read(alias + ".req");
DerUtils.checkAlg(req, "10", sigAlg); // signatureAlgorithm
DerUtils.checkAlg(req, "0200", keyAlg); // certificationRequestInfo.subjectPKInfo.algorithm
DerUtils.checkAlg(req, "10", sigOID); // signatureAlgorithm
DerUtils.checkAlg(req, "0200", keyOID); // certificationRequestInfo.subjectPKInfo.algorithm
byte[] crl = read(alias + ".crl");
DerUtils.checkAlg(crl, "000", sigAlg); // tbsCertList.signature
DerUtils.checkAlg(crl, "000", sigOID); // tbsCertList.signature
assertEquals(
DerUtils.innerDerValue(crl, "00"), // tbsCertList.signature
DerUtils.innerDerValue(crl, "1")); // signatureAlgorithm

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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
@ -96,18 +96,17 @@ public class DerUtils {
* Ensures that the inner DerValue is the expected ObjectIdentifier.
*/
public static void checkAlg(byte[] der, String location,
Object expected) throws Exception {
ObjectIdentifier oid;
if (expected instanceof ObjectIdentifier) {
oid = (ObjectIdentifier)expected;
} else if (expected instanceof KnownOIDs) {
oid = ObjectIdentifier.of((KnownOIDs) expected);
} else if (expected instanceof String) {
oid = ObjectIdentifier.of(KnownOIDs.findMatch((String)expected));
} else {
throw new IllegalArgumentException(expected.toString());
}
Asserts.assertEQ(innerDerValue(der, location).getOID(), oid);
ObjectIdentifier expected) throws Exception {
Asserts.assertEQ(innerDerValue(der, location).getOID(), expected);
}
/**
* Ensures that the inner DerValue is the expected ObjectIdentifier.
*/
public static void checkAlg(byte[] der, String location,
KnownOIDs expected) throws Exception {
Asserts.assertEQ(innerDerValue(der, location).getOID(),
ObjectIdentifier.of(expected));
}
/**