1、问题描述
RA在合成PFX证书时,证书的MAC算法1.2.840.113549.1.5.13 和1.2.840.113549.1.12.1.6会交替出现。
2、问题分析
在经过本人绞尽脑汁的分析后,发现与JDK的版本存在关系。1.2.840.113549.1.5.13出现的JDK版本为JDK1.8_301,1.2.840.113549.1.12.1.6出现的JDK版本相比于此版本较低。参见:https://bugs.openjdk.java.net/browse/JDK-8231581,Oracle官网也做了简略说明。
3、源码分析
3.1 sun.pkcs12
由于我比较菜,没有找到sun的源码,本部分为反编译结果:
private byte[] encryptPrivateKey(byte[] var1, PasswordProtection var2) throws IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
Object var3 = null;
try {
String var4 = var2.getProtectionAlgorithm();
AlgorithmParameters var14;
if (var4 != null) {
AlgorithmParameterSpec var7 = var2.getProtectionParameters();
if (var7 != null) {
var14 = AlgorithmParameters.getInstance(var4);
var14.init(var7);
} else {
var14 = this.getPBEAlgorithmParameters(var4, defaultKeyPbeIterationCount());
}
} else {
var4 = defaultKeyProtectionAlgorithm();
System.out.println("升级前的MAC算法var4:" + var4);
var14 = this.getPBEAlgorithmParameters(var4, defaultKeyPbeIterationCount());
}
System.out.println("升级前的MAC算法var14:" + var14);
ObjectIdentifier var15 = mapPBEAlgorithmToOID(var4);
System.out.println("升级后的MAC算法var15:" + var15);
if (var15 == null) {
throw new IOException("PBE algorithm '" + var4 + " 'is not supported for key entry protection");
} else {
SecretKey var8 = this.getPBEKey(var2.getPassword());
Cipher var9 = Cipher.getInstance(var4);
var9.init(1, var8, var14);
byte[] var10 = var9.doFinal(var1);
AlgorithmId var6 = new AlgorithmId(var15, var9.getParameters());
if (debug != null) {
debug.println(" (Cipher algorithm: " + var9.getAlgorithm() + ")");
}
EncryptedPrivateKeyInfo var11 = new EncryptedPrivateKeyInfo(var6, var10);
byte[] var13 = var11.getEncoded();
return var13;
}
} catch (Exception var12) {
UnrecoverableKeyException var5 = new UnrecoverableKeyException("Encrypt Private Key failed: " + var12.getMessage());
var5.initCause(var12);
throw var5;
}
}
private static String defaultKeyProtectionAlgorithm() {
if (useLegacy()) {
return "PBEWithSHA1AndDESede";
} else {
String var0 = (String) AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
String var2 = "keystore.pkcs12.keyProtectionAlgorithm";
String var3 = "keystore.PKCS12.keyProtectionAlgorithm";
String var1 = System.getProperty(var2);
if (var1 != null) {
return var1;
} else {
var1 = System.getProperty(var3);
if (var1 != null) {
return var1;
} else {
var1 = Security.getProperty(var2);
return var1 != null ? var1 : Security.getProperty(var3);
}
}
}
});
return var0 != null && !var0.isEmpty() ? var0 : "PBEWithHmacSHA256AndAES_256";
}
}
private static String defaultMacAlgorithm() {
if (useLegacy()) {
return "HmacPBESHA1";
} else {
String var0 = SecurityProperties.privilegedGetOverridable("keystore.pkcs12.macAlgorithm");
return var0 != null && !var0.isEmpty() ? var0 : "HmacPBESHA1";
}
}
可以看到上面三个函数是默认的MAC算法做了升级,下面是运行结果:将默认的PBEWithHmacSHA256AndAES_256升级成了1.2.840.113549.1.5.13。
升级前的MAC算法var4:PBEWithHmacSHA256AndAES_256
升级前的MAC算法var14:PBEWithHmacSHA256AndAES_256
=====>PBEWithHmacSHA256AndAES_256
升级后的MAC算法var15:1.2.840.113549.1.5.13
=====>PBEWithHmacSHA256AndAES_256
3.2 其他版本JDK
以openjdk为例
=====>algorithm: PBEWithSHA1AndDESede
=====>1111 PBEWithSHA1AndDESede
=====>pbeOID: 1.2.840.113549.1.12.1.3
algid:PBEWithSHA1AndDESede,
salt:
[0000: 1D D8 59 DB 0E 23 C0 F0 47 5E 2F F1 DE 3D 24 FA ..Y..#..G^/..=$.
0010: DF 20 10 1C . ..
]
iterationCount:
0400
1.2.840.113549.1.12.10.1.2
可以看到使用的默认mac oid是1.2.840.113549.1.12.1.6。