56
56
import java .security .cert .CertificateException ;
57
57
import java .security .cert .X509Certificate ;
58
58
import java .util .ArrayList ;
59
+ import java .util .HashMap ;
59
60
import java .util .List ;
61
+ import java .util .Map ;
60
62
61
63
import static java .lang .String .format ;
62
64
import static java .util .Arrays .asList ;
@@ -78,6 +80,7 @@ public class SMIMESupport implements SMIMEModule {
78
80
79
81
private static final Logger LOGGER = LoggerFactory .getLogger (SMIMESupport .class );
80
82
private static final List <String > SMIME_MIMETYPES = asList ("application/pkcs7-mime" , "application/x-pkcs7-mime" , "multipart/signed" );
83
+ private static final Map <Pkcs12Config , SmimeKey > SIMPLE_SMIMESTORE_CACHE = new HashMap <>();
81
84
82
85
static {
83
86
Security .addProvider (new BouncyCastleProvider ());
@@ -456,7 +459,14 @@ public MimeMessage encryptMessage(@Nullable Session session, @NotNull MimeMessag
456
459
}
457
460
458
461
private SmimeKey retrieveSmimeKeyFromPkcs12Keystore (@ NotNull Pkcs12Config pkcs12 ) {
459
- SmimeKeyStore store = new SmimeKeyStore (new ByteArrayInputStream (pkcs12 .getPkcs12StoreData ()), pkcs12 .getStorePassword ());
460
- return store .getPrivateKey (pkcs12 .getKeyAlias (), pkcs12 .getKeyPassword ());
462
+ if (!SIMPLE_SMIMESTORE_CACHE .containsKey (pkcs12 )) {
463
+ SIMPLE_SMIMESTORE_CACHE .put (pkcs12 , produceSmimeKey (pkcs12 ));
464
+ }
465
+ return SIMPLE_SMIMESTORE_CACHE .get (pkcs12 );
466
+ }
467
+
468
+ private SmimeKey produceSmimeKey (final @ NotNull Pkcs12Config pkcs12 ) {
469
+ return new SmimeKeyStore (new ByteArrayInputStream (pkcs12 .getPkcs12StoreData ()), pkcs12 .getStorePassword ())
470
+ .getPrivateKey (pkcs12 .getKeyAlias (), pkcs12 .getKeyPassword ());
461
471
}
462
472
}
0 commit comments