Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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