@@ -561,47 +561,73 @@ static class P11RSAPrivateKeyInternal extends P11PrivateKey {
561
561
static P11RSAPrivateKeyInternal of (Session session , long keyID ,
562
562
String algorithm , int keyLength , CK_ATTRIBUTE [] attrs ,
563
563
boolean keySensitive ) {
564
- if (keySensitive ) {
565
- return new P11RSAPrivateKeyInternal (session , keyID , algorithm ,
564
+ P11RSAPrivateKeyInternal p11Key = null ;
565
+ if (!keySensitive ) {
566
+ // Key is not sensitive: try to interpret as CRT or non-CRT.
567
+ p11Key = asCRT (session , keyID , algorithm , keyLength , attrs );
568
+ if (p11Key == null ) {
569
+ p11Key = asNonCRT (session , keyID , algorithm , keyLength ,
570
+ attrs );
571
+ }
572
+ }
573
+ if (p11Key == null ) {
574
+ // Key is sensitive or there was a failure while querying its
575
+ // attributes: handle as opaque.
576
+ p11Key = new P11RSAPrivateKeyInternal (session , keyID , algorithm ,
566
577
keyLength , attrs );
567
- } else {
568
- CK_ATTRIBUTE [] rsaAttrs = new CK_ATTRIBUTE [] {
569
- new CK_ATTRIBUTE (CKA_MODULUS ),
570
- new CK_ATTRIBUTE (CKA_PRIVATE_EXPONENT ),
571
- new CK_ATTRIBUTE (CKA_PUBLIC_EXPONENT ),
572
- new CK_ATTRIBUTE (CKA_PRIME_1 ),
573
- new CK_ATTRIBUTE (CKA_PRIME_2 ),
574
- new CK_ATTRIBUTE (CKA_EXPONENT_1 ),
575
- new CK_ATTRIBUTE (CKA_EXPONENT_2 ),
576
- new CK_ATTRIBUTE (CKA_COEFFICIENT ),
577
- };
578
- boolean isCRT = true ;
579
- Session tempSession = null ;
580
- try {
581
- tempSession = session .token .getOpSession ();
582
- session .token .p11 .C_GetAttributeValue (tempSession .id (),
583
- keyID , rsaAttrs );
584
- for (CK_ATTRIBUTE attr : rsaAttrs ) {
585
- isCRT &= (attr .pValue instanceof byte []);
586
- if (!isCRT ) break ;
578
+ }
579
+ return p11Key ;
580
+ }
581
+
582
+ private static CK_ATTRIBUTE [] tryFetchAttributes (Session session ,
583
+ long keyID , long ... attrTypes ) {
584
+ int i = 0 ;
585
+ CK_ATTRIBUTE [] attrs = new CK_ATTRIBUTE [attrTypes .length ];
586
+ for (long attrType : attrTypes ) {
587
+ attrs [i ++] = new CK_ATTRIBUTE (attrType );
588
+ }
589
+ try {
590
+ session .token .p11 .C_GetAttributeValue (session .id (), keyID ,
591
+ attrs );
592
+ for (CK_ATTRIBUTE attr : attrs ) {
593
+ if (!(attr .pValue instanceof byte [])) {
594
+ return null ;
587
595
}
588
- } catch (PKCS11Exception e ) {
589
- // ignore, assume not available
590
- isCRT = false ;
591
- } finally {
592
- session .token .releaseSession (tempSession );
593
- }
594
- BigInteger n = rsaAttrs [0 ].getBigInteger ();
595
- BigInteger d = rsaAttrs [1 ].getBigInteger ();
596
- if (isCRT ) {
597
- return new P11RSAPrivateKey (session , keyID , algorithm ,
598
- keyLength , attrs , n , d ,
599
- Arrays .copyOfRange (rsaAttrs , 2 , rsaAttrs .length ));
600
- } else {
601
- return new P11RSAPrivateNonCRTKey (session , keyID ,
602
- algorithm , keyLength , attrs , n , d );
603
596
}
597
+ return attrs ;
598
+ } catch (PKCS11Exception ignored ) {
599
+ // ignore, assume not available
600
+ return null ;
601
+ }
602
+ }
603
+
604
+ private static P11RSAPrivateKeyInternal asCRT (Session session ,
605
+ long keyID , String algorithm , int keyLength ,
606
+ CK_ATTRIBUTE [] attrs ) {
607
+ CK_ATTRIBUTE [] rsaCRTAttrs = tryFetchAttributes (session , keyID ,
608
+ CKA_MODULUS , CKA_PRIVATE_EXPONENT , CKA_PUBLIC_EXPONENT ,
609
+ CKA_PRIME_1 , CKA_PRIME_2 , CKA_EXPONENT_1 , CKA_EXPONENT_2 ,
610
+ CKA_COEFFICIENT );
611
+ if (rsaCRTAttrs == null ) {
612
+ return null ;
613
+ }
614
+ return new P11RSAPrivateKey (session , keyID , algorithm , keyLength ,
615
+ attrs , rsaCRTAttrs [0 ].getBigInteger (),
616
+ rsaCRTAttrs [1 ].getBigInteger (),
617
+ Arrays .copyOfRange (rsaCRTAttrs , 2 , rsaCRTAttrs .length ));
618
+ }
619
+
620
+ private static P11RSAPrivateKeyInternal asNonCRT (Session session ,
621
+ long keyID , String algorithm , int keyLength ,
622
+ CK_ATTRIBUTE [] attrs ) {
623
+ CK_ATTRIBUTE [] rsaNonCRTAttrs = tryFetchAttributes (session , keyID ,
624
+ CKA_MODULUS , CKA_PRIVATE_EXPONENT );
625
+ if (rsaNonCRTAttrs == null ) {
626
+ return null ;
604
627
}
628
+ return new P11RSAPrivateNonCRTKey (session , keyID , algorithm ,
629
+ keyLength , attrs , rsaNonCRTAttrs [0 ].getBigInteger (),
630
+ rsaNonCRTAttrs [1 ].getBigInteger ());
605
631
}
606
632
607
633
protected transient BigInteger n ;
0 commit comments