No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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