@@ -200,6 +200,7 @@ protected byte[] engineGenerateSecret() throws IllegalStateException {
200200 CK_ATTRIBUTE [] attributes = new CK_ATTRIBUTE [] {
201201 new CK_ATTRIBUTE (CKA_CLASS , CKO_SECRET_KEY ),
202202 new CK_ATTRIBUTE (CKA_KEY_TYPE , CKK_GENERIC_SECRET ),
203+ new CK_ATTRIBUTE (CKA_VALUE_LEN , secretLen ),
203204 };
204205 attributes = token .getAttributes
205206 (O_GENERATE , CKO_SECRET_KEY , CKK_GENERIC_SECRET , attributes );
@@ -213,22 +214,11 @@ protected byte[] engineGenerateSecret() throws IllegalStateException {
213214 token .p11 .C_GetAttributeValue (session .id (), keyID , attributes );
214215 byte [] secret = attributes [0 ].getByteArray ();
215216 token .p11 .C_DestroyObject (session .id (), keyID );
216- // Some vendors, e.g. NSS, trim off the leading 0x00 byte(s) from
217- // the generated secret. Thus, we need to check the secret length
218- // and trim/pad it so the returned value has the same length as
219- // the modulus size
220- if (secret .length == secretLen ) {
221- return secret ;
222- } else {
223- if (secret .length > secretLen ) {
224- // Shouldn't happen; but check just in case
225- throw new ProviderException ("generated secret is out-of-range" );
226- }
227- byte [] newSecret = new byte [secretLen ];
228- System .arraycopy (secret , 0 , newSecret , secretLen - secret .length ,
229- secret .length );
230- return newSecret ;
217+ if (secret .length != secretLen ) {
218+ // Shouldn't happen; but check just in case
219+ throw new ProviderException ("generated secret is out-of-range" );
231220 }
221+ return secret ;
232222 } catch (PKCS11Exception e ) {
233223 throw new ProviderException ("Could not derive key" , e );
234224 } finally {
@@ -321,10 +311,20 @@ private SecretKey nativeGenerateSecret(String algorithm)
321311 long privKeyID = privateKey .getKeyID ();
322312 try {
323313 session = token .getObjSession ();
324- CK_ATTRIBUTE [] attributes = new CK_ATTRIBUTE [] {
325- new CK_ATTRIBUTE (CKA_CLASS , CKO_SECRET_KEY ),
326- new CK_ATTRIBUTE (CKA_KEY_TYPE , keyType ),
327- };
314+ CK_ATTRIBUTE [] attributes ;
315+ if ("TlsPremasterSecret" .equalsIgnoreCase (algorithm )) {
316+ attributes = new CK_ATTRIBUTE []{
317+ new CK_ATTRIBUTE (CKA_CLASS , CKO_SECRET_KEY ),
318+ new CK_ATTRIBUTE (CKA_KEY_TYPE , keyType ),
319+ };
320+ } else {
321+ // keep the leading zeroes
322+ attributes = new CK_ATTRIBUTE []{
323+ new CK_ATTRIBUTE (CKA_CLASS , CKO_SECRET_KEY ),
324+ new CK_ATTRIBUTE (CKA_KEY_TYPE , keyType ),
325+ new CK_ATTRIBUTE (CKA_VALUE_LEN , secretLen ),
326+ };
327+ }
328328 attributes = token .getAttributes
329329 (O_GENERATE , CKO_SECRET_KEY , keyType , attributes );
330330 long keyID = token .p11 .C_DeriveKey (session .id (),
@@ -337,19 +337,6 @@ private SecretKey nativeGenerateSecret(String algorithm)
337337 int keyLen = (int )lenAttributes [0 ].getLong ();
338338 SecretKey key = P11Key .secretKey
339339 (session , keyID , algorithm , keyLen << 3 , attributes );
340- if ("RAW" .equals (key .getFormat ())
341- && algorithm .equalsIgnoreCase ("TlsPremasterSecret" )) {
342- // Workaround for Solaris bug 6318543.
343- // Strip leading zeroes ourselves if possible (key not sensitive).
344- // This should be removed once the Solaris fix is available
345- // as here we always retrieve the CKA_VALUE even for tokens
346- // that do not have that bug.
347- byte [] keyBytes = key .getEncoded ();
348- byte [] newBytes = KeyUtil .trimZeroes (keyBytes );
349- if (keyBytes != newBytes ) {
350- key = new SecretKeySpec (newBytes , algorithm );
351- }
352- }
353340 return key ;
354341 } catch (PKCS11Exception e ) {
355342 throw new InvalidKeyException ("Could not derive key" , e );
0 commit comments