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.
 
 
 
 
 
 

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