ustream-openssl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * ustream-ssl - library for SSL over ustream
  3. *
  4. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include "ustream-ssl.h"
  21. #include "ustream-internal.h"
  22. #if !defined(HAVE_WOLFSSL)
  23. #include <openssl/x509v3.h>
  24. #endif
  25. #if defined(HAVE_WOLFSSL) && defined(DEBUG)
  26. #include <wolfssl/test.h>
  27. #endif
  28. /* Ciphersuite preference:
  29. * - for server, no weak ciphers are used if you use an ECDSA key.
  30. * - forward-secret (pfs), authenticated (AEAD) ciphers are at the top:
  31. * chacha20-poly1305, the fastest in software, 256-bits
  32. * aes128-gcm, 128-bits
  33. * aes256-gcm, 256-bits
  34. * - key exchange: prefer ECDHE, then DHE (client only)
  35. * - forward-secret ECDSA CBC ciphers (client-only)
  36. * - forward-secret RSA CBC ciphers
  37. * - non-pfs ciphers
  38. * aes128, aes256, 3DES(client only)
  39. */
  40. #ifdef WOLFSSL_SSL_H
  41. # define top_ciphers \
  42. "TLS13-CHACHA20-POLY1305-SHA256:" \
  43. "TLS13-AES128-GCM-SHA256:" \
  44. "TLS13-AES256-GCM-SHA384:" \
  45. ecdhe_aead_ciphers
  46. #else
  47. # define tls13_ciphersuites "TLS_CHACHA20_POLY1305_SHA256:" \
  48. "TLS_AES_128_GCM_SHA256:" \
  49. "TLS_AES_256_GCM_SHA384"
  50. # define top_ciphers \
  51. ecdhe_aead_ciphers
  52. #endif
  53. #define ecdhe_aead_ciphers \
  54. "ECDHE-ECDSA-CHACHA20-POLY1305:" \
  55. "ECDHE-ECDSA-AES128-GCM-SHA256:" \
  56. "ECDHE-ECDSA-AES256-GCM-SHA384:" \
  57. "ECDHE-RSA-CHACHA20-POLY1305:" \
  58. "ECDHE-RSA-AES128-GCM-SHA256:" \
  59. "ECDHE-RSA-AES256-GCM-SHA384"
  60. #define dhe_aead_ciphers \
  61. "DHE-RSA-CHACHA20-POLY1305:" \
  62. "DHE-RSA-AES128-GCM-SHA256:" \
  63. "DHE-RSA-AES256-GCM-SHA384"
  64. #define ecdhe_ecdsa_cbc_ciphers \
  65. "ECDHE-ECDSA-AES128-SHA:" \
  66. "ECDHE-ECDSA-AES256-SHA"
  67. #define ecdhe_rsa_cbc_ciphers \
  68. "ECDHE-RSA-AES128-SHA:" \
  69. "ECDHE-RSA-AES256-SHA"
  70. #define dhe_cbc_ciphers \
  71. "DHE-RSA-AES128-SHA:" \
  72. "DHE-RSA-AES256-SHA:" \
  73. "DHE-DES-CBC3-SHA"
  74. #define non_pfs_aes \
  75. "AES128-GCM-SHA256:" \
  76. "AES256-GCM-SHA384:" \
  77. "AES128-SHA:" \
  78. "AES256-SHA"
  79. #define server_cipher_list \
  80. top_ciphers ":" \
  81. ecdhe_rsa_cbc_ciphers ":" \
  82. non_pfs_aes
  83. #define client_cipher_list \
  84. top_ciphers ":" \
  85. dhe_aead_ciphers ":" \
  86. ecdhe_ecdsa_cbc_ciphers ":" \
  87. ecdhe_rsa_cbc_ciphers ":" \
  88. dhe_cbc_ciphers ":" \
  89. non_pfs_aes ":" \
  90. "DES-CBC3-SHA"
  91. __hidden struct ustream_ssl_ctx *
  92. __ustream_ssl_context_new(bool server)
  93. {
  94. struct ustream_ssl_ctx *ctx;
  95. const void *m;
  96. SSL_CTX *c;
  97. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  98. static bool _init = false;
  99. if (!_init) {
  100. SSL_load_error_strings();
  101. SSL_library_init();
  102. _init = true;
  103. }
  104. # ifndef TLS_server_method
  105. # define TLS_server_method SSLv23_server_method
  106. # endif
  107. # ifndef TLS_client_method
  108. # define TLS_client_method SSLv23_client_method
  109. # endif
  110. #endif
  111. if (server) {
  112. m = TLS_server_method();
  113. } else
  114. m = TLS_client_method();
  115. c = SSL_CTX_new((void *) m);
  116. if (!c)
  117. return NULL;
  118. ctx = calloc(1, sizeof(*ctx));
  119. ctx->ssl = c;
  120. #if defined(HAVE_WOLFSSL)
  121. if (server)
  122. SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
  123. else
  124. SSL_CTX_set_verify(c, SSL_VERIFY_PEER, NULL);
  125. #else
  126. SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
  127. #endif
  128. SSL_CTX_set_options(c, SSL_OP_NO_COMPRESSION | SSL_OP_SINGLE_ECDH_USE |
  129. SSL_OP_CIPHER_SERVER_PREFERENCE);
  130. #if defined(SSL_CTX_set_ecdh_auto) && OPENSSL_VERSION_NUMBER < 0x10100000L
  131. SSL_CTX_set_ecdh_auto(c, 1);
  132. #elif OPENSSL_VERSION_NUMBER >= 0x10101000L
  133. SSL_CTX_set_ciphersuites(c, tls13_ciphersuites);
  134. #endif
  135. if (server) {
  136. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  137. SSL_CTX_set_min_proto_version(c, TLS1_2_VERSION);
  138. #else
  139. SSL_CTX_set_options(c, SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 |
  140. SSL_OP_NO_TLSv1_1);
  141. #endif
  142. #if defined(HAVE_WOLFSSL)
  143. SSL_CTX_set_options(c, SSL_AD_NO_RENEGOTIATION);
  144. #else
  145. SSL_CTX_set_options(c, SSL_OP_NO_RENEGOTIATION);
  146. #endif
  147. SSL_CTX_set_cipher_list(c, server_cipher_list);
  148. } else {
  149. SSL_CTX_set_cipher_list(c, client_cipher_list);
  150. }
  151. SSL_CTX_set_quiet_shutdown(c, 1);
  152. return ctx;
  153. }
  154. __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
  155. {
  156. int ret;
  157. ret = SSL_CTX_load_verify_locations(ctx->ssl, file, NULL);
  158. if (ret < 1)
  159. return -1;
  160. return 0;
  161. }
  162. __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
  163. {
  164. int ret;
  165. ret = SSL_CTX_use_certificate_chain_file(ctx->ssl, file);
  166. if (ret < 1)
  167. ret = SSL_CTX_use_certificate_file(ctx->ssl, file, SSL_FILETYPE_ASN1);
  168. if (ret < 1)
  169. return -1;
  170. return 0;
  171. }
  172. __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
  173. {
  174. int ret;
  175. ret = SSL_CTX_use_PrivateKey_file(ctx->ssl, file, SSL_FILETYPE_PEM);
  176. if (ret < 1)
  177. ret = SSL_CTX_use_PrivateKey_file(ctx->ssl, file, SSL_FILETYPE_ASN1);
  178. if (ret < 1)
  179. return -1;
  180. return 0;
  181. }
  182. __hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
  183. {
  184. int ret = SSL_CTX_set_cipher_list(ctx->ssl, ciphers);
  185. if (ret == 0)
  186. return -1;
  187. return 0;
  188. }
  189. __hidden int __ustream_ssl_set_require_validation(struct ustream_ssl_ctx *ctx, bool require)
  190. {
  191. int mode = SSL_VERIFY_PEER;
  192. if (!require)
  193. mode = SSL_VERIFY_NONE;
  194. SSL_CTX_set_verify(ctx->ssl, mode, NULL);
  195. return 0;
  196. }
  197. __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
  198. {
  199. SSL_CTX_free(ctx->ssl);
  200. if (ctx->debug_bio)
  201. BIO_free(ctx->debug_bio);
  202. free(ctx);
  203. }
  204. __hidden void __ustream_ssl_session_free(struct ustream_ssl *us)
  205. {
  206. BIO *bio = SSL_get_wbio(us->ssl);
  207. struct bio_ctx *ctx;
  208. SSL_shutdown(us->ssl);
  209. SSL_free(us->ssl);
  210. if (!us->conn)
  211. return;
  212. ctx = BIO_get_data(bio);
  213. if (ctx) {
  214. BIO_meth_free(ctx->meth);
  215. free(ctx);
  216. }
  217. }
  218. static void ustream_ssl_error(struct ustream_ssl *us, int ret)
  219. {
  220. us->error = ret;
  221. uloop_timeout_set(&us->error_timer, 0);
  222. }
  223. static bool ustream_ssl_verify_cn(struct ustream_ssl *us, X509 *cert)
  224. {
  225. int ret;
  226. if (!us->peer_cn)
  227. return false;
  228. # ifndef WOLFSSL_OPENSSL_H_
  229. ret = X509_check_host(cert, us->peer_cn, 0, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS, NULL);
  230. # else
  231. ret = wolfSSL_X509_check_host(cert, us->peer_cn, 0, 0, NULL);
  232. # endif
  233. return ret == 1;
  234. }
  235. static void ustream_ssl_verify_cert(struct ustream_ssl *us)
  236. {
  237. void *ssl = us->ssl;
  238. X509 *cert;
  239. int res;
  240. #if defined(HAVE_WOLFSSL) && defined(DEBUG)
  241. showPeer(ssl);
  242. #endif
  243. res = SSL_get_verify_result(ssl);
  244. if (res != X509_V_OK) {
  245. if (us->notify_verify_error)
  246. us->notify_verify_error(us, res, X509_verify_cert_error_string(res));
  247. return;
  248. }
  249. #if defined(HAVE_WOLFSSL)
  250. cert = SSL_get_peer_certificate(ssl);
  251. #else
  252. cert = SSL_get1_peer_certificate(ssl);
  253. #endif
  254. if (!cert)
  255. return;
  256. us->valid_cert = true;
  257. us->valid_cn = ustream_ssl_verify_cn(us, cert);
  258. X509_free(cert);
  259. }
  260. #ifdef WOLFSSL_SSL_H
  261. static bool handle_wolfssl_asn_error(struct ustream_ssl *us, int r)
  262. {
  263. switch (r) {
  264. case ASN_PARSE_E:
  265. case ASN_VERSION_E:
  266. case ASN_GETINT_E:
  267. case ASN_RSA_KEY_E:
  268. case ASN_OBJECT_ID_E:
  269. case ASN_TAG_NULL_E:
  270. case ASN_EXPECT_0_E:
  271. case ASN_BITSTR_E:
  272. case ASN_UNKNOWN_OID_E:
  273. case ASN_DATE_SZ_E:
  274. case ASN_BEFORE_DATE_E:
  275. case ASN_AFTER_DATE_E:
  276. case ASN_SIG_OID_E:
  277. case ASN_TIME_E:
  278. case ASN_INPUT_E:
  279. case ASN_SIG_CONFIRM_E:
  280. case ASN_SIG_HASH_E:
  281. case ASN_SIG_KEY_E:
  282. case ASN_DH_KEY_E:
  283. #if LIBWOLFSSL_VERSION_HEX < 0x05000000
  284. case ASN_NTRU_KEY_E:
  285. #endif
  286. case ASN_CRIT_EXT_E:
  287. case ASN_ALT_NAME_E:
  288. case ASN_NO_PEM_HEADER:
  289. case ASN_ECC_KEY_E:
  290. case ASN_NO_SIGNER_E:
  291. case ASN_CRL_CONFIRM_E:
  292. case ASN_CRL_NO_SIGNER_E:
  293. case ASN_OCSP_CONFIRM_E:
  294. case ASN_NAME_INVALID_E:
  295. case ASN_NO_SKID:
  296. case ASN_NO_AKID:
  297. case ASN_NO_KEYUSAGE:
  298. case ASN_COUNTRY_SIZE_E:
  299. case ASN_PATHLEN_SIZE_E:
  300. case ASN_PATHLEN_INV_E:
  301. case ASN_SELF_SIGNED_E:
  302. if (us->notify_verify_error)
  303. us->notify_verify_error(us, r, wc_GetErrorString(r));
  304. return true;
  305. }
  306. return false;
  307. }
  308. #endif
  309. __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
  310. {
  311. void *ssl = us->ssl;
  312. int r;
  313. ERR_clear_error();
  314. if (us->server)
  315. r = SSL_accept(ssl);
  316. else
  317. r = SSL_connect(ssl);
  318. if (r == 1) {
  319. ustream_ssl_verify_cert(us);
  320. return U_SSL_OK;
  321. }
  322. r = SSL_get_error(ssl, r);
  323. if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
  324. return U_SSL_PENDING;
  325. #ifdef WOLFSSL_SSL_H
  326. if (handle_wolfssl_asn_error(us, r))
  327. return U_SSL_OK;
  328. #endif
  329. ustream_ssl_error(us, r);
  330. return U_SSL_ERROR;
  331. }
  332. __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
  333. {
  334. void *ssl = us->ssl;
  335. int ret;
  336. ERR_clear_error();
  337. ret = SSL_write(ssl, buf, len);
  338. if (ret < 0) {
  339. int err = SSL_get_error(ssl, ret);
  340. if (err == SSL_ERROR_WANT_WRITE)
  341. return 0;
  342. ustream_ssl_error(us, err);
  343. return -1;
  344. }
  345. return ret;
  346. }
  347. __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
  348. {
  349. int ret;
  350. ERR_clear_error();
  351. ret = SSL_read(us->ssl, buf, len);
  352. if (ret < 0) {
  353. ret = SSL_get_error(us->ssl, ret);
  354. if (ret == SSL_ERROR_WANT_READ)
  355. return U_SSL_PENDING;
  356. ustream_ssl_error(us, ret);
  357. return U_SSL_ERROR;
  358. }
  359. return ret;
  360. }
  361. #ifndef WOLFSSL_SSL_H
  362. static long
  363. debug_cb(BIO *bio, int cmd, const char *argp, size_t len, int argi, long argl,
  364. int ret, size_t *processed)
  365. {
  366. struct ustream_ssl_ctx *ctx = (void *)BIO_get_callback_arg(bio);
  367. char buf[256];
  368. char *str, *sep;
  369. ssize_t cur_len;
  370. if (cmd != (BIO_CB_WRITE|BIO_CB_RETURN))
  371. goto out;
  372. while (1) {
  373. cur_len = BIO_get_mem_data(bio, (void *)&str);
  374. if (!cur_len)
  375. break;
  376. sep = memchr(str, '\n', cur_len);
  377. if (!sep)
  378. break;
  379. cur_len = sep + 1 - str;
  380. if (cur_len >= (ssize_t)sizeof(buf))
  381. cur_len = sizeof(buf) - 1;
  382. cur_len = BIO_read(bio, buf, cur_len);
  383. if (cur_len <= 1)
  384. break;
  385. cur_len--;
  386. buf[cur_len] = 0;
  387. if (ctx->debug_cb)
  388. ctx->debug_cb(ctx->debug_cb_priv, 1, buf);
  389. }
  390. out:
  391. return ret;
  392. }
  393. #endif
  394. __hidden void __ustream_ssl_set_debug(struct ustream_ssl_ctx *ctx, int level,
  395. ustream_ssl_debug_cb cb, void *cb_priv)
  396. {
  397. #ifndef WOLFSSL_SSL_H
  398. if (!ctx->debug_bio)
  399. ctx->debug_bio = BIO_new(BIO_s_mem());
  400. ctx->debug_cb = cb;
  401. ctx->debug_cb_priv = cb_priv;
  402. SSL_CTX_set_msg_callback(ctx->ssl, SSL_trace);
  403. SSL_CTX_set_msg_callback_arg(ctx->ssl, ctx->debug_bio);
  404. BIO_set_callback_ex(ctx->debug_bio, debug_cb);
  405. BIO_set_callback_arg(ctx->debug_bio, (void *)ctx);
  406. #endif
  407. }