@@ -103,13 +103,37 @@ private static void putKeyInfo(KeyInfo ki) {
103
103
keyInfo .put (ki .algo .toUpperCase (Locale .ENGLISH ), ki );
104
104
}
105
105
106
- static sealed class KeyInfo permits PBEKeyInfo , HMACKeyInfo , HKDFKeyInfo {
106
+ /*
107
+ * The KeyInfo class represents information about a symmetric PKCS #11 key
108
+ * type or about the output of a key-based computation (e.g. HMAC). A
109
+ * KeyInfo instance may describe the key/output itself, or the type of
110
+ * key/output that a service accepts/produces. Used by P11SecretKeyFactory,
111
+ * P11PBECipher, P11Mac, and P11HKDF.
112
+ */
113
+ static sealed class KeyInfo permits PBEKeyInfo , HMACKeyInfo , HKDFKeyInfo ,
114
+ TLSKeyInfo {
115
+ // Java Standard Algorithm Name.
107
116
public final String algo ;
117
+
118
+ // Key type (CKK_*).
108
119
public final long keyType ;
109
120
121
+ // Mechanism for C_GenerateKey to generate a key of this type (CKM_*).
122
+ // While keys may be generated with other APIs and mechanisms (e.g. AES
123
+ // key generated with C_DeriveKey and CKM_HKDF_DERIVE instead of
124
+ // C_GenerateKey and CKM_AES_KEY_GEN), this information is used by
125
+ // P11KeyGenerator::checkKeySize in a best-effort attempt to validate
126
+ // that the key size is within a valid range (see CK_MECHANISM_INFO).
127
+ public final long keyGenMech ;
128
+
110
129
KeyInfo (String algo , long keyType ) {
130
+ this (algo , keyType , CK_UNAVAILABLE_INFORMATION );
131
+ }
132
+
133
+ KeyInfo (String algo , long keyType , long keyGenMech ) {
111
134
this .algo = algo ;
112
135
this .keyType = keyType ;
136
+ this .keyGenMech = keyGenMech ;
113
137
}
114
138
115
139
// The P11SecretKeyFactory::convertKey method needs to know if a service
@@ -134,8 +158,26 @@ static boolean checkUse(KeyInfo ki, KeyInfo si) {
134
158
}
135
159
}
136
160
161
+ /*
162
+ * KeyInfo specialization for keys that are either input or result of a TLS
163
+ * key derivation. Keys of this type are typically handled by JSSE and their
164
+ * algorithm name start with "Tls". Used by P11HKDF.
165
+ */
166
+ static final class TLSKeyInfo extends KeyInfo {
167
+ TLSKeyInfo (String algo ) {
168
+ super (algo , CKK_GENERIC_SECRET );
169
+ }
170
+ }
171
+
172
+ /*
173
+ * KeyInfo specialization for outputs of a HMAC computation. Used by
174
+ * P11SecretKeyFactory and P11Mac.
175
+ */
137
176
static final class HMACKeyInfo extends KeyInfo {
177
+ // HMAC mechanism (CKM_*) to generate the output.
138
178
public final long mech ;
179
+
180
+ // HMAC output length (in bits).
139
181
public final int keyLen ;
140
182
141
183
HMACKeyInfo (String algo , long mech , int keyLen ) {
@@ -145,6 +187,10 @@ static final class HMACKeyInfo extends KeyInfo {
145
187
}
146
188
}
147
189
190
+ /*
191
+ * KeyInfo specialization for HKDF key derivation. Used by
192
+ * P11SecretKeyFactory and P11HKDF.
193
+ */
148
194
static final class HKDFKeyInfo extends KeyInfo {
149
195
public static final long UNKNOWN_KEY_TYPE = -1 ;
150
196
public final long hmacMech ;
@@ -157,6 +203,10 @@ static final class HKDFKeyInfo extends KeyInfo {
157
203
}
158
204
}
159
205
206
+ /*
207
+ * KeyInfo specialization for PBE key derivation. Used by
208
+ * P11SecretKeyFactory, P11PBECipher and P11Mac.
209
+ */
160
210
abstract static sealed class PBEKeyInfo extends KeyInfo
161
211
permits AESPBEKeyInfo , PBKDF2KeyInfo , P12MacPBEKeyInfo {
162
212
public static final long INVALID_PRF = -1 ;
@@ -204,24 +254,39 @@ static final class P12MacPBEKeyInfo extends PBEKeyInfo {
204
254
}
205
255
206
256
static {
207
- putKeyInfo (new KeyInfo ("RC4" , CKK_RC4 ));
208
- putKeyInfo (new KeyInfo ("ARCFOUR" , CKK_RC4 ));
209
- putKeyInfo (new KeyInfo ("DES" , CKK_DES ));
210
- putKeyInfo (new KeyInfo ("DESede" , CKK_DES3 ));
211
- putKeyInfo (new KeyInfo ("AES" , CKK_AES ));
212
- putKeyInfo (new KeyInfo ("Blowfish" , CKK_BLOWFISH ));
213
- putKeyInfo (new KeyInfo ("ChaCha20" , CKK_CHACHA20 ));
214
- putKeyInfo (new KeyInfo ("ChaCha20-Poly1305" , CKK_CHACHA20 ));
257
+ putKeyInfo (new KeyInfo ("RC4" , CKK_RC4 , CKM_RC4_KEY_GEN ));
258
+ putKeyInfo (new KeyInfo ("ARCFOUR" , CKK_RC4 , CKM_RC4_KEY_GEN ));
259
+ putKeyInfo (new KeyInfo ("DES" , CKK_DES , CKM_DES_KEY_GEN ));
260
+ putKeyInfo (new KeyInfo ("DESede" , CKK_DES3 , CKM_DES3_KEY_GEN ));
261
+ putKeyInfo (new KeyInfo ("AES" , CKK_AES , CKM_AES_KEY_GEN ));
262
+ putKeyInfo (new KeyInfo ("Blowfish" , CKK_BLOWFISH , CKM_BLOWFISH_KEY_GEN ));
263
+ putKeyInfo (new KeyInfo ("ChaCha20" , CKK_CHACHA20 , CKM_CHACHA20_KEY_GEN ));
264
+ putKeyInfo (new KeyInfo ("ChaCha20-Poly1305" , CKK_CHACHA20 ,
265
+ CKM_CHACHA20_KEY_GEN ));
215
266
216
267
// we don't implement RC2 or IDEA, but we want to be able to generate
217
268
// keys for those SSL/TLS ciphersuites.
218
- putKeyInfo (new KeyInfo ("RC2" , CKK_RC2 ));
219
- putKeyInfo (new KeyInfo ("IDEA" , CKK_IDEA ));
220
-
221
- putKeyInfo (new KeyInfo ("TlsPremasterSecret" , PCKK_TLSPREMASTER ));
222
- putKeyInfo (new KeyInfo ("TlsRsaPremasterSecret" , PCKK_TLSRSAPREMASTER ));
223
- putKeyInfo (new KeyInfo ("TlsMasterSecret" , PCKK_TLSMASTER ));
224
- putKeyInfo (new KeyInfo ("Generic" , CKK_GENERIC_SECRET ));
269
+ putKeyInfo (new KeyInfo ("RC2" , CKK_RC2 , CKM_RC2_KEY_GEN ));
270
+ putKeyInfo (new KeyInfo ("IDEA" , CKK_IDEA , CKM_IDEA_KEY_GEN ));
271
+
272
+ putKeyInfo (new TLSKeyInfo ("TlsPremasterSecret" ));
273
+ putKeyInfo (new TLSKeyInfo ("TlsRsaPremasterSecret" ));
274
+ putKeyInfo (new TLSKeyInfo ("TlsMasterSecret" ));
275
+ putKeyInfo (new TLSKeyInfo ("TlsBinderKey" ));
276
+ putKeyInfo (new TLSKeyInfo ("TlsClientAppTrafficSecret" ));
277
+ putKeyInfo (new TLSKeyInfo ("TlsClientHandshakeTrafficSecret" ));
278
+ putKeyInfo (new TLSKeyInfo ("TlsEarlySecret" ));
279
+ putKeyInfo (new TLSKeyInfo ("TlsFinishedSecret" ));
280
+ putKeyInfo (new TLSKeyInfo ("TlsHandshakeSecret" ));
281
+ putKeyInfo (new TLSKeyInfo ("TlsKey" ));
282
+ putKeyInfo (new TLSKeyInfo ("TlsResumptionMasterSecret" ));
283
+ putKeyInfo (new TLSKeyInfo ("TlsSaltSecret" ));
284
+ putKeyInfo (new TLSKeyInfo ("TlsServerAppTrafficSecret" ));
285
+ putKeyInfo (new TLSKeyInfo ("TlsServerHandshakeTrafficSecret" ));
286
+ putKeyInfo (new TLSKeyInfo ("TlsUpdateNplus1" ));
287
+
288
+ putKeyInfo (new KeyInfo ("Generic" , CKK_GENERIC_SECRET ,
289
+ CKM_GENERIC_SECRET_KEY_GEN ));
225
290
226
291
HMACKeyInfo hmacSHA1 =
227
292
new HMACKeyInfo ("HmacSHA1" , CKM_SHA_1_HMAC , 160 );
@@ -549,34 +614,23 @@ private static P11Key createKey(Token token, byte[] encoded,
549
614
long keyType = ki .keyType ;
550
615
try {
551
616
switch ((int ) keyType ) {
552
- case (int ) CKK_DES -> {
553
- keyLength =
554
- P11KeyGenerator .checkKeySize (CKM_DES_KEY_GEN , n , token );
555
- fixDESParity (encoded , 0 );
556
- }
557
- case (int ) CKK_DES3 -> {
558
- keyLength =
559
- P11KeyGenerator .checkKeySize (CKM_DES3_KEY_GEN , n , token );
560
- fixDESParity (encoded , 0 );
561
- fixDESParity (encoded , 8 );
562
- if (keyLength == 112 ) {
563
- keyType = CKK_DES2 ;
564
- } else {
565
- keyType = CKK_DES3 ;
566
- fixDESParity (encoded , 16 );
617
+ case (int ) CKK_DES , (int ) CKK_DES3 , (int ) CKK_AES , (int ) CKK_RC4 ,
618
+ (int ) CKK_BLOWFISH , (int ) CKK_CHACHA20 -> {
619
+ keyLength = P11KeyGenerator .checkKeySize (ki .keyGenMech , n ,
620
+ token );
621
+ if (keyType == CKK_DES || keyType == CKK_DES3 ) {
622
+ fixDESParity (encoded , 0 );
623
+ if (keyType == CKK_DES3 ) {
624
+ fixDESParity (encoded , 8 );
625
+ if (keyLength == 112 ) {
626
+ keyType = CKK_DES2 ;
627
+ } else {
628
+ fixDESParity (encoded , 16 );
629
+ }
630
+ }
567
631
}
568
632
}
569
- case (int ) CKK_AES -> keyLength =
570
- P11KeyGenerator .checkKeySize (CKM_AES_KEY_GEN , n , token );
571
- case (int ) CKK_RC4 -> keyLength =
572
- P11KeyGenerator .checkKeySize (CKM_RC4_KEY_GEN , n , token );
573
- case (int ) CKK_BLOWFISH -> keyLength =
574
- P11KeyGenerator .checkKeySize (CKM_BLOWFISH_KEY_GEN , n ,
575
- token );
576
- case (int ) CKK_CHACHA20 -> keyLength = P11KeyGenerator .checkKeySize (
577
- CKM_CHACHA20_KEY_GEN , n , token );
578
- case (int ) CKK_GENERIC_SECRET , (int ) PCKK_TLSPREMASTER , (int ) PCKK_TLSRSAPREMASTER , (int ) PCKK_TLSMASTER ->
579
- keyType = CKK_GENERIC_SECRET ;
633
+ case (int ) CKK_GENERIC_SECRET -> {}
580
634
default -> throw new InvalidKeyException ("Unknown algorithm " +
581
635
algorithm );
582
636
}
0 commit comments