1616 */
1717package org .apache .qpid .jms .transports ;
1818
19- import java . io .File ;
20- import java . io .FileInputStream ;
21- import java . io .InputStream ;
22- import java . net . URI ;
23- import java . security . KeyStore ;
24- import java . security . KeyStoreException ;
25- import java . security . SecureRandom ;
26- import java . util .ArrayList ;
27- import java . util . Arrays ;
28- import java . util . List ;
19+ import io .netty . buffer . ByteBufAllocator ;
20+ import io .netty . handler . ssl . OpenSsl ;
21+ import io .netty . handler . ssl . OpenSslX509KeyManagerFactory ;
22+ import io . netty . handler . ssl . SslContext ;
23+ import io . netty . handler . ssl . SslContextBuilder ;
24+ import io . netty . handler . ssl . SslHandler ;
25+ import io . netty . handler . ssl . SslProvider ;
26+ import io . netty . handler . ssl . util .InsecureTrustManagerFactory ;
27+ import org . slf4j . Logger ;
28+ import org . slf4j . LoggerFactory ;
2929
3030import javax .net .ssl .KeyManager ;
3131import javax .net .ssl .KeyManagerFactory ;
3535import javax .net .ssl .TrustManager ;
3636import javax .net .ssl .TrustManagerFactory ;
3737import javax .net .ssl .X509ExtendedKeyManager ;
38-
39- import org .slf4j .Logger ;
40- import org .slf4j .LoggerFactory ;
41-
42- import io .netty .buffer .ByteBufAllocator ;
43- import io .netty .handler .ssl .OpenSsl ;
44- import io .netty .handler .ssl .OpenSslX509KeyManagerFactory ;
45- import io .netty .handler .ssl .SslContext ;
46- import io .netty .handler .ssl .SslContextBuilder ;
47- import io .netty .handler .ssl .SslHandler ;
48- import io .netty .handler .ssl .SslProvider ;
49- import io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
38+ import java .io .ByteArrayInputStream ;
39+ import java .io .FileInputStream ;
40+ import java .io .InputStream ;
41+ import java .net .URI ;
42+ import java .security .KeyStore ;
43+ import java .security .KeyStoreException ;
44+ import java .security .SecureRandom ;
45+ import java .util .ArrayList ;
46+ import java .util .Arrays ;
47+ import java .util .Base64 ;
48+ import java .util .List ;
5049
5150/**
5251 * Static class that provides various utility methods used by Transport implementations.
@@ -341,39 +340,39 @@ private static TrustManagerFactory loadTrustManagerFactory(TransportOptions opti
341340 return InsecureTrustManagerFactory .INSTANCE ;
342341 }
343342
344- if (options .getTrustStoreLocation () == null ) {
343+ String storeLocation = options .getTrustStoreLocation ();
344+ String storeBase64Property = options .getTrustStoreBase64Property ();
345+ if (storeLocation == null && storeBase64Property == null ) {
345346 return null ;
347+ } else if (storeLocation != null && storeBase64Property != null ) {
348+ throw new IllegalArgumentException ("Only one of trustStoreLocation and trustStoreBase64Property should be defined" );
346349 }
347350
348351 TrustManagerFactory fact = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
349352
350- String storeLocation = options .getTrustStoreLocation ();
351353 String storePassword = options .getTrustStorePassword ();
352354 String storeType = options .getTrustStoreType ();
353-
354- LOG .trace ("Attempt to load TrustStore from location {} of type {}" , storeLocation , storeType );
355-
356- KeyStore trustStore = loadStore (storeLocation , storePassword , storeType );
355+ KeyStore trustStore = loadStore (storeLocation , storeBase64Property , storePassword , storeType );
357356 fact .init (trustStore );
358357
359358 return fact ;
360359 }
361360
362361 private static KeyManager [] loadKeyManagers (TransportOptions options ) throws Exception {
363- if (options .getKeyStoreLocation () == null ) {
362+ String storeLocation = options .getKeyStoreLocation ();
363+ String storeBase64Property = options .getKeyStoreBase64Property ();
364+ if (storeLocation == null && storeBase64Property == null ) {
364365 return null ;
366+ } else if (storeLocation != null && storeBase64Property != null ) {
367+ throw new IllegalArgumentException ("Only one of keyStoreLocation and keyStoreBase64Property should be defined" );
365368 }
366369
367370 KeyManagerFactory fact = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
368371
369- String storeLocation = options .getKeyStoreLocation ();
370372 String storePassword = options .getKeyStorePassword ();
371373 String storeType = options .getKeyStoreType ();
372374 String alias = options .getKeyAlias ();
373-
374- LOG .trace ("Attempt to load KeyStore from location {} of type {}" , storeLocation , storeType );
375-
376- KeyStore keyStore = loadStore (storeLocation , storePassword , storeType );
375+ KeyStore keyStore = loadStore (storeLocation , storeBase64Property , storePassword , storeType );
377376 fact .init (keyStore , storePassword != null ? storePassword .toCharArray () : null );
378377
379378 if (alias == null ) {
@@ -385,8 +384,12 @@ private static KeyManager[] loadKeyManagers(TransportOptions options) throws Exc
385384 }
386385
387386 private static KeyManagerFactory loadKeyManagerFactory (TransportOptions options , SslProvider provider ) throws Exception {
388- if (options .getKeyStoreLocation () == null ) {
387+ String storeLocation = options .getKeyStoreLocation ();
388+ String storeBase64Property = options .getKeyStoreBase64Property ();
389+ if (storeLocation == null && storeBase64Property == null ) {
389390 return null ;
391+ } else if (storeLocation != null && storeBase64Property != null ) {
392+ throw new IllegalArgumentException ("Only one of keyStoreLocation and keyStoreBase64Property should be defined" );
390393 }
391394
392395 final KeyManagerFactory factory ;
@@ -396,13 +399,9 @@ private static KeyManagerFactory loadKeyManagerFactory(TransportOptions options,
396399 factory = new OpenSslX509KeyManagerFactory ();
397400 }
398401
399- String storeLocation = options .getKeyStoreLocation ();
400402 String storePassword = options .getKeyStorePassword ();
401403 String storeType = options .getKeyStoreType ();
402-
403- LOG .trace ("Attempt to load KeyStore from location {} of type {}" , storeLocation , storeType );
404-
405- KeyStore keyStore = loadStore (storeLocation , storePassword , storeType );
404+ KeyStore keyStore = loadStore (storeLocation , storeBase64Property , storePassword , storeType );
406405 factory .init (keyStore , storePassword != null ? storePassword .toCharArray () : null );
407406
408407 return factory ;
@@ -432,12 +431,33 @@ private static void validateAlias(KeyStore store, String alias) throws IllegalAr
432431 }
433432 }
434433
435- private static KeyStore loadStore (String storePath , final String password , String storeType ) throws Exception {
434+ private static KeyStore loadStore (final String storeLocation , final String storeBase64Property , final String password , String storeType ) throws Exception {
435+ KeyStore store ;
436+ if (storeLocation != null ) {
437+ LOG .trace ("Attempt to load store from location {} of type {}" , storeLocation , storeType );
438+ store = loadStoreFromFile (storeLocation , password , storeType );
439+ } else {
440+ LOG .trace ("Attempt to load store from system property {} of type {}" , storeBase64Property , storeType );
441+ store = loadStoreFromSystemProperty (storeBase64Property , password , storeType );
442+ }
443+ return store ;
444+ }
445+
446+ private static KeyStore loadStoreFromFile (final String storePath , final String password , final String storeType ) throws Exception {
436447 KeyStore store = KeyStore .getInstance (storeType );
437- try (InputStream in = new FileInputStream (new File ( storePath )); ) {
448+ try (InputStream in = new FileInputStream (storePath )) {
438449 store .load (in , password != null ? password .toCharArray () : null );
439450 }
440451
441452 return store ;
442453 }
454+
455+ private static KeyStore loadStoreFromSystemProperty (final String property , final String password , final String storeType ) throws Exception {
456+ KeyStore store = KeyStore .getInstance (storeType );
457+ String keyStoreBase64 = System .getProperty (property );
458+ byte [] keyStoreBytes = Base64 .getDecoder ().decode (keyStoreBase64 );
459+ store .load (new ByteArrayInputStream (keyStoreBytes ), password != null ? password .toCharArray () : null );
460+
461+ return store ;
462+ }
443463}
0 commit comments