You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

908 lines
28 KiB

  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/ec_key.h>
  122. #include <openssl/err.h>
  123. #include <openssl/mem.h>
  124. #include <openssl/sha.h>
  125. #include <openssl/x509.h>
  126. #include "../crypto/internal.h"
  127. #include "internal.h"
  128. CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) {
  129. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  130. if (ret == NULL) {
  131. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  132. return NULL;
  133. }
  134. OPENSSL_memset(ret, 0, sizeof(CERT));
  135. ret->x509_method = x509_method;
  136. return ret;
  137. }
  138. static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
  139. CRYPTO_BUFFER_up_ref(buffer);
  140. return buffer;
  141. }
  142. CERT *ssl_cert_dup(CERT *cert) {
  143. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  144. if (ret == NULL) {
  145. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  146. return NULL;
  147. }
  148. OPENSSL_memset(ret, 0, sizeof(CERT));
  149. ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
  150. CRYPTO_BUFFER_free);
  151. if (cert->privatekey != NULL) {
  152. EVP_PKEY_up_ref(cert->privatekey);
  153. ret->privatekey = cert->privatekey;
  154. }
  155. ret->key_method = cert->key_method;
  156. ret->x509_method = cert->x509_method;
  157. if (cert->sigalgs != NULL) {
  158. ret->sigalgs =
  159. BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
  160. if (ret->sigalgs == NULL) {
  161. goto err;
  162. }
  163. }
  164. ret->num_sigalgs = cert->num_sigalgs;
  165. ret->cert_cb = cert->cert_cb;
  166. ret->cert_cb_arg = cert->cert_cb_arg;
  167. ret->x509_method->cert_dup(ret, cert);
  168. if (cert->signed_cert_timestamp_list != NULL) {
  169. CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list);
  170. ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list;
  171. }
  172. if (cert->ocsp_response != NULL) {
  173. CRYPTO_BUFFER_up_ref(cert->ocsp_response);
  174. ret->ocsp_response = cert->ocsp_response;
  175. }
  176. ret->sid_ctx_length = cert->sid_ctx_length;
  177. OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
  178. ret->enable_early_data = cert->enable_early_data;
  179. return ret;
  180. err:
  181. ssl_cert_free(ret);
  182. return NULL;
  183. }
  184. /* Free up and clear all certificates and chains */
  185. void ssl_cert_clear_certs(CERT *cert) {
  186. if (cert == NULL) {
  187. return;
  188. }
  189. cert->x509_method->cert_clear(cert);
  190. sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
  191. cert->chain = NULL;
  192. EVP_PKEY_free(cert->privatekey);
  193. cert->privatekey = NULL;
  194. cert->key_method = NULL;
  195. }
  196. void ssl_cert_free(CERT *c) {
  197. if (c == NULL) {
  198. return;
  199. }
  200. ssl_cert_clear_certs(c);
  201. c->x509_method->cert_free(c);
  202. OPENSSL_free(c->sigalgs);
  203. CRYPTO_BUFFER_free(c->signed_cert_timestamp_list);
  204. CRYPTO_BUFFER_free(c->ocsp_response);
  205. OPENSSL_free(c);
  206. }
  207. static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
  208. void *arg) {
  209. c->cert_cb = cb;
  210. c->cert_cb_arg = arg;
  211. }
  212. enum leaf_cert_and_privkey_result_t {
  213. leaf_cert_and_privkey_error,
  214. leaf_cert_and_privkey_ok,
  215. leaf_cert_and_privkey_mismatch,
  216. };
  217. /* check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer|
  218. * and the private key in |privkey| are suitable and coherent. It returns
  219. * |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is
  220. * found. If the certificate and private key are valid, but incoherent, it
  221. * returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns
  222. * |leaf_cert_and_privkey_ok|. */
  223. static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey(
  224. CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) {
  225. enum leaf_cert_and_privkey_result_t ret = leaf_cert_and_privkey_error;
  226. CBS cert_cbs;
  227. CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs);
  228. EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
  229. if (pubkey == NULL) {
  230. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  231. goto out;
  232. }
  233. if (!ssl_is_key_type_supported(pubkey->type)) {
  234. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  235. goto out;
  236. }
  237. /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
  238. * certificates, so sanity-check the key usage extension. */
  239. if (pubkey->type == EVP_PKEY_EC &&
  240. !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
  241. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  242. goto out;
  243. }
  244. if (privkey != NULL &&
  245. /* Sanity-check that the private key and the certificate match. */
  246. !ssl_compare_public_and_private_key(pubkey, privkey)) {
  247. ERR_clear_error();
  248. ret = leaf_cert_and_privkey_mismatch;
  249. goto out;
  250. }
  251. ret = leaf_cert_and_privkey_ok;
  252. out:
  253. EVP_PKEY_free(pubkey);
  254. return ret;
  255. }
  256. static int cert_set_chain_and_key(
  257. CERT *cert, CRYPTO_BUFFER *const *certs, size_t num_certs,
  258. EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) {
  259. if (num_certs == 0 ||
  260. (privkey == NULL && privkey_method == NULL)) {
  261. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  262. return 0;
  263. }
  264. if (privkey != NULL && privkey_method != NULL) {
  265. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
  266. return 0;
  267. }
  268. switch (check_leaf_cert_and_privkey(certs[0], privkey)) {
  269. case leaf_cert_and_privkey_error:
  270. return 0;
  271. case leaf_cert_and_privkey_mismatch:
  272. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
  273. return 0;
  274. case leaf_cert_and_privkey_ok:
  275. break;
  276. }
  277. STACK_OF(CRYPTO_BUFFER) *certs_sk = sk_CRYPTO_BUFFER_new_null();
  278. if (certs_sk == NULL) {
  279. return 0;
  280. }
  281. for (size_t i = 0; i < num_certs; i++) {
  282. if (!sk_CRYPTO_BUFFER_push(certs_sk, certs[i])) {
  283. sk_CRYPTO_BUFFER_pop_free(certs_sk, CRYPTO_BUFFER_free);
  284. return 0;
  285. }
  286. CRYPTO_BUFFER_up_ref(certs[i]);
  287. }
  288. EVP_PKEY_free(cert->privatekey);
  289. cert->privatekey = privkey;
  290. if (privkey != NULL) {
  291. EVP_PKEY_up_ref(privkey);
  292. }
  293. cert->key_method = privkey_method;
  294. sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
  295. cert->chain = certs_sk;
  296. return 1;
  297. }
  298. int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
  299. size_t num_certs, EVP_PKEY *privkey,
  300. const SSL_PRIVATE_KEY_METHOD *privkey_method) {
  301. return cert_set_chain_and_key(ssl->cert, certs, num_certs, privkey,
  302. privkey_method);
  303. }
  304. int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
  305. size_t num_certs, EVP_PKEY *privkey,
  306. const SSL_PRIVATE_KEY_METHOD *privkey_method) {
  307. return cert_set_chain_and_key(ctx->cert, certs, num_certs, privkey,
  308. privkey_method);
  309. }
  310. int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) {
  311. switch (check_leaf_cert_and_privkey(buffer, cert->privatekey)) {
  312. case leaf_cert_and_privkey_error:
  313. return 0;
  314. case leaf_cert_and_privkey_mismatch:
  315. /* don't fail for a cert/key mismatch, just free current private key
  316. * (when switching to a different cert & key, first this function should
  317. * be used, then |ssl_set_pkey|. */
  318. EVP_PKEY_free(cert->privatekey);
  319. cert->privatekey = NULL;
  320. break;
  321. case leaf_cert_and_privkey_ok:
  322. break;
  323. }
  324. cert->x509_method->cert_flush_cached_leaf(cert);
  325. if (cert->chain != NULL) {
  326. CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0));
  327. sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer);
  328. CRYPTO_BUFFER_up_ref(buffer);
  329. return 1;
  330. }
  331. cert->chain = sk_CRYPTO_BUFFER_new_null();
  332. if (cert->chain == NULL) {
  333. return 0;
  334. }
  335. if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
  336. sk_CRYPTO_BUFFER_free(cert->chain);
  337. cert->chain = NULL;
  338. return 0;
  339. }
  340. CRYPTO_BUFFER_up_ref(buffer);
  341. return 1;
  342. }
  343. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
  344. const uint8_t *der) {
  345. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
  346. if (buffer == NULL) {
  347. return 0;
  348. }
  349. const int ok = ssl_set_cert(ctx->cert, buffer);
  350. CRYPTO_BUFFER_free(buffer);
  351. return ok;
  352. }
  353. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  354. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
  355. if (buffer == NULL) {
  356. return 0;
  357. }
  358. const int ok = ssl_set_cert(ssl->cert, buffer);
  359. CRYPTO_BUFFER_free(buffer);
  360. return ok;
  361. }
  362. int ssl_has_certificate(const SSL *ssl) {
  363. return ssl->cert->chain != NULL &&
  364. sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
  365. ssl_has_private_key(ssl);
  366. }
  367. STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
  368. EVP_PKEY **out_pubkey,
  369. uint8_t *out_leaf_sha256,
  370. CBS *cbs,
  371. CRYPTO_BUFFER_POOL *pool) {
  372. *out_pubkey = NULL;
  373. STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
  374. if (ret == NULL) {
  375. *out_alert = SSL_AD_INTERNAL_ERROR;
  376. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  377. return NULL;
  378. }
  379. CBS certificate_list;
  380. if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
  381. *out_alert = SSL_AD_DECODE_ERROR;
  382. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  383. goto err;
  384. }
  385. while (CBS_len(&certificate_list) > 0) {
  386. CBS certificate;
  387. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
  388. CBS_len(&certificate) == 0) {
  389. *out_alert = SSL_AD_DECODE_ERROR;
  390. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  391. goto err;
  392. }
  393. if (sk_CRYPTO_BUFFER_num(ret) == 0) {
  394. *out_pubkey = ssl_cert_parse_pubkey(&certificate);
  395. if (*out_pubkey == NULL) {
  396. *out_alert = SSL_AD_DECODE_ERROR;
  397. goto err;
  398. }
  399. /* Retain the hash of the leaf certificate if requested. */
  400. if (out_leaf_sha256 != NULL) {
  401. SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
  402. }
  403. }
  404. CRYPTO_BUFFER *buf =
  405. CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
  406. if (buf == NULL) {
  407. *out_alert = SSL_AD_DECODE_ERROR;
  408. goto err;
  409. }
  410. if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
  411. *out_alert = SSL_AD_INTERNAL_ERROR;
  412. CRYPTO_BUFFER_free(buf);
  413. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  414. goto err;
  415. }
  416. }
  417. return ret;
  418. err:
  419. EVP_PKEY_free(*out_pubkey);
  420. *out_pubkey = NULL;
  421. sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
  422. return NULL;
  423. }
  424. int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
  425. if (!ssl_has_certificate(ssl)) {
  426. return CBB_add_u24(cbb, 0);
  427. }
  428. CBB certs;
  429. if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
  430. goto err;
  431. }
  432. STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
  433. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
  434. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
  435. CBB child;
  436. if (!CBB_add_u24_length_prefixed(&certs, &child) ||
  437. !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  438. CRYPTO_BUFFER_len(buffer)) ||
  439. !CBB_flush(&certs)) {
  440. goto err;
  441. }
  442. }
  443. return CBB_flush(cbb);
  444. err:
  445. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  446. return 0;
  447. }
  448. /* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
  449. * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
  450. * subjectPublicKeyInfo. */
  451. static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
  452. /* From RFC 5280, section 4.1
  453. * Certificate ::= SEQUENCE {
  454. * tbsCertificate TBSCertificate,
  455. * signatureAlgorithm AlgorithmIdentifier,
  456. * signatureValue BIT STRING }
  457. * TBSCertificate ::= SEQUENCE {
  458. * version [0] EXPLICIT Version DEFAULT v1,
  459. * serialNumber CertificateSerialNumber,
  460. * signature AlgorithmIdentifier,
  461. * issuer Name,
  462. * validity Validity,
  463. * subject Name,
  464. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  465. * ... } */
  466. CBS buf = *in;
  467. CBS toplevel;
  468. if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
  469. CBS_len(&buf) != 0 ||
  470. !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
  471. /* version */
  472. !CBS_get_optional_asn1(
  473. out_tbs_cert, NULL, NULL,
  474. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
  475. /* serialNumber */
  476. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
  477. /* signature algorithm */
  478. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  479. /* issuer */
  480. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  481. /* validity */
  482. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  483. /* subject */
  484. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
  485. return 0;
  486. }
  487. return 1;
  488. }
  489. EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
  490. CBS buf = *in, tbs_cert;
  491. if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
  492. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  493. return NULL;
  494. }
  495. return EVP_parse_public_key(&tbs_cert);
  496. }
  497. int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
  498. const EVP_PKEY *privkey) {
  499. if (EVP_PKEY_is_opaque(privkey)) {
  500. /* We cannot check an opaque private key and have to trust that it
  501. * matches. */
  502. return 1;
  503. }
  504. int ret = 0;
  505. switch (EVP_PKEY_cmp(pubkey, privkey)) {
  506. case 1:
  507. ret = 1;
  508. break;
  509. case 0:
  510. OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
  511. break;
  512. case -1:
  513. OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
  514. break;
  515. case -2:
  516. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
  517. default:
  518. assert(0);
  519. break;
  520. }
  521. return ret;
  522. }
  523. int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
  524. if (privkey == NULL) {
  525. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  526. return 0;
  527. }
  528. if (cert->chain == NULL ||
  529. sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
  530. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  531. return 0;
  532. }
  533. CBS cert_cbs;
  534. CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
  535. EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
  536. if (!pubkey) {
  537. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
  538. return 0;
  539. }
  540. const int ok = ssl_compare_public_and_private_key(pubkey, privkey);
  541. EVP_PKEY_free(pubkey);
  542. return ok;
  543. }
  544. int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
  545. CBS buf = *in;
  546. CBS tbs_cert, outer_extensions;
  547. int has_extensions;
  548. if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
  549. /* subjectPublicKeyInfo */
  550. !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  551. /* issuerUniqueID */
  552. !CBS_get_optional_asn1(
  553. &tbs_cert, NULL, NULL,
  554. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
  555. /* subjectUniqueID */
  556. !CBS_get_optional_asn1(
  557. &tbs_cert, NULL, NULL,
  558. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
  559. !CBS_get_optional_asn1(
  560. &tbs_cert, &outer_extensions, &has_extensions,
  561. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
  562. goto parse_err;
  563. }
  564. if (!has_extensions) {
  565. return 1;
  566. }
  567. CBS extensions;
  568. if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
  569. goto parse_err;
  570. }
  571. while (CBS_len(&extensions) > 0) {
  572. CBS extension, oid, contents;
  573. if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
  574. !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
  575. (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
  576. !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
  577. !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
  578. CBS_len(&extension) != 0) {
  579. goto parse_err;
  580. }
  581. static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
  582. if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
  583. OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
  584. 0) {
  585. continue;
  586. }
  587. CBS bit_string;
  588. if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
  589. CBS_len(&contents) != 0) {
  590. goto parse_err;
  591. }
  592. /* This is the KeyUsage extension. See
  593. * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
  594. if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
  595. goto parse_err;
  596. }
  597. if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
  598. OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  599. return 0;
  600. }
  601. return 1;
  602. }
  603. /* No KeyUsage extension found. */
  604. return 1;
  605. parse_err:
  606. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  607. return 0;
  608. }
  609. STACK_OF(CRYPTO_BUFFER) *
  610. ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
  611. CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
  612. STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
  613. if (ret == NULL) {
  614. *out_alert = SSL_AD_INTERNAL_ERROR;
  615. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  616. return NULL;
  617. }
  618. CBS child;
  619. if (!CBS_get_u16_length_prefixed(cbs, &child)) {
  620. *out_alert = SSL_AD_DECODE_ERROR;
  621. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  622. goto err;
  623. }
  624. while (CBS_len(&child) > 0) {
  625. CBS distinguished_name;
  626. if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
  627. *out_alert = SSL_AD_DECODE_ERROR;
  628. OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
  629. goto err;
  630. }
  631. CRYPTO_BUFFER *buffer =
  632. CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool);
  633. if (buffer == NULL ||
  634. !sk_CRYPTO_BUFFER_push(ret, buffer)) {
  635. CRYPTO_BUFFER_free(buffer);
  636. *out_alert = SSL_AD_INTERNAL_ERROR;
  637. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  638. goto err;
  639. }
  640. }
  641. if (!ssl->ctx->x509_method->check_client_CA_list(ret)) {
  642. *out_alert = SSL_AD_INTERNAL_ERROR;
  643. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  644. goto err;
  645. }
  646. return ret;
  647. err:
  648. sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
  649. return NULL;
  650. }
  651. int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
  652. CBB child, name_cbb;
  653. if (!CBB_add_u16_length_prefixed(cbb, &child)) {
  654. return 0;
  655. }
  656. STACK_OF(CRYPTO_BUFFER) *names = ssl->client_CA;
  657. if (names == NULL) {
  658. names = ssl->ctx->client_CA;
  659. }
  660. if (names == NULL) {
  661. return CBB_flush(cbb);
  662. }
  663. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
  664. const CRYPTO_BUFFER *name = sk_CRYPTO_BUFFER_value(names, i);
  665. if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
  666. !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
  667. CRYPTO_BUFFER_len(name))) {
  668. return 0;
  669. }
  670. }
  671. return CBB_flush(cbb);
  672. }
  673. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  674. void *arg) {
  675. ssl_cert_set_cert_cb(ctx->cert, cb, arg);
  676. }
  677. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  678. ssl_cert_set_cert_cb(ssl->cert, cb, arg);
  679. }
  680. STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
  681. SSL_SESSION *session = SSL_get_session(ssl);
  682. if (session == NULL) {
  683. return NULL;
  684. }
  685. return session->certs;
  686. }
  687. STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
  688. if (ssl->s3->hs == NULL) {
  689. return NULL;
  690. }
  691. return ssl->s3->hs->ca_names;
  692. }
  693. int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
  694. const CRYPTO_BUFFER *leaf) {
  695. SSL *const ssl = hs->ssl;
  696. assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
  697. /* Check the certificate's type matches the cipher. */
  698. if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
  699. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
  700. return 0;
  701. }
  702. /* Check key usages for all key types but RSA. This is needed to distinguish
  703. * ECDH certificates, which we do not support, from ECDSA certificates. In
  704. * principle, we should check RSA key usages based on cipher, but this breaks
  705. * buggy antivirus deployments. Other key types are always used for signing.
  706. *
  707. * TODO(davidben): Get more recent data on RSA key usages. */
  708. if (EVP_PKEY_id(pkey) != EVP_PKEY_RSA) {
  709. CBS leaf_cbs;
  710. CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
  711. if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
  712. return 0;
  713. }
  714. }
  715. if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
  716. /* Check the key's group and point format are acceptable. */
  717. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  718. uint16_t group_id;
  719. if (!ssl_nid_to_group_id(
  720. &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
  721. !tls1_check_group_id(ssl, group_id) ||
  722. EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
  723. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  724. return 0;
  725. }
  726. }
  727. return 1;
  728. }
  729. int ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
  730. SSL *const ssl = hs->ssl;
  731. if (!ssl_has_certificate(ssl)) {
  732. /* Nothing to do. */
  733. return 1;
  734. }
  735. if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(ssl)) {
  736. return 0;
  737. }
  738. CBS leaf;
  739. CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0), &leaf);
  740. EVP_PKEY_free(hs->local_pubkey);
  741. hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
  742. return hs->local_pubkey != NULL;
  743. }
  744. static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
  745. size_t list_len) {
  746. CBS sct_list;
  747. CBS_init(&sct_list, list, list_len);
  748. if (!ssl_is_sct_list_valid(&sct_list)) {
  749. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
  750. return 0;
  751. }
  752. CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
  753. cert->signed_cert_timestamp_list =
  754. CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL);
  755. return cert->signed_cert_timestamp_list != NULL;
  756. }
  757. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  758. size_t list_len) {
  759. return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
  760. }
  761. int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
  762. size_t list_len) {
  763. return set_signed_cert_timestamp_list(ssl->cert, list, list_len);
  764. }
  765. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  766. size_t response_len) {
  767. CRYPTO_BUFFER_free(ctx->cert->ocsp_response);
  768. ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
  769. return ctx->cert->ocsp_response != NULL;
  770. }
  771. int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
  772. size_t response_len) {
  773. CRYPTO_BUFFER_free(ssl->cert->ocsp_response);
  774. ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
  775. return ssl->cert->ocsp_response != NULL;
  776. }