1 module hunt.net.secure.SecureUtils;
2 
3 // dfmt off
4 version(WITH_HUNT_SECURITY):
5 // dfmt on
6 
7 import hunt.net.ssl.SSLContext;
8 import hunt.net.Connection;
9 import hunt.net.KeyCertOptions;
10 import hunt.net.secure.SecureSession;
11 import hunt.net.secure.SecureSessionFactory;
12 import hunt.net.secure.conscrypt.AbstractConscryptSSLContextFactory;
13 import hunt.net.secure.conscrypt.ConscryptSecureSessionFactory;
14 
15 import std.array;
16 import std.concurrency : initOnce;
17 
18 /**
19  * 
20  */
21 struct SecureUtils {
22     /**
23      * Get the SSL/TLS connection factory.
24      *
25      * @return the SSL/TLS connection factory.
26      */
27     static SecureSessionFactory secureSessionFactory() {
28         __gshared ConscryptSecureSessionFactory inst;
29         return initOnce!inst(new ConscryptSecureSessionFactory());
30     }
31 
32     static void setServerCertificate(KeyCertOptions options) {
33         assert(options !is null);
34         FileCredentialConscryptSSLContextFactory fc = 
35             new FileCredentialConscryptSSLContextFactory(options);
36         SSLContext context = fc.getSSLContext(); // initlialize the default session context
37         secureSessionFactory().setServerSSLContextFactory(fc);
38     }
39 
40     static SecureSession createClientSession(Connection connection, SecureSessionHandshakeListener handler) {
41         return secureSessionFactory().create(connection, true, handler);
42     }
43 
44     static SecureSession createClientSession(Connection connection, SecureSessionHandshakeListener handler, 
45             KeyCertOptions options) {
46         return secureSessionFactory().create(connection, true, handler, options);
47     }
48 
49     static SecureSession createServerSession(Connection connection, SecureSessionHandshakeListener handler) {
50         return secureSessionFactory().create(connection, false, handler);
51     }
52 
53     SSLContext getServerSslContext() {
54         AbstractConscryptSSLContextFactory factory = 
55             cast(AbstractConscryptSSLContextFactory)secureSessionFactory().getServerSSLContextFactory();
56         return factory.getSSLContext();
57     }
58 
59 }