Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

811 рядки
25 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. #include <openssl/ssl.h>
  57. #include <limits.h>
  58. #include <openssl/ec.h>
  59. #include <openssl/ec_key.h>
  60. #include <openssl/err.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/type_check.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/x509v3.h>
  66. #include "internal.h"
  67. static int ssl_set_cert(CERT *c, X509 *x509);
  68. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  69. static int is_key_type_supported(int key_type) {
  70. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC;
  71. }
  72. int SSL_use_certificate(SSL *ssl, X509 *x) {
  73. if (x == NULL) {
  74. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  75. return 0;
  76. }
  77. return ssl_set_cert(ssl->cert, x);
  78. }
  79. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  80. if (der_len > LONG_MAX) {
  81. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  82. return 0;
  83. }
  84. const uint8_t *p = der;
  85. X509 *x509 = d2i_X509(NULL, &p, (long)der_len);
  86. if (x509 == NULL || p != der + der_len) {
  87. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  88. X509_free(x509);
  89. return 0;
  90. }
  91. int ret = SSL_use_certificate(ssl, x509);
  92. X509_free(x509);
  93. return ret;
  94. }
  95. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  96. EVP_PKEY *pkey;
  97. int ret;
  98. if (rsa == NULL) {
  99. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  100. return 0;
  101. }
  102. pkey = EVP_PKEY_new();
  103. if (pkey == NULL) {
  104. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  105. return 0;
  106. }
  107. RSA_up_ref(rsa);
  108. EVP_PKEY_assign_RSA(pkey, rsa);
  109. ret = ssl_set_pkey(ssl->cert, pkey);
  110. EVP_PKEY_free(pkey);
  111. return ret;
  112. }
  113. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) {
  114. if (!is_key_type_supported(pkey->type)) {
  115. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  116. return 0;
  117. }
  118. X509 *x509_leaf = c->x509_leaf;
  119. if (x509_leaf != NULL) {
  120. /* Sanity-check that the private key and the certificate match, unless the
  121. * key is opaque (in case of, say, a smartcard). */
  122. if (!EVP_PKEY_is_opaque(pkey) &&
  123. !X509_check_private_key(x509_leaf, pkey)) {
  124. X509_free(c->x509_leaf);
  125. c->x509_leaf = NULL;
  126. return 0;
  127. }
  128. }
  129. EVP_PKEY_free(c->privatekey);
  130. EVP_PKEY_up_ref(pkey);
  131. c->privatekey = pkey;
  132. return 1;
  133. }
  134. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  135. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  136. if (rsa == NULL) {
  137. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  138. return 0;
  139. }
  140. int ret = SSL_use_RSAPrivateKey(ssl, rsa);
  141. RSA_free(rsa);
  142. return ret;
  143. }
  144. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  145. int ret;
  146. if (pkey == NULL) {
  147. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  148. return 0;
  149. }
  150. ret = ssl_set_pkey(ssl->cert, pkey);
  151. return ret;
  152. }
  153. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
  154. size_t der_len) {
  155. if (der_len > LONG_MAX) {
  156. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  157. return 0;
  158. }
  159. const uint8_t *p = der;
  160. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  161. if (pkey == NULL || p != der + der_len) {
  162. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  163. EVP_PKEY_free(pkey);
  164. return 0;
  165. }
  166. int ret = SSL_use_PrivateKey(ssl, pkey);
  167. EVP_PKEY_free(pkey);
  168. return ret;
  169. }
  170. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  171. if (x == NULL) {
  172. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  173. return 0;
  174. }
  175. return ssl_set_cert(ctx->cert, x);
  176. }
  177. static int ssl_set_cert(CERT *c, X509 *x) {
  178. EVP_PKEY *pkey = X509_get_pubkey(x);
  179. if (pkey == NULL) {
  180. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_LIB);
  181. return 0;
  182. }
  183. if (!is_key_type_supported(pkey->type)) {
  184. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  185. EVP_PKEY_free(pkey);
  186. return 0;
  187. }
  188. /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
  189. * certificates, so sanity-check the key usage extension. */
  190. if (pkey->type == EVP_PKEY_EC) {
  191. /* This call populates extension flags (ex_flags). */
  192. X509_check_purpose(x, -1, 0);
  193. if ((x->ex_flags & EXFLAG_KUSAGE) &&
  194. !(x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
  195. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  196. EVP_PKEY_free(pkey);
  197. return 0;
  198. }
  199. }
  200. if (c->privatekey != NULL) {
  201. /* Sanity-check that the private key and the certificate match, unless the
  202. * key is opaque (in case of, say, a smartcard). */
  203. if (!EVP_PKEY_is_opaque(c->privatekey) &&
  204. !X509_check_private_key(x, c->privatekey)) {
  205. /* don't fail for a cert/key mismatch, just free current private key
  206. * (when switching to a different cert & key, first this function should
  207. * be used, then ssl_set_pkey */
  208. EVP_PKEY_free(c->privatekey);
  209. c->privatekey = NULL;
  210. /* clear error queue */
  211. ERR_clear_error();
  212. }
  213. }
  214. EVP_PKEY_free(pkey);
  215. X509_free(c->x509_leaf);
  216. X509_up_ref(x);
  217. c->x509_leaf = x;
  218. return 1;
  219. }
  220. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
  221. const uint8_t *der) {
  222. if (der_len > LONG_MAX) {
  223. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  224. return 0;
  225. }
  226. const uint8_t *p = der;
  227. X509 *x509 = d2i_X509(NULL, &p, (long)der_len);
  228. if (x509 == NULL || p != der + der_len) {
  229. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  230. X509_free(x509);
  231. return 0;
  232. }
  233. int ret = SSL_CTX_use_certificate(ctx, x509);
  234. X509_free(x509);
  235. return ret;
  236. }
  237. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  238. int ret;
  239. EVP_PKEY *pkey;
  240. if (rsa == NULL) {
  241. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  242. return 0;
  243. }
  244. pkey = EVP_PKEY_new();
  245. if (pkey == NULL) {
  246. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  247. return 0;
  248. }
  249. RSA_up_ref(rsa);
  250. EVP_PKEY_assign_RSA(pkey, rsa);
  251. ret = ssl_set_pkey(ctx->cert, pkey);
  252. EVP_PKEY_free(pkey);
  253. return ret;
  254. }
  255. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  256. size_t der_len) {
  257. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  258. if (rsa == NULL) {
  259. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  260. return 0;
  261. }
  262. int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  263. RSA_free(rsa);
  264. return ret;
  265. }
  266. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  267. if (pkey == NULL) {
  268. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  269. return 0;
  270. }
  271. return ssl_set_pkey(ctx->cert, pkey);
  272. }
  273. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
  274. size_t der_len) {
  275. if (der_len > LONG_MAX) {
  276. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  277. return 0;
  278. }
  279. const uint8_t *p = der;
  280. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  281. if (pkey == NULL || p != der + der_len) {
  282. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  283. EVP_PKEY_free(pkey);
  284. return 0;
  285. }
  286. int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  287. EVP_PKEY_free(pkey);
  288. return ret;
  289. }
  290. void SSL_set_private_key_method(SSL *ssl,
  291. const SSL_PRIVATE_KEY_METHOD *key_method) {
  292. ssl->cert->key_method = key_method;
  293. }
  294. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  295. const SSL_PRIVATE_KEY_METHOD *key_method) {
  296. ctx->cert->key_method = key_method;
  297. }
  298. static int set_signing_algorithm_prefs(CERT *cert, const uint16_t *prefs,
  299. size_t num_prefs) {
  300. OPENSSL_free(cert->sigalgs);
  301. cert->num_sigalgs = 0;
  302. cert->sigalgs = BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
  303. if (cert->sigalgs == NULL) {
  304. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  305. return 0;
  306. }
  307. cert->num_sigalgs = num_prefs;
  308. return 1;
  309. }
  310. int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  311. size_t num_prefs) {
  312. return set_signing_algorithm_prefs(ctx->cert, prefs, num_prefs);
  313. }
  314. int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
  315. size_t num_prefs) {
  316. return set_signing_algorithm_prefs(ssl->cert, prefs, num_prefs);
  317. }
  318. OPENSSL_COMPILE_ASSERT(sizeof(int) >= 2 * sizeof(uint16_t),
  319. digest_list_conversion_cannot_overflow);
  320. int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
  321. size_t num_digests) {
  322. OPENSSL_free(ssl->cert->sigalgs);
  323. ssl->cert->num_sigalgs = 0;
  324. ssl->cert->sigalgs = OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests);
  325. if (ssl->cert->sigalgs == NULL) {
  326. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  327. return 0;
  328. }
  329. /* Convert the digest list to a signature algorithms list.
  330. *
  331. * TODO(davidben): Replace this API with one that can express RSA-PSS, etc. */
  332. for (size_t i = 0; i < num_digests; i++) {
  333. switch (digest_nids[i]) {
  334. case NID_sha1:
  335. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1;
  336. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1;
  337. ssl->cert->num_sigalgs += 2;
  338. break;
  339. case NID_sha256:
  340. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256;
  341. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  342. SSL_SIGN_ECDSA_SECP256R1_SHA256;
  343. ssl->cert->num_sigalgs += 2;
  344. break;
  345. case NID_sha384:
  346. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384;
  347. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  348. SSL_SIGN_ECDSA_SECP384R1_SHA384;
  349. ssl->cert->num_sigalgs += 2;
  350. break;
  351. case NID_sha512:
  352. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512;
  353. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  354. SSL_SIGN_ECDSA_SECP521R1_SHA512;
  355. ssl->cert->num_sigalgs += 2;
  356. break;
  357. }
  358. }
  359. return 1;
  360. }
  361. int ssl_has_private_key(const SSL *ssl) {
  362. return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
  363. }
  364. int ssl_is_ecdsa_key_type(int type) {
  365. switch (type) {
  366. /* TODO(davidben): Remove support for |EVP_PKEY_EC| key types. */
  367. case EVP_PKEY_EC:
  368. case NID_X9_62_prime256v1:
  369. case NID_secp384r1:
  370. case NID_secp521r1:
  371. return 1;
  372. default:
  373. return 0;
  374. }
  375. }
  376. int ssl_private_key_type(SSL *ssl) {
  377. if (ssl->cert->key_method != NULL) {
  378. return ssl->cert->key_method->type(ssl);
  379. }
  380. switch (EVP_PKEY_id(ssl->cert->privatekey)) {
  381. case EVP_PKEY_RSA:
  382. return NID_rsaEncryption;
  383. case EVP_PKEY_EC:
  384. return EC_GROUP_get_curve_name(
  385. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(ssl->cert->privatekey)));
  386. default:
  387. return NID_undef;
  388. }
  389. }
  390. size_t ssl_private_key_max_signature_len(SSL *ssl) {
  391. if (ssl->cert->key_method != NULL) {
  392. return ssl->cert->key_method->max_signature_len(ssl);
  393. }
  394. return EVP_PKEY_size(ssl->cert->privatekey);
  395. }
  396. /* TODO(davidben): Forbid RSA-PKCS1 in TLS 1.3. For now we allow it because NSS
  397. * has yet to start doing RSA-PSS, so enforcing it would complicate interop
  398. * testing. */
  399. static int is_rsa_pkcs1(const EVP_MD **out_md, uint16_t sigalg) {
  400. switch (sigalg) {
  401. case SSL_SIGN_RSA_PKCS1_MD5_SHA1:
  402. *out_md = EVP_md5_sha1();
  403. return 1;
  404. case SSL_SIGN_RSA_PKCS1_SHA1:
  405. *out_md = EVP_sha1();
  406. return 1;
  407. case SSL_SIGN_RSA_PKCS1_SHA256:
  408. *out_md = EVP_sha256();
  409. return 1;
  410. case SSL_SIGN_RSA_PKCS1_SHA384:
  411. *out_md = EVP_sha384();
  412. return 1;
  413. case SSL_SIGN_RSA_PKCS1_SHA512:
  414. *out_md = EVP_sha512();
  415. return 1;
  416. default:
  417. return 0;
  418. }
  419. }
  420. static int ssl_sign_rsa_pkcs1(SSL *ssl, uint8_t *out, size_t *out_len,
  421. size_t max_out, const EVP_MD *md,
  422. const uint8_t *in, size_t in_len) {
  423. EVP_MD_CTX ctx;
  424. EVP_MD_CTX_init(&ctx);
  425. *out_len = max_out;
  426. int ret = EVP_DigestSignInit(&ctx, NULL, md, NULL, ssl->cert->privatekey) &&
  427. EVP_DigestSignUpdate(&ctx, in, in_len) &&
  428. EVP_DigestSignFinal(&ctx, out, out_len);
  429. EVP_MD_CTX_cleanup(&ctx);
  430. return ret;
  431. }
  432. static int ssl_verify_rsa_pkcs1(SSL *ssl, const uint8_t *signature,
  433. size_t signature_len, const EVP_MD *md,
  434. EVP_PKEY *pkey, const uint8_t *in,
  435. size_t in_len) {
  436. if (pkey->type != EVP_PKEY_RSA) {
  437. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  438. return 0;
  439. }
  440. EVP_MD_CTX md_ctx;
  441. EVP_MD_CTX_init(&md_ctx);
  442. int ret = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) &&
  443. EVP_DigestVerifyUpdate(&md_ctx, in, in_len) &&
  444. EVP_DigestVerifyFinal(&md_ctx, signature, signature_len);
  445. EVP_MD_CTX_cleanup(&md_ctx);
  446. return ret;
  447. }
  448. static int is_ecdsa(int *out_curve, const EVP_MD **out_md, uint16_t sigalg) {
  449. switch (sigalg) {
  450. case SSL_SIGN_ECDSA_SHA1:
  451. *out_curve = NID_undef;
  452. *out_md = EVP_sha1();
  453. return 1;
  454. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  455. *out_curve = NID_X9_62_prime256v1;
  456. *out_md = EVP_sha256();
  457. return 1;
  458. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  459. *out_curve = NID_secp384r1;
  460. *out_md = EVP_sha384();
  461. return 1;
  462. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  463. *out_curve = NID_secp521r1;
  464. *out_md = EVP_sha512();
  465. return 1;
  466. default:
  467. return 0;
  468. }
  469. }
  470. static int ssl_sign_ecdsa(SSL *ssl, uint8_t *out, size_t *out_len,
  471. size_t max_out, int curve, const EVP_MD *md,
  472. const uint8_t *in, size_t in_len) {
  473. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->cert->privatekey);
  474. if (ec_key == NULL) {
  475. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  476. return 0;
  477. }
  478. /* In TLS 1.3, the curve is also specified by the signature algorithm. */
  479. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  480. (curve == NID_undef ||
  481. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) != curve)) {
  482. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  483. return 0;
  484. }
  485. EVP_MD_CTX ctx;
  486. EVP_MD_CTX_init(&ctx);
  487. *out_len = max_out;
  488. int ret = EVP_DigestSignInit(&ctx, NULL, md, NULL, ssl->cert->privatekey) &&
  489. EVP_DigestSignUpdate(&ctx, in, in_len) &&
  490. EVP_DigestSignFinal(&ctx, out, out_len);
  491. EVP_MD_CTX_cleanup(&ctx);
  492. return ret;
  493. }
  494. static int ssl_verify_ecdsa(SSL *ssl, const uint8_t *signature,
  495. size_t signature_len, int curve, const EVP_MD *md,
  496. EVP_PKEY *pkey, const uint8_t *in, size_t in_len) {
  497. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  498. if (ec_key == NULL) {
  499. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  500. return 0;
  501. }
  502. /* In TLS 1.3, the curve is also specified by the signature algorithm. */
  503. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  504. (curve == NID_undef ||
  505. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) != curve)) {
  506. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  507. return 0;
  508. }
  509. EVP_MD_CTX md_ctx;
  510. EVP_MD_CTX_init(&md_ctx);
  511. int ret = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) &&
  512. EVP_DigestVerifyUpdate(&md_ctx, in, in_len) &&
  513. EVP_DigestVerifyFinal(&md_ctx, signature, signature_len);
  514. EVP_MD_CTX_cleanup(&md_ctx);
  515. return ret;
  516. }
  517. static int is_rsa_pss(const EVP_MD **out_md, uint16_t sigalg) {
  518. switch (sigalg) {
  519. case SSL_SIGN_RSA_PSS_SHA256:
  520. *out_md = EVP_sha256();
  521. return 1;
  522. case SSL_SIGN_RSA_PSS_SHA384:
  523. *out_md = EVP_sha384();
  524. return 1;
  525. case SSL_SIGN_RSA_PSS_SHA512:
  526. *out_md = EVP_sha512();
  527. return 1;
  528. default:
  529. return 0;
  530. }
  531. }
  532. static int ssl_sign_rsa_pss(SSL *ssl, uint8_t *out, size_t *out_len,
  533. size_t max_out, const EVP_MD *md,
  534. const uint8_t *in, size_t in_len) {
  535. EVP_MD_CTX ctx;
  536. EVP_MD_CTX_init(&ctx);
  537. *out_len = max_out;
  538. EVP_PKEY_CTX *pctx;
  539. int ret =
  540. EVP_DigestSignInit(&ctx, &pctx, md, NULL, ssl->cert->privatekey) &&
  541. EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) &&
  542. EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */) &&
  543. EVP_DigestSignUpdate(&ctx, in, in_len) &&
  544. EVP_DigestSignFinal(&ctx, out, out_len);
  545. EVP_MD_CTX_cleanup(&ctx);
  546. return ret;
  547. }
  548. static int ssl_verify_rsa_pss(SSL *ssl, const uint8_t *signature,
  549. size_t signature_len, const EVP_MD *md,
  550. EVP_PKEY *pkey, const uint8_t *in,
  551. size_t in_len) {
  552. if (pkey->type != EVP_PKEY_RSA) {
  553. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  554. return 0;
  555. }
  556. EVP_MD_CTX md_ctx;
  557. EVP_MD_CTX_init(&md_ctx);
  558. EVP_PKEY_CTX *pctx;
  559. int ret =
  560. EVP_DigestVerifyInit(&md_ctx, &pctx, md, NULL, pkey) &&
  561. EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) &&
  562. EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */) &&
  563. EVP_DigestVerifyUpdate(&md_ctx, in, in_len) &&
  564. EVP_DigestVerifyFinal(&md_ctx, signature, signature_len);
  565. EVP_MD_CTX_cleanup(&md_ctx);
  566. return ret;
  567. }
  568. enum ssl_private_key_result_t ssl_private_key_sign(
  569. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  570. uint16_t signature_algorithm, const uint8_t *in, size_t in_len) {
  571. if (ssl->cert->key_method != NULL) {
  572. if (ssl->cert->key_method->sign != NULL) {
  573. return ssl->cert->key_method->sign(ssl, out, out_len, max_out,
  574. signature_algorithm, in, in_len);
  575. }
  576. /* TODO(davidben): Remove support for |sign_digest|-only
  577. * |SSL_PRIVATE_KEY_METHOD|s. */
  578. const EVP_MD *md;
  579. int curve;
  580. if (!is_rsa_pkcs1(&md, signature_algorithm) &&
  581. !is_ecdsa(&curve, &md, signature_algorithm)) {
  582. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  583. return ssl_private_key_failure;
  584. }
  585. uint8_t hash[EVP_MAX_MD_SIZE];
  586. unsigned hash_len;
  587. if (!EVP_Digest(in, in_len, hash, &hash_len, md, NULL)) {
  588. return ssl_private_key_failure;
  589. }
  590. return ssl->cert->key_method->sign_digest(ssl, out, out_len, max_out, md,
  591. hash, hash_len);
  592. }
  593. const EVP_MD *md;
  594. if (is_rsa_pkcs1(&md, signature_algorithm) &&
  595. ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  596. return ssl_sign_rsa_pkcs1(ssl, out, out_len, max_out, md, in, in_len)
  597. ? ssl_private_key_success
  598. : ssl_private_key_failure;
  599. }
  600. int curve;
  601. if (is_ecdsa(&curve, &md, signature_algorithm)) {
  602. return ssl_sign_ecdsa(ssl, out, out_len, max_out, curve, md, in, in_len)
  603. ? ssl_private_key_success
  604. : ssl_private_key_failure;
  605. }
  606. if (is_rsa_pss(&md, signature_algorithm)) {
  607. return ssl_sign_rsa_pss(ssl, out, out_len, max_out, md, in, in_len)
  608. ? ssl_private_key_success
  609. : ssl_private_key_failure;
  610. }
  611. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  612. return ssl_private_key_failure;
  613. }
  614. int ssl_public_key_verify(SSL *ssl, const uint8_t *signature,
  615. size_t signature_len, uint16_t signature_algorithm,
  616. EVP_PKEY *pkey, const uint8_t *in, size_t in_len) {
  617. const EVP_MD *md;
  618. if (is_rsa_pkcs1(&md, signature_algorithm) &&
  619. ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  620. return ssl_verify_rsa_pkcs1(ssl, signature, signature_len, md, pkey, in,
  621. in_len);
  622. }
  623. int curve;
  624. if (is_ecdsa(&curve, &md, signature_algorithm)) {
  625. return ssl_verify_ecdsa(ssl, signature, signature_len, curve, md, pkey, in,
  626. in_len);
  627. }
  628. if (is_rsa_pss(&md, signature_algorithm)) {
  629. return ssl_verify_rsa_pss(ssl, signature, signature_len, md, pkey, in,
  630. in_len);
  631. }
  632. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  633. return 0;
  634. }
  635. enum ssl_private_key_result_t ssl_private_key_decrypt(
  636. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  637. const uint8_t *in, size_t in_len) {
  638. if (ssl->cert->key_method != NULL) {
  639. return ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
  640. in_len);
  641. }
  642. RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey);
  643. if (rsa == NULL) {
  644. /* Decrypt operations are only supported for RSA keys. */
  645. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  646. return ssl_private_key_failure;
  647. }
  648. /* Decrypt with no padding. PKCS#1 padding will be removed as part
  649. * of the timing-sensitive code by the caller. */
  650. if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
  651. return ssl_private_key_failure;
  652. }
  653. return ssl_private_key_success;
  654. }
  655. enum ssl_private_key_result_t ssl_private_key_complete(SSL *ssl, uint8_t *out,
  656. size_t *out_len,
  657. size_t max_out) {
  658. /* Only custom keys may be asynchronous. */
  659. return ssl->cert->key_method->complete(ssl, out, out_len, max_out);
  660. }
  661. int ssl_private_key_supports_signature_algorithm(SSL *ssl,
  662. uint16_t signature_algorithm) {
  663. const EVP_MD *md;
  664. if (is_rsa_pkcs1(&md, signature_algorithm) &&
  665. ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  666. return ssl_private_key_type(ssl) == NID_rsaEncryption;
  667. }
  668. int curve;
  669. if (is_ecdsa(&curve, &md, signature_algorithm)) {
  670. int type = ssl_private_key_type(ssl);
  671. if (!ssl_is_ecdsa_key_type(type)) {
  672. return 0;
  673. }
  674. /* Prior to TLS 1.3, ECDSA curves did not match the signature algorithm. */
  675. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  676. return 1;
  677. }
  678. return curve != NID_undef && type == curve;
  679. }
  680. if (is_rsa_pss(&md, signature_algorithm)) {
  681. if (ssl_private_key_type(ssl) != NID_rsaEncryption) {
  682. return 0;
  683. }
  684. /* Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
  685. * emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
  686. * hash in TLS. Reasonable RSA key sizes are large enough for the largest
  687. * defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too large for
  688. * SHA-512. 1024-bit RSA is sometimes used for test credentials, so check
  689. * the size to fall back to another algorithm. */
  690. if (ssl_private_key_max_signature_len(ssl) < 2 * EVP_MD_size(md) + 2) {
  691. return 0;
  692. }
  693. /* RSA-PSS is only supported by message-based private keys. */
  694. if (ssl->cert->key_method != NULL && ssl->cert->key_method->sign == NULL) {
  695. return 0;
  696. }
  697. return 1;
  698. }
  699. return 0;
  700. }