@@ -343,6 +343,24 @@ static SecretKey secretKey(Session session, long keyID, String algorithm,
343343 new CK_ATTRIBUTE (CKA_SENSITIVE ),
344344 new CK_ATTRIBUTE (CKA_EXTRACTABLE ),
345345 });
346+
347+ if ((SunPKCS11 .mysunpkcs11 != null ) && !SunPKCS11 .isExportWrapKey .get ()
348+ && ("AES" .equals (algorithm ) || "TripleDES" .equals (algorithm ))
349+ ) {
350+ if (attributes [0 ].getBoolean () || attributes [1 ].getBoolean () || (attributes [2 ].getBoolean () == false )) {
351+ try {
352+ byte [] key = SunPKCS11 .mysunpkcs11 .exportKey (session .id (), attributes , keyID );
353+ SecretKey secretKey = new SecretKeySpec (key , algorithm );
354+ return new P11SecretKeyFIPS (session , keyID , algorithm , keyLength , attributes , secretKey );
355+ } catch (PKCS11Exception e ) {
356+ // Attempt failed, create a P11SecretKey object.
357+ if (debug != null ) {
358+ debug .println ("Attempt failed, creating a SecretKey object for " + algorithm );
359+ }
360+ }
361+ }
362+ }
363+
346364 return new P11SecretKey (session , keyID , algorithm , keyLength ,
347365 attributes );
348366 }
@@ -495,6 +513,29 @@ byte[] getEncodedInternal() {
495513 }
496514 }
497515
516+ private static final class P11SecretKeyFIPS extends P11Key implements SecretKey {
517+ @ Serial
518+ private static final long serialVersionUID = -9186806495402041696L ;
519+ private final SecretKey key ;
520+
521+ P11SecretKeyFIPS (Session session , long keyID , String algorithm ,
522+ int keyLength , CK_ATTRIBUTE [] attributes , SecretKey key ) {
523+ super (SECRET , session , keyID , algorithm , keyLength , attributes );
524+ this .key = key ;
525+ }
526+
527+ @ Override
528+ public String getFormat () {
529+ return "RAW" ;
530+ }
531+
532+ @ Override
533+ byte [] getEncodedInternal () {
534+ return key .getEncoded ();
535+ }
536+
537+ }
538+
498539 private static class P11SecretKey extends P11Key implements SecretKey {
499540 private static final long serialVersionUID = -7828241727014329084L ;
500541 private volatile byte [] encoded ;
0 commit comments