8370245: Change default PKCS12 mac algorithm to PBMAC1

Reviewed-by: mullan, weijun
This commit is contained in:
Mark Powers
2026-09-02 20:28:05 +00:00
parent edb410c466
commit 2d02c8eaac
6 changed files with 101 additions and 35 deletions
@@ -83,7 +83,7 @@ public final class PKCS12KeyStore extends KeyStoreSpi {
= "PBEWithHmacSHA256AndAES_256";
private static final String DEFAULT_KEY_PBE_ALGORITHM
= "PBEWithHmacSHA256AndAES_256";
private static final String DEFAULT_MAC_ALGORITHM = "HmacPBESHA256";
private static final String DEFAULT_MAC_ALGORITHM = "PBEWithHmacSHA256";
private static final int DEFAULT_CERT_PBE_ITERATION_COUNT = 10000;
private static final int DEFAULT_KEY_PBE_ITERATION_COUNT = 10000;
private static final int DEFAULT_MAC_ITERATION_COUNT = 10000;
@@ -1373,8 +1373,8 @@ jceks.key.serialFilter = java.base/java.lang.Enum;java.base/java.security.KeyRep
# file. This can be any HmacPBE<digest> or PBEWith<mac> algorithm defined in
# the Mac section of the Java Security Standard Algorithm Names Specification,
# for example, HmacPBESHA256 or PBEWithHmacSHA256. When set to "NONE",
# no Mac is generated. The default value is "HmacPBESHA256".
#keystore.pkcs12.macAlgorithm = HmacPBESHA256
# no Mac is generated. The default value is "PBEWithHmacSHA256".
#keystore.pkcs12.macAlgorithm = PBEWithHmacSHA256
# The iteration count used by the MacData algorithm. This value must be a
# positive integer. The default value is 10000.
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@@ -33,6 +34,9 @@ import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
/*
@@ -60,7 +64,9 @@ public final class ImportKeyToP12 extends PKCS11Test {
};
private static final String[] pbeMacAlgs = new String[] {
"HmacPBESHA1", "HmacPBESHA224", "HmacPBESHA256",
"HmacPBESHA384", "HmacPBESHA512"
"HmacPBESHA384", "HmacPBESHA512", "PBEWithHmacSHA1",
"PBEWithHmacSHA224", "PBEWithHmacSHA256", "PBEWithHmacSHA384",
"PBEWithHmacSHA512"
};
private static final KeyStore p12;
private static final String sep = "======================================" +
@@ -74,6 +80,14 @@ public final class ImportKeyToP12 extends PKCS11Test {
p12 = tP12;
}
private record PBMAC1Algorithms(String pbkdf2, String hmac) {}
private static PBMAC1Algorithms pbmac1Algorithms(String algorithm) {
// PBMAC1 algorithms with matching PBKDF2 PRF and HMAC names.
String hmac = algorithm.substring("PBEWith".length());
return new PBMAC1Algorithms("PBKDF2With" + hmac, hmac);
}
public void main(Provider sunPKCS11) throws Exception {
System.out.println("SunPKCS11: " + sunPKCS11.getName());
// Test all privacy PBE algorithms with an integrity algorithm fixed
@@ -84,8 +98,25 @@ public final class ImportKeyToP12 extends PKCS11Test {
}
// Test all integrity PBE algorithms with a privacy algorithm fixed
for (String pbeMacAlg : pbeMacAlgs) {
// Make sure that SunPKCS11 implements the Mac algorithm
Mac.getInstance(pbeMacAlg, sunPKCS11);
// Verify the PBKDF2/HMAC components needed for PBMAC1 are present
if (pbeMacAlg.startsWith("PBEWith")) {
byte[] salt = { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15 };
PBMAC1Algorithms algorithms = pbmac1Algorithms(pbeMacAlg);
Mac m = Mac.getInstance(algorithms.hmac(), sunPKCS11);
int keyLength = m.getMacLength() * Byte.SIZE;
SecretKeyFactory skf = SecretKeyFactory.getInstance(
algorithms.pbkdf2(), sunPKCS11);
PBEKeySpec keySpec = new PBEKeySpec(password,
salt, 10000, keyLength);
SecretKey pbeKey = skf.generateSecret(keySpec);
m.init(pbeKey);
m.doFinal();
} else {
Mac.getInstance(pbeMacAlg, sunPKCS11);
}
testWith(sunPKCS11, pbeCipherAlgs[0], pbeMacAlg);
}
System.out.println("TEST PASS - OK");
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@@ -182,8 +182,8 @@ public class KeytoolOpensslInteropTest {
+ "-destkeystore ksnormal -deststorepass changeit");
data = Files.readAllBytes(Path.of("ksnormal"));
checkInt(data, "22", 10000); // Mac ic
checkAlg(data, "2000", SHA_256); // Mac alg
checkInt(data, "2001011", 10000); // Mac ic
checkAlg(data, "2000", PBMAC1); // Mac alg
checkAlg(data, "110c010c01000", PBES2); // key alg
checkInt(data, "110c010c01001011", 10000); // key ic
checkAlg(data, "110c10", ENCRYPTED_DATA_OID);
@@ -210,8 +210,8 @@ public class KeytoolOpensslInteropTest {
+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE "
+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE");
data = Files.readAllBytes(Path.of("ksnormal"));
checkInt(data, "22", 10000); // Mac ic
checkAlg(data, "2000", SHA_256); // Mac alg
checkInt(data, "2001011", 10000); // Mac ic
checkAlg(data, "2000", PBMAC1); // Mac alg
checkAlg(data, "110c010c01000", PBES2); // key alg
checkInt(data, "110c010c01001011", 10000); // key ic
checkAlg(data, "110c010c11000", PBES2); // new key alg
@@ -255,8 +255,8 @@ public class KeytoolOpensslInteropTest {
+ "-J-Dkeystore.pkcs12.certPbeIterationCount=6666 "
+ "-J-Dkeystore.pkcs12.keyPbeIterationCount=7777");
data = Files.readAllBytes(Path.of("ksnewic"));
checkInt(data, "22", 5555); // Mac ic
checkAlg(data, "2000", SHA_256); // Mac alg
checkInt(data, "2001011", 5555); // Mac ic
checkAlg(data, "2000", PBMAC1); // Mac alg
checkAlg(data, "110c010c01000", PBES2); // key alg
checkInt(data, "110c010c01001011", 7777); // key ic
checkAlg(data, "110c110110", PBES2); // cert alg
@@ -274,8 +274,8 @@ public class KeytoolOpensslInteropTest {
+ "-storepass changeit -alias b -dname CN=B "
+ "-J-Dkeystore.pkcs12.keyProtectionAlgorithm=PBEWithSHA1AndRC4_128");
data = Files.readAllBytes(Path.of("ksnewic"));
checkInt(data, "22", 5555); // Mac ic
checkAlg(data, "2000", SHA_256); // Mac alg
checkInt(data, "2001011", 5555); // Mac ic
checkAlg(data, "2000", PBMAC1); // Mac alg
checkAlg(data, "110c010c01000", PBES2); // key alg
checkInt(data, "110c010c01001011", 7777); // key ic
checkAlg(data, "110c010c11000", PBEWithSHA1AndRC4_128); // new key alg
@@ -291,8 +291,8 @@ public class KeytoolOpensslInteropTest {
ks.store(fos, "changeit".toCharArray());
}
data = Files.readAllBytes(Path.of("ksnormaldup"));
checkInt(data, "22", 10000); // Mac ic
checkAlg(data, "2000", SHA_256); // Mac alg
checkInt(data, "2001011", 10000); // Mac ic
checkAlg(data, "2000", PBMAC1); // Mac alg
checkAlg(data, "110c010c01000", PBES2); // key alg
checkInt(data, "110c010c01001011", 10000); // key ic
checkAlg(data, "110c010c11000", PBES2); // new key alg
@@ -320,8 +320,8 @@ public class KeytoolOpensslInteropTest {
ks.store(fos, "changeit".toCharArray());
}
data = Files.readAllBytes(Path.of("ksnewicdup"));
checkInt(data, "22", 5555); // Mac ic
checkAlg(data, "2000", SHA_256); // Mac alg
checkInt(data, "2001011", 5555); // Mac ic
checkAlg(data, "2000", PBMAC1); // Mac alg
checkAlg(data, "110c010c01000", PBES2); // key alg
checkInt(data, "110c010c01001011", 7777); // key ic
checkAlg(data, "110c010c11000", PBEWithSHA1AndRC4_128); // new key alg
@@ -476,7 +476,7 @@ public class KeytoolOpensslInteropTest {
"pkcs12", "-in", "ksnormal", "-passin", "pass:changeit",
"-info", "-nokeys", "-nocerts");
output1.shouldHaveExitValue(0)
.shouldMatch("MAC:.*sha256.*Iteration 10000")
.shouldMatch("MAC:.*PBMAC1.*Iteration 10000")
.shouldContain("Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC,"
+ " Iteration 10000, PRF hmacWithSHA256")
.shouldContain("PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC,"
@@ -521,7 +521,7 @@ public class KeytoolOpensslInteropTest {
"ksnewic", "-passin", "pass:changeit", "-info", "-nokeys",
"-nocerts");
output1.shouldHaveExitValue(0)
.shouldMatch("MAC:.*sha256.*Iteration 5555")
.shouldMatch("MAC:.*PBMAC1.*Iteration 5555")
.shouldContain("Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC,"
+ " Iteration 7777, PRF hmacWithSHA256")
.shouldContain("Shrouded Keybag: pbeWithSHA1And128BitRC4,"
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@@ -54,7 +54,7 @@ public class ParamsPreferences {
Map.of(),
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
SHA_256, 10000);
PBMAC1, 10000);
// legacy settings
test(c++,
@@ -116,7 +116,33 @@ public class ParamsPreferences {
"keystore.pkcs12.macAlgorithm", "NONE"),
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
SHA_256, 10000);
PBMAC1, 10000);
// configure PBMAC1 MAC with system property
test(c++,
Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndDESede",
"keystore.pkcs12.certPbeIterationCount", 3000,
"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40",
"keystore.pkcs12.keyPbeIterationCount", 4000,
"keystore.pkcs12.macAlgorithm", "PBEWithHmacSHA256",
"keystore.pkcs12.macIterationCount", 2000),
Map.of(),
PBEWithSHA1AndDESede, 3000,
PBEWithSHA1AndRC2_40, 4000,
PBMAC1, 2000);
// configure PBMAC1 MAC with security property
test(c++,
Map.of(),
Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndDESede",
"keystore.pkcs12.certPbeIterationCount", 3000,
"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40",
"keystore.pkcs12.keyPbeIterationCount", 4000,
"keystore.pkcs12.macAlgorithm", "PBEWithHmacSHA256",
"keystore.pkcs12.macIterationCount", 2000),
PBEWithSHA1AndDESede, 3000,
PBEWithSHA1AndRC2_40, 4000,
PBMAC1, 2000);
// change everything with system property
test(c++,
@@ -170,21 +196,21 @@ public class ParamsPreferences {
Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128"),
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
PBEWithSHA1AndRC2_128, 10000,
SHA_256, 10000);
PBMAC1, 10000);
test(c++,
Map.of(),
Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128",
"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40"),
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
PBEWithSHA1AndRC2_40, 10000,
SHA_256, 10000);
PBMAC1, 10000);
test(c++,
Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC4_128"),
Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128",
"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40"),
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
PBEWithSHA1AndRC4_128, 10000,
SHA_256, 10000);
PBMAC1, 10000);
test(c++,
Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC4_128",
"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC4_40"),
@@ -192,7 +218,7 @@ public class ParamsPreferences {
"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40"),
PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,
PBEWithSHA1AndRC4_40, 10000,
SHA_256, 10000);
PBMAC1, 10000);
// 8266293
test(c++,
@@ -201,7 +227,7 @@ public class ParamsPreferences {
Map.of(),
PBEWithMD5AndDES, 10000,
PBEWithMD5AndDES, 10000,
SHA_256, 10000);
PBMAC1, 10000);
}
/**
@@ -269,6 +295,9 @@ public class ParamsPreferences {
KnownOIDs macAlg = (KnownOIDs)args[i++];
if (macAlg == null) {
shouldNotExist(data, "2");
} else if (macAlg.stdName().equals("PBMAC1")) {
checkAlg(data, "2000", macAlg);
checkInt(data, "2001011", (int) args[i++]);
} else {
checkAlg(data, "2000", macAlg);
checkInt(data, "22", (int) args[i++]);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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
@@ -43,6 +43,12 @@ import org.openjdk.jmh.annotations.*;
@BenchmarkMode(Mode.AverageTime)
@Fork(jvmArgs = {"-Xms1024m", "-Xmx1024m", "-Xmn768m", "-XX:+UseParallelGC"}, value = 3)
public class PKCS12KeyStores {
@Param({"false", "true"})
private boolean pbmac1;
private String macAlgorithm(String legacyAlgorithm) {
return pbmac1 ? "PBEWithHmacSHA256" : legacyAlgorithm;
}
private static final char[] PASS = "changeit".toCharArray();
@@ -151,14 +157,14 @@ public class PKCS12KeyStores {
public byte[] outweak2048() throws Exception {
return out("PBEWithSHA1AndRC2_40", "2048",
"PBEWithSHA1AndDESede", "2048",
"HmacPBESHA1", "2048");
macAlgorithm("HmacPBESHA1"), "2048");
}
@Benchmark
public byte[] outweak50000_Old() throws Exception {
return out("PBEWithSHA1AndRC2_40", "50000",
"PBEWithSHA1AndDESede", "50000",
"HmacPBESHA1", "100000");
macAlgorithm("HmacPBESHA1"), "100000");
// Attention: 100000 is old default Mac ic
}
@@ -166,7 +172,7 @@ public class PKCS12KeyStores {
public byte[] outstrong50000() throws Exception {
return out("PBEWithHmacSHA256AndAES_256", "50000",
"PBEWithHmacSHA256AndAES_256", "50000",
"HmacPBESHA256", "100000");
macAlgorithm("HmacPBESHA256"), "100000");
// Attention: 100000 is old default Mac ic
}
@@ -174,13 +180,13 @@ public class PKCS12KeyStores {
public byte[] outstrong10000_New() throws Exception {
return out("PBEWithHmacSHA256AndAES_256", "10000",
"PBEWithHmacSHA256AndAES_256", "10000",
"HmacPBESHA256", "10000");
macAlgorithm("HmacPBESHA256"), "10000");
}
@Benchmark
public byte[] outstrong2048() throws Exception {
return out("PBEWithHmacSHA256AndAES_256", "2048",
"PBEWithHmacSHA256AndAES_256", "2048",
"HmacPBESHA256", "2048");
macAlgorithm("HmacPBESHA256"), "2048");
}
}