31
31
import java .io .InputStream ;
32
32
import java .security .cert .CertificateException ;
33
33
import java .util .Properties ;
34
- import javax .net .ssl .SSLException ;
35
34
import org .apache .rocketmq .remoting .common .RemotingHelper ;
36
35
import org .slf4j .Logger ;
37
36
import org .slf4j .LoggerFactory ;
38
37
39
38
public class SslHelper {
40
39
40
+ public interface DecryptionStrategy {
41
+ /**
42
+ * Decrypt the target encrpted private key file.
43
+ *
44
+ * @param privateKeyEncryptPath A pathname string
45
+ * @param forClient tells whether it's a client-side key file
46
+ * @return An input stream for a decrypted key file
47
+ * @throws IOException if an I/O error has occurred
48
+ */
49
+ InputStream decryptPrivateKey (String privateKeyEncryptPath , boolean forClient ) throws IOException ;
50
+ }
51
+
41
52
private static final Logger LOGGER = LoggerFactory .getLogger (RemotingHelper .ROCKETMQ_REMOTING );
42
53
43
- public static SslContext buildSslContext (boolean forClient ) throws SSLException , CertificateException {
54
+ private static DecryptionStrategy decryptionStrategy = new DecryptionStrategy () {
55
+ @ Override
56
+ public InputStream decryptPrivateKey (final String privateKeyEncryptPath ,
57
+ final boolean forClient ) throws IOException {
58
+ return new FileInputStream (privateKeyEncryptPath );
59
+ }
60
+ };
61
+
62
+
63
+ public static void registerDecryptionStrategy (final DecryptionStrategy decryptionStrategy ) {
64
+ SslHelper .decryptionStrategy = decryptionStrategy ;
65
+ }
66
+
67
+ public static SslContext buildSslContext (boolean forClient ) throws IOException , CertificateException {
44
68
45
69
File configFile = new File (NettySystemConfig .sslConfigFile );
46
70
boolean testMode = !(configFile .exists () && configFile .isFile () && configFile .canRead ());
@@ -92,8 +116,8 @@ public static SslContext buildSslContext(boolean forClient) throws SSLException,
92
116
}
93
117
94
118
return sslContextBuilder .keyManager (
95
- properties .containsKey ("client.keyCertChainFile" ) ? new File (properties .getProperty ("client.keyCertChainFile" )) : null ,
96
- properties .containsKey ("client.keyFile" ) ? new File (properties .getProperty ("client.keyFile" )) : null ,
119
+ properties .containsKey ("client.keyCertChainFile" ) ? new FileInputStream (properties .getProperty ("client.keyCertChainFile" )) : null ,
120
+ properties .containsKey ("client.keyFile" ) ? decryptionStrategy . decryptPrivateKey (properties .getProperty ("client.keyFile" ), true ) : null ,
97
121
properties .containsKey ("client.password" ) ? properties .getProperty ("client.password" ) : null )
98
122
.build ();
99
123
}
@@ -108,8 +132,8 @@ public static SslContext buildSslContext(boolean forClient) throws SSLException,
108
132
.build ();
109
133
} else {
110
134
return SslContextBuilder .forServer (
111
- properties .containsKey ("server.keyCertChainFile" ) ? new File (properties .getProperty ("server.keyCertChainFile" )) : null ,
112
- properties .containsKey ("server.keyFile" ) ? new File (properties .getProperty ("server.keyFile" )) : null ,
135
+ properties .containsKey ("server.keyCertChainFile" ) ? new FileInputStream (properties .getProperty ("server.keyCertChainFile" )) : null ,
136
+ properties .containsKey ("server.keyFile" ) ? decryptionStrategy . decryptPrivateKey (properties .getProperty ("server.keyFile" ), false ) : null ,
113
137
properties .containsKey ("server.password" ) ? properties .getProperty ("server.password" ) : null )
114
138
.sslProvider (provider )
115
139
.trustManager (new File (properties .getProperty ("server.trustManager" )))
0 commit comments