Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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