mbedtls-client.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. static int
  26. OpenSSL_client_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
  27. {
  28. return 0;
  29. }
  30. int
  31. lws_ssl_client_bio_create(struct lws *wsi)
  32. {
  33. char hostname[128], *p;
  34. const char *alpn_comma = wsi->a.context->tls.alpn_default;
  35. struct alpn_ctx protos;
  36. if (wsi->stash)
  37. lws_strncpy(hostname, wsi->stash->cis[CIS_HOST], sizeof(hostname));
  38. else
  39. if (lws_hdr_copy(wsi, hostname, sizeof(hostname),
  40. _WSI_TOKEN_CLIENT_HOST) <= 0) {
  41. lwsl_err("%s: Unable to get hostname\n", __func__);
  42. return -1;
  43. }
  44. /*
  45. * remove any :port part on the hostname... necessary for network
  46. * connection but typical certificates do not contain it
  47. */
  48. p = hostname;
  49. while (*p) {
  50. if (*p == ':') {
  51. *p = '\0';
  52. break;
  53. }
  54. p++;
  55. }
  56. wsi->tls.ssl = SSL_new(wsi->a.vhost->tls.ssl_client_ctx);
  57. if (!wsi->tls.ssl) {
  58. lwsl_info("%s: SSL_new() failed\n", __func__);
  59. return -1;
  60. }
  61. if (wsi->a.vhost->tls.ssl_info_event_mask)
  62. SSL_set_info_callback(wsi->tls.ssl, lws_ssl_info_callback);
  63. if (!(wsi->tls.use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)) {
  64. X509_VERIFY_PARAM *param = SSL_get0_param(wsi->tls.ssl);
  65. /* Enable automatic hostname checks */
  66. // X509_VERIFY_PARAM_set_hostflags(param,
  67. // X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  68. X509_VERIFY_PARAM_set1_host(param, hostname, 0);
  69. }
  70. if (wsi->a.vhost->tls.alpn)
  71. alpn_comma = wsi->a.vhost->tls.alpn;
  72. if (wsi->stash) {
  73. lws_strncpy(hostname, wsi->stash->cis[CIS_HOST], sizeof(hostname));
  74. alpn_comma = wsi->stash->cis[CIS_ALPN];
  75. } else {
  76. if (lws_hdr_copy(wsi, hostname, sizeof(hostname),
  77. _WSI_TOKEN_CLIENT_ALPN) > 0)
  78. alpn_comma = hostname;
  79. }
  80. lwsl_info("%s: %p: client conn sending ALPN list '%s'\n",
  81. __func__, wsi, alpn_comma);
  82. protos.len = lws_alpn_comma_to_openssl(alpn_comma, protos.data,
  83. sizeof(protos.data) - 1);
  84. /* with mbedtls, protos is not pointed to after exit from this call */
  85. SSL_set_alpn_select_cb(wsi->tls.ssl, &protos);
  86. /*
  87. * use server name indication (SNI), if supported,
  88. * when establishing connection
  89. */
  90. SSL_set_verify(wsi->tls.ssl, SSL_VERIFY_PEER,
  91. OpenSSL_client_verify_callback);
  92. SSL_set_fd(wsi->tls.ssl, wsi->desc.sockfd);
  93. if (wsi->sys_tls_client_cert) {
  94. lws_system_blob_t *b = lws_system_get_blob(wsi->a.context,
  95. LWS_SYSBLOB_TYPE_CLIENT_CERT_DER,
  96. wsi->sys_tls_client_cert - 1);
  97. const uint8_t *pem_data = NULL;
  98. uint8_t *data = NULL;
  99. lws_filepos_t flen;
  100. size_t size;
  101. int err = 0;
  102. if (!b)
  103. goto no_client_cert;
  104. /*
  105. * Set up the per-connection client cert
  106. */
  107. size = lws_system_blob_get_size(b);
  108. if (!size)
  109. goto no_client_cert;
  110. if (lws_system_blob_get_single_ptr(b, &pem_data))
  111. goto no_client_cert;
  112. if (lws_tls_alloc_pem_to_der_file(wsi->a.context, NULL,
  113. (const char *)pem_data, size,
  114. &data, &flen))
  115. goto no_client_cert;
  116. size = (size_t) flen;
  117. err = SSL_use_certificate_ASN1(wsi->tls.ssl, data, size);
  118. lws_free_set_NULL(data);
  119. if (err != 1)
  120. goto no_client_cert;
  121. b = lws_system_get_blob(wsi->a.context,
  122. LWS_SYSBLOB_TYPE_CLIENT_KEY_DER,
  123. wsi->sys_tls_client_cert - 1);
  124. if (!b)
  125. goto no_client_cert;
  126. size = lws_system_blob_get_size(b);
  127. if (!size)
  128. goto no_client_cert;
  129. if (lws_system_blob_get_single_ptr(b, &pem_data))
  130. goto no_client_cert;
  131. if (lws_tls_alloc_pem_to_der_file(wsi->a.context, NULL,
  132. (const char *)pem_data, size,
  133. &data, &flen))
  134. goto no_client_cert;
  135. size = (size_t) flen;
  136. err = SSL_use_PrivateKey_ASN1(0, wsi->tls.ssl, data, size);
  137. lws_free_set_NULL(data);
  138. if (err != 1)
  139. goto no_client_cert;
  140. /* no wrapper api for check key */
  141. lwsl_notice("%s: set system client cert %u\n", __func__,
  142. wsi->sys_tls_client_cert - 1);
  143. }
  144. return 0;
  145. no_client_cert:
  146. lwsl_err("%s: unable to set up system client cert %d\n", __func__,
  147. wsi->sys_tls_client_cert - 1);
  148. return 1;
  149. }
  150. int ERR_get_error(void)
  151. {
  152. return 0;
  153. }
  154. enum lws_ssl_capable_status
  155. lws_tls_client_connect(struct lws *wsi, char *errbuf, int elen)
  156. {
  157. int m, n = SSL_connect(wsi->tls.ssl);
  158. const unsigned char *prot;
  159. unsigned int len;
  160. if (n == 1) {
  161. SSL_get0_alpn_selected(wsi->tls.ssl, &prot, &len);
  162. lws_role_call_alpn_negotiated(wsi, (const char *)prot);
  163. lwsl_info("client connect OK\n");
  164. return LWS_SSL_CAPABLE_DONE;
  165. }
  166. m = SSL_get_error(wsi->tls.ssl, n);
  167. if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl))
  168. return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
  169. if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl))
  170. return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
  171. if (!n) /* we don't know what he wants, but he says to retry */
  172. return LWS_SSL_CAPABLE_MORE_SERVICE;
  173. lws_snprintf(errbuf, elen, "mbedtls connect %d %d %d", n, m, errno);
  174. return LWS_SSL_CAPABLE_ERROR;
  175. }
  176. int
  177. lws_tls_client_confirm_peer_cert(struct lws *wsi, char *ebuf, int ebuf_len)
  178. {
  179. int n;
  180. X509 *peer = SSL_get_peer_certificate(wsi->tls.ssl);
  181. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  182. char *sb = (char *)&pt->serv_buf[0];
  183. if (!peer) {
  184. lwsl_info("peer did not provide cert\n");
  185. lws_snprintf(ebuf, ebuf_len, "no peer cert");
  186. return -1;
  187. }
  188. lwsl_info("peer provided cert\n");
  189. n = SSL_get_verify_result(wsi->tls.ssl);
  190. lwsl_debug("get_verify says %d\n", n);
  191. if (n == X509_V_OK)
  192. return 0;
  193. if (n == X509_V_ERR_HOSTNAME_MISMATCH &&
  194. (wsi->tls.use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)) {
  195. lwsl_info("accepting certificate for invalid hostname\n");
  196. return 0;
  197. }
  198. if (n == X509_V_ERR_INVALID_CA &&
  199. (wsi->tls.use_ssl & LCCSCF_ALLOW_SELFSIGNED)) {
  200. lwsl_info("accepting certificate from untrusted CA\n");
  201. return 0;
  202. }
  203. if ((n == X509_V_ERR_CERT_NOT_YET_VALID ||
  204. n == X509_V_ERR_CERT_HAS_EXPIRED) &&
  205. (wsi->tls.use_ssl & LCCSCF_ALLOW_EXPIRED)) {
  206. lwsl_info("accepting expired or not yet valid certificate\n");
  207. return 0;
  208. }
  209. lws_snprintf(ebuf, ebuf_len,
  210. "server's cert didn't look good, (use_ssl 0x%x) X509_V_ERR = %d: %s\n",
  211. (unsigned int)wsi->tls.use_ssl, n, ERR_error_string(n, sb));
  212. lwsl_info("%s\n", ebuf);
  213. lws_tls_err_describe_clear();
  214. return -1;
  215. }
  216. int
  217. lws_tls_client_create_vhost_context(struct lws_vhost *vh,
  218. const struct lws_context_creation_info *info,
  219. const char *cipher_list,
  220. const char *ca_filepath,
  221. const void *ca_mem,
  222. unsigned int ca_mem_len,
  223. const char *cert_filepath,
  224. const void *cert_mem,
  225. unsigned int cert_mem_len,
  226. const char *private_key_filepath,
  227. const void *key_mem,
  228. unsigned int key_mem_len
  229. )
  230. {
  231. X509 *d2i_X509(X509 **cert, const unsigned char *buffer, long len);
  232. SSL_METHOD *method = (SSL_METHOD *)TLS_client_method();
  233. unsigned long error;
  234. int n;
  235. if (!method) {
  236. error = ERR_get_error();
  237. lwsl_err("problem creating ssl method %lu: %s\n",
  238. error, ERR_error_string(error,
  239. (char *)vh->context->pt[0].serv_buf));
  240. return 1;
  241. }
  242. /* create context */
  243. vh->tls.ssl_client_ctx = SSL_CTX_new(method);
  244. if (!vh->tls.ssl_client_ctx) {
  245. error = ERR_get_error();
  246. lwsl_err("problem creating ssl context %lu: %s\n",
  247. error, ERR_error_string(error,
  248. (char *)vh->context->pt[0].serv_buf));
  249. return 1;
  250. }
  251. if (!ca_filepath && (!ca_mem || !ca_mem_len))
  252. return 0;
  253. if (ca_filepath) {
  254. #if !defined(LWS_PLAT_OPTEE)
  255. uint8_t *buf;
  256. lws_filepos_t len;
  257. if (alloc_file(vh->context, ca_filepath, &buf, &len)) {
  258. lwsl_err("Load CA cert file %s failed\n", ca_filepath);
  259. return 1;
  260. }
  261. vh->tls.x509_client_CA = d2i_X509(NULL, buf, len);
  262. free(buf);
  263. lwsl_notice("Loading client CA for verification %s\n", ca_filepath);
  264. #endif
  265. } else {
  266. vh->tls.x509_client_CA = d2i_X509(NULL, (uint8_t*)ca_mem, ca_mem_len);
  267. lwsl_notice("%s: using mem client CA cert %d\n",
  268. __func__, ca_mem_len);
  269. }
  270. if (!vh->tls.x509_client_CA) {
  271. lwsl_err("client CA: x509 parse failed\n");
  272. return 1;
  273. }
  274. if (!vh->tls.ssl_ctx)
  275. SSL_CTX_add_client_CA(vh->tls.ssl_client_ctx, vh->tls.x509_client_CA);
  276. else
  277. SSL_CTX_add_client_CA(vh->tls.ssl_ctx, vh->tls.x509_client_CA);
  278. /* support for client-side certificate authentication */
  279. if (cert_filepath) {
  280. #if !defined(LWS_PLAT_OPTEE)
  281. uint8_t *buf;
  282. lws_filepos_t amount;
  283. if (lws_tls_use_any_upgrade_check_extant(cert_filepath) !=
  284. LWS_TLS_EXTANT_YES &&
  285. (info->options & LWS_SERVER_OPTION_IGNORE_MISSING_CERT))
  286. return 0;
  287. lwsl_notice("%s: doing cert filepath %s\n", __func__,
  288. cert_filepath);
  289. if (alloc_file(vh->context, cert_filepath, &buf, &amount))
  290. return 1;
  291. buf[amount++] = '\0';
  292. SSL_CTX_use_PrivateKey_ASN1(0, vh->tls.ssl_client_ctx,
  293. buf, amount);
  294. n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx,
  295. amount, buf);
  296. lws_free(buf);
  297. if (n < 1) {
  298. lwsl_err("problem %d getting cert '%s'\n", n,
  299. cert_filepath);
  300. lws_tls_err_describe_clear();
  301. return 1;
  302. }
  303. lwsl_notice("Loaded client cert %s\n", cert_filepath);
  304. #endif
  305. } else if (cert_mem && cert_mem_len) {
  306. /* lwsl_hexdump_notice(cert_mem, cert_mem_len - 1); */
  307. SSL_CTX_use_PrivateKey_ASN1(0, vh->tls.ssl_client_ctx,
  308. cert_mem, cert_mem_len - 1);
  309. n = SSL_CTX_use_certificate_ASN1(vh->tls.ssl_client_ctx,
  310. cert_mem_len, cert_mem);
  311. if (n < 1) {
  312. lwsl_err("%s: (mbedtls) problem interpreting client cert\n",
  313. __func__);
  314. lws_tls_err_describe_clear();
  315. return 1;
  316. }
  317. lwsl_notice("%s: using mem client cert %d\n",
  318. __func__, cert_mem_len);
  319. }
  320. return 0;
  321. }
  322. int
  323. lws_tls_client_vhost_extra_cert_mem(struct lws_vhost *vh,
  324. const uint8_t *der, size_t der_len)
  325. {
  326. if (SSL_CTX_add_client_CA_ASN1(vh->tls.ssl_client_ctx, der_len, der) != 1) {
  327. lwsl_err("%s: failed\n", __func__);
  328. return 1;
  329. }
  330. return 0;
  331. }