65
65
import javax .security .auth .DestroyFailedException ;
66
66
import javax .security .auth .x500 .X500Principal ;
67
67
68
+ import sun .security .action .GetPropertyAction ;
68
69
import sun .security .tools .KeyStoreUtil ;
69
70
import sun .security .util .*;
70
71
import sun .security .pkcs .ContentInfo ;
79
80
* Implements the PKCS#12 PFX protected using the Password privacy mode.
80
81
* The contents are protected using Password integrity mode.
81
82
*
82
- * Currently these PBE algorithms are used by default:
83
- * - PBEWithSHA1AndDESede to encrypt private keys, iteration count 50000.
84
- * - PBEWithSHA1AndRC2_40 to encrypt certificates, iteration count 50000.
85
- *
86
- * The default Mac algorithm is HmacPBESHA1, iteration count 100000.
87
- *
88
- * Supported encryption of various implementations :
89
- *
90
- * Software and mode. Certificate encryption Private key encryption
91
- * ---------------------------------------------------------------------
92
- * MSIE4 (domestic 40 bit RC2. 40 bit RC2
93
- * and xport versions)
94
- * PKCS#12 export.
95
- *
96
- * MSIE4, 5 (domestic 40 bit RC2, 40 bit RC2,
97
- * and export versions) 3 key triple DES 3 key triple DES
98
- * PKCS#12 import.
99
- *
100
- * MSIE5 40 bit RC2 3 key triple DES,
101
- * PKCS#12 export. with SHA1 (168 bits)
102
- *
103
- * Netscape Communicator 40 bit RC2 3 key triple DES,
104
- * (domestic and export with SHA1 (168 bits)
105
- * versions) PKCS#12 export
106
- *
107
- * Netscape Communicator 40 bit ciphers only All.
108
- * (export version)
109
- * PKCS#12 import.
110
- *
111
- * Netscape Communicator All. All.
112
- * (domestic or fortified
113
- * version) PKCS#12 import.
114
- *
115
- * OpenSSL PKCS#12 code. All. All.
116
- * ---------------------------------------------------------------------
117
- *
118
- * NOTE: PKCS12 KeyStore supports PrivateKeyEntry and TrustedCertficateEntry.
119
- * PKCS#12 is mainly used to deliver private keys with their associated
120
- * certificate chain and aliases. In a PKCS12 keystore, entries are
121
- * identified by the alias, and a localKeyId is required to match the
122
- * private key with the certificate. Trusted certificate entries are identified
123
- * by the presence of an trustedKeyUsage attribute.
83
+ * NOTE: In a PKCS12 keystore, entries are identified by the alias, and
84
+ * a localKeyId is required to match the private key with the certificate.
85
+ * Trusted certificate entries are identified by the presence of an
86
+ * trustedKeyUsage attribute.
124
87
*
125
88
* @author Seema Malkani
126
89
* @author Jeff Nisewanger
130
93
*/
131
94
public final class PKCS12KeyStore extends KeyStoreSpi {
132
95
96
+ // Hardcoded defaults. They should be the same with commented out
97
+ // lines inside the java.security file.
98
+ private static final String DEFAULT_CERT_PBE_ALGORITHM
99
+ = "PBEWithHmacSHA256AndAES_256" ;
100
+ private static final String DEFAULT_KEY_PBE_ALGORITHM
101
+ = "PBEWithHmacSHA256AndAES_256" ;
102
+ private static final String DEFAULT_MAC_ALGORITHM = "HmacPBESHA256" ;
103
+ private static final int DEFAULT_CERT_PBE_ITERATION_COUNT = 10000 ;
104
+ private static final int DEFAULT_KEY_PBE_ITERATION_COUNT = 10000 ;
105
+ private static final int DEFAULT_MAC_ITERATION_COUNT = 10000 ;
106
+
107
+ // Legacy settings. Used when "keystore.pkcs12.legacy" is set.
108
+ private static final String LEGACY_CERT_PBE_ALGORITHM
109
+ = "PBEWithSHA1AndRC2_40" ;
110
+ private static final String LEGACY_KEY_PBE_ALGORITHM
111
+ = "PBEWithSHA1AndDESede" ;
112
+ private static final String LEGACY_MAC_ALGORITHM = "HmacPBESHA1" ;
113
+ private static final int LEGACY_PBE_ITERATION_COUNT = 50000 ;
114
+ private static final int LEGACY_MAC_ITERATION_COUNT = 100000 ;
115
+
116
+ // Big switch. When this system property is set. Legacy settings
117
+ // are used no matter what other keystore.pkcs12.* properties are set.
118
+ // Note: This is only a system property, there's no same-name
119
+ // security property defined.
120
+ private static final String USE_LEGACY_PROP = "keystore.pkcs12.legacy" ;
121
+
133
122
// special PKCS12 keystore that supports PKCS12 and JKS file formats
134
123
public static final class DualFormatPKCS12 extends KeyStoreDelegator {
135
124
public DualFormatPKCS12 () {
@@ -845,9 +834,6 @@ private SecretKey getPBEKey(char[] password) throws IOException
845
834
* Encrypt private key or secret key using Password-based encryption (PBE)
846
835
* as defined in PKCS#5.
847
836
*
848
- * NOTE: By default, pbeWithSHAAnd3-KeyTripleDES-CBC algorithmID is
849
- * used to derive the key and IV.
850
- *
851
837
* @return encrypted private key or secret key encoded as
852
838
* EncryptedPrivateKeyInfo
853
839
*/
@@ -1866,9 +1852,6 @@ private byte[] createSafeContent()
1866
1852
* Encrypt the contents using Password-based (PBE) encryption
1867
1853
* as defined in PKCS #5.
1868
1854
*
1869
- * NOTE: Currently pbeWithSHAAnd40BiteRC2-CBC algorithmID is used
1870
- * to derive the key and IV.
1871
- *
1872
1855
* @return encrypted contents encoded as EncryptedContentInfo
1873
1856
*/
1874
1857
private byte [] encryptContent (byte [] data , char [] password )
@@ -2640,25 +2623,42 @@ public boolean engineProbe(InputStream stream) throws IOException {
2640
2623
return result ;
2641
2624
}
2642
2625
2643
- // 8076190: Customizing the generation of a PKCS12 keystore
2626
+ // The following methods are related to customizing
2627
+ // the generation of a PKCS12 keystore or private/secret
2628
+ // key entries.
2629
+
2630
+ private static boolean useLegacy () {
2631
+ return GetPropertyAction .privilegedGetProperty (
2632
+ USE_LEGACY_PROP ) != null ;
2633
+ }
2644
2634
2645
2635
private static String defaultCertProtectionAlgorithm () {
2636
+ if (useLegacy ()) {
2637
+ return LEGACY_CERT_PBE_ALGORITHM ;
2638
+ }
2646
2639
String result = SecurityProperties .privilegedGetOverridable (
2647
2640
"keystore.pkcs12.certProtectionAlgorithm" );
2648
2641
return (result != null && !result .isEmpty ())
2649
- ? result : "PBEWithSHA1AndRC2_40" ;
2642
+ ? result : DEFAULT_CERT_PBE_ALGORITHM ;
2650
2643
}
2651
2644
2652
2645
private static int defaultCertPbeIterationCount () {
2646
+ if (useLegacy ()) {
2647
+ return LEGACY_PBE_ITERATION_COUNT ;
2648
+ }
2653
2649
String result = SecurityProperties .privilegedGetOverridable (
2654
2650
"keystore.pkcs12.certPbeIterationCount" );
2655
2651
return (result != null && !result .isEmpty ())
2656
- ? string2IC ("certPbeIterationCount" , result ) : 50000 ;
2652
+ ? string2IC ("certPbeIterationCount" , result )
2653
+ : DEFAULT_CERT_PBE_ITERATION_COUNT ;
2657
2654
}
2658
2655
2659
2656
// Read both "keystore.pkcs12.keyProtectionAlgorithm" and
2660
2657
// "keystore.PKCS12.keyProtectionAlgorithm" for compatibility.
2661
2658
private static String defaultKeyProtectionAlgorithm () {
2659
+ if (useLegacy ()) {
2660
+ return LEGACY_KEY_PBE_ALGORITHM ;
2661
+ }
2662
2662
String result = AccessController .doPrivileged (new PrivilegedAction <String >() {
2663
2663
public String run () {
2664
2664
String result ;
@@ -2680,28 +2680,39 @@ public String run() {
2680
2680
}
2681
2681
});
2682
2682
return (result != null && !result .isEmpty ())
2683
- ? result : "PBEWithSHA1AndDESede" ;
2683
+ ? result : DEFAULT_KEY_PBE_ALGORITHM ;
2684
2684
}
2685
2685
2686
2686
private static int defaultKeyPbeIterationCount () {
2687
+ if (useLegacy ()) {
2688
+ return LEGACY_PBE_ITERATION_COUNT ;
2689
+ }
2687
2690
String result = SecurityProperties .privilegedGetOverridable (
2688
2691
"keystore.pkcs12.keyPbeIterationCount" );
2689
2692
return (result != null && !result .isEmpty ())
2690
- ? string2IC ("keyPbeIterationCount" , result ) : 50000 ;
2693
+ ? string2IC ("keyPbeIterationCount" , result )
2694
+ : DEFAULT_KEY_PBE_ITERATION_COUNT ;
2691
2695
}
2692
2696
2693
2697
private static String defaultMacAlgorithm () {
2698
+ if (useLegacy ()) {
2699
+ return LEGACY_MAC_ALGORITHM ;
2700
+ }
2694
2701
String result = SecurityProperties .privilegedGetOverridable (
2695
2702
"keystore.pkcs12.macAlgorithm" );
2696
2703
return (result != null && !result .isEmpty ())
2697
- ? result : "HmacPBESHA1" ;
2704
+ ? result : DEFAULT_MAC_ALGORITHM ;
2698
2705
}
2699
2706
2700
2707
private static int defaultMacIterationCount () {
2708
+ if (useLegacy ()) {
2709
+ return LEGACY_MAC_ITERATION_COUNT ;
2710
+ }
2701
2711
String result = SecurityProperties .privilegedGetOverridable (
2702
2712
"keystore.pkcs12.macIterationCount" );
2703
2713
return (result != null && !result .isEmpty ())
2704
- ? string2IC ("macIterationCount" , result ) : 100000 ;
2714
+ ? string2IC ("macIterationCount" , result )
2715
+ : DEFAULT_MAC_ITERATION_COUNT ;
2705
2716
}
2706
2717
2707
2718
private static int string2IC (String type , String value ) {
0 commit comments