1 module hunt.net.secure.conscrypt.ConscryptSecureSessionFactory;
2 
3 // dfmt off
4 version(WITH_HUNT_SECURITY):
5 // dfmt on
6 
7 import hunt.net.secure.conscrypt.AbstractConscryptSSLContextFactory;
8 import hunt.net.secure.conscrypt.ApplicationProtocolSelector;
9 import hunt.net.secure.conscrypt.ConscryptSSLSession;
10 
11 import hunt.net.secure.ProtocolSelector;
12 import hunt.net.secure.SecureSession;
13 import hunt.net.secure.SecureSessionFactory;
14 import hunt.net.secure.SSLContextFactory;
15 
16 import hunt.net.Connection;
17 import hunt.net.KeyCertOptions;
18 import hunt.net.ssl;
19 
20 import hunt.Exceptions;
21 import hunt.logging;
22 import hunt.util.TypeUtils;
23 
24 import std.typecons;
25 
26 /**
27  * 
28  */
29 class ConscryptSecureSessionFactory : SecureSessionFactory {
30 
31     private SSLContextFactory _clientSSLContextFactory; 
32     private SSLContextFactory _serverSSLContextFactory; 
33     private string[] supportedProtocols;
34 
35     this() {
36         // _clientSSLContextFactory = new NoCheckConscryptSSLContextFactory();
37         // _serverSSLContextFactory = new DefaultCredentialConscryptSSLContextFactory();
38     }
39 
40     SSLContextFactory getClientSSLContextFactory() {
41         return _clientSSLContextFactory;
42     }
43 
44     void setClientSSLContextFactory(SSLContextFactory clientSSLContextFactory) {
45         this._clientSSLContextFactory = clientSSLContextFactory;
46     }
47 
48     SSLContextFactory getServerSSLContextFactory() {
49         return _serverSSLContextFactory;
50     }
51 
52     void setServerSSLContextFactory(SSLContextFactory serverSSLContextFactory) {
53         this._serverSSLContextFactory = serverSSLContextFactory;
54     }
55 
56     SecureSession create(Connection session, bool clientMode, 
57         SecureSessionHandshakeListener secureSessionHandshakeListener) {
58 
59         SSLContextFactory sslContextFactory = from(clientMode);
60         sslContextFactory.setSupportedProtocols(supportedProtocols);
61         Pair!(SSLEngine, ProtocolSelector) p = sslContextFactory.createSSLEngine(clientMode);
62         return new ConscryptSSLSession(session, p.first, p.second, secureSessionHandshakeListener);
63     }
64 
65     SecureSession create(Connection session, bool clientMode,
66             SecureSessionHandshakeListener secureSessionHandshakeListener, 
67             KeyCertOptions options) {
68         
69         // assert(clientMode, "only client"); // only client
70 
71         SSLContextFactory sslContextFactory = new FileCredentialConscryptSSLContextFactory(options);
72         sslContextFactory.setSupportedProtocols(supportedProtocols);
73         Pair!(SSLEngine, ProtocolSelector) p = sslContextFactory.createSSLEngine(clientMode);
74         
75         return new ConscryptSSLSession(session, p.first, p.second, secureSessionHandshakeListener);
76     }
77 
78     protected SSLContextFactory from(bool clientMode) {
79         version(HUNT_NET_DEBUG) warning("clientMode: ", clientMode);
80         if(clientMode) {
81             if(_clientSSLContextFactory is null) _clientSSLContextFactory = new NoCheckConscryptSSLContextFactory();
82             return _clientSSLContextFactory;
83         } else {
84             return _serverSSLContextFactory;
85         }
86     }
87 
88     string[] getSupportedProtocols() {
89         return supportedProtocols;
90     }
91 
92     void setSupportedProtocols(string[] supportedProtocols) {
93         this.supportedProtocols = supportedProtocols;
94     }
95 
96 }