1 module requests.ssl_adapter;
2 
3 import std.stdio;
4 import std.string;
5 import std.format;
6 import std.typecons;
7 import core.stdc.stdlib;
8 import core.sys.posix.dlfcn;
9 import std.experimental.logger;
10 
11 version(Windows) {
12     import core.sys.windows.windows;
13     alias DLSYM = GetProcAddress;
14 } else {
15     alias DLSYM = dlsym;
16 }
17 
18 /*
19  * /usr/include/openssl/tls1.h:# define TLS_ANY_VERSION 0x10000
20  */
21 
22 immutable int TLS_ANY_VERSION = 0x10000;
23 immutable int TLS1_VERSION = 0x0301;
24 immutable int TLS1_2_VERSION = 0x0303;
25 
26 struct SSL {};
27 struct SSL_CTX {};
28 struct SSL_METHOD {};
29 
30 string SSL_Function_decl(string N, R, A...)() {
31     string F = "extern (C) %s function %s adapter_%s;".format(R.stringof, A.stringof, N);
32     return F;
33 }
34 string SSL_Function_set_i(string N, R, A...)() {
35     string F = "openssl.adapter_%s = cast(typeof(openssl.adapter_%s))DLSYM(cast(void*)openssl._libssl, \"%s\");".format(N, N, N);
36     return F;
37 }
38 string CRYPTO_Function_set_i(string N, R, A...)() {
39     string F = "openssl.adapter_%s = cast(typeof(openssl.adapter_%s))DLSYM(cast(void*)openssl._libcrypto, \"%s\");".format(N, N, N);
40     return F;
41 }
42 
43 private alias Version = Tuple!(int, "major", int, "minor");
44 
45 immutable static OpenSSL openssl;
46 
47 static this() {
48     if ( openssl._libssl !is null ) {
49         return;
50     }
51     version(OSX) {
52         openssl._libssl = cast(typeof(openssl._libssl))dlopen("libssl.dylib", RTLD_LAZY);
53         openssl._libcrypto = cast(typeof(openssl._libcrypto))dlopen("libcrypto.dylib", RTLD_LAZY);
54     } else
55     version(linux) {
56         immutable string[] libsslname = [
57             "libssl.so",
58             "libssl.so.1.1",
59             "libssl.so.1.0.2",
60             "libssl.so.1.0.1",
61             "libssl.so.1.0.0"
62         ];
63         foreach(lib; libsslname) {
64             openssl._libssl = cast(typeof(openssl._libssl))dlopen(lib.ptr, RTLD_LAZY);
65             if ( openssl._libssl !is null ) {
66                 debug(requests) tracef("will use %s".format(lib));
67                 break;
68             }
69         }
70         immutable string[] libcryptoname = [
71             "libcrypto.so",
72             "libcrypto.so.1.1",
73             "libcrypto.so.1.0.2",
74             "libcrypto.so.1.0.1",
75             "libcrypto.so.1.0.0"
76         ];
77         foreach(lib; libcryptoname) {
78             openssl._libcrypto = cast(typeof(openssl._libcrypto))dlopen(lib.ptr, RTLD_LAZY);
79             if ( openssl._libcrypto !is null ) {
80                 debug(requests) tracef("will use %s".format(lib));
81                 break;
82             }
83         }
84     } else
85     version(Windows) {
86         openssl._libssl = cast(typeof(openssl._libssl))LoadLibrary("libssl32.dll");
87         openssl._libcrypto = cast(typeof(openssl._libcrypto))LoadLibrary("libeay32.dll");
88     } else {
89         throw new Exception("loading openssl: unsupported system");
90     }
91     if ( openssl._libssl is null ) {
92         error("warning: failed to load libssl - first access over https will fail");
93         return;
94     }
95     if ( openssl._libcrypto is null ) {
96         error("warning: failed to load libcrypto - first access over https will fail");
97         return;
98     }
99     openssl._ver = openssl.OpenSSL_version_detect();
100 
101     mixin(SSL_Function_set_i!("SSL_library_init", int));
102     mixin(CRYPTO_Function_set_i!("OpenSSL_add_all_ciphers", void));
103     mixin(CRYPTO_Function_set_i!("OpenSSL_add_all_digests", void));
104     mixin(SSL_Function_set_i!("SSL_load_error_strings", void));
105 
106     mixin(SSL_Function_set_i!("OPENSSL_init_ssl", int, ulong, void*));
107     mixin(CRYPTO_Function_set_i!("OPENSSL_init_crypto", int, ulong, void*));
108 
109     mixin(SSL_Function_set_i!("TLSv1_client_method", SSL_METHOD*));
110     mixin(SSL_Function_set_i!("TLSv1_2_client_method", SSL_METHOD*));
111     mixin(SSL_Function_set_i!("TLS_method", SSL_METHOD*));
112     mixin(SSL_Function_set_i!("SSLv23_client_method", SSL_METHOD*));
113     mixin(SSL_Function_set_i!("SSL_CTX_new", SSL_CTX*, SSL_METHOD*));
114     mixin(SSL_Function_set_i!("SSL_CTX_set_default_verify_paths", int, SSL_CTX*));
115     mixin(SSL_Function_set_i!("SSL_CTX_load_verify_locations", int, SSL_CTX*, char*, char*));
116     mixin(SSL_Function_set_i!("SSL_CTX_set_verify", void, SSL_CTX*, int, void*));
117     mixin(SSL_Function_set_i!("SSL_CTX_use_PrivateKey_file", int, SSL_CTX*, const char*, int));
118     mixin(SSL_Function_set_i!("SSL_CTX_use_certificate_file", int, SSL_CTX*, const char*, int));
119     mixin(SSL_Function_set_i!("SSL_CTX_set_cipher_list", int, SSL_CTX*, const char*));
120     mixin(SSL_Function_set_i!("SSL_CTX_ctrl", long, SSL_CTX*, int, long, void*));
121     mixin(SSL_Function_set_i!("SSL_new", SSL*, SSL_CTX*));
122     mixin(SSL_Function_set_i!("SSL_set_fd", int, SSL*, int));
123     mixin(SSL_Function_set_i!("SSL_connect", int, SSL*));
124     mixin(SSL_Function_set_i!("SSL_write", int, SSL*, const void*, int));
125     mixin(SSL_Function_set_i!("SSL_read", int, SSL*, void*, int));
126     mixin(SSL_Function_set_i!("SSL_free", void, SSL*));
127     mixin(SSL_Function_set_i!("SSL_CTX_free", void, SSL_CTX*));
128     mixin(SSL_Function_set_i!("SSL_get_error", int, SSL*, int));
129     mixin(SSL_Function_set_i!("SSL_ctrl", long, SSL*, int, long, void*));
130     mixin(SSL_Function_set_i!("ERR_reason_error_string", char*, ulong));
131     mixin(SSL_Function_set_i!("ERR_get_error", ulong));
132 
133     void delegate()[Version] init_matrix;
134     init_matrix[Version(1,0)] = &openssl.init1_0;
135     init_matrix[Version(1,1)] = &openssl.init1_1;
136     auto init = init_matrix.get(openssl._ver, null);
137     if ( init is null ) {
138         throw new Exception("loading openssl: unknown version for init");
139     }
140     init();
141 }
142 
143 struct OpenSSL {
144 
145     private {
146         Version         _ver;
147         void*           _libssl;
148         void*           _libcrypto;
149 
150         // openssl 1.0.x init functions
151         mixin(SSL_Function_decl!("SSL_library_init", int));
152         mixin(SSL_Function_decl!("OpenSSL_add_all_ciphers", void));
153         mixin(SSL_Function_decl!("OpenSSL_add_all_digests", void));
154         mixin(SSL_Function_decl!("SSL_load_error_strings", void));
155 
156         // openssl 1.1.x init functions
157         mixin(SSL_Function_decl!("OPENSSL_init_ssl", int, ulong, void*));
158         mixin(SSL_Function_decl!("OPENSSL_init_crypto", int, ulong, void*));
159 
160         // all other functions
161         mixin(SSL_Function_decl!("TLSv1_client_method", SSL_METHOD*));
162         mixin(SSL_Function_decl!("TLSv1_2_client_method", SSL_METHOD*));
163         mixin(SSL_Function_decl!("TLS_method", SSL_METHOD*));
164         mixin(SSL_Function_decl!("SSLv23_client_method", SSL_METHOD*));
165         mixin(SSL_Function_decl!("SSL_CTX_new", SSL_CTX*, SSL_METHOD*));
166         mixin(SSL_Function_decl!("SSL_CTX_set_default_verify_paths", int, SSL_CTX*));
167         mixin(SSL_Function_decl!("SSL_CTX_load_verify_locations", int, SSL_CTX*, char*, char*));
168         mixin(SSL_Function_decl!("SSL_CTX_set_verify", void, SSL_CTX*, int, void*));
169         mixin(SSL_Function_decl!("SSL_CTX_use_PrivateKey_file", int, SSL_CTX*, const char*, int));
170         mixin(SSL_Function_decl!("SSL_CTX_use_certificate_file", int, SSL_CTX*, const char*, int));
171         mixin(SSL_Function_decl!("SSL_CTX_set_cipher_list", int, SSL_CTX*, const char*));
172         mixin(SSL_Function_decl!("SSL_CTX_ctrl", long, SSL_CTX*, int, long, void*));
173         mixin(SSL_Function_decl!("SSL_new", SSL*, SSL_CTX*));
174         mixin(SSL_Function_decl!("SSL_set_fd", int, SSL*, int));
175         mixin(SSL_Function_decl!("SSL_connect", int, SSL*));
176         mixin(SSL_Function_decl!("SSL_write", int, SSL*, const void*, int));
177         mixin(SSL_Function_decl!("SSL_read", int, SSL*, void*, int));
178         mixin(SSL_Function_decl!("SSL_free", void, SSL*));
179         mixin(SSL_Function_decl!("SSL_CTX_free", void, SSL_CTX*));
180         mixin(SSL_Function_decl!("SSL_get_error", int, SSL*, int));
181         mixin(SSL_Function_decl!("SSL_ctrl", long, SSL*, int, long, void*));
182         mixin(SSL_Function_decl!("ERR_reason_error_string", char*, ulong));
183         mixin(SSL_Function_decl!("ERR_get_error", ulong));
184     }
185 
186     Version reportVersion() const @nogc nothrow pure {
187         return _ver;
188     };
189 
190     private Version OpenSSL_version_detect() const {
191         ulong function() OpenSSL_version_num = cast(ulong function())DLSYM(cast(void*)_libcrypto, "OpenSSL_version_num".ptr);
192         if ( OpenSSL_version_num ) {
193             auto v = OpenSSL_version_num() & 0xffffffff;
194             return Version((v>>>20) & 0xff, (v>>>28) & 0xff);
195         }
196         return Version(1, 0);
197     }
198 
199     private void init1_0() const {
200         adapter_SSL_library_init();
201         adapter_OpenSSL_add_all_ciphers();
202         adapter_OpenSSL_add_all_digests();
203         adapter_SSL_load_error_strings();
204     }
205     private void init1_1() const {
206         /**
207         Standard initialisation options
208 
209         #define OPENSSL_INIT_LOAD_SSL_STRINGS       0x00200000L
210 
211         # define OPENSSL_INIT_LOAD_CRYPTO_STRINGS    0x00000002L
212         # define OPENSSL_INIT_ADD_ALL_CIPHERS        0x00000004L
213         # define OPENSSL_INIT_ADD_ALL_DIGESTS        0x00000008L
214         **/
215         enum OPENSSL_INIT_LOAD_SSL_STRINGS = 0x00200000L;
216         enum OPENSSL_INIT_LOAD_CRYPTO_STRINGS = 0x00000002L;
217         enum OPENSSL_INIT_ADD_ALL_CIPHERS = 0x00000004L;
218         enum OPENSSL_INIT_ADD_ALL_DIGESTS = 0x00000008L;
219         adapter_OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, null);
220         adapter_OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, null);
221     }
222 
223     SSL_METHOD* TLSv1_client_method() const {
224         if ( adapter_TLSv1_client_method is null ) {
225             throw new Exception("openssl not initialized - is it installed?");
226         }
227         return adapter_TLSv1_client_method();
228     }
229     SSL_METHOD* TLSv1_2_client_method() const {
230         if ( adapter_TLSv1_2_client_method is null ) {
231             throw new Exception("openssl not initialized - is it installed?");
232         }
233         return adapter_TLSv1_2_client_method();
234     }
235     SSL_METHOD* SSLv23_client_method() const {
236         if ( adapter_SSLv23_client_method is null ) {
237             throw new Exception("can't complete call to SSLv23_client_method");
238         }
239         return adapter_SSLv23_client_method();
240     }
241     SSL_METHOD* TLS_method() const {
242         if ( adapter_TLS_method !is null ) {
243             return adapter_TLS_method();
244         }
245         if ( adapter_SSLv23_client_method !is null ) {
246             return adapter_SSLv23_client_method();
247         }
248         throw new Exception("can't complete call to TLS_method");
249     }
250     SSL_CTX* SSL_CTX_new(SSL_METHOD* method) const {
251         if ( adapter_SSL_CTX_new is null ) {
252             throw new Exception("openssl not initialized - is it installed?");
253         }
254         return adapter_SSL_CTX_new(method);
255     }
256     int SSL_CTX_set_default_verify_paths(SSL_CTX* ctx) const {
257         return adapter_SSL_CTX_set_default_verify_paths(ctx);
258     }
259     int SSL_CTX_load_verify_locations(SSL_CTX* ctx, char* CAFile, char* CAPath) const {
260         return adapter_SSL_CTX_load_verify_locations(ctx, CAFile, CAPath);
261     }
262     void SSL_CTX_set_verify(SSL_CTX* ctx, int mode, void* callback) const {
263         adapter_SSL_CTX_set_verify(ctx, mode, callback);
264     }
265     int SSL_CTX_use_PrivateKey_file(SSL_CTX* ctx, const char* file, int type) const {
266         return adapter_SSL_CTX_use_PrivateKey_file(ctx, file, type);
267     }
268     int SSL_CTX_use_certificate_file(SSL_CTX* ctx, const char* file, int type) const {
269         return adapter_SSL_CTX_use_certificate_file(ctx, file, type);
270     }
271     int SSL_CTX_set_cipher_list(SSL_CTX* ssl_ctx, const char* c) const {
272         return adapter_SSL_CTX_set_cipher_list(ssl_ctx, c);
273     }
274     /*
275      *
276      * # define SSL_CTRL_SET_MIN_PROTO_VERSION          123
277      * # define SSL_CTRL_SET_MAX_PROTO_VERSION          124
278     */
279     enum int SSL_CTRL_SET_MIN_PROTO_VERSION = 123;
280     enum int SSL_CTRL_SET_MAX_PROTO_VERSION = 124;
281     int SSL_CTX_set_min_proto_version(SSL_CTX* ctx, int v) const {
282         int r = cast(int)adapter_SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION, cast(long)v, null);
283         return r;
284     }
285     int SSL_CTX_set_max_proto_version(SSL_CTX* ctx, int v) const {
286         int r = cast(int)adapter_SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION, cast(long)v, null);
287         return r;
288     }
289     SSL* SSL_new(SSL_CTX* ctx) const {
290         return adapter_SSL_new(ctx);
291     }
292     int SSL_set_fd(SSL* ssl, int fd) const {
293         return adapter_SSL_set_fd(ssl, fd);
294     }
295     int SSL_connect(SSL* ssl) const {
296         return adapter_SSL_connect(ssl);
297     }
298     int SSL_read(SSL* ssl, void *b, int n) const {
299         return adapter_SSL_read(ssl, b, n);
300     }
301     int SSL_write(SSL* ssl, const void *b, int n) const {
302         return adapter_SSL_write(ssl, b, n);
303     }
304     void SSL_free(SSL* ssl) const {
305         adapter_SSL_free(ssl);
306     }
307     void SSL_CTX_free(SSL_CTX* ctx) const {
308         adapter_SSL_CTX_free(ctx);
309     }
310     int SSL_get_error(SSL* ssl, int err) const {
311         return adapter_SSL_get_error(ssl, err);
312     }
313     long SSL_set_tlsext_host_name(SSL* ssl, const char* host) const {
314         enum int SSL_CTRL_SET_TLSEXT_HOSTNAME = 55;
315         enum long TLSEXT_NAMETYPE_host_name = 0;
316         return adapter_SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME,TLSEXT_NAMETYPE_host_name, cast(void*)host);
317     }
318     char* ERR_reason_error_string(ulong code) const {
319         return adapter_ERR_reason_error_string(code);
320     }
321     ulong ERR_get_error() const {
322         return adapter_ERR_get_error();
323     }
324 }
325 /*
326 int main() {
327     import std.socket;
328 
329     auto v = openssl.reportVersion();
330     writefln("openSSL v.%s.%s", v.major, v.minor);
331     openssl.SSL_library_init();
332     writeln("InitSSL - ok");
333     SSL_CTX* ctx = openssl.SSL_CTX_new(openssl.TLSv1_client_method());
334     writefln("SSL_CTX_new = %x", ctx);
335     int r = openssl.adapter_SSL_CTX_set_default_verify_paths(ctx);
336     writefln("SSL_CTX_set_default_verify_paths = %d(%s)", r, r==1?"ok":"fail");
337     r = openssl.adapter_SSL_CTX_load_verify_locations(ctx, cast(char*)null, cast(char*)null);
338     writefln("SSL_CTX_load_verify_locations - ok");
339     openssl.SSL_CTX_set_verify(ctx, 0, null);
340     writefln("SSL_CTX_set_verify - ok");
341     //r = openssl.SSL_CTX_use_PrivateKey_file(ctx, null, 0);
342     //writefln("SSL_CTX_use_PrivateKey_file = %d(%s)", r, r==1?"ok":"fail");
343     //r = openssl.SSL_CTX_use_certificate_file(ctx, cast(char*), 0);
344     //writefln("SSL_CTX_use_certificate_file = %d(%s)", r, r==1?"ok":"fail");
345     SSL* ssl = openssl.SSL_new(ctx);
346     writefln("SSL_new = %x", ssl);
347     auto s = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
348     Address[] a = getAddress("ns.od.ua", 443);
349     writeln(a[0]);
350     s.connect(a[0]);
351     r = openssl.SSL_set_fd(ssl, s.handle);
352     writefln("SSL_set_fd = %d(%s)", r, r==1?"ok":"fail");
353     r = openssl.SSL_connect(ssl);
354     writefln("SSL_connect = %d(%s)", r, r==1?"ok":"fail");
355     if ( r < 0 ) {
356         writefln("code: %d", openssl.SSL_get_error(ssl, r));
357     }
358     string req = "GET / HTTP/1.0\n\n";
359     r = openssl.SSL_write(ssl, cast(void*)req.ptr, cast(int)req.length);
360     writefln("SSL_write = %d", r);
361     do {
362         ubyte[]  resp = new ubyte[](1024);
363         r = openssl.SSL_read(ssl, cast(void*)resp.ptr, cast(int)1024);
364         writefln("SSL_read = %d", r);
365         if ( r > 0 ) {
366             writeln(cast(string)resp);
367         }
368     } while(r > 0);
369     openssl.SSL_free(ssl);
370     openssl.SSL_CTX_free(ctx);
371     s.close();
372     return 0;
373 }
374 */