PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tls/vibe/stream/openssl.d

http://github.com/rejectedsoftware/vibe.d
D | 1419 lines | 1058 code | 199 blank | 162 comment | 164 complexity | 9477d2a25016516ac5df98478e44369d MD5 | raw file
  1. /**
  2. OpenSSL based SSL/TLS stream implementation
  3. Copyright: © 2012-2014 Sönke Ludwig
  4. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  5. Authors: Sönke Ludwig
  6. */
  7. module vibe.stream.openssl;
  8. version(Have_openssl):
  9. import vibe.core.log;
  10. import vibe.core.net;
  11. import vibe.core.stream;
  12. import vibe.core.sync;
  13. import vibe.stream.tls;
  14. import vibe.internal.interfaceproxy : InterfaceProxy;
  15. import std.algorithm;
  16. import std.array;
  17. import std.conv;
  18. import std.exception;
  19. import std.socket;
  20. import std.string;
  21. import core.stdc.string : strlen;
  22. import core.sync.mutex;
  23. import core.thread;
  24. /**************************************************************************************************/
  25. /* Public types */
  26. /**************************************************************************************************/
  27. import deimos.openssl.bio;
  28. import deimos.openssl.err;
  29. import deimos.openssl.rand;
  30. import deimos.openssl.ssl;
  31. import deimos.openssl.stack;
  32. import deimos.openssl.x509v3;
  33. // auto-detect OpenSSL 1.1.0
  34. version (VibeUseOpenSSL11)
  35. enum OPENSSL_VERSION = "1.1.0";
  36. else version (VibeUseOpenSSL10)
  37. enum OPENSSL_VERSION = "1.0.0";
  38. else version (Botan)
  39. enum OPENSSL_VERSION = "0.0.0";
  40. else
  41. {
  42. // Only use the openssl_version file if it has been generated
  43. static if (__traits(compiles, {import openssl_version; }))
  44. mixin("public import openssl_version : OPENSSL_VERSION;");
  45. else
  46. // try 1.1.0 as softfallback if old other means failed
  47. enum OPENSSL_VERSION = "1.1.0";
  48. }
  49. version (VibePragmaLib) {
  50. pragma(lib, "ssl");
  51. version (Windows) pragma(lib, "eay");
  52. }
  53. private enum haveECDH = OPENSSL_VERSION_NUMBER >= 0x10001000;
  54. version(VibeForceALPN) enum alpn_forced = true;
  55. else enum alpn_forced = false;
  56. enum haveALPN = OPENSSL_VERSION_NUMBER >= 0x10200000 || alpn_forced;
  57. // openssl/1.1.0 hack: provides a 1.0.x API in terms of the 1.1.x API
  58. static if (OPENSSL_VERSION.startsWith("1.1")) {
  59. extern(C) const(SSL_METHOD)* TLS_client_method();
  60. alias SSLv23_client_method = TLS_client_method;
  61. extern(C) const(SSL_METHOD)* TLS_server_method();
  62. alias SSLv23_server_method = TLS_server_method;
  63. // this does nothing in > openssl 1.1.0
  64. void SSL_load_error_strings() {}
  65. extern(C) int OPENSSL_init_ssl(ulong opts, const void* settings);
  66. // # define SSL_library_init() OPENSSL_init_ssl(0, NULL)
  67. int SSL_library_init() {
  68. return OPENSSL_init_ssl(0, null);
  69. }
  70. //# define CRYPTO_num_locks() (1)
  71. int CRYPTO_num_locks() {
  72. return 1;
  73. }
  74. void CRYPTO_set_id_callback(T)(T t) {
  75. }
  76. void CRYPTO_set_locking_callback(T)(T t) {
  77. }
  78. // #define SSL_get_ex_new_index(l, p, newf, dupf, freef) \
  79. // CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, l, p, newf, dupf, freef)
  80. extern(C) int CRYPTO_get_ex_new_index(int class_index, c_long argl, void *argp,
  81. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  82. CRYPTO_EX_free *free_func);
  83. int SSL_get_ex_new_index(c_long argl, void *argp,
  84. CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
  85. CRYPTO_EX_free *free_func) {
  86. // # define CRYPTO_EX_INDEX_SSL 0
  87. return CRYPTO_get_ex_new_index(0, argl, argp, new_func, dup_func,
  88. free_func);
  89. }
  90. extern(C) BIGNUM* BN_get_rfc3526_prime_2048(BIGNUM *bn);
  91. alias get_rfc3526_prime_2048 = BN_get_rfc3526_prime_2048;
  92. // # define sk_num OPENSSL_sk_num
  93. static if (!is(typeof(OPENSSL_sk_num)))
  94. {
  95. extern(C) int OPENSSL_sk_num(const void *);
  96. extern(C) int sk_num(const(_STACK)* p) { return OPENSSL_sk_num(p); }
  97. }
  98. // # define sk_value OPENSSL_sk_value
  99. static if (!is(typeof(OPENSSL_sk_value)))
  100. {
  101. extern(C) void *OPENSSL_sk_value(const void *, int);
  102. extern(C) void* sk_value(const(_STACK)* p, int i) { return OPENSSL_sk_value(p, i); }
  103. }
  104. private enum SSL_CTRL_SET_MIN_PROTO_VERSION = 123;
  105. private enum SSL_CTRL_SET_MAX_PROTO_VERSION = 124;
  106. private int SSL_CTX_set_min_proto_version(ssl_ctx_st* ctx, int ver) {
  107. return cast(int) SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION, ver, null);
  108. }
  109. private int SSL_CTX_set_max_proto_version(ssl_ctx_st* ctx, int ver) {
  110. return cast(int) SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION, ver, null);
  111. }
  112. private int SSL_set_min_proto_version(ssl_st* s, int ver) {
  113. return cast(int) SSL_ctrl(s, SSL_CTRL_SET_MIN_PROTO_VERSION, ver, null);
  114. }
  115. extern(C) nothrow {
  116. void BIO_set_init(BIO* bio, int init_) @trusted;
  117. int BIO_get_init(BIO* bio) @trusted;
  118. void BIO_set_data(BIO* bio, void* ptr) @trusted;
  119. void* BIO_get_data(BIO* bio) @trusted;
  120. void BIO_set_shutdown(BIO* bio, int shut) @trusted;
  121. int BIO_get_shutdown(BIO* bio) @trusted;
  122. void BIO_clear_flags(BIO* b, int flags) @trusted;
  123. int BIO_test_flags(BIO* b, int flags) @trusted;
  124. void BIO_set_flags(BIO* b, int flags) @trusted;
  125. alias BIOMethWriteCallback = int function(BIO*, const(char)*, int);
  126. alias BIOMethReadCallback = int function(BIO*, const(char)*, int);
  127. alias BIOMethCtrlCallback = c_long function(BIO*, int, c_long, void*);
  128. alias BIOMethCreateCallback = int function(BIO*);
  129. alias BIOMethDestroyCallback = int function(BIO*);
  130. int BIO_get_new_index();
  131. BIO_METHOD* BIO_meth_new(int type, const(char)* name);
  132. void BIO_meth_free(BIO_METHOD* biom);
  133. int BIO_meth_set_write(BIO_METHOD* biom, BIOMethWriteCallback cb);
  134. int BIO_meth_set_read(BIO_METHOD* biom, BIOMethReadCallback cb);
  135. int BIO_meth_set_ctrl(BIO_METHOD* biom, BIOMethCtrlCallback cb);
  136. int BIO_meth_set_create(BIO_METHOD* biom, BIOMethCreateCallback cb);
  137. int BIO_meth_set_destroy(BIO_METHOD* biom, BIOMethDestroyCallback cb);
  138. c_ulong SSL_CTX_set_options(SSL_CTX *ctx, c_ulong op);
  139. }
  140. } else {
  141. private void BIO_set_init(BIO* b, int init_) @safe nothrow {
  142. b.init_ = 1;
  143. }
  144. private int BIO_get_init(BIO* b) @safe nothrow {
  145. return b.init_;
  146. }
  147. private void BIO_set_data(BIO* b, void* ptr) @safe nothrow {
  148. b.ptr = ptr;
  149. }
  150. private void* BIO_get_data(BIO* b) @safe nothrow {
  151. return b.ptr;
  152. }
  153. private void BIO_set_shutdown(BIO* b, int shut) @safe nothrow {
  154. b.shutdown = shut;
  155. }
  156. private int BIO_get_shutdown(BIO* b) @safe nothrow {
  157. return b.shutdown;
  158. }
  159. private void BIO_clear_flags(BIO *b, int flags) @safe nothrow {
  160. b.flags &= ~flags;
  161. }
  162. private int BIO_test_flags(BIO *b, int flags) @safe nothrow {
  163. return (b.flags & flags);
  164. }
  165. private void BIO_set_flags(BIO *b, int flags) @safe nothrow {
  166. b.flags |= flags;
  167. }
  168. }
  169. private int SSL_set_tlsext_host_name(ssl_st* s, const(char)* c) @trusted {
  170. return cast(int) SSL_ctrl(s, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, cast(void*)c);
  171. }
  172. /**
  173. Creates an SSL/TLS tunnel within an existing stream.
  174. Note: Be sure to call finalize before finalizing/closing the outer stream so that the SSL
  175. tunnel is properly closed first.
  176. */
  177. final class OpenSSLStream : TLSStream {
  178. @safe:
  179. private {
  180. InterfaceProxy!Stream m_stream;
  181. TLSContext m_tlsCtx;
  182. TLSStreamState m_state;
  183. SSLState m_tls;
  184. BIO* m_bio;
  185. ubyte[64] m_peekBuffer;
  186. TLSCertificateInformation m_peerCertificateInfo;
  187. X509* m_peerCertificate;
  188. }
  189. this(InterfaceProxy!Stream underlying, OpenSSLContext ctx, TLSStreamState state, string peer_name = null, NetworkAddress peer_address = NetworkAddress.init, string[] alpn = null)
  190. {
  191. // sanity check to distinguish any error that might have slipped
  192. // somewhere else from errors generated here
  193. validateSSLErrors();
  194. m_stream = underlying;
  195. m_state = state;
  196. m_tlsCtx = ctx;
  197. m_tls = ctx.createClientCtx();
  198. scope (failure) {
  199. () @trusted { SSL_free(m_tls); } ();
  200. m_tls = null;
  201. }
  202. static if (OPENSSL_VERSION.startsWith("1.1")) {
  203. if (!s_bio_methods) initBioMethods();
  204. m_bio = () @trusted { return BIO_new(s_bio_methods); } ();
  205. } else
  206. m_bio = () @trusted { return BIO_new(&s_bio_methods); } ();
  207. enforce(m_bio !is null, "SSL failed: failed to create BIO structure.");
  208. BIO_set_init(m_bio, 1);
  209. BIO_set_data(m_bio, () @trusted { return cast(void*)this; } ()); // lifetime is shorter than this, so no GC.addRange needed.
  210. BIO_set_shutdown(m_bio, 0);
  211. () @trusted { SSL_set_bio(m_tls, m_bio, m_bio); } ();
  212. if (state != TLSStreamState.connected) {
  213. OpenSSLContext.VerifyData vdata;
  214. vdata.verifyDepth = ctx.maxCertChainLength;
  215. vdata.validationMode = ctx.peerValidationMode;
  216. vdata.callback = ctx.peerValidationCallback;
  217. vdata.peerName = peer_name;
  218. vdata.peerAddress = peer_address;
  219. checkSSLRet(() @trusted { return SSL_set_ex_data(m_tls, gs_verifyDataIndex, &vdata); } (), "Setting SSL user data");
  220. scope (exit) () @trusted { SSL_set_ex_data(m_tls, gs_verifyDataIndex, null); } ();
  221. final switch (state) {
  222. case TLSStreamState.accepting:
  223. //SSL_set_accept_state(m_tls);
  224. checkSSLRet(() @trusted { return SSL_accept(m_tls); } (), "Accepting SSL tunnel");
  225. break;
  226. case TLSStreamState.connecting:
  227. // a client stream can override the default ALPN setting for this context
  228. if (alpn.length) setClientALPN(alpn);
  229. if (peer_name.length)
  230. SSL_set_tlsext_host_name(m_tls, peer_name.toStringz);
  231. //SSL_set_connect_state(m_tls);
  232. validateSSLErrors();
  233. checkSSLRet(() @trusted { return SSL_connect(m_tls); } (), "Connecting TLS tunnel");
  234. break;
  235. case TLSStreamState.connected:
  236. break;
  237. }
  238. // ensure that the SSL tunnel gets terminated when an error happens during verification
  239. scope (failure) () @trusted { SSL_shutdown(m_tls); } ();
  240. m_peerCertificate = () @trusted { return SSL_get_peer_certificate(m_tls); } ();
  241. if (m_peerCertificate) {
  242. readPeerCertInfo();
  243. auto result = () @trusted { return SSL_get_verify_result(m_tls); } ();
  244. if (result == X509_V_OK && (ctx.peerValidationMode & TLSPeerValidationMode.checkPeer)) {
  245. if (!verifyCertName(m_peerCertificate, GENERAL_NAME.GEN_DNS, vdata.peerName)) {
  246. version(Windows) import core.sys.windows.winsock2;
  247. else import core.sys.posix.netinet.in_;
  248. logDiagnostic("TLS peer name '%s' couldn't be verified, trying IP address.", vdata.peerName);
  249. char* addr;
  250. int addrlen;
  251. switch (vdata.peerAddress.family) {
  252. default: break;
  253. case AF_INET:
  254. addr = cast(char*)&vdata.peerAddress.sockAddrInet4.sin_addr;
  255. addrlen = vdata.peerAddress.sockAddrInet4.sin_addr.sizeof;
  256. break;
  257. case AF_INET6:
  258. addr = cast(char*)&vdata.peerAddress.sockAddrInet6.sin6_addr;
  259. addrlen = vdata.peerAddress.sockAddrInet6.sin6_addr.sizeof;
  260. break;
  261. }
  262. if (!verifyCertName(m_peerCertificate, GENERAL_NAME.GEN_IPADD, () @trusted { return addr[0 .. addrlen]; } ())) {
  263. logDiagnostic("Error validating TLS peer address");
  264. result = X509_V_ERR_APPLICATION_VERIFICATION;
  265. }
  266. }
  267. }
  268. enforce(result == X509_V_OK, "Peer failed the certificate validation: "~to!string(result));
  269. } //else enforce(ctx.verifyMode < requireCert);
  270. }
  271. }
  272. /** Read certificate info into the clientInformation field */
  273. private void readPeerCertInfo()
  274. {
  275. X509_NAME* name = () @trusted { return X509_get_subject_name(m_peerCertificate); } ();
  276. int c = () @trusted { return X509_NAME_entry_count(name); } ();
  277. foreach (i; 0 .. c) {
  278. X509_NAME_ENTRY *e = () @trusted { return X509_NAME_get_entry(name, i); } ();
  279. ASN1_OBJECT *obj = () @trusted { return X509_NAME_ENTRY_get_object(e); } ();
  280. ASN1_STRING *val = () @trusted { return X509_NAME_ENTRY_get_data(e); } ();
  281. auto longName = () @trusted { return OBJ_nid2ln(OBJ_obj2nid(obj)).to!string; } ();
  282. auto valStr = () @trusted { return cast(string)val.data[0 .. val.length]; } (); // FIXME: .idup?
  283. m_peerCertificateInfo.subjectName.addField(longName, valStr);
  284. }
  285. m_peerCertificateInfo._x509 = m_peerCertificate;
  286. }
  287. ~this()
  288. {
  289. if (m_peerCertificate) () @trusted { X509_free(m_peerCertificate); } ();
  290. if (m_tls) () @trusted { SSL_free(m_tls); } ();
  291. }
  292. @property bool empty()
  293. {
  294. return leastSize() == 0;
  295. }
  296. @property ulong leastSize()
  297. {
  298. if(m_tls == null) return 0;
  299. auto ret = () @trusted { return SSL_peek(m_tls, m_peekBuffer.ptr, 1); } ();
  300. if (ret != 0) // zero means the connection got closed
  301. checkSSLRet(ret, "Peeking TLS stream");
  302. return () @trusted { return SSL_pending(m_tls); } ();
  303. }
  304. @property bool dataAvailableForRead()
  305. {
  306. return () @trusted { return SSL_pending(m_tls); } () > 0 || m_stream.dataAvailableForRead;
  307. }
  308. const(ubyte)[] peek()
  309. {
  310. auto ret = checkSSLRet(() @trusted { return SSL_peek(m_tls, m_peekBuffer.ptr, m_peekBuffer.length); } (), "Peeking TLS stream");
  311. return ret > 0 ? m_peekBuffer[0 .. ret] : null;
  312. }
  313. size_t read(scope ubyte[] dst, IOMode mode)
  314. {
  315. size_t nbytes = 0;
  316. if(m_tls == null)
  317. throw new Exception("Reading from closed stream");
  318. while (dst.length > 0) {
  319. int readlen = min(dst.length, int.max);
  320. auto ret = checkSSLRet(() @trusted { return SSL_read(m_tls, dst.ptr, readlen); } (), "Reading from TLS stream");
  321. //logTrace("SSL read %d/%d", ret, dst.length);
  322. dst = dst[ret .. $];
  323. nbytes += ret;
  324. if (mode == IOMode.immediate || mode == IOMode.once)
  325. break;
  326. }
  327. return nbytes;
  328. }
  329. alias read = Stream.read;
  330. size_t write(in ubyte[] bytes_, IOMode mode)
  331. {
  332. const(ubyte)[] bytes = bytes_;
  333. size_t nbytes = 0;
  334. while (bytes.length > 0) {
  335. int writelen = min(bytes.length, int.max);
  336. auto ret = checkSSLRet(() @trusted { return SSL_write(m_tls, bytes.ptr, writelen); } (), "Writing to TLS stream");
  337. //logTrace("SSL write %s", cast(string)bytes[0 .. ret]);
  338. bytes = bytes[ret .. $];
  339. nbytes += ret;
  340. if (mode == IOMode.immediate || mode == IOMode.once)
  341. break;
  342. }
  343. return nbytes;
  344. }
  345. alias write = Stream.write;
  346. void flush()
  347. {
  348. m_stream.flush();
  349. }
  350. void finalize()
  351. {
  352. if( !m_tls ) return;
  353. logTrace("OpenSSLStream finalize");
  354. () @trusted {
  355. auto ret = SSL_shutdown(m_tls);
  356. if (ret != 0) checkSSLRet(ret, "SSL_shutdown");
  357. SSL_free(m_tls);
  358. ERR_clear_error();
  359. } ();
  360. m_tls = null;
  361. m_stream = InterfaceProxy!Stream.init;
  362. }
  363. private void validateSSLErrors()
  364. @safe {
  365. auto err = () @trusted { return ERR_get_error(); } ();
  366. if (err != SSL_ERROR_NONE) {
  367. throw new Exception("OpenSSL error occured previously: " ~ processSSLError(err));
  368. }
  369. }
  370. private int checkSSLRet(int ret, string what)
  371. @safe {
  372. if (ret > 0) return ret;
  373. auto err = () @trusted { return SSL_get_error(m_tls, ret); } ();
  374. string desc = processSSLError(err, what);
  375. enforce(ret != 0, format("%s was unsuccessful with ret 0", what));
  376. enforce(ret >= 0, format("%s returned an error: %s", what, desc));
  377. return ret;
  378. }
  379. private string processSSLError(c_ulong err, string what = "OpenSSL")
  380. @safe {
  381. string desc;
  382. switch (err) {
  383. default: desc = format("Unknown error (%s)", err); break;
  384. case SSL_ERROR_NONE: desc = "No error"; break;
  385. case SSL_ERROR_ZERO_RETURN: desc = "SSL/TLS tunnel closed"; break;
  386. case SSL_ERROR_WANT_READ: desc = "Need to block for read"; break;
  387. case SSL_ERROR_WANT_WRITE: desc = "Need to block for write"; break;
  388. case SSL_ERROR_WANT_CONNECT: desc = "Need to block for connect"; break;
  389. case SSL_ERROR_WANT_ACCEPT: desc = "Need to block for accept"; break;
  390. case SSL_ERROR_WANT_X509_LOOKUP: desc = "Need to block for certificate lookup"; break;
  391. case SSL_ERROR_SYSCALL:
  392. version (linux) {
  393. import core.sys.linux.errno : errno;
  394. import core.stdc.string : strerror;
  395. desc = format("non-recoverable socket I/O error: %s (%s)", errno, (() @trusted => strerror(errno).to!string)());
  396. } else {
  397. desc = "non-recoverable socket I/O error";
  398. }
  399. break;
  400. case SSL_ERROR_SSL:
  401. throwSSL(what);
  402. assert(false);
  403. }
  404. const(char)* file = null, data = null;
  405. int line;
  406. int flags;
  407. c_ulong eret;
  408. char[120] ebuf;
  409. while( (eret = () @trusted { return ERR_get_error_line_data(&file, &line, &data, &flags); } ()) != 0 ){
  410. () @trusted { ERR_error_string(eret, ebuf.ptr); } ();
  411. logDebug("%s error at %s:%d: %s (%s)", what,
  412. () @trusted { return to!string(file); } (), line,
  413. () @trusted { return to!string(ebuf.ptr); } (),
  414. flags & ERR_TXT_STRING ? () @trusted { return to!string(data); } () : "-");
  415. }
  416. return desc;
  417. }
  418. @property TLSCertificateInformation peerCertificate()
  419. {
  420. return m_peerCertificateInfo;
  421. }
  422. @property X509* peerCertificateX509()
  423. {
  424. return m_peerCertificate;
  425. }
  426. @property string alpn()
  427. const {
  428. static if (!haveALPN) assert(false, "OpenSSL support not compiled with ALPN enabled. Use VibeForceALPN.");
  429. else {
  430. // modified since C functions expects a NULL pointer
  431. const(ubyte)* data = null;
  432. uint datalen;
  433. string ret;
  434. () @trusted {
  435. SSL_get0_alpn_selected(m_tls, &data, &datalen);
  436. ret = cast(string)data[0 .. datalen].idup;
  437. } ();
  438. logDebug("alpn selected: ", ret);
  439. return ret;
  440. }
  441. }
  442. /// Invoked by client to offer alpn
  443. private void setClientALPN(string[] alpn_list)
  444. {
  445. logDebug("SetClientALPN: ", alpn_list);
  446. import vibe.internal.allocator : dispose, makeArray, vibeThreadAllocator;
  447. ubyte[] alpn;
  448. size_t len;
  449. foreach (string alpn_val; alpn_list)
  450. len += alpn_val.length + 1;
  451. alpn = () @trusted { return vibeThreadAllocator.makeArray!ubyte(len); } ();
  452. size_t i;
  453. foreach (string alpn_val; alpn_list)
  454. {
  455. alpn[i++] = cast(ubyte)alpn_val.length;
  456. alpn[i .. i+alpn_val.length] = cast(immutable(ubyte)[])alpn_val;
  457. i += alpn_val.length;
  458. }
  459. assert(i == len);
  460. () @trusted {
  461. static if (haveALPN)
  462. SSL_set_alpn_protos(m_tls, cast(const char*) alpn.ptr, cast(uint) len);
  463. vibeThreadAllocator.dispose(alpn);
  464. } ();
  465. }
  466. }
  467. private int enforceSSL(int ret, string message)
  468. @safe {
  469. if (ret > 0) return ret;
  470. throwSSL(message);
  471. assert(false);
  472. }
  473. private void throwSSL(string message)
  474. @safe {
  475. c_ulong eret;
  476. const(char)* file = null, data = null;
  477. int line;
  478. int flags;
  479. string estr;
  480. char[120] ebuf = 0;
  481. while ((eret = () @trusted { return ERR_get_error_line_data(&file, &line, &data, &flags); } ()) != 0) {
  482. () @trusted { ERR_error_string_n(eret, ebuf.ptr, ebuf.length); } ();
  483. estr = () @trusted { return ebuf.ptr.to!string; } ();
  484. // throw the last error code as an exception
  485. logDebug("OpenSSL error at %s:%d: %s (%s)",
  486. () @trusted { return file.to!string; } (), line, estr,
  487. flags & ERR_TXT_STRING ? () @trusted { return to!string(data); } () : "-");
  488. if (!() @trusted { return ERR_peek_error(); } ()) break;
  489. }
  490. throw new Exception(format("%s: %s (%s)", message, estr, eret));
  491. }
  492. /**
  493. Encapsulates the configuration for an SSL tunnel.
  494. Note that when creating an SSLContext with SSLContextKind.client, the
  495. peerValidationMode will be set to SSLPeerValidationMode.trustedCert,
  496. but no trusted certificate authorities are added by default. Use
  497. useTrustedCertificateFile to add those.
  498. */
  499. final class OpenSSLContext : TLSContext {
  500. @safe:
  501. private {
  502. TLSContextKind m_kind;
  503. ssl_ctx_st* m_ctx;
  504. TLSPeerValidationCallback m_peerValidationCallback;
  505. TLSPeerValidationMode m_validationMode;
  506. int m_verifyDepth;
  507. TLSServerNameCallback m_sniCallback;
  508. TLSALPNCallback m_alpnCallback;
  509. }
  510. this(TLSContextKind kind, TLSVersion ver = TLSVersion.any)
  511. {
  512. m_kind = kind;
  513. const(SSL_METHOD)* method;
  514. c_ulong veroptions = SSL_OP_NO_SSLv2;
  515. c_ulong options = SSL_OP_NO_COMPRESSION;
  516. static if (OPENSSL_VERSION.startsWith("1.1")) {}
  517. else
  518. options |= SSL_OP_SINGLE_DH_USE|SSL_OP_SINGLE_ECDH_USE; // There are always enabled in OpenSSL 1.1.0.
  519. int minver = TLS1_VERSION;
  520. int maxver = TLS1_2_VERSION;
  521. () @trusted {
  522. final switch (kind) {
  523. case TLSContextKind.client:
  524. final switch (ver) {
  525. case TLSVersion.any: method = SSLv23_client_method(); veroptions |= SSL_OP_NO_SSLv3; break;
  526. case TLSVersion.ssl3: method = SSLv23_client_method(); veroptions |= SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_2; minver = SSL3_VERSION; maxver = SSL3_VERSION; break;
  527. case TLSVersion.tls1: method = TLSv1_client_method(); veroptions |= SSL_OP_NO_SSLv3; break;
  528. //case TLSVersion.tls1_1: method = TLSv1_1_client_method(); break;
  529. //case TLSVersion.tls1_2: method = TLSv1_2_client_method(); break;
  530. case TLSVersion.tls1_1: method = SSLv23_client_method(); veroptions |= SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_2; minver = TLS1_1_VERSION; maxver = TLS1_1_VERSION; break;
  531. case TLSVersion.tls1_2: method = SSLv23_client_method(); veroptions |= SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_1; minver = TLS1_2_VERSION; break;
  532. case TLSVersion.dtls1: method = DTLSv1_client_method(); minver = DTLS1_VERSION; maxver = DTLS1_VERSION; break;
  533. }
  534. break;
  535. case TLSContextKind.server:
  536. case TLSContextKind.serverSNI:
  537. final switch (ver) {
  538. case TLSVersion.any: method = SSLv23_server_method(); veroptions |= SSL_OP_NO_SSLv3; break;
  539. case TLSVersion.ssl3: method = SSLv23_server_method(); veroptions |= SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_2; minver = SSL3_VERSION; maxver = SSL3_VERSION; break;
  540. case TLSVersion.tls1: method = TLSv1_server_method(); veroptions |= SSL_OP_NO_SSLv3; break;
  541. case TLSVersion.tls1_1: method = SSLv23_server_method(); veroptions |= SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_2; minver = TLS1_1_VERSION; maxver = TLS1_1_VERSION; break;
  542. case TLSVersion.tls1_2: method = SSLv23_server_method(); veroptions |= SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_1; minver = TLS1_2_VERSION; break;
  543. //case TLSVersion.tls1_1: method = TLSv1_1_server_method(); break;
  544. //case TLSVersion.tls1_2: method = TLSv1_2_server_method(); break;
  545. case TLSVersion.dtls1: method = DTLSv1_server_method(); minver = DTLS1_VERSION; maxver = DTLS1_VERSION; break;
  546. }
  547. options |= SSL_OP_CIPHER_SERVER_PREFERENCE;
  548. break;
  549. }
  550. } ();
  551. m_ctx = () @trusted { return SSL_CTX_new(method); } ();
  552. if (!m_ctx) {
  553. enforceSSL(0, "Failed to create SSL context");
  554. assert(false);
  555. }
  556. static if (OPENSSL_VERSION.startsWith("1.1")) {
  557. () @trusted { return SSL_CTX_set_min_proto_version(m_ctx, minver); }()
  558. .enforceSSL("Failed setting minimum protocol version");
  559. () @trusted { return SSL_CTX_set_max_proto_version(m_ctx, maxver); }()
  560. .enforceSSL("Failed setting maximum protocol version");
  561. } else {
  562. options |= veroptions;
  563. }
  564. auto newopts = () @trusted { return SSL_CTX_set_options(m_ctx, options); }();
  565. if ((newopts & options) != options)
  566. logDiagnostic("Not all SSL options applied: passed 0x%08x vs applied 0x%08x", options, newopts);
  567. if (kind == TLSContextKind.server) {
  568. setDHParams();
  569. static if (haveECDH) setECDHCurve();
  570. guessSessionIDContext();
  571. }
  572. setCipherList();
  573. maxCertChainLength = 9;
  574. if (kind == TLSContextKind.client) peerValidationMode = TLSPeerValidationMode.trustedCert;
  575. else peerValidationMode = TLSPeerValidationMode.none;
  576. // while it would be nice to use the system's certificate store, this
  577. // seems to be difficult to get right across all systems. The most
  578. // popular alternative is to use Mozilla's certificate store and
  579. // distribute it along with the library (e.g. in source code form.
  580. /*version (Posix) {
  581. enforce(SSL_CTX_load_verify_locations(m_ctx, null, "/etc/ssl/certs"),
  582. "Failed to load system certificate store.");
  583. }
  584. version (Windows) {
  585. auto store = CertOpenSystemStore(null, "ROOT");
  586. enforce(store !is null, "Failed to load system certificate store.");
  587. scope (exit) CertCloseStore(store, 0);
  588. PCCERT_CONTEXT ctx;
  589. while((ctx = CertEnumCertificatesInStore(store, ctx)) !is null) {
  590. X509* x509cert;
  591. auto buffer = ctx.pbCertEncoded;
  592. auto len = ctx.cbCertEncoded;
  593. if (ctx.dwCertEncodingType & X509_ASN_ENCODING) {
  594. x509cert = d2i_X509(null, &buffer, len);
  595. X509_STORE_add_cert(SSL_CTX_get_cert_store(m_ctx), x509cert);
  596. }
  597. }
  598. }*/
  599. }
  600. ~this()
  601. {
  602. () @trusted { SSL_CTX_free(m_ctx); } ();
  603. m_ctx = null;
  604. }
  605. /// The kind of SSL context (client/server)
  606. @property TLSContextKind kind() const { return m_kind; }
  607. /// Callback function invoked by server to choose alpn
  608. @property void alpnCallback(TLSALPNCallback alpn_chooser)
  609. {
  610. logDebug("Choosing ALPN callback");
  611. m_alpnCallback = alpn_chooser;
  612. static if (haveALPN) {
  613. logDebug("Call select cb");
  614. () @trusted {
  615. SSL_CTX_set_alpn_select_cb(m_ctx, &chooser, cast(void*)this);
  616. } ();
  617. }
  618. }
  619. /// Get the current ALPN callback function
  620. @property TLSALPNCallback alpnCallback() const { return m_alpnCallback; }
  621. /// Invoked by client to offer alpn
  622. void setClientALPN(string[] alpn_list)
  623. {
  624. static if (!haveALPN) assert(false, "OpenSSL support not compiled with ALPN enabled. Use VibeForceALPN.");
  625. else {
  626. import vibe.utils.memory : allocArray, freeArray, manualAllocator;
  627. ubyte[] alpn;
  628. size_t len;
  629. foreach (string alpn_value; alpn_list)
  630. len += alpn_value.length + 1;
  631. () @trusted {
  632. alpn = allocArray!ubyte(manualAllocator(), len);
  633. } ();
  634. size_t i;
  635. foreach (string alpn_value; alpn_list)
  636. {
  637. () @trusted {
  638. alpn[i++] = cast(ubyte)alpn_value.length;
  639. alpn[i .. i+alpn_value.length] = cast(ubyte[])alpn_value;
  640. } ();
  641. i += alpn_value.length;
  642. }
  643. assert(i == len);
  644. () @trusted {
  645. SSL_CTX_set_alpn_protos(m_ctx, cast(const char*) alpn.ptr, cast(uint) len);
  646. freeArray(manualAllocator(), alpn);
  647. } ();
  648. }
  649. }
  650. /** Specifies the validation level of remote peers.
  651. The default mode for TLSContextKind.client is
  652. TLSPeerValidationMode.trustedCert and the default for
  653. TLSContextKind.server is TLSPeerValidationMode.none.
  654. */
  655. @property void peerValidationMode(TLSPeerValidationMode mode)
  656. {
  657. m_validationMode = mode;
  658. int sslmode;
  659. with (TLSPeerValidationMode) {
  660. if (mode == none) sslmode = SSL_VERIFY_NONE;
  661. else {
  662. sslmode |= SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
  663. if (mode & requireCert) sslmode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  664. }
  665. }
  666. () @trusted { SSL_CTX_set_verify(m_ctx, sslmode, &verify_callback); } ();
  667. }
  668. /// ditto
  669. @property TLSPeerValidationMode peerValidationMode() const { return m_validationMode; }
  670. /** The maximum length of an accepted certificate chain.
  671. Any certificate chain longer than this will result in the SSL/TLS
  672. negitiation failing.
  673. The default value is 9.
  674. */
  675. @property void maxCertChainLength(int val)
  676. {
  677. m_verifyDepth = val;
  678. // + 1 to let the validation callback handle the error
  679. () @trusted { SSL_CTX_set_verify_depth(m_ctx, val + 1); } ();
  680. }
  681. /// ditto
  682. @property int maxCertChainLength() const { return m_verifyDepth; }
  683. /** An optional user callback for peer validation.
  684. This callback will be called for each peer and each certificate of
  685. its certificate chain to allow overriding the validation decision
  686. based on the selected peerValidationMode (e.g. to allow invalid
  687. certificates or to reject valid ones). This is mainly useful for
  688. presenting the user with a dialog in case of untrusted or mismatching
  689. certificates.
  690. */
  691. @property void peerValidationCallback(TLSPeerValidationCallback callback) { m_peerValidationCallback = callback; }
  692. /// ditto
  693. @property inout(TLSPeerValidationCallback) peerValidationCallback() inout { return m_peerValidationCallback; }
  694. @property void sniCallback(TLSServerNameCallback callback)
  695. {
  696. m_sniCallback = callback;
  697. if (m_kind == TLSContextKind.serverSNI) {
  698. () @trusted {
  699. SSL_CTX_callback_ctrl(m_ctx, SSL_CTRL_SET_TLSEXT_SERVERNAME_CB, cast(OSSLCallback)&onContextForServerName);
  700. SSL_CTX_ctrl(m_ctx, SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG, 0, cast(void*)this);
  701. } ();
  702. }
  703. }
  704. @property inout(TLSServerNameCallback) sniCallback() inout { return m_sniCallback; }
  705. private extern(C) alias OSSLCallback = void function();
  706. private static extern(C) int onContextForServerName(SSL *s, int *ad, void *arg)
  707. {
  708. auto ctx = () @trusted { return cast(OpenSSLContext)arg; } ();
  709. auto servername = () @trusted { return SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); } ();
  710. if (!servername) return SSL_TLSEXT_ERR_NOACK;
  711. auto newctx = cast(OpenSSLContext)ctx.m_sniCallback(() @trusted { return servername.to!string; } ());
  712. if (!newctx) return SSL_TLSEXT_ERR_NOACK;
  713. () @trusted { SSL_set_SSL_CTX(s, newctx.m_ctx); } ();
  714. return SSL_TLSEXT_ERR_OK;
  715. }
  716. OpenSSLStream createStream(InterfaceProxy!Stream underlying, TLSStreamState state, string peer_name = null, NetworkAddress peer_address = NetworkAddress.init)
  717. {
  718. return new OpenSSLStream(underlying, this, state, peer_name, peer_address);
  719. }
  720. /** Set the list of cipher specifications to use for SSL/TLS tunnels.
  721. The list must be a colon separated list of cipher
  722. specifications as accepted by OpenSSL. Calling this function
  723. without argument will restore the default.
  724. See_also: $(LINK https://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT)
  725. */
  726. void setCipherList(string list = null)
  727. @trusted
  728. {
  729. if (list is null)
  730. SSL_CTX_set_cipher_list(m_ctx,
  731. "ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:"
  732. ~ "RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS").enforceSSL("Setting cipher list");
  733. else
  734. SSL_CTX_set_cipher_list(m_ctx, toStringz(list)).enforceSSL("Setting cipher list");
  735. }
  736. /** Make up a context ID to assign to the SSL context.
  737. This is required when doing client cert authentication, otherwise many
  738. connections will go aborted as the client tries to revive a session
  739. that it used to have on another machine.
  740. The session ID context should be unique within a pool of servers.
  741. Currently, this is achieved by taking the hostname.
  742. */
  743. private void guessSessionIDContext()
  744. @trusted
  745. {
  746. string contextID = Socket.hostName;
  747. SSL_CTX_set_session_id_context(m_ctx, cast(ubyte*)contextID.toStringz(), cast(uint)contextID.length);
  748. }
  749. /** Set params to use for DH cipher.
  750. *
  751. * By default the 2048-bit prime from RFC 3526 is used.
  752. *
  753. * Params:
  754. * pem_file = Path to a PEM file containing the DH parameters. Calling
  755. * this function without argument will restore the default.
  756. */
  757. void setDHParams(string pem_file=null)
  758. @trusted {
  759. DH* dh;
  760. scope(exit) DH_free(dh);
  761. if (pem_file is null) {
  762. dh = enforce(DH_new(), "Unable to create DH structure.");
  763. dh.p = get_rfc3526_prime_2048(null);
  764. ubyte dh_generator = 2;
  765. dh.g = BN_bin2bn(&dh_generator, dh_generator.sizeof, null);
  766. } else {
  767. import core.stdc.stdio : fclose, fopen;
  768. auto f = enforce(fopen(toStringz(pem_file), "r"), "Failed to load dhparams file "~pem_file);
  769. scope(exit) fclose(f);
  770. dh = enforce(PEM_read_DHparams(f, null, null, null), "Failed to read dhparams file "~pem_file);
  771. }
  772. SSL_CTX_set_tmp_dh(m_ctx, dh);
  773. }
  774. /** Set the elliptic curve to use for ECDH cipher.
  775. *
  776. * By default a curve is either chosen automatically or prime256v1 is used.
  777. *
  778. * Params:
  779. * curve = The short name of the elliptic curve to use. Calling this
  780. * function without argument will restore the default.
  781. *
  782. */
  783. void setECDHCurve(string curve = null)
  784. @trusted {
  785. static if (haveECDH) {
  786. static if (OPENSSL_VERSION_NUMBER >= 0x10200000) {
  787. // use automatic ecdh curve selection by default
  788. if (curve is null) {
  789. SSL_CTX_set_ecdh_auto(m_ctx, true);
  790. return;
  791. }
  792. // but disable it when an explicit curve is given
  793. SSL_CTX_set_ecdh_auto(m_ctx, false);
  794. }
  795. int nid;
  796. if (curve is null)
  797. nid = NID_X9_62_prime256v1;
  798. else
  799. nid = enforce(OBJ_sn2nid(toStringz(curve)), "Unknown ECDH curve '"~curve~"'.");
  800. auto ecdh = enforce(EC_KEY_new_by_curve_name(nid), "Unable to create ECDH curve.");
  801. SSL_CTX_set_tmp_ecdh(m_ctx, ecdh);
  802. EC_KEY_free(ecdh);
  803. } else assert(false, "ECDH curve selection not available for old versions of OpenSSL");
  804. }
  805. /// Sets a certificate file to use for authenticating to the remote peer
  806. void useCertificateChainFile(string path)
  807. {
  808. enforce(() @trusted { return SSL_CTX_use_certificate_chain_file(m_ctx, toStringz(path)); } (), "Failed to load certificate file " ~ path);
  809. }
  810. /// Sets the private key to use for authenticating to the remote peer based
  811. /// on the configured certificate chain file.
  812. void usePrivateKeyFile(string path)
  813. {
  814. enforce(() @trusted { return SSL_CTX_use_PrivateKey_file(m_ctx, toStringz(path), SSL_FILETYPE_PEM); } (), "Failed to load private key file " ~ path);
  815. }
  816. /** Sets the list of trusted certificates for verifying peer certificates.
  817. If this is a server context, this also entails that the given
  818. certificates are advertised to connecting clients during handshake.
  819. On Linux, the system's root certificate authority list is usually
  820. found at "/etc/ssl/certs/ca-certificates.crt",
  821. "/etc/pki/tls/certs/ca-bundle.crt", or "/etc/ssl/ca-bundle.pem".
  822. */
  823. void useTrustedCertificateFile(string path)
  824. @trusted {
  825. immutable cPath = toStringz(path);
  826. enforce(SSL_CTX_load_verify_locations(m_ctx, cPath, null),
  827. "Failed to load trusted certificate file " ~ path);
  828. if (m_kind == TLSContextKind.server) {
  829. auto certNames = enforce(SSL_load_client_CA_file(cPath),
  830. "Failed to load client CA name list from file " ~ path);
  831. SSL_CTX_set_client_CA_list(m_ctx, certNames);
  832. }
  833. }
  834. private SSLState createClientCtx()
  835. {
  836. SSLState ret = () @trusted { return SSL_new(m_ctx); } ();
  837. if (!ret) {
  838. enforceSSL(0, "Failed to create SSL context");
  839. assert(false);
  840. }
  841. return ret;
  842. }
  843. private static struct VerifyData {
  844. int verifyDepth;
  845. TLSPeerValidationMode validationMode;
  846. TLSPeerValidationCallback callback;
  847. string peerName;
  848. NetworkAddress peerAddress;
  849. }
  850. private static extern(C) nothrow
  851. int verify_callback(int valid, X509_STORE_CTX* ctx)
  852. @trusted {
  853. X509* err_cert = X509_STORE_CTX_get_current_cert(ctx);
  854. int err = X509_STORE_CTX_get_error(ctx);
  855. int depth = X509_STORE_CTX_get_error_depth(ctx);
  856. SSL* ssl = cast(SSL*)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
  857. VerifyData* vdata = cast(VerifyData*)SSL_get_ex_data(ssl, gs_verifyDataIndex);
  858. char[1024] buf;
  859. X509_NAME_oneline(X509_get_subject_name(err_cert), buf.ptr, 256);
  860. buf[$-1] = 0;
  861. try {
  862. logDebug("validate callback for %s", buf.ptr.to!string);
  863. if (depth > vdata.verifyDepth) {
  864. logDiagnostic("SSL cert chain too long: %s vs. %s", depth, vdata.verifyDepth);
  865. valid = false;
  866. err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  867. }
  868. if (err != X509_V_OK)
  869. logDebug("SSL cert initial error: %s", X509_verify_cert_error_string(err).to!string);
  870. if (!valid) {
  871. switch (err) {
  872. default: break;
  873. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  874. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  875. case X509_V_ERR_CERT_UNTRUSTED:
  876. assert(err_cert !is null);
  877. X509_NAME_oneline(X509_get_issuer_name(err_cert), buf.ptr, buf.length);
  878. buf[$-1] = 0;
  879. logDebug("SSL cert not trusted or unknown issuer: %s", buf.ptr.to!string);
  880. if (!(vdata.validationMode & TLSPeerValidationMode.checkTrust)) {
  881. valid = true;
  882. err = X509_V_OK;
  883. }
  884. break;
  885. }
  886. }
  887. if (!(vdata.validationMode & TLSPeerValidationMode.checkCert)) {
  888. valid = true;
  889. err = X509_V_OK;
  890. }
  891. if (vdata.callback) {
  892. TLSPeerValidationData pvdata;
  893. // ...
  894. if (!valid) {
  895. if (vdata.callback(pvdata)) {
  896. valid = true;
  897. err = X509_V_OK;
  898. }
  899. } else {
  900. if (!vdata.callback(pvdata)) {
  901. logDebug("SSL application verification failed");
  902. valid = false;
  903. err = X509_V_ERR_APPLICATION_VERIFICATION;
  904. }
  905. }
  906. }
  907. } catch (Exception e) {
  908. logWarn("SSL verification failed due to exception: %s", e.msg);
  909. err = X509_V_ERR_APPLICATION_VERIFICATION;
  910. valid = false;
  911. }
  912. X509_STORE_CTX_set_error(ctx, err);
  913. logDebug("SSL validation result: %s (%s)", valid, err);
  914. return valid;
  915. }
  916. }
  917. alias SSLState = ssl_st*;
  918. /**************************************************************************************************/
  919. /* Private functions */
  920. /**************************************************************************************************/
  921. private {
  922. __gshared InterruptibleTaskMutex[] g_cryptoMutexes;
  923. __gshared int gs_verifyDataIndex;
  924. }
  925. shared static this()
  926. {
  927. logDebug("Initializing OpenSSL...");
  928. SSL_load_error_strings();
  929. SSL_library_init();
  930. g_cryptoMutexes.length = CRYPTO_num_locks();
  931. // TODO: investigate if a normal Mutex is enough - not sure if BIO is called in a locked state
  932. foreach (i; 0 .. g_cryptoMutexes.length)
  933. g_cryptoMutexes[i] = new InterruptibleTaskMutex;
  934. foreach (ref m; g_cryptoMutexes) {
  935. assert(m !is null);
  936. }
  937. CRYPTO_set_id_callback(&onCryptoGetThreadID);
  938. CRYPTO_set_locking_callback(&onCryptoLock);
  939. enforce(RAND_poll(), "Fatal: failed to initialize random number generator entropy (RAND_poll).");
  940. logDebug("... done.");
  941. gs_verifyDataIndex = SSL_get_ex_new_index(0, cast(void*)"VerifyData".ptr, null, null, null);
  942. }
  943. private bool verifyCertName(X509* cert, int field, in char[] value, bool allow_wildcards = true)
  944. @trusted {
  945. bool delegate(in char[]) @safe str_match;
  946. bool check_value(ASN1_STRING* str, int type) {
  947. if (!str.data || !str.length) return false;
  948. if (type > 0) {
  949. if (type != str.type) return 0;
  950. auto strstr = cast(string)str.data[0 .. str.length];
  951. return type == V_ASN1_IA5STRING ? str_match(strstr) : strstr == value;
  952. }
  953. char* utfstr;
  954. auto utflen = ASN1_STRING_to_UTF8(&utfstr, str);
  955. enforce (utflen >= 0, "Error converting ASN1 string to UTF-8.");
  956. scope (exit) OPENSSL_free(utfstr);
  957. return str_match(utfstr[0 .. utflen]);
  958. }
  959. int cnid;
  960. int alt_type;
  961. final switch (field) {
  962. case GENERAL_NAME.GEN_DNS:
  963. cnid = NID_commonName;
  964. alt_type = V_ASN1_IA5STRING;
  965. str_match = allow_wildcards ? (in s) => matchWildcard(value, s) : (in s) => s.icmp(value) == 0;
  966. break;
  967. case GENERAL_NAME.GEN_IPADD:
  968. cnid = 0;
  969. alt_type = V_ASN1_OCTET_STRING;
  970. str_match = (in s) => s == value;
  971. break;
  972. }
  973. if (auto gens = cast(STACK_OF!GENERAL_NAME*)X509_get_ext_d2i(cert, NID_subject_alt_name, null, null)) {
  974. scope(exit) GENERAL_NAMES_free(gens);
  975. foreach (i; 0 .. sk_GENERAL_NAME_num(gens)) {
  976. auto gen = sk_GENERAL_NAME_value(gens, i);
  977. if (gen.type != field) continue;
  978. ASN1_STRING *cstr = field == GENERAL_NAME.GEN_DNS ? gen.d.dNSName : gen.d.iPAddress;
  979. if (check_value(cstr, alt_type)) return true;
  980. }
  981. if (!cnid) return false;
  982. }
  983. X509_NAME* name = X509_get_subject_name(cert);
  984. int i = -1;
  985. while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) {
  986. X509_NAME_ENTRY* ne = X509_NAME_get_entry(name, i);
  987. ASN1_STRING* str = X509_NAME_ENTRY_get_data(ne);
  988. if (check_value(str, -1)) return true;
  989. }
  990. return false;
  991. }
  992. private bool matchWildcard(const(char)[] str, const(char)[] pattern)
  993. @safe {
  994. auto strparts = str.split(".");
  995. auto patternparts = pattern.split(".");
  996. if (strparts.length != patternparts.length) return false;
  997. bool isValidChar(dchar ch) {
  998. if (ch >= '0' && ch <= '9') return true;
  999. if (ch >= 'a' && ch <= 'z') return true;
  1000. if (ch >= 'A' && ch <= 'Z') return true;
  1001. if (ch == '-' || ch == '.') return true;
  1002. return false;
  1003. }
  1004. if (!pattern.all!(c => isValidChar(c) || c == '*') || !str.all!(c => isValidChar(c)))
  1005. return false;
  1006. foreach (i; 0 .. strparts.length) {
  1007. import std.regex;
  1008. auto p = patternparts[i];
  1009. auto s = strparts[i];
  1010. if (!p.length || !s.length) return false;
  1011. auto rex = "^" ~ std.array.replace(p, "*", "[^.]*") ~ "$";
  1012. if (!match(s, rex)) return false;
  1013. }
  1014. return true;
  1015. }
  1016. unittest {
  1017. assert(matchWildcard("www.example.org", "*.example.org"));
  1018. assert(matchWildcard("www.example.org", "*w.example.org"));
  1019. assert(matchWildcard("www.example.org", "w*w.example.org"));
  1020. assert(matchWildcard("www.example.org", "*w*.example.org"));
  1021. assert(matchWildcard("test.abc.example.org", "test.*.example.org"));
  1022. assert(!matchWildcard("test.abc.example.org", "abc.example.org"));
  1023. assert(!matchWildcard("test.abc.example.org", ".abc.example.org"));
  1024. assert(!matchWildcard("abc.example.org", "a.example.org"));
  1025. assert(!matchWildcard("abc.example.org", "bc.example.org"));
  1026. assert(!matchWildcard("abcdexample.org", "abc.example.org"));
  1027. }
  1028. private nothrow @safe extern(C)
  1029. {
  1030. import core.stdc.config;
  1031. int chooser(SSL* ssl, const(char)** output, ubyte* outlen, const(char) *input_, uint inlen, void* arg) {
  1032. const(char)[] input = () @trusted { return input_[0 .. inlen]; } ();
  1033. OpenSSLContext ctx = () @trusted { return cast(OpenSSLContext) arg; } ();
  1034. import vibe.utils.array : AllocAppender, AppenderResetMode;
  1035. size_t i;
  1036. size_t len;
  1037. Appender!(string[]) alpn_list;
  1038. while (i < inlen)
  1039. {
  1040. len = cast(size_t) input[i];
  1041. ++i;
  1042. auto proto = input[i .. i+len];
  1043. i += len;
  1044. () @trusted { alpn_list ~= cast(string)proto; } ();
  1045. }
  1046. string alpn;
  1047. try { alpn = ctx.m_alpnCallback(alpn_list.data); } catch (Exception e) { }
  1048. if (alpn) {
  1049. i = 0;
  1050. while (i < inlen)
  1051. {
  1052. len = input[i];
  1053. ++i;
  1054. auto proto = input[i .. i+len];
  1055. i += len;
  1056. if (proto == alpn) {
  1057. *output = &proto[0];
  1058. *outlen = cast(ubyte) proto.length;
  1059. }
  1060. }
  1061. }
  1062. if (!output) {
  1063. logError("None of the proposed ALPN were selected: %s / falling back on HTTP/1.1", input);
  1064. enum hdr = "http/1.1";
  1065. *output = &hdr[0];
  1066. *outlen = cast(ubyte)hdr.length;
  1067. }
  1068. return 0;
  1069. }
  1070. c_ulong onCryptoGetThreadID()
  1071. {
  1072. try {
  1073. return cast(c_ulong)(cast(size_t)() @trusted { return cast(void*)Thread.getThis(); } () * 0x35d2c57);
  1074. } catch (Exception e) {
  1075. logWarn("OpenSSL: failed to get current thread ID: %s", e.msg);
  1076. return 0;
  1077. }
  1078. }
  1079. void onCryptoLock(int mode, int n, const(char)* file, int line)
  1080. {
  1081. try {
  1082. enforce(n >= 0 && n < () @trusted { return g_cryptoMutexes; } ().length, "Mutex index out of range.");
  1083. auto mutex = () @trusted { return g_cryptoMutexes[n]; } ();
  1084. assert(mutex !is null);
  1085. if (mode & CRYPTO_LOCK) mutex.lock();
  1086. else mutex.unlock();
  1087. } catch (Exception e) {
  1088. logWarn("OpenSSL: failed to lock/unlock mutex: %s", e.msg);
  1089. }
  1090. }
  1091. int onBioNew(BIO *b) nothrow
  1092. {
  1093. BIO_set_init(b, 0);
  1094. //b.num = -1;
  1095. BIO_set_data(b, null);
  1096. BIO_clear_flags(b, ~0);
  1097. return 1;
  1098. }
  1099. int onBioFree(BIO *b)
  1100. {
  1101. if( !b ) return 0;
  1102. if(BIO_get_shutdown(b)){
  1103. //if( b.init && b.ptr ) b.ptr.stream.free();
  1104. BIO_set_init(b, 0);
  1105. BIO_clear_flags(b, ~0);
  1106. BIO_set_data(b, null);
  1107. }
  1108. return 1;
  1109. }
  1110. int onBioRead(BIO *b, const(char)* outb, int outlen)
  1111. {
  1112. auto stream = () @trusted { return cast(OpenSSLStream)BIO_get_data(b); } ();
  1113. try {
  1114. outlen = min(outlen, stream.m_stream.leastSize);
  1115. stream.m_stream.read(() @trusted { return cast(ubyte[])outb[0 .. outlen]; } ());
  1116. } catch (Exception e) {
  1117. setSSLError("Error reading from underlying stream", e.msg);
  1118. return -1;
  1119. }
  1120. return outlen;
  1121. }
  1122. int onBioWrite(BIO *b, const(char) *inb, int inlen)
  1123. {
  1124. auto stream = () @trusted { return cast(OpenSSLStream)BIO_get_data(b); } ();
  1125. try {
  1126. stream.m_stream.write(() @trusted { return inb[0 .. inlen]; } ());
  1127. } catch (Exception e) {
  1128. setSSLError("Error writing to underlying stream", e.msg);
  1129. return -1;
  1130. }
  1131. return inlen;
  1132. }
  1133. c_long onBioCtrl(BIO *b, int cmd, c_long num, void *ptr)
  1134. {
  1135. auto stream = () @trusted { return cast(OpenSSLStream)BIO_get_data(b); } ();
  1136. c_long ret = 1;
  1137. switch(cmd){
  1138. case BIO_CTRL_GET_CLOSE: ret = BIO_get_shutdown(b); break;
  1139. case BIO_CTRL_SET_CLOSE:
  1140. logTrace("SSL set close %d", num);
  1141. BIO_set_shutdown(b, cast(int)num);
  1142. break;
  1143. case BIO_CTRL_PENDING:
  1144. try {
  1145. auto sz = stream.m_stream.leastSize; // FIXME: .peek.length should be sufficient here
  1146. return sz <= c_long.max ? cast(c_long)sz : c_long.max;
  1147. } catch( Exception e ){
  1148. setSSLError("Error reading from underlying stream", e.msg);
  1149. return -1;
  1150. }
  1151. case BIO_CTRL_WPENDING: return 0;
  1152. case BIO_CTRL_DUP:
  1153. case BIO_CTRL_FLUSH:
  1154. ret = 1;
  1155. break;
  1156. default:
  1157. ret = 0;
  1158. break;
  1159. }
  1160. return ret;
  1161. }
  1162. int onBioPuts(BIO *b, const(char) *s)
  1163. {
  1164. return onBioWrite(b, s, cast(int)() @trusted { return strlen(s); } ());
  1165. }
  1166. }
  1167. private void setSSLError(string msg, string submsg, int line = __LINE__, string file = __FILE__)
  1168. @trusted nothrow {
  1169. import std.string : toStringz;
  1170. ERR_put_error(ERR_LIB_USER, 0, 1, file.toStringz, line);
  1171. ERR_add_error_data(3, msg.toStringz, ": ".ptr, submsg.toStringz);
  1172. }
  1173. static if (OPENSSL_VERSION.startsWith("1.1")) {
  1174. private BIO_METHOD* s_bio_methods;
  1175. private void initBioMethods()
  1176. @trusted {
  1177. s_bio_methods = BIO_meth_new(BIO_get_new_index(), "SslStream");
  1178. BIO_meth_set_write(s_bio_methods, &onBioWrite);
  1179. BIO_meth_set_read(s_bio_methods, &onBioRead);
  1180. BIO_meth_set_ctrl(s_bio_methods, &onBioCtrl);
  1181. BIO_meth_set_create(s_bio_methods, &onBioNew);
  1182. BIO_meth_set_destroy(s_bio_methods, &onBioFree);
  1183. }
  1184. } else {
  1185. private BIO_METHOD s_bio_methods = {
  1186. 57, "SslStream",
  1187. &onBioWrite,
  1188. &onBioRead,
  1189. &onBioPuts,
  1190. null, // &onBioGets
  1191. &onBioCtrl,
  1192. &onBioNew,
  1193. &onBioFree,
  1194. null, // &onBioCallbackCtrl
  1195. };
  1196. }
  1197. private nothrow extern(C):
  1198. static if (haveALPN) {
  1199. alias ALPNCallback = int function(SSL *ssl, const(char) **output, ubyte* outlen, const(char) *input, uint inlen, void *arg);
  1200. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, ALPNCallback cb, void *arg);
  1201. int SSL_set_alpn_protos(SSL *ssl, const char *data, uint len);
  1202. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const char* protos, uint protos_len);
  1203. void SSL_get0_alpn_selected(const SSL *ssl, const ubyte** data, uint *len);
  1204. }
  1205. const(ssl_method_st)* TLSv1_2_server_method();