1 module requests.ssl_adapter_static;
2 
3 version(staticssl):
4 
5 import std.typecons;
6 import core.stdc.stdlib;
7 import core.stdc.config;
8 
9 version(Windows) {
10     static assert("static build not implemented for windows");
11 }
12 
13 struct SSL {};
14 struct SSL_CTX {};
15 struct SSL_METHOD {};
16 
17 immutable int TLS_ANY_VERSION = 0x10000;
18 immutable int TLS1_VERSION = 0x0301;
19 immutable int TLS1_2_VERSION = 0x0303;
20 
21 private alias Version = Tuple!(int, "major", int, "minor");
22 
23 struct openssl {
24     static Version reportVersion() @nogc nothrow pure {
25         return OpenSSL_version_detect();
26     }
27 
28     static private Version OpenSSL_version_detect() @nogc nothrow pure {
29         auto v = OpenSSL_version_num() & 0xffffffff;
30         return Version((v>>>20) & 0xff, (v>>>28) & 0xff);
31     }
32 
33     static int SSL_CTX_set_min_proto_version(SSL_CTX* ctx, int v) @nogc nothrow {
34         int r = cast(int)SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION, cast(c_long)v, null);
35         return r;
36     }
37     static int SSL_CTX_set_max_proto_version(SSL_CTX* ctx, int v) @nogc nothrow {
38         int r = cast(int)SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION, cast(c_long)v, null);
39         return r;
40     }
41 
42     static c_long SSL_set_tlsext_host_name(SSL* ssl, const char* host) @nogc nothrow {
43         enum SSL_CTRL_SET_TLSEXT_HOSTNAME = 55;
44         enum TLSEXT_NAMETYPE_host_name = 0;
45         return SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME,TLSEXT_NAMETYPE_host_name, cast(void*)host);
46     }
47     // extern (C) {
48         alias SSL_library_init = .SSL_library_init;
49         alias OpenSSL_add_all_ciphers = .OpenSSL_add_all_ciphers;
50         alias OpenSSL_add_all_digests = .OpenSSL_add_all_digests;
51         alias OpenSSL_version_num = .OpenSSL_version_num;
52         alias SSL_load_error_strings = .SSL_load_error_strings;
53         alias OPENSSL_init_ssl = .OPENSSL_init_ssl;
54         alias OPENSSL_init_crypto = .OPENSSL_init_crypto;
55         alias TLSv1_client_method = .TLSv1_client_method;
56         alias TLSv1_2_client_method = .TLSv1_2_client_method;
57         alias SSLv23_client_method = .SSLv23_client_method;
58         alias TLS_method = .TLS_method;
59         alias SSL_CTX_new = .SSL_CTX_new;
60         alias SSL_CTX_set_default_verify_paths = .SSL_CTX_set_default_verify_paths;
61         alias SSL_CTX_load_verify_locations = .SSL_CTX_load_verify_locations;
62         alias SSL_CTX_set_verify = .SSL_CTX_set_verify;
63         alias SSL_CTX_use_PrivateKey_file = .SSL_CTX_use_PrivateKey_file;
64         alias SSL_CTX_use_certificate_file = .SSL_CTX_use_certificate_file;
65         alias SSL_CTX_set_cipher_list = .SSL_CTX_set_cipher_list;
66 
67         /*
68          *
69          * # define SSL_CTRL_SET_MIN_PROTO_VERSION          123
70          * # define SSL_CTRL_SET_MAX_PROTO_VERSION          124
71          */
72         enum int SSL_CTRL_SET_MIN_PROTO_VERSION = 123;
73         enum int SSL_CTRL_SET_MAX_PROTO_VERSION = 124;
74         alias SSL_new = .SSL_new;
75         alias SSL_set_fd = .SSL_set_fd;
76         alias SSL_connect = .SSL_connect;
77         alias SSL_read = .SSL_read;
78         alias SSL_write = .SSL_write;
79         alias SSL_free = .SSL_free;
80         alias SSL_CTX_free = .SSL_CTX_free;
81         alias SSL_get_error = .SSL_get_error;
82         alias ERR_reason_error_string = .ERR_reason_error_string;
83         alias ERR_get_error = .ERR_get_error;
84         alias SSL_CTX_ctrl = .SSL_CTX_ctrl;
85         alias SSL_ctrl = .SSL_ctrl;
86     // }
87 }
88 
89 extern (C) {
90     static int SSL_library_init() @nogc nothrow @trusted;
91     static void OpenSSL_add_all_ciphers() @nogc nothrow @trusted;
92     static void OpenSSL_add_all_digests() @nogc nothrow @trusted;
93     static c_ulong OpenSSL_version_num() @nogc nothrow @trusted pure;
94     static void SSL_load_error_strings() @nogc nothrow @trusted;
95     static int OPENSSL_init_ssl(ulong, void*) @nogc nothrow @trusted;
96     static int OPENSSL_init_crypto(ulong, void*) @nogc nothrow @trusted;
97     static SSL_METHOD* TLSv1_client_method() nothrow @trusted;
98     static SSL_METHOD* TLSv1_2_client_method() nothrow @trusted;
99     static SSL_METHOD* SSLv23_client_method() nothrow @trusted;
100     static SSL_METHOD* TLS_method() nothrow @trusted;
101     static SSL_CTX* SSL_CTX_new(SSL_METHOD* method) @nogc nothrow @trusted;
102     static int SSL_CTX_set_default_verify_paths(SSL_CTX* ctx) @nogc nothrow;
103     static int SSL_CTX_load_verify_locations(SSL_CTX* ctx, char* CAFile, char* CAPath) @nogc nothrow;
104     static void SSL_CTX_set_verify(SSL_CTX* ctx, int mode, void* callback) @nogc nothrow;
105     static int SSL_CTX_use_PrivateKey_file(SSL_CTX* ctx, const char* file, int type) @nogc nothrow;
106     static int SSL_CTX_use_certificate_file(SSL_CTX* ctx, const char* file, int type) @nogc nothrow;
107     static int SSL_CTX_set_cipher_list(SSL_CTX* ssl_ctx, const char* c) @nogc nothrow;
108 
109     /*
110      *
111      * # define SSL_CTRL_SET_MIN_PROTO_VERSION          123
112      * # define SSL_CTRL_SET_MAX_PROTO_VERSION          124
113      */
114     enum int SSL_CTRL_SET_MIN_PROTO_VERSION = 123;
115     enum int SSL_CTRL_SET_MAX_PROTO_VERSION = 124;
116     static SSL* SSL_new(SSL_CTX* ctx) @nogc nothrow;
117     static int SSL_set_fd(SSL* ssl, int fd) @nogc nothrow;
118     static int SSL_connect(SSL* ssl) @nogc nothrow;
119     static int SSL_read(SSL* ssl, void *b, int n) @nogc nothrow;
120     static int SSL_write(SSL* ssl, const void *b, int n) @nogc nothrow;
121     static void SSL_free(SSL* ssl) @nogc nothrow @trusted;
122     static void SSL_CTX_free(SSL_CTX* ctx) @nogc nothrow @trusted;
123     static int SSL_get_error(SSL* ssl, int err) @nogc nothrow;
124     static char* ERR_reason_error_string(c_ulong code) @nogc nothrow;
125     static c_ulong ERR_get_error() @nogc nothrow;
126     static c_ulong SSL_CTX_ctrl(SSL_CTX*, int, c_long, void*) @nogc nothrow;
127     static c_ulong SSL_ctrl(SSL*, int, c_long, void*) @nogc nothrow;
128 }