ssl_cert.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  114. #include <openssl/ssl.h>
  115. #include <assert.h>
  116. #include <limits.h>
  117. #include <string.h>
  118. #include <openssl/bn.h>
  119. #include <openssl/buf.h>
  120. #include <openssl/bytestring.h>
  121. #include <openssl/dh.h>
  122. #include <openssl/ec_key.h>
  123. #include <openssl/err.h>
  124. #include <openssl/mem.h>
  125. #include <openssl/sha.h>
  126. #include <openssl/x509.h>
  127. #include <openssl/x509v3.h>
  128. #include "../crypto/internal.h"
  129. #include "internal.h"
  130. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  131. /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
  132. * reserved app_data slot. Before ex_data was introduced, app_data was used.
  133. * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
  134. * works. */
  135. return 0;
  136. }
  137. CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) {
  138. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  139. if (ret == NULL) {
  140. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  141. return NULL;
  142. }
  143. OPENSSL_memset(ret, 0, sizeof(CERT));
  144. ret->x509_method = x509_method;
  145. return ret;
  146. }
  147. static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
  148. CRYPTO_BUFFER_up_ref(buffer);
  149. return buffer;
  150. }
  151. CERT *ssl_cert_dup(CERT *cert) {
  152. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  153. if (ret == NULL) {
  154. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  155. return NULL;
  156. }
  157. OPENSSL_memset(ret, 0, sizeof(CERT));
  158. ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
  159. CRYPTO_BUFFER_free);
  160. if (cert->privatekey != NULL) {
  161. EVP_PKEY_up_ref(cert->privatekey);
  162. ret->privatekey = cert->privatekey;
  163. }
  164. ret->key_method = cert->key_method;
  165. ret->x509_method = cert->x509_method;
  166. if (cert->dh_tmp != NULL) {
  167. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  168. if (ret->dh_tmp == NULL) {
  169. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  170. goto err;
  171. }
  172. }
  173. ret->dh_tmp_cb = cert->dh_tmp_cb;
  174. if (cert->sigalgs != NULL) {
  175. ret->sigalgs =
  176. BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
  177. if (ret->sigalgs == NULL) {
  178. goto err;
  179. }
  180. }
  181. ret->num_sigalgs = cert->num_sigalgs;
  182. ret->cert_cb = cert->cert_cb;
  183. ret->cert_cb_arg = cert->cert_cb_arg;
  184. if (cert->verify_store != NULL) {
  185. X509_STORE_up_ref(cert->verify_store);
  186. ret->verify_store = cert->verify_store;
  187. }
  188. if (cert->signed_cert_timestamp_list != NULL) {
  189. CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list);
  190. ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list;
  191. }
  192. if (cert->ocsp_response != NULL) {
  193. CRYPTO_BUFFER_up_ref(cert->ocsp_response);
  194. ret->ocsp_response = cert->ocsp_response;
  195. }
  196. ret->sid_ctx_length = cert->sid_ctx_length;
  197. OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
  198. return ret;
  199. err:
  200. ssl_cert_free(ret);
  201. return NULL;
  202. }
  203. /* Free up and clear all certificates and chains */
  204. void ssl_cert_clear_certs(CERT *cert) {
  205. if (cert == NULL) {
  206. return;
  207. }
  208. cert->x509_method->cert_clear(cert);
  209. sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
  210. cert->chain = NULL;
  211. EVP_PKEY_free(cert->privatekey);
  212. cert->privatekey = NULL;
  213. cert->key_method = NULL;
  214. }
  215. void ssl_cert_free(CERT *c) {
  216. if (c == NULL) {
  217. return;
  218. }
  219. DH_free(c->dh_tmp);
  220. ssl_cert_clear_certs(c);
  221. OPENSSL_free(c->sigalgs);
  222. X509_STORE_free(c->verify_store);
  223. CRYPTO_BUFFER_free(c->signed_cert_timestamp_list);
  224. CRYPTO_BUFFER_free(c->ocsp_response);
  225. OPENSSL_free(c);
  226. }
  227. static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
  228. void *arg) {
  229. c->cert_cb = cb;
  230. c->cert_cb_arg = arg;
  231. }
  232. int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) {
  233. CBS cert_cbs;
  234. CRYPTO_BUFFER_init_CBS(buffer, &cert_cbs);
  235. EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
  236. if (pubkey == NULL) {
  237. return 0;
  238. }
  239. if (!ssl_is_key_type_supported(pubkey->type)) {
  240. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  241. EVP_PKEY_free(pubkey);
  242. return 0;
  243. }
  244. /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
  245. * certificates, so sanity-check the key usage extension. */
  246. if (pubkey->type == EVP_PKEY_EC &&
  247. !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
  248. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  249. EVP_PKEY_free(pubkey);
  250. return 0;
  251. }
  252. if (cert->privatekey != NULL) {
  253. /* Sanity-check that the private key and the certificate match, unless the
  254. * key is opaque (in case of, say, a smartcard). */
  255. if (!EVP_PKEY_is_opaque(cert->privatekey) &&
  256. !ssl_compare_public_and_private_key(pubkey, cert->privatekey)) {
  257. /* don't fail for a cert/key mismatch, just free current private key
  258. * (when switching to a different cert & key, first this function should
  259. * be used, then ssl_set_pkey */
  260. EVP_PKEY_free(cert->privatekey);
  261. cert->privatekey = NULL;
  262. /* clear error queue */
  263. ERR_clear_error();
  264. }
  265. }
  266. EVP_PKEY_free(pubkey);
  267. cert->x509_method->cert_flush_cached_leaf(cert);
  268. if (cert->chain != NULL) {
  269. CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0));
  270. sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer);
  271. CRYPTO_BUFFER_up_ref(buffer);
  272. return 1;
  273. }
  274. cert->chain = sk_CRYPTO_BUFFER_new_null();
  275. if (cert->chain == NULL) {
  276. return 0;
  277. }
  278. if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
  279. sk_CRYPTO_BUFFER_free(cert->chain);
  280. cert->chain = NULL;
  281. return 0;
  282. }
  283. CRYPTO_BUFFER_up_ref(buffer);
  284. return 1;
  285. }
  286. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
  287. const uint8_t *der) {
  288. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
  289. if (buffer == NULL) {
  290. return 0;
  291. }
  292. const int ok = ssl_set_cert(ctx->cert, buffer);
  293. CRYPTO_BUFFER_free(buffer);
  294. return ok;
  295. }
  296. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  297. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
  298. if (buffer == NULL) {
  299. return 0;
  300. }
  301. const int ok = ssl_set_cert(ssl->cert, buffer);
  302. CRYPTO_BUFFER_free(buffer);
  303. return ok;
  304. }
  305. int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
  306. STACK_OF(X509) *cert_chain) {
  307. if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
  308. return 0;
  309. }
  310. X509_STORE *verify_store = ssl->ctx->cert_store;
  311. if (ssl->cert->verify_store != NULL) {
  312. verify_store = ssl->cert->verify_store;
  313. }
  314. X509 *leaf = sk_X509_value(cert_chain, 0);
  315. int ret = 0;
  316. X509_STORE_CTX ctx;
  317. if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
  318. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  319. return 0;
  320. }
  321. if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
  322. ssl)) {
  323. goto err;
  324. }
  325. /* We need to inherit the verify parameters. These can be determined by the
  326. * context: if its a server it will verify SSL client certificates or vice
  327. * versa. */
  328. X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
  329. /* Anything non-default in "param" should overwrite anything in the ctx. */
  330. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
  331. if (ssl->verify_callback) {
  332. X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
  333. }
  334. int verify_ret;
  335. if (ssl->ctx->app_verify_callback != NULL) {
  336. verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
  337. } else {
  338. verify_ret = X509_verify_cert(&ctx);
  339. }
  340. *out_verify_result = ctx.error;
  341. /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
  342. if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
  343. ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
  344. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
  345. goto err;
  346. }
  347. ERR_clear_error();
  348. ret = 1;
  349. err:
  350. X509_STORE_CTX_cleanup(&ctx);
  351. return ret;
  352. }
  353. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  354. STACK_OF(X509_NAME) *name_list) {
  355. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  356. *ca_list = name_list;
  357. }
  358. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
  359. STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
  360. if (ret == NULL) {
  361. return NULL;
  362. }
  363. for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
  364. X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
  365. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  366. X509_NAME_free(name);
  367. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  368. return NULL;
  369. }
  370. }
  371. return ret;
  372. }
  373. void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
  374. set_client_CA_list(&ssl->client_CA, name_list);
  375. }
  376. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
  377. set_client_CA_list(&ctx->client_CA, name_list);
  378. }
  379. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  380. return ctx->client_CA;
  381. }
  382. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
  383. /* For historical reasons, this function is used both to query configuration
  384. * state on a server as well as handshake state on a client. However, whether
  385. * |ssl| is a client or server is not known until explicitly configured with
  386. * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
  387. * indeterminate mode and |ssl->server| is unset. */
  388. if (ssl->handshake_func != NULL && !ssl->server) {
  389. if (ssl->s3->hs != NULL) {
  390. return ssl->s3->hs->ca_names;
  391. }
  392. return NULL;
  393. }
  394. if (ssl->client_CA != NULL) {
  395. return ssl->client_CA;
  396. }
  397. return ssl->ctx->client_CA;
  398. }
  399. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
  400. X509_NAME *name;
  401. if (x509 == NULL) {
  402. return 0;
  403. }
  404. if (*sk == NULL) {
  405. *sk = sk_X509_NAME_new_null();
  406. if (*sk == NULL) {
  407. return 0;
  408. }
  409. }
  410. name = X509_NAME_dup(X509_get_subject_name(x509));
  411. if (name == NULL) {
  412. return 0;
  413. }
  414. if (!sk_X509_NAME_push(*sk, name)) {
  415. X509_NAME_free(name);
  416. return 0;
  417. }
  418. return 1;
  419. }
  420. int SSL_add_client_CA(SSL *ssl, X509 *x509) {
  421. return add_client_CA(&ssl->client_CA, x509);
  422. }
  423. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
  424. return add_client_CA(&ctx->client_CA, x509);
  425. }
  426. int ssl_has_certificate(const SSL *ssl) {
  427. return ssl->cert->chain != NULL &&
  428. sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
  429. ssl_has_private_key(ssl);
  430. }
  431. STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
  432. EVP_PKEY **out_pubkey,
  433. uint8_t *out_leaf_sha256,
  434. CBS *cbs,
  435. CRYPTO_BUFFER_POOL *pool) {
  436. *out_pubkey = NULL;
  437. STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
  438. if (ret == NULL) {
  439. *out_alert = SSL_AD_INTERNAL_ERROR;
  440. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  441. return NULL;
  442. }
  443. CBS certificate_list;
  444. if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
  445. *out_alert = SSL_AD_DECODE_ERROR;
  446. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  447. goto err;
  448. }
  449. while (CBS_len(&certificate_list) > 0) {
  450. CBS certificate;
  451. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
  452. CBS_len(&certificate) == 0) {
  453. *out_alert = SSL_AD_DECODE_ERROR;
  454. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  455. goto err;
  456. }
  457. if (sk_CRYPTO_BUFFER_num(ret) == 0) {
  458. *out_pubkey = ssl_cert_parse_pubkey(&certificate);
  459. if (*out_pubkey == NULL) {
  460. *out_alert = SSL_AD_DECODE_ERROR;
  461. goto err;
  462. }
  463. /* Retain the hash of the leaf certificate if requested. */
  464. if (out_leaf_sha256 != NULL) {
  465. SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
  466. }
  467. }
  468. CRYPTO_BUFFER *buf =
  469. CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
  470. if (buf == NULL) {
  471. *out_alert = SSL_AD_DECODE_ERROR;
  472. goto err;
  473. }
  474. if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
  475. *out_alert = SSL_AD_INTERNAL_ERROR;
  476. CRYPTO_BUFFER_free(buf);
  477. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  478. goto err;
  479. }
  480. }
  481. return ret;
  482. err:
  483. EVP_PKEY_free(*out_pubkey);
  484. *out_pubkey = NULL;
  485. sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
  486. return NULL;
  487. }
  488. int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
  489. if (!ssl_has_certificate(ssl)) {
  490. return CBB_add_u24(cbb, 0);
  491. }
  492. CBB certs;
  493. if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
  494. goto err;
  495. }
  496. STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
  497. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
  498. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
  499. CBB child;
  500. if (!CBB_add_u24_length_prefixed(&certs, &child) ||
  501. !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  502. CRYPTO_BUFFER_len(buffer)) ||
  503. !CBB_flush(&certs)) {
  504. goto err;
  505. }
  506. }
  507. return CBB_flush(cbb);
  508. err:
  509. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  510. return 0;
  511. }
  512. /* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
  513. * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
  514. * subjectPublicKeyInfo. */
  515. static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
  516. /* From RFC 5280, section 4.1
  517. * Certificate ::= SEQUENCE {
  518. * tbsCertificate TBSCertificate,
  519. * signatureAlgorithm AlgorithmIdentifier,
  520. * signatureValue BIT STRING }
  521. * TBSCertificate ::= SEQUENCE {
  522. * version [0] EXPLICIT Version DEFAULT v1,
  523. * serialNumber CertificateSerialNumber,
  524. * signature AlgorithmIdentifier,
  525. * issuer Name,
  526. * validity Validity,
  527. * subject Name,
  528. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  529. * ... } */
  530. CBS buf = *in;
  531. CBS toplevel;
  532. if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
  533. CBS_len(&buf) != 0 ||
  534. !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
  535. /* version */
  536. !CBS_get_optional_asn1(
  537. out_tbs_cert, NULL, NULL,
  538. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
  539. /* serialNumber */
  540. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
  541. /* signature algorithm */
  542. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  543. /* issuer */
  544. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  545. /* validity */
  546. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  547. /* subject */
  548. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
  549. return 0;
  550. }
  551. return 1;
  552. }
  553. EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
  554. CBS buf = *in, tbs_cert;
  555. if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
  556. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  557. return NULL;
  558. }
  559. return EVP_parse_public_key(&tbs_cert);
  560. }
  561. int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
  562. const EVP_PKEY *privkey) {
  563. int ret = 0;
  564. switch (EVP_PKEY_cmp(pubkey, privkey)) {
  565. case 1:
  566. ret = 1;
  567. break;
  568. case 0:
  569. OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
  570. break;
  571. case -1:
  572. OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
  573. break;
  574. case -2:
  575. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
  576. default:
  577. assert(0);
  578. break;
  579. }
  580. return ret;
  581. }
  582. int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
  583. if (privkey == NULL) {
  584. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  585. return 0;
  586. }
  587. if (cert->chain == NULL ||
  588. sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
  589. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  590. return 0;
  591. }
  592. CBS cert_cbs;
  593. CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
  594. EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
  595. if (!pubkey) {
  596. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
  597. return 0;
  598. }
  599. const int ok = ssl_compare_public_and_private_key(pubkey, privkey);
  600. EVP_PKEY_free(pubkey);
  601. return ok;
  602. }
  603. int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
  604. CBS buf = *in;
  605. CBS tbs_cert, outer_extensions;
  606. int has_extensions;
  607. if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
  608. /* subjectPublicKeyInfo */
  609. !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  610. /* issuerUniqueID */
  611. !CBS_get_optional_asn1(
  612. &tbs_cert, NULL, NULL,
  613. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
  614. /* subjectUniqueID */
  615. !CBS_get_optional_asn1(
  616. &tbs_cert, NULL, NULL,
  617. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
  618. !CBS_get_optional_asn1(
  619. &tbs_cert, &outer_extensions, &has_extensions,
  620. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
  621. goto parse_err;
  622. }
  623. if (!has_extensions) {
  624. return 1;
  625. }
  626. CBS extensions;
  627. if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
  628. goto parse_err;
  629. }
  630. while (CBS_len(&extensions) > 0) {
  631. CBS extension, oid, contents;
  632. if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
  633. !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
  634. (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
  635. !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
  636. !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
  637. CBS_len(&extension) != 0) {
  638. goto parse_err;
  639. }
  640. static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
  641. if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
  642. OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
  643. 0) {
  644. continue;
  645. }
  646. CBS bit_string;
  647. if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
  648. CBS_len(&contents) != 0) {
  649. goto parse_err;
  650. }
  651. /* This is the KeyUsage extension. See
  652. * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
  653. if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
  654. goto parse_err;
  655. }
  656. if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
  657. OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  658. return 0;
  659. }
  660. return 1;
  661. }
  662. /* No KeyUsage extension found. */
  663. return 1;
  664. parse_err:
  665. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  666. return 0;
  667. }
  668. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
  669. return X509_NAME_cmp(*a, *b);
  670. }
  671. STACK_OF(X509_NAME) *
  672. ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
  673. STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
  674. X509_NAME *name = NULL;
  675. if (ret == NULL) {
  676. *out_alert = SSL_AD_INTERNAL_ERROR;
  677. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  678. return NULL;
  679. }
  680. CBS child;
  681. if (!CBS_get_u16_length_prefixed(cbs, &child)) {
  682. *out_alert = SSL_AD_DECODE_ERROR;
  683. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  684. goto err;
  685. }
  686. while (CBS_len(&child) > 0) {
  687. CBS distinguished_name;
  688. if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
  689. *out_alert = SSL_AD_DECODE_ERROR;
  690. OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
  691. goto err;
  692. }
  693. const uint8_t *ptr = CBS_data(&distinguished_name);
  694. /* A u16 length cannot overflow a long. */
  695. name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
  696. if (name == NULL ||
  697. ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
  698. *out_alert = SSL_AD_DECODE_ERROR;
  699. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  700. goto err;
  701. }
  702. if (!sk_X509_NAME_push(ret, name)) {
  703. *out_alert = SSL_AD_INTERNAL_ERROR;
  704. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  705. goto err;
  706. }
  707. name = NULL;
  708. }
  709. return ret;
  710. err:
  711. X509_NAME_free(name);
  712. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  713. return NULL;
  714. }
  715. int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
  716. CBB child, name_cbb;
  717. if (!CBB_add_u16_length_prefixed(cbb, &child)) {
  718. return 0;
  719. }
  720. STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
  721. if (sk == NULL) {
  722. return CBB_flush(cbb);
  723. }
  724. for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
  725. X509_NAME *name = sk_X509_NAME_value(sk, i);
  726. int len = i2d_X509_NAME(name, NULL);
  727. if (len < 0) {
  728. return 0;
  729. }
  730. uint8_t *ptr;
  731. if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
  732. !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
  733. (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
  734. return 0;
  735. }
  736. }
  737. return CBB_flush(cbb);
  738. }
  739. static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
  740. X509_STORE_free(*store_ptr);
  741. *store_ptr = new_store;
  742. if (new_store != NULL && take_ref) {
  743. X509_STORE_up_ref(new_store);
  744. }
  745. return 1;
  746. }
  747. int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  748. return set_cert_store(&ctx->cert->verify_store, store, 0);
  749. }
  750. int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  751. return set_cert_store(&ctx->cert->verify_store, store, 1);
  752. }
  753. int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
  754. return set_cert_store(&ssl->cert->verify_store, store, 0);
  755. }
  756. int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
  757. return set_cert_store(&ssl->cert->verify_store, store, 1);
  758. }
  759. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  760. void *arg) {
  761. ssl_cert_set_cert_cb(ctx->cert, cb, arg);
  762. }
  763. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  764. ssl_cert_set_cert_cb(ssl->cert, cb, arg);
  765. }
  766. int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
  767. const CRYPTO_BUFFER *leaf) {
  768. SSL *const ssl = hs->ssl;
  769. assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
  770. /* Check the certificate's type matches the cipher. */
  771. int expected_type = ssl_cipher_get_key_type(hs->new_cipher);
  772. assert(expected_type != EVP_PKEY_NONE);
  773. if (pkey->type != expected_type) {
  774. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
  775. return 0;
  776. }
  777. if (hs->new_cipher->algorithm_auth & SSL_aECDSA) {
  778. CBS leaf_cbs;
  779. CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
  780. /* ECDSA and ECDH certificates use the same public key format. Instead,
  781. * they are distinguished by the key usage extension in the certificate. */
  782. if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
  783. return 0;
  784. }
  785. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  786. if (ec_key == NULL) {
  787. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  788. return 0;
  789. }
  790. /* Check the key's group and point format are acceptable. */
  791. uint16_t group_id;
  792. if (!ssl_nid_to_group_id(
  793. &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
  794. !tls1_check_group_id(ssl, group_id) ||
  795. EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
  796. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  797. return 0;
  798. }
  799. }
  800. return 1;
  801. }
  802. static int do_client_cert_cb(SSL *ssl, void *arg) {
  803. if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
  804. return 1;
  805. }
  806. X509 *x509 = NULL;
  807. EVP_PKEY *pkey = NULL;
  808. int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
  809. if (ret < 0) {
  810. return -1;
  811. }
  812. if (ret != 0) {
  813. if (!SSL_use_certificate(ssl, x509) ||
  814. !SSL_use_PrivateKey(ssl, pkey)) {
  815. return 0;
  816. }
  817. }
  818. X509_free(x509);
  819. EVP_PKEY_free(pkey);
  820. return 1;
  821. }
  822. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
  823. X509 **out_x509,
  824. EVP_PKEY **out_pkey)) {
  825. /* Emulate the old client certificate callback with the new one. */
  826. SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
  827. ctx->client_cert_cb = cb;
  828. }
  829. static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
  830. size_t list_len) {
  831. CBS sct_list;
  832. CBS_init(&sct_list, list, list_len);
  833. if (!ssl_is_sct_list_valid(&sct_list)) {
  834. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
  835. return 0;
  836. }
  837. CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
  838. cert->signed_cert_timestamp_list =
  839. CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL);
  840. return cert->signed_cert_timestamp_list != NULL;
  841. }
  842. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  843. size_t list_len) {
  844. return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
  845. }
  846. int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
  847. size_t list_len) {
  848. return set_signed_cert_timestamp_list(ssl->cert, list, list_len);
  849. }
  850. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  851. size_t response_len) {
  852. CRYPTO_BUFFER_free(ctx->cert->ocsp_response);
  853. ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
  854. return ctx->cert->ocsp_response != NULL;
  855. }
  856. int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
  857. size_t response_len) {
  858. CRYPTO_BUFFER_free(ssl->cert->ocsp_response);
  859. ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
  860. return ssl->cert->ocsp_response != NULL;
  861. }