ssl_privkey.c 22 KB

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