Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

802 lignes
26 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. BSSL_NAMESPACE_BEGIN
  67. bool 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 bool 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 false;
  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 false;
  81. }
  82. cert->privatekey = UpRef(pkey);
  83. return true;
  84. }
  85. typedef struct {
  86. uint16_t sigalg;
  87. int pkey_type;
  88. int curve;
  89. const EVP_MD *(*digest_func)(void);
  90. bool is_rsa_pss;
  91. } SSL_SIGNATURE_ALGORITHM;
  92. static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
  93. {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1,
  94. false},
  95. {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, false},
  96. {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, false},
  97. {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, false},
  98. {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, false},
  99. {SSL_SIGN_RSA_PSS_RSAE_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, true},
  100. {SSL_SIGN_RSA_PSS_RSAE_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, true},
  101. {SSL_SIGN_RSA_PSS_RSAE_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, true},
  102. {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, false},
  103. {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
  104. &EVP_sha256, false},
  105. {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
  106. false},
  107. {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
  108. false},
  109. {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, false},
  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. bool ssl_has_private_key(const SSL_CONFIG *cfg) {
  120. return cfg->cert->privatekey != nullptr || cfg->cert->key_method != nullptr;
  121. }
  122. static bool 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 false;
  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 false;
  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 false;
  140. }
  141. }
  142. return true;
  143. }
  144. static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
  145. uint16_t sigalg, bool is_verify) {
  146. if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
  147. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  148. return false;
  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 false;
  156. }
  157. } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
  158. return false;
  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 false;
  164. }
  165. }
  166. return true;
  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 (hs->config->cert->key_method != NULL) {
  173. enum ssl_private_key_result_t ret;
  174. if (hs->pending_private_key_op) {
  175. ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out);
  176. } else {
  177. ret = hs->config->cert->key_method->sign(ssl, out, out_len, max_out,
  178. sigalg, 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(), hs->config->cert->privatekey.get(), sigalg,
  189. false /* 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, true /* 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 (hs->config->cert->key_method != NULL) {
  210. enum ssl_private_key_result_t ret;
  211. if (hs->pending_private_key_op) {
  212. ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out);
  213. } else {
  214. ret = hs->config->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(hs->config->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. BSSL_NAMESPACE_END
  257. using namespace bssl;
  258. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  259. if (rsa == NULL || ssl->config == 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->config->cert.get(), 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 || ssl->config == NULL) {
  281. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  282. return 0;
  283. }
  284. return ssl_set_pkey(ssl->config->cert.get(), 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.get(), 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.get(), 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. if (!ssl->config) {
  346. return;
  347. }
  348. ssl->config->cert->key_method = key_method;
  349. }
  350. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  351. const SSL_PRIVATE_KEY_METHOD *key_method) {
  352. ctx->cert->key_method = key_method;
  353. }
  354. static constexpr size_t kMaxSignatureAlgorithmNameLen = 23;
  355. // This was "constexpr" rather than "const", but that triggered a bug in MSVC
  356. // where it didn't pad the strings to the correct length.
  357. static const struct {
  358. uint16_t signature_algorithm;
  359. const char name[kMaxSignatureAlgorithmNameLen];
  360. } kSignatureAlgorithmNames[] = {
  361. {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"},
  362. {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"},
  363. {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"},
  364. {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384"},
  365. {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512"},
  366. {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1"},
  367. {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256"},
  368. {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384"},
  369. {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512"},
  370. {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256"},
  371. {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384"},
  372. {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512"},
  373. {SSL_SIGN_ED25519, "ed25519"},
  374. };
  375. const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
  376. int include_curve) {
  377. if (!include_curve) {
  378. switch (sigalg) {
  379. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  380. return "ecdsa_sha256";
  381. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  382. return "ecdsa_sha384";
  383. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  384. return "ecdsa_sha512";
  385. }
  386. }
  387. for (const auto &candidate : kSignatureAlgorithmNames) {
  388. if (candidate.signature_algorithm == sigalg) {
  389. return candidate.name;
  390. }
  391. }
  392. return NULL;
  393. }
  394. int SSL_get_signature_algorithm_key_type(uint16_t sigalg) {
  395. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  396. return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE;
  397. }
  398. const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) {
  399. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  400. if (alg == nullptr || alg->digest_func == nullptr) {
  401. return nullptr;
  402. }
  403. return alg->digest_func();
  404. }
  405. int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) {
  406. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  407. return alg != nullptr && alg->is_rsa_pss;
  408. }
  409. int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  410. size_t num_prefs) {
  411. return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  412. }
  413. int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
  414. size_t num_prefs) {
  415. if (!ssl->config) {
  416. return 0;
  417. }
  418. return ssl->config->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  419. }
  420. static constexpr struct {
  421. int pkey_type;
  422. int hash_nid;
  423. uint16_t signature_algorithm;
  424. } kSignatureAlgorithmsMapping[] = {
  425. {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1},
  426. {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256},
  427. {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384},
  428. {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512},
  429. {EVP_PKEY_RSA_PSS, NID_sha256, SSL_SIGN_RSA_PSS_RSAE_SHA256},
  430. {EVP_PKEY_RSA_PSS, NID_sha384, SSL_SIGN_RSA_PSS_RSAE_SHA384},
  431. {EVP_PKEY_RSA_PSS, NID_sha512, SSL_SIGN_RSA_PSS_RSAE_SHA512},
  432. {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1},
  433. {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256},
  434. {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384},
  435. {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512},
  436. {EVP_PKEY_ED25519, NID_undef, SSL_SIGN_ED25519},
  437. };
  438. static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values,
  439. size_t num_values) {
  440. if ((num_values & 1) == 1) {
  441. return false;
  442. }
  443. const size_t num_pairs = num_values / 2;
  444. if (!out->Init(num_pairs)) {
  445. return false;
  446. }
  447. for (size_t i = 0; i < num_values; i += 2) {
  448. const int hash_nid = values[i];
  449. const int pkey_type = values[i+1];
  450. bool found = false;
  451. for (const auto &candidate : kSignatureAlgorithmsMapping) {
  452. if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) {
  453. (*out)[i / 2] = candidate.signature_algorithm;
  454. found = true;
  455. break;
  456. }
  457. }
  458. if (!found) {
  459. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  460. ERR_add_error_dataf("unknown hash:%d pkey:%d", hash_nid, pkey_type);
  461. return false;
  462. }
  463. }
  464. return true;
  465. }
  466. static int compare_uint16_t(const void *p1, const void *p2) {
  467. uint16_t u1 = *((const uint16_t *)p1);
  468. uint16_t u2 = *((const uint16_t *)p2);
  469. if (u1 < u2) {
  470. return -1;
  471. } else if (u1 > u2) {
  472. return 1;
  473. } else {
  474. return 0;
  475. }
  476. }
  477. static bool sigalgs_unique(Span<const uint16_t> in_sigalgs) {
  478. Array<uint16_t> sigalgs;
  479. if (!sigalgs.CopyFrom(in_sigalgs)) {
  480. return false;
  481. }
  482. qsort(sigalgs.data(), sigalgs.size(), sizeof(uint16_t), compare_uint16_t);
  483. for (size_t i = 1; i < sigalgs.size(); i++) {
  484. if (sigalgs[i - 1] == sigalgs[i]) {
  485. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_SIGNATURE_ALGORITHM);
  486. return false;
  487. }
  488. }
  489. return true;
  490. }
  491. int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) {
  492. Array<uint16_t> sigalgs;
  493. if (!parse_sigalg_pairs(&sigalgs, values, num_values) ||
  494. !sigalgs_unique(sigalgs)) {
  495. return 0;
  496. }
  497. if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(),
  498. sigalgs.size()) ||
  499. !ctx->verify_sigalgs.CopyFrom(sigalgs)) {
  500. return 0;
  501. }
  502. return 1;
  503. }
  504. int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) {
  505. if (!ssl->config) {
  506. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  507. return 0;
  508. }
  509. Array<uint16_t> sigalgs;
  510. if (!parse_sigalg_pairs(&sigalgs, values, num_values) ||
  511. !sigalgs_unique(sigalgs)) {
  512. return 0;
  513. }
  514. if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
  515. !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) {
  516. return 0;
  517. }
  518. return 1;
  519. }
  520. static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) {
  521. // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256".
  522. // Count colons to give the number of output elements from any successful
  523. // parse.
  524. size_t num_elements = 1;
  525. size_t len = 0;
  526. for (const char *p = str; *p; p++) {
  527. len++;
  528. if (*p == ':') {
  529. num_elements++;
  530. }
  531. }
  532. if (!out->Init(num_elements)) {
  533. return false;
  534. }
  535. size_t out_i = 0;
  536. enum {
  537. pkey_or_name,
  538. hash_name,
  539. } state = pkey_or_name;
  540. char buf[kMaxSignatureAlgorithmNameLen];
  541. // buf_used is always < sizeof(buf). I.e. it's always safe to write
  542. // buf[buf_used] = 0.
  543. size_t buf_used = 0;
  544. int pkey_type = 0, hash_nid = 0;
  545. // Note that the loop runs to len+1, i.e. it'll process the terminating NUL.
  546. for (size_t offset = 0; offset < len+1; offset++) {
  547. const char c = str[offset];
  548. switch (c) {
  549. case '+':
  550. if (state == hash_name) {
  551. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  552. ERR_add_error_dataf("+ found in hash name at offset %zu", offset);
  553. return false;
  554. }
  555. if (buf_used == 0) {
  556. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  557. ERR_add_error_dataf("empty public key type at offset %zu", offset);
  558. return false;
  559. }
  560. buf[buf_used] = 0;
  561. if (strcmp(buf, "RSA") == 0) {
  562. pkey_type = EVP_PKEY_RSA;
  563. } else if (strcmp(buf, "RSA-PSS") == 0 ||
  564. strcmp(buf, "PSS") == 0) {
  565. pkey_type = EVP_PKEY_RSA_PSS;
  566. } else if (strcmp(buf, "ECDSA") == 0) {
  567. pkey_type = EVP_PKEY_EC;
  568. } else {
  569. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  570. ERR_add_error_dataf("unknown public key type '%s'", buf);
  571. return false;
  572. }
  573. state = hash_name;
  574. buf_used = 0;
  575. break;
  576. case ':':
  577. OPENSSL_FALLTHROUGH;
  578. case 0:
  579. if (buf_used == 0) {
  580. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  581. ERR_add_error_dataf("empty element at offset %zu", offset);
  582. return false;
  583. }
  584. buf[buf_used] = 0;
  585. if (state == pkey_or_name) {
  586. // No '+' was seen thus this is a TLS 1.3-style name.
  587. bool found = false;
  588. for (const auto &candidate : kSignatureAlgorithmNames) {
  589. if (strcmp(candidate.name, buf) == 0) {
  590. assert(out_i < num_elements);
  591. (*out)[out_i++] = candidate.signature_algorithm;
  592. found = true;
  593. break;
  594. }
  595. }
  596. if (!found) {
  597. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  598. ERR_add_error_dataf("unknown signature algorithm '%s'", buf);
  599. return false;
  600. }
  601. } else {
  602. if (strcmp(buf, "SHA1") == 0) {
  603. hash_nid = NID_sha1;
  604. } else if (strcmp(buf, "SHA256") == 0) {
  605. hash_nid = NID_sha256;
  606. } else if (strcmp(buf, "SHA384") == 0) {
  607. hash_nid = NID_sha384;
  608. } else if (strcmp(buf, "SHA512") == 0) {
  609. hash_nid = NID_sha512;
  610. } else {
  611. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  612. ERR_add_error_dataf("unknown hash function '%s'", buf);
  613. return false;
  614. }
  615. bool found = false;
  616. for (const auto &candidate : kSignatureAlgorithmsMapping) {
  617. if (candidate.pkey_type == pkey_type &&
  618. candidate.hash_nid == hash_nid) {
  619. assert(out_i < num_elements);
  620. (*out)[out_i++] = candidate.signature_algorithm;
  621. found = true;
  622. break;
  623. }
  624. }
  625. if (!found) {
  626. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  627. ERR_add_error_dataf("unknown pkey:%d hash:%s", pkey_type, buf);
  628. return false;
  629. }
  630. }
  631. state = pkey_or_name;
  632. buf_used = 0;
  633. break;
  634. default:
  635. if (buf_used == sizeof(buf) - 1) {
  636. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  637. ERR_add_error_dataf("substring too long at offset %zu", offset);
  638. return false;
  639. }
  640. if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
  641. (c >= 'A' && c <= 'Z') || c == '-' || c == '_') {
  642. buf[buf_used++] = c;
  643. } else {
  644. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  645. ERR_add_error_dataf("invalid character 0x%02x at offest %zu", c,
  646. offset);
  647. return false;
  648. }
  649. }
  650. }
  651. assert(out_i == out->size());
  652. return true;
  653. }
  654. int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str) {
  655. Array<uint16_t> sigalgs;
  656. if (!parse_sigalgs_list(&sigalgs, str) ||
  657. !sigalgs_unique(sigalgs)) {
  658. return 0;
  659. }
  660. if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(),
  661. sigalgs.size()) ||
  662. !ctx->verify_sigalgs.CopyFrom(sigalgs)) {
  663. return 0;
  664. }
  665. return 1;
  666. }
  667. int SSL_set1_sigalgs_list(SSL *ssl, const char *str) {
  668. if (!ssl->config) {
  669. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  670. return 0;
  671. }
  672. Array<uint16_t> sigalgs;
  673. if (!parse_sigalgs_list(&sigalgs, str) ||
  674. !sigalgs_unique(sigalgs)) {
  675. return 0;
  676. }
  677. if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
  678. !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) {
  679. return 0;
  680. }
  681. return 1;
  682. }
  683. int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  684. size_t num_prefs) {
  685. return ctx->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  686. }