Skip to content

Commit

Permalink
8265138: Simplify DerUtils::checkAlg
Browse files Browse the repository at this point in the history
Reviewed-by: xuelei
  • Loading branch information
wangweij committed Apr 13, 2021
1 parent c797511 commit 9cd5400
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
10 changes: 5 additions & 5 deletions test/jdk/sun/security/pkcs12/ParamsPreferences.java
@@ -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
Expand Down Expand Up @@ -236,8 +236,8 @@ static void test(int n, Map<String, ?> sysProps,
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++]);
Expand All @@ -249,8 +249,8 @@ static void test(int n, Map<String, ?> sysProps,
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++]);
Expand Down
35 changes: 19 additions & 16 deletions test/jdk/sun/security/tools/keytool/GenerateAll.java
@@ -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
Expand Down Expand Up @@ -74,18 +74,18 @@ public void beforeTest() throws Exception {
@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},
};
}

Expand All @@ -96,7 +96,7 @@ public Object[][] eddsaData() {
* @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
Expand Down Expand Up @@ -177,19 +177,22 @@ public void test(String alias, String keyAlg, String sigAlg, String ext,
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
Expand Down
25 changes: 12 additions & 13 deletions test/lib/jdk/test/lib/security/DerUtils.java
@@ -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
Expand Down Expand Up @@ -96,18 +96,17 @@ public static DerValue innerDerValue(byte[] data, String location)
* 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));
}

/**
Expand Down

1 comment on commit 9cd5400

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.