ssl_cert.cc 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. if (cert->dc) {
  165. ret->dc = cert->dc->Dup();
  166. if (!ret->dc) {
  167. return nullptr;
  168. }
  169. }
  170. ret->dc_privatekey = UpRef(cert->dc_privatekey);
  171. ret->dc_key_method = cert->dc_key_method;
  172. return ret;
  173. }
  174. // Free up and clear all certificates and chains
  175. void ssl_cert_clear_certs(CERT *cert) {
  176. if (cert == NULL) {
  177. return;
  178. }
  179. cert->x509_method->cert_clear(cert);
  180. cert->chain.reset();
  181. cert->privatekey.reset();
  182. cert->key_method = nullptr;
  183. cert->dc.reset();
  184. cert->dc_privatekey.reset();
  185. cert->dc_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_key_usage(&cert_cbs, key_usage_digital_signature)) {
  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 (!PushToStack(certs_sk.get(), UpRef(certs[i]))) {
  258. return 0;
  259. }
  260. }
  261. cert->privatekey = UpRef(privkey);
  262. cert->key_method = privkey_method;
  263. cert->chain = std::move(certs_sk);
  264. return 1;
  265. }
  266. bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) {
  267. switch (check_leaf_cert_and_privkey(buffer.get(), cert->privatekey.get())) {
  268. case leaf_cert_and_privkey_error:
  269. return false;
  270. case leaf_cert_and_privkey_mismatch:
  271. // don't fail for a cert/key mismatch, just free current private key
  272. // (when switching to a different cert & key, first this function should
  273. // be used, then |ssl_set_pkey|.
  274. cert->privatekey.reset();
  275. break;
  276. case leaf_cert_and_privkey_ok:
  277. break;
  278. }
  279. cert->x509_method->cert_flush_cached_leaf(cert);
  280. if (cert->chain != nullptr) {
  281. CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0));
  282. sk_CRYPTO_BUFFER_set(cert->chain.get(), 0, buffer.release());
  283. return true;
  284. }
  285. cert->chain.reset(sk_CRYPTO_BUFFER_new_null());
  286. if (cert->chain == nullptr) {
  287. return false;
  288. }
  289. if (!PushToStack(cert->chain.get(), std::move(buffer))) {
  290. cert->chain.reset();
  291. return false;
  292. }
  293. return true;
  294. }
  295. bool ssl_has_certificate(const SSL_HANDSHAKE *hs) {
  296. return hs->config->cert->chain != nullptr &&
  297. sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0) != nullptr &&
  298. ssl_has_private_key(hs);
  299. }
  300. bool ssl_parse_cert_chain(uint8_t *out_alert,
  301. UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
  302. UniquePtr<EVP_PKEY> *out_pubkey,
  303. uint8_t *out_leaf_sha256, CBS *cbs,
  304. CRYPTO_BUFFER_POOL *pool) {
  305. out_chain->reset();
  306. out_pubkey->reset();
  307. CBS certificate_list;
  308. if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
  309. *out_alert = SSL_AD_DECODE_ERROR;
  310. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  311. return false;
  312. }
  313. if (CBS_len(&certificate_list) == 0) {
  314. return true;
  315. }
  316. UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null());
  317. if (!chain) {
  318. *out_alert = SSL_AD_INTERNAL_ERROR;
  319. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  320. return false;
  321. }
  322. UniquePtr<EVP_PKEY> pubkey;
  323. while (CBS_len(&certificate_list) > 0) {
  324. CBS certificate;
  325. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
  326. CBS_len(&certificate) == 0) {
  327. *out_alert = SSL_AD_DECODE_ERROR;
  328. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  329. return false;
  330. }
  331. if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) {
  332. pubkey = ssl_cert_parse_pubkey(&certificate);
  333. if (!pubkey) {
  334. *out_alert = SSL_AD_DECODE_ERROR;
  335. return false;
  336. }
  337. // Retain the hash of the leaf certificate if requested.
  338. if (out_leaf_sha256 != NULL) {
  339. SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
  340. }
  341. }
  342. UniquePtr<CRYPTO_BUFFER> buf(
  343. CRYPTO_BUFFER_new_from_CBS(&certificate, pool));
  344. if (!buf ||
  345. !PushToStack(chain.get(), std::move(buf))) {
  346. *out_alert = SSL_AD_INTERNAL_ERROR;
  347. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  348. return false;
  349. }
  350. }
  351. *out_chain = std::move(chain);
  352. *out_pubkey = std::move(pubkey);
  353. return true;
  354. }
  355. bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb) {
  356. if (!ssl_has_certificate(hs)) {
  357. return CBB_add_u24(cbb, 0);
  358. }
  359. CBB certs;
  360. if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
  361. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  362. return false;
  363. }
  364. STACK_OF(CRYPTO_BUFFER) *chain = hs->config->cert->chain.get();
  365. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
  366. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
  367. CBB child;
  368. if (!CBB_add_u24_length_prefixed(&certs, &child) ||
  369. !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
  370. CRYPTO_BUFFER_len(buffer)) ||
  371. !CBB_flush(&certs)) {
  372. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  373. return false;
  374. }
  375. }
  376. return CBB_flush(cbb);
  377. }
  378. // ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
  379. // positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
  380. // subjectPublicKeyInfo.
  381. static bool ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
  382. /* From RFC 5280, section 4.1
  383. * Certificate ::= SEQUENCE {
  384. * tbsCertificate TBSCertificate,
  385. * signatureAlgorithm AlgorithmIdentifier,
  386. * signatureValue BIT STRING }
  387. * TBSCertificate ::= SEQUENCE {
  388. * version [0] EXPLICIT Version DEFAULT v1,
  389. * serialNumber CertificateSerialNumber,
  390. * signature AlgorithmIdentifier,
  391. * issuer Name,
  392. * validity Validity,
  393. * subject Name,
  394. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  395. * ... } */
  396. CBS buf = *in;
  397. CBS toplevel;
  398. if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
  399. CBS_len(&buf) != 0 ||
  400. !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
  401. // version
  402. !CBS_get_optional_asn1(
  403. out_tbs_cert, NULL, NULL,
  404. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
  405. // serialNumber
  406. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
  407. // signature algorithm
  408. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  409. // issuer
  410. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  411. // validity
  412. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  413. // subject
  414. !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
  415. return false;
  416. }
  417. return true;
  418. }
  419. UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) {
  420. CBS buf = *in, tbs_cert;
  421. if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
  422. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  423. return nullptr;
  424. }
  425. return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert));
  426. }
  427. bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
  428. const EVP_PKEY *privkey) {
  429. if (EVP_PKEY_is_opaque(privkey)) {
  430. // We cannot check an opaque private key and have to trust that it
  431. // matches.
  432. return true;
  433. }
  434. switch (EVP_PKEY_cmp(pubkey, privkey)) {
  435. case 1:
  436. return true;
  437. case 0:
  438. OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
  439. return false;
  440. case -1:
  441. OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
  442. return false;
  443. case -2:
  444. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
  445. return false;
  446. }
  447. assert(0);
  448. return false;
  449. }
  450. bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
  451. if (privkey == nullptr) {
  452. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  453. return false;
  454. }
  455. if (cert->chain == nullptr ||
  456. sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) {
  457. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  458. return false;
  459. }
  460. CBS cert_cbs;
  461. CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0),
  462. &cert_cbs);
  463. UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
  464. if (!pubkey) {
  465. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
  466. return false;
  467. }
  468. return ssl_compare_public_and_private_key(pubkey.get(), privkey);
  469. }
  470. bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit) {
  471. CBS buf = *in;
  472. CBS tbs_cert, outer_extensions;
  473. int has_extensions;
  474. if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
  475. // subjectPublicKeyInfo
  476. !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
  477. // issuerUniqueID
  478. !CBS_get_optional_asn1(
  479. &tbs_cert, NULL, NULL,
  480. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
  481. // subjectUniqueID
  482. !CBS_get_optional_asn1(
  483. &tbs_cert, NULL, NULL,
  484. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
  485. !CBS_get_optional_asn1(
  486. &tbs_cert, &outer_extensions, &has_extensions,
  487. CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
  488. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  489. return false;
  490. }
  491. if (!has_extensions) {
  492. return true;
  493. }
  494. CBS extensions;
  495. if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
  496. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  497. return false;
  498. }
  499. while (CBS_len(&extensions) > 0) {
  500. CBS extension, oid, contents;
  501. if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
  502. !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
  503. (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
  504. !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
  505. !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
  506. CBS_len(&extension) != 0) {
  507. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  508. return false;
  509. }
  510. static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
  511. if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
  512. OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
  513. 0) {
  514. continue;
  515. }
  516. CBS bit_string;
  517. if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
  518. CBS_len(&contents) != 0) {
  519. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  520. return false;
  521. }
  522. // This is the KeyUsage extension. See
  523. // https://tools.ietf.org/html/rfc5280#section-4.2.1.3
  524. if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
  525. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
  526. return false;
  527. }
  528. if (!CBS_asn1_bitstring_has_bit(&bit_string, bit)) {
  529. OPENSSL_PUT_ERROR(SSL, SSL_R_KEY_USAGE_BIT_INCORRECT);
  530. return false;
  531. }
  532. return true;
  533. }
  534. // No KeyUsage extension found.
  535. return true;
  536. }
  537. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
  538. uint8_t *out_alert,
  539. CBS *cbs) {
  540. CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
  541. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
  542. if (!ret) {
  543. *out_alert = SSL_AD_INTERNAL_ERROR;
  544. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  545. return nullptr;
  546. }
  547. CBS child;
  548. if (!CBS_get_u16_length_prefixed(cbs, &child)) {
  549. *out_alert = SSL_AD_DECODE_ERROR;
  550. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  551. return nullptr;
  552. }
  553. while (CBS_len(&child) > 0) {
  554. CBS distinguished_name;
  555. if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
  556. *out_alert = SSL_AD_DECODE_ERROR;
  557. OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
  558. return nullptr;
  559. }
  560. UniquePtr<CRYPTO_BUFFER> buffer(
  561. CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool));
  562. if (!buffer ||
  563. !PushToStack(ret.get(), std::move(buffer))) {
  564. *out_alert = SSL_AD_INTERNAL_ERROR;
  565. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  566. return nullptr;
  567. }
  568. }
  569. if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) {
  570. *out_alert = SSL_AD_DECODE_ERROR;
  571. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  572. return nullptr;
  573. }
  574. return ret;
  575. }
  576. bool ssl_has_client_CAs(const SSL_CONFIG *cfg) {
  577. const STACK_OF(CRYPTO_BUFFER) *names = cfg->client_CA.get();
  578. if (names == nullptr) {
  579. names = cfg->ssl->ctx->client_CA.get();
  580. }
  581. if (names == nullptr) {
  582. return false;
  583. }
  584. return sk_CRYPTO_BUFFER_num(names) > 0;
  585. }
  586. bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb) {
  587. CBB child, name_cbb;
  588. if (!CBB_add_u16_length_prefixed(cbb, &child)) {
  589. return false;
  590. }
  591. const STACK_OF(CRYPTO_BUFFER) *names = hs->config->client_CA.get();
  592. if (names == NULL) {
  593. names = hs->ssl->ctx->client_CA.get();
  594. }
  595. if (names == NULL) {
  596. return CBB_flush(cbb);
  597. }
  598. for (const CRYPTO_BUFFER *name : names) {
  599. if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
  600. !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
  601. CRYPTO_BUFFER_len(name))) {
  602. return false;
  603. }
  604. }
  605. return CBB_flush(cbb);
  606. }
  607. bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
  608. const CRYPTO_BUFFER *leaf) {
  609. assert(ssl_protocol_version(hs->ssl) < TLS1_3_VERSION);
  610. // Check the certificate's type matches the cipher.
  611. if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
  612. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
  613. return false;
  614. }
  615. if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
  616. // Check the key's group and point format are acceptable.
  617. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  618. uint16_t group_id;
  619. if (!ssl_nid_to_group_id(
  620. &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
  621. !tls1_check_group_id(hs, group_id) ||
  622. EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
  623. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  624. return false;
  625. }
  626. }
  627. return true;
  628. }
  629. bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
  630. SSL *const ssl = hs->ssl;
  631. if (!ssl_has_certificate(hs)) {
  632. // Nothing to do.
  633. return true;
  634. }
  635. if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(hs)) {
  636. return false;
  637. }
  638. CBS leaf;
  639. CRYPTO_BUFFER_init_CBS(
  640. sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0), &leaf);
  641. if (ssl_signing_with_dc(hs)) {
  642. hs->local_pubkey = UpRef(hs->config->cert->dc->pkey);
  643. } else {
  644. hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
  645. }
  646. return hs->local_pubkey != NULL;
  647. }
  648. // Delegated credentials.
  649. DC::DC() = default;
  650. DC::~DC() = default;
  651. UniquePtr<DC> DC::Dup() {
  652. bssl::UniquePtr<DC> ret = MakeUnique<DC>();
  653. if (!ret) {
  654. return nullptr;
  655. }
  656. ret->raw = UpRef(raw);
  657. ret->expected_cert_verify_algorithm = expected_cert_verify_algorithm;
  658. ret->pkey = UpRef(pkey);
  659. return ret;
  660. }
  661. // static
  662. UniquePtr<DC> DC::Parse(CRYPTO_BUFFER *in, uint8_t *out_alert) {
  663. UniquePtr<DC> dc = MakeUnique<DC>();
  664. if (!dc) {
  665. *out_alert = SSL_AD_INTERNAL_ERROR;
  666. return nullptr;
  667. }
  668. dc->raw = UpRef(in);
  669. CBS pubkey, deleg, sig;
  670. uint32_t valid_time;
  671. uint16_t algorithm;
  672. CRYPTO_BUFFER_init_CBS(dc->raw.get(), &deleg);
  673. if (!CBS_get_u32(&deleg, &valid_time) ||
  674. !CBS_get_u16(&deleg, &dc->expected_cert_verify_algorithm) ||
  675. !CBS_get_u24_length_prefixed(&deleg, &pubkey) ||
  676. !CBS_get_u16(&deleg, &algorithm) ||
  677. !CBS_get_u16_length_prefixed(&deleg, &sig) ||
  678. CBS_len(&deleg) != 0) {
  679. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  680. *out_alert = SSL_AD_DECODE_ERROR;
  681. return nullptr;
  682. }
  683. dc->pkey.reset(EVP_parse_public_key(&pubkey));
  684. if (dc->pkey == nullptr) {
  685. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  686. *out_alert = SSL_AD_DECODE_ERROR;
  687. return nullptr;
  688. }
  689. return dc;
  690. }
  691. // ssl_can_serve_dc returns true if the host has configured a DC that it can
  692. // serve in the handshake. Specifically, it checks that a DC has been
  693. // configured and that the DC signature algorithm is supported by the peer.
  694. static bool ssl_can_serve_dc(const SSL_HANDSHAKE *hs) {
  695. // Check that a DC has been configured.
  696. const CERT *cert = hs->config->cert.get();
  697. if (cert->dc == nullptr ||
  698. cert->dc->raw == nullptr ||
  699. (cert->dc_privatekey == nullptr && cert->dc_key_method == nullptr)) {
  700. return false;
  701. }
  702. // Check that 1.3 or higher has been negotiated.
  703. const DC *dc = cert->dc.get();
  704. assert(hs->ssl->s3->have_version);
  705. if (ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
  706. return false;
  707. }
  708. // Check that the DC signature algorithm is supported by the peer.
  709. Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs);
  710. bool sigalg_found = false;
  711. for (uint16_t peer_sigalg : peer_sigalgs) {
  712. if (dc->expected_cert_verify_algorithm == peer_sigalg) {
  713. sigalg_found = true;
  714. break;
  715. }
  716. }
  717. return sigalg_found;
  718. }
  719. bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs) {
  720. // As of draft-ietf-tls-subcert-03, only the server may use delegated
  721. // credentials to authenticate itself.
  722. return hs->ssl->server &&
  723. hs->delegated_credential_requested &&
  724. ssl_can_serve_dc(hs);
  725. }
  726. static int cert_set_dc(CERT *cert, CRYPTO_BUFFER *const raw, EVP_PKEY *privkey,
  727. const SSL_PRIVATE_KEY_METHOD *key_method) {
  728. if (privkey == nullptr && key_method == nullptr) {
  729. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  730. return 0;
  731. }
  732. if (privkey != nullptr && key_method != nullptr) {
  733. OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
  734. return 0;
  735. }
  736. uint8_t alert;
  737. UniquePtr<DC> dc = DC::Parse(raw, &alert);
  738. if (dc == nullptr) {
  739. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_DELEGATED_CREDENTIAL);
  740. return 0;
  741. }
  742. if (privkey) {
  743. // Check that the public and private keys match.
  744. if (!ssl_compare_public_and_private_key(dc->pkey.get(), privkey)) {
  745. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
  746. return 0;
  747. }
  748. }
  749. cert->dc = std::move(dc);
  750. cert->dc_privatekey = UpRef(privkey);
  751. cert->dc_key_method = key_method;
  752. return 1;
  753. }
  754. BSSL_NAMESPACE_END
  755. using namespace bssl;
  756. int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
  757. size_t num_certs, EVP_PKEY *privkey,
  758. const SSL_PRIVATE_KEY_METHOD *privkey_method) {
  759. if (!ssl->config) {
  760. return 0;
  761. }
  762. return cert_set_chain_and_key(ssl->config->cert.get(), certs, num_certs,
  763. privkey, privkey_method);
  764. }
  765. int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
  766. size_t num_certs, EVP_PKEY *privkey,
  767. const SSL_PRIVATE_KEY_METHOD *privkey_method) {
  768. return cert_set_chain_and_key(ctx->cert.get(), certs, num_certs, privkey,
  769. privkey_method);
  770. }
  771. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
  772. const uint8_t *der) {
  773. UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
  774. if (!buffer) {
  775. return 0;
  776. }
  777. return ssl_set_cert(ctx->cert.get(), std::move(buffer));
  778. }
  779. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  780. UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
  781. if (!buffer || !ssl->config) {
  782. return 0;
  783. }
  784. return ssl_set_cert(ssl->config->cert.get(), std::move(buffer));
  785. }
  786. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  787. void *arg) {
  788. ssl_cert_set_cert_cb(ctx->cert.get(), cb, arg);
  789. }
  790. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  791. if (!ssl->config) {
  792. return;
  793. }
  794. ssl_cert_set_cert_cb(ssl->config->cert.get(), cb, arg);
  795. }
  796. const STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
  797. SSL_SESSION *session = SSL_get_session(ssl);
  798. if (session == NULL) {
  799. return NULL;
  800. }
  801. return session->certs.get();
  802. }
  803. const STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
  804. if (ssl->s3->hs == NULL) {
  805. return NULL;
  806. }
  807. return ssl->s3->hs->ca_names.get();
  808. }
  809. static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
  810. size_t list_len) {
  811. CBS sct_list;
  812. CBS_init(&sct_list, list, list_len);
  813. if (!ssl_is_sct_list_valid(&sct_list)) {
  814. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
  815. return 0;
  816. }
  817. cert->signed_cert_timestamp_list.reset(
  818. CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), nullptr));
  819. return cert->signed_cert_timestamp_list != nullptr;
  820. }
  821. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  822. size_t list_len) {
  823. return set_signed_cert_timestamp_list(ctx->cert.get(), list, list_len);
  824. }
  825. int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
  826. size_t list_len) {
  827. if (!ssl->config) {
  828. return 0;
  829. }
  830. return set_signed_cert_timestamp_list(ssl->config->cert.get(), list,
  831. list_len);
  832. }
  833. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  834. size_t response_len) {
  835. ctx->cert->ocsp_response.reset(
  836. CRYPTO_BUFFER_new(response, response_len, nullptr));
  837. return ctx->cert->ocsp_response != nullptr;
  838. }
  839. int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
  840. size_t response_len) {
  841. if (!ssl->config) {
  842. return 0;
  843. }
  844. ssl->config->cert->ocsp_response.reset(
  845. CRYPTO_BUFFER_new(response, response_len, nullptr));
  846. return ssl->config->cert->ocsp_response != nullptr;
  847. }
  848. void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) {
  849. ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
  850. ctx->client_CA.reset(name_list);
  851. }
  852. void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) {
  853. if (!ssl->config) {
  854. return;
  855. }
  856. ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get());
  857. ssl->config->client_CA.reset(name_list);
  858. }
  859. int SSL_set1_delegated_credential(SSL *ssl, CRYPTO_BUFFER *dc, EVP_PKEY *pkey,
  860. const SSL_PRIVATE_KEY_METHOD *key_method) {
  861. if (!ssl->config) {
  862. return 0;
  863. }
  864. return cert_set_dc(ssl->config->cert.get(), dc, pkey, key_method);
  865. }
  866. int SSL_delegated_credential_used(const SSL *ssl) {
  867. return ssl->s3->delegated_credential_used;
  868. }