1 module hio.tls.openssl;
2 
3 version(openssl11):
4 //
5 shared static this()
6 {
7     init_ssl_library();
8 }
9 import std.experimental.logger;
10 import hio.tls.common;
11 
12 /+
13 # define SSL_ERROR_NONE                  0
14 # define SSL_ERROR_SSL                   1
15 # define SSL_ERROR_WANT_READ             2
16 # define SSL_ERROR_WANT_WRITE            3
17 # define SSL_ERROR_WANT_X509_LOOKUP      4
18 # define SSL_ERROR_SYSCALL               5/* look at error stack/return
19                                            * value/errno */
20 # define SSL_ERROR_ZERO_RETURN           6
21 # define SSL_ERROR_WANT_CONNECT          7
22 # define SSL_ERROR_WANT_ACCEPT           8
23 # define SSL_ERROR_WANT_ASYNC            9
24 # define SSL_ERROR_WANT_ASYNC_JOB       10
25 # define SSL_ERROR_WANT_CLIENT_HELLO_CB 11
26 +/
27 
28 package enum {
29     SSL_ERROR_NONE = 0,
30     SSL_ERROR_SSL = 1,
31     SSL_ERROR_WANT_READ = 2,
32     SSL_ERROR_WANT_WRITE = 3,
33     SSL_ERROR_WANT_X509_LOOKUP = 4,
34     SSL_ERROR_SYSCALL = 5, /* look at error stack/return
35                                             * value/errno */
36     SSL_ERROR_ZERO_RETURN = 6,
37     SSL_ERROR_WANT_CONNECT = 7,
38     SSL_ERROR_WANT_ACCEPT = 8,
39     SSL_ERROR_WANT_ASYNC = 9,
40     SSL_ERROR_WANT_ASYNC_JOB = 10,
41     SSL_ERROR_WANT_CLIENT_HELLO_CB = 11
42 }
43 
44 immutable SSL_error_strings = [
45     "SSL_ERROR_NONE",
46     "SSL_ERROR_SSL",
47     "SSL_ERROR_WANT_READ",
48     "SSL_ERROR_WANT_WRITE",
49     "SSL_ERROR_WANT_X509_LOOKUP",
50     "SSL_ERROR_SYSCALL",
51     "SSL_ERROR_ZERO_RETURN",
52     "SSL_ERROR_WANT_CONNECT",
53     "SSL_ERROR_WANT_ACCEPT",
54     "SSL_ERROR_WANT_ASYNC",
55     "SSL_ERROR_WANT_ASYNC_JOB",
56     "SSL_ERROR_WANT_CLIENT_HELLO_CB"
57 ];
58 
59 package struct SSL {}
60 package struct SSL_CTX {}
61 package struct SSL_METHOD {}
62 
63 enum SSL_FILETYPE_PEM = 1;
64 
65 package extern(C)
66 {
67     int         OPENSSL_init_ssl(ulong, void*) @trusted nothrow;
68     int         OPENSSL_init_crypto(ulong, void*) @trusted nothrow;
69     SSL_METHOD* TLS_method() @trusted nothrow;
70     SSL_METHOD* TLS_client_method() @trusted nothrow;
71     SSL_METHOD* TLS_server_method() @trusted nothrow;
72     SSL_CTX*    SSL_CTX_new(SSL_METHOD*) @trusted nothrow;
73     void        SSL_CTX_free(SSL_CTX*) @trusted nothrow;
74     int         SSL_CTX_use_PrivateKey_file(SSL_CTX*, const char*, int) @trusted nothrow;
75     int         SSL_CTX_use_certificate_file(SSL_CTX*, const char*, int) @trusted nothrow;
76     void        SSL_CTX_set_verify(SSL_CTX*, int, void*) @trusted nothrow;
77     int         SSL_CTX_set_cipher_list(SSL_CTX*, const char *str) @trusted nothrow;
78     SSL*        SSL_new(SSL_CTX*) @trusted nothrow;
79     int         SSL_set_fd(SSL*, int) @trusted nothrow;
80     int         SSL_connect(SSL*) @trusted nothrow;
81     int         SSL_accept(SSL*) @trusted nothrow;
82     int         SSL_get_error(SSL*, int) @trusted nothrow;
83     long        SSL_ctrl(SSL*, int, long, void*) @trusted nothrow;
84     void        SSL_set_connect_state(SSL*) @trusted nothrow;
85     void        SSL_set_accept_state(SSL*) @trusted nothrow;
86     int         SSL_set_cipher_list(SSL *ssl, const char *str) @trusted nothrow;
87     int         SSL_read(SSL*, void *, int) @trusted nothrow;
88     int         SSL_write(SSL*, void*, int) @trusted nothrow;
89     void        SSL_free(SSL*) @trusted nothrow;
90     char*       ERR_reason_error_string(ulong) @trusted nothrow;
91     char*       ERR_error_string(ulong, char*) @trusted nothrow;
92     ulong       ERR_get_error() @trusted nothrow;
93     void        OPENSSL_cleanup() @trusted nothrow;
94 }
95 
96 void init_ssl_library()
97 {
98         /**
99         Standard initialisation options
100 
101         #define OPENSSL_INIT_LOAD_SSL_STRINGS       0x00200000L
102 
103         # define OPENSSL_INIT_LOAD_CRYPTO_STRINGS    0x00000002L
104         # define OPENSSL_INIT_ADD_ALL_CIPHERS        0x00000004L
105         # define OPENSSL_INIT_ADD_ALL_DIGESTS        0x00000008L
106         **/
107         enum OPENSSL_INIT_LOAD_SSL_STRINGS = 0x00200000L;
108         enum OPENSSL_INIT_LOAD_CRYPTO_STRINGS = 0x00000002L;
109         enum OPENSSL_INIT_ADD_ALL_CIPHERS = 0x00000004L;
110         enum OPENSSL_INIT_ADD_ALL_DIGESTS = 0x00000008L;
111         OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, null);
112         OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, null);
113 }