You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

493 line
17 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 <assert.h>
  58. #include <limits.h>
  59. #include <openssl/ec.h>
  60. #include <openssl/ec_key.h>
  61. #include <openssl/err.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/mem.h>
  64. #include "internal.h"
  65. #include "../crypto/internal.h"
  66. namespace bssl {
  67. int ssl_is_key_type_supported(int key_type) {
  68. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
  69. key_type == EVP_PKEY_ED25519;
  70. }
  71. static int ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
  72. if (!ssl_is_key_type_supported(pkey->type)) {
  73. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  74. return 0;
  75. }
  76. if (cert->chain != nullptr &&
  77. sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr &&
  78. // Sanity-check that the private key and the certificate match.
  79. !ssl_cert_check_private_key(cert, pkey)) {
  80. return 0;
  81. }
  82. EVP_PKEY_up_ref(pkey);
  83. cert->privatekey.reset(pkey);
  84. return 1;
  85. }
  86. typedef struct {
  87. uint16_t sigalg;
  88. int pkey_type;
  89. int curve;
  90. const EVP_MD *(*digest_func)(void);
  91. char is_rsa_pss;
  92. } SSL_SIGNATURE_ALGORITHM;
  93. static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
  94. {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, 0},
  95. {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, 0},
  96. {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 0},
  97. {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 0},
  98. {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 0},
  99. {SSL_SIGN_RSA_PSS_RSAE_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 1},
  100. {SSL_SIGN_RSA_PSS_RSAE_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 1},
  101. {SSL_SIGN_RSA_PSS_RSAE_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 1},
  102. {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, 0},
  103. {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
  104. &EVP_sha256, 0},
  105. {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
  106. 0},
  107. {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
  108. 0},
  109. {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, NULL, 0},
  110. };
  111. static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) {
  112. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
  113. if (kSignatureAlgorithms[i].sigalg == sigalg) {
  114. return &kSignatureAlgorithms[i];
  115. }
  116. }
  117. return NULL;
  118. }
  119. int ssl_has_private_key(const SSL *ssl) {
  120. return ssl->cert->privatekey != nullptr || ssl->cert->key_method != nullptr;
  121. }
  122. static int pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
  123. uint16_t sigalg) {
  124. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  125. if (alg == NULL ||
  126. EVP_PKEY_id(pkey) != alg->pkey_type) {
  127. return 0;
  128. }
  129. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  130. // RSA keys may only be used with RSA-PSS.
  131. if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
  132. return 0;
  133. }
  134. // EC keys have a curve requirement.
  135. if (alg->pkey_type == EVP_PKEY_EC &&
  136. (alg->curve == NID_undef ||
  137. EC_GROUP_get_curve_name(
  138. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
  139. return 0;
  140. }
  141. }
  142. return 1;
  143. }
  144. static int setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, uint16_t sigalg,
  145. int is_verify) {
  146. if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
  147. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  148. return 0;
  149. }
  150. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  151. const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
  152. EVP_PKEY_CTX *pctx;
  153. if (is_verify) {
  154. if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
  155. return 0;
  156. }
  157. } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
  158. return 0;
  159. }
  160. if (alg->is_rsa_pss) {
  161. if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
  162. !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
  163. return 0;
  164. }
  165. }
  166. return 1;
  167. }
  168. enum ssl_private_key_result_t ssl_private_key_sign(
  169. SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
  170. uint16_t sigalg, Span<const uint8_t> in) {
  171. SSL *const ssl = hs->ssl;
  172. if (ssl->cert->key_method != NULL) {
  173. enum ssl_private_key_result_t ret;
  174. if (hs->pending_private_key_op) {
  175. ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
  176. } else {
  177. ret = ssl->cert->key_method->sign(ssl, out, out_len, max_out, sigalg,
  178. in.data(), in.size());
  179. }
  180. if (ret == ssl_private_key_failure) {
  181. OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
  182. }
  183. hs->pending_private_key_op = ret == ssl_private_key_retry;
  184. return ret;
  185. }
  186. *out_len = max_out;
  187. ScopedEVP_MD_CTX ctx;
  188. if (!setup_ctx(ssl, ctx.get(), ssl->cert->privatekey.get(), sigalg,
  189. 0 /* sign */) ||
  190. !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) {
  191. return ssl_private_key_failure;
  192. }
  193. return ssl_private_key_success;
  194. }
  195. bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
  196. uint16_t sigalg, EVP_PKEY *pkey,
  197. Span<const uint8_t> in) {
  198. ScopedEVP_MD_CTX ctx;
  199. return setup_ctx(ssl, ctx.get(), pkey, sigalg, 1 /* verify */) &&
  200. EVP_DigestVerify(ctx.get(), signature.data(), signature.size(),
  201. in.data(), in.size());
  202. }
  203. enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
  204. uint8_t *out,
  205. size_t *out_len,
  206. size_t max_out,
  207. Span<const uint8_t> in) {
  208. SSL *const ssl = hs->ssl;
  209. if (ssl->cert->key_method != NULL) {
  210. enum ssl_private_key_result_t ret;
  211. if (hs->pending_private_key_op) {
  212. ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
  213. } else {
  214. ret = ssl->cert->key_method->decrypt(ssl, out, out_len, max_out,
  215. in.data(), in.size());
  216. }
  217. if (ret == ssl_private_key_failure) {
  218. OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
  219. }
  220. hs->pending_private_key_op = ret == ssl_private_key_retry;
  221. return ret;
  222. }
  223. RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey.get());
  224. if (rsa == NULL) {
  225. // Decrypt operations are only supported for RSA keys.
  226. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  227. return ssl_private_key_failure;
  228. }
  229. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  230. // timing-sensitive code by the caller.
  231. if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(),
  232. RSA_NO_PADDING)) {
  233. return ssl_private_key_failure;
  234. }
  235. return ssl_private_key_success;
  236. }
  237. bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
  238. uint16_t sigalg) {
  239. SSL *const ssl = hs->ssl;
  240. if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) {
  241. return false;
  242. }
  243. // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
  244. // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
  245. // hash in TLS. Reasonable RSA key sizes are large enough for the largest
  246. // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
  247. // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
  248. // size so that we can fall back to another algorithm in that case.
  249. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  250. if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) <
  251. 2 * EVP_MD_size(alg->digest_func()) + 2) {
  252. return false;
  253. }
  254. return true;
  255. }
  256. } // namespace bssl
  257. using namespace bssl;
  258. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  259. if (rsa == NULL) {
  260. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  261. return 0;
  262. }
  263. UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
  264. if (!pkey ||
  265. !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
  266. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  267. return 0;
  268. }
  269. return ssl_set_pkey(ssl->cert, pkey.get());
  270. }
  271. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  272. UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
  273. if (!rsa) {
  274. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  275. return 0;
  276. }
  277. return SSL_use_RSAPrivateKey(ssl, rsa.get());
  278. }
  279. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  280. if (pkey == NULL) {
  281. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  282. return 0;
  283. }
  284. return ssl_set_pkey(ssl->cert, pkey);
  285. }
  286. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
  287. size_t der_len) {
  288. if (der_len > LONG_MAX) {
  289. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  290. return 0;
  291. }
  292. const uint8_t *p = der;
  293. UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
  294. if (!pkey || p != der + der_len) {
  295. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  296. return 0;
  297. }
  298. return SSL_use_PrivateKey(ssl, pkey.get());
  299. }
  300. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  301. if (rsa == NULL) {
  302. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  303. return 0;
  304. }
  305. UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
  306. if (!pkey ||
  307. !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
  308. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  309. return 0;
  310. }
  311. return ssl_set_pkey(ctx->cert, pkey.get());
  312. }
  313. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  314. size_t der_len) {
  315. UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
  316. if (!rsa) {
  317. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  318. return 0;
  319. }
  320. return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get());
  321. }
  322. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  323. if (pkey == NULL) {
  324. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  325. return 0;
  326. }
  327. return ssl_set_pkey(ctx->cert, pkey);
  328. }
  329. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
  330. size_t der_len) {
  331. if (der_len > LONG_MAX) {
  332. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  333. return 0;
  334. }
  335. const uint8_t *p = der;
  336. UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
  337. if (!pkey || p != der + der_len) {
  338. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  339. return 0;
  340. }
  341. return SSL_CTX_use_PrivateKey(ctx, pkey.get());
  342. }
  343. void SSL_set_private_key_method(SSL *ssl,
  344. const SSL_PRIVATE_KEY_METHOD *key_method) {
  345. ssl->cert->key_method = key_method;
  346. }
  347. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  348. const SSL_PRIVATE_KEY_METHOD *key_method) {
  349. ctx->cert->key_method = key_method;
  350. }
  351. const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
  352. int include_curve) {
  353. switch (sigalg) {
  354. case SSL_SIGN_RSA_PKCS1_MD5_SHA1:
  355. return "rsa_pkcs1_md5_sha1";
  356. case SSL_SIGN_RSA_PKCS1_SHA1:
  357. return "rsa_pkcs1_sha1";
  358. case SSL_SIGN_RSA_PKCS1_SHA256:
  359. return "rsa_pkcs1_sha256";
  360. case SSL_SIGN_RSA_PKCS1_SHA384:
  361. return "rsa_pkcs1_sha384";
  362. case SSL_SIGN_RSA_PKCS1_SHA512:
  363. return "rsa_pkcs1_sha512";
  364. case SSL_SIGN_ECDSA_SHA1:
  365. return "ecdsa_sha1";
  366. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  367. return include_curve ? "ecdsa_secp256r1_sha256" : "ecdsa_sha256";
  368. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  369. return include_curve ? "ecdsa_secp384r1_sha384" : "ecdsa_sha384";
  370. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  371. return include_curve ? "ecdsa_secp521r1_sha512" : "ecdsa_sha512";
  372. case SSL_SIGN_RSA_PSS_RSAE_SHA256:
  373. return "rsa_pss_rsae_sha256";
  374. case SSL_SIGN_RSA_PSS_RSAE_SHA384:
  375. return "rsa_pss_rsae_sha384";
  376. case SSL_SIGN_RSA_PSS_RSAE_SHA512:
  377. return "rsa_pss_rsae_sha512";
  378. case SSL_SIGN_ED25519:
  379. return "ed25519";
  380. default:
  381. return NULL;
  382. }
  383. }
  384. int SSL_get_signature_algorithm_key_type(uint16_t sigalg) {
  385. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  386. return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE;
  387. }
  388. const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) {
  389. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  390. if (alg == nullptr || alg->digest_func == nullptr) {
  391. return nullptr;
  392. }
  393. return alg->digest_func();
  394. }
  395. int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) {
  396. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  397. return alg != nullptr && alg->is_rsa_pss;
  398. }
  399. static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs,
  400. const uint16_t *prefs, size_t num_prefs) {
  401. OPENSSL_free(*out_prefs);
  402. *out_num_prefs = 0;
  403. *out_prefs = (uint16_t *)BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
  404. if (*out_prefs == NULL) {
  405. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  406. return 0;
  407. }
  408. *out_num_prefs = num_prefs;
  409. return 1;
  410. }
  411. int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  412. size_t num_prefs) {
  413. return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  414. }
  415. int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
  416. size_t num_prefs) {
  417. return ssl->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  418. }
  419. int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  420. size_t num_prefs) {
  421. return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs,
  422. prefs, num_prefs);
  423. }