ssl_privkey.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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_HANDSHAKE *hs) {
  120. if (hs->config->cert->privatekey != nullptr ||
  121. hs->config->cert->key_method != nullptr ||
  122. ssl_signing_with_dc(hs)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
  128. uint16_t sigalg) {
  129. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  130. if (alg == NULL ||
  131. EVP_PKEY_id(pkey) != alg->pkey_type) {
  132. return false;
  133. }
  134. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  135. // RSA keys may only be used with RSA-PSS.
  136. if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
  137. return false;
  138. }
  139. // EC keys have a curve requirement.
  140. if (alg->pkey_type == EVP_PKEY_EC &&
  141. (alg->curve == NID_undef ||
  142. EC_GROUP_get_curve_name(
  143. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
  150. uint16_t sigalg, bool is_verify) {
  151. if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
  152. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  153. return false;
  154. }
  155. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  156. const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
  157. EVP_PKEY_CTX *pctx;
  158. if (is_verify) {
  159. if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
  160. return false;
  161. }
  162. } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
  163. return false;
  164. }
  165. if (alg->is_rsa_pss) {
  166. if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
  167. !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. enum ssl_private_key_result_t ssl_private_key_sign(
  174. SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
  175. uint16_t sigalg, Span<const uint8_t> in) {
  176. SSL *const ssl = hs->ssl;
  177. const SSL_PRIVATE_KEY_METHOD *key_method = hs->config->cert->key_method;
  178. EVP_PKEY *privatekey = hs->config->cert->privatekey.get();
  179. if (ssl_signing_with_dc(hs)) {
  180. key_method = hs->config->cert->dc_key_method;
  181. privatekey = hs->config->cert->dc_privatekey.get();
  182. }
  183. if (key_method != NULL) {
  184. enum ssl_private_key_result_t ret;
  185. if (hs->pending_private_key_op) {
  186. ret = key_method->complete(ssl, out, out_len, max_out);
  187. } else {
  188. ret = key_method->sign(ssl, out, out_len, max_out,
  189. sigalg, in.data(), in.size());
  190. }
  191. if (ret == ssl_private_key_failure) {
  192. OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
  193. }
  194. hs->pending_private_key_op = ret == ssl_private_key_retry;
  195. return ret;
  196. }
  197. *out_len = max_out;
  198. ScopedEVP_MD_CTX ctx;
  199. if (!setup_ctx(ssl, ctx.get(), privatekey, sigalg, false /* sign */) ||
  200. !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) {
  201. return ssl_private_key_failure;
  202. }
  203. return ssl_private_key_success;
  204. }
  205. bool ssl_public_key_verify(SSL *ssl, Span<const uint8_t> signature,
  206. uint16_t sigalg, EVP_PKEY *pkey,
  207. Span<const uint8_t> in) {
  208. ScopedEVP_MD_CTX ctx;
  209. if (!setup_ctx(ssl, ctx.get(), pkey, sigalg, true /* verify */)) {
  210. return false;
  211. }
  212. bool ok = EVP_DigestVerify(ctx.get(), signature.data(), signature.size(),
  213. in.data(), in.size());
  214. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  215. ok = true;
  216. ERR_clear_error();
  217. #endif
  218. return ok;
  219. }
  220. enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
  221. uint8_t *out,
  222. size_t *out_len,
  223. size_t max_out,
  224. Span<const uint8_t> in) {
  225. SSL *const ssl = hs->ssl;
  226. if (hs->config->cert->key_method != NULL) {
  227. enum ssl_private_key_result_t ret;
  228. if (hs->pending_private_key_op) {
  229. ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out);
  230. } else {
  231. ret = hs->config->cert->key_method->decrypt(ssl, out, out_len, max_out,
  232. in.data(), in.size());
  233. }
  234. if (ret == ssl_private_key_failure) {
  235. OPENSSL_PUT_ERROR(SSL, SSL_R_PRIVATE_KEY_OPERATION_FAILED);
  236. }
  237. hs->pending_private_key_op = ret == ssl_private_key_retry;
  238. return ret;
  239. }
  240. RSA *rsa = EVP_PKEY_get0_RSA(hs->config->cert->privatekey.get());
  241. if (rsa == NULL) {
  242. // Decrypt operations are only supported for RSA keys.
  243. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  244. return ssl_private_key_failure;
  245. }
  246. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  247. // timing-sensitive code by the caller.
  248. if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(),
  249. RSA_NO_PADDING)) {
  250. return ssl_private_key_failure;
  251. }
  252. return ssl_private_key_success;
  253. }
  254. bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
  255. uint16_t sigalg) {
  256. SSL *const ssl = hs->ssl;
  257. if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) {
  258. return false;
  259. }
  260. // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
  261. // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
  262. // hash in TLS. Reasonable RSA key sizes are large enough for the largest
  263. // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
  264. // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
  265. // size so that we can fall back to another algorithm in that case.
  266. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  267. if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) <
  268. 2 * EVP_MD_size(alg->digest_func()) + 2) {
  269. return false;
  270. }
  271. return true;
  272. }
  273. BSSL_NAMESPACE_END
  274. using namespace bssl;
  275. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  276. if (rsa == NULL || ssl->config == NULL) {
  277. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  278. return 0;
  279. }
  280. UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
  281. if (!pkey ||
  282. !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
  283. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  284. return 0;
  285. }
  286. return ssl_set_pkey(ssl->config->cert.get(), pkey.get());
  287. }
  288. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  289. UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
  290. if (!rsa) {
  291. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  292. return 0;
  293. }
  294. return SSL_use_RSAPrivateKey(ssl, rsa.get());
  295. }
  296. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  297. if (pkey == NULL || ssl->config == NULL) {
  298. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  299. return 0;
  300. }
  301. return ssl_set_pkey(ssl->config->cert.get(), pkey);
  302. }
  303. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
  304. size_t der_len) {
  305. if (der_len > LONG_MAX) {
  306. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  307. return 0;
  308. }
  309. const uint8_t *p = der;
  310. UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
  311. if (!pkey || p != der + der_len) {
  312. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  313. return 0;
  314. }
  315. return SSL_use_PrivateKey(ssl, pkey.get());
  316. }
  317. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  318. if (rsa == NULL) {
  319. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  320. return 0;
  321. }
  322. UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
  323. if (!pkey ||
  324. !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
  325. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  326. return 0;
  327. }
  328. return ssl_set_pkey(ctx->cert.get(), pkey.get());
  329. }
  330. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  331. size_t der_len) {
  332. UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
  333. if (!rsa) {
  334. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  335. return 0;
  336. }
  337. return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get());
  338. }
  339. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  340. if (pkey == NULL) {
  341. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  342. return 0;
  343. }
  344. return ssl_set_pkey(ctx->cert.get(), pkey);
  345. }
  346. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
  347. size_t der_len) {
  348. if (der_len > LONG_MAX) {
  349. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  350. return 0;
  351. }
  352. const uint8_t *p = der;
  353. UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
  354. if (!pkey || p != der + der_len) {
  355. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  356. return 0;
  357. }
  358. return SSL_CTX_use_PrivateKey(ctx, pkey.get());
  359. }
  360. void SSL_set_private_key_method(SSL *ssl,
  361. const SSL_PRIVATE_KEY_METHOD *key_method) {
  362. if (!ssl->config) {
  363. return;
  364. }
  365. ssl->config->cert->key_method = key_method;
  366. }
  367. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  368. const SSL_PRIVATE_KEY_METHOD *key_method) {
  369. ctx->cert->key_method = key_method;
  370. }
  371. static constexpr size_t kMaxSignatureAlgorithmNameLen = 23;
  372. // This was "constexpr" rather than "const", but that triggered a bug in MSVC
  373. // where it didn't pad the strings to the correct length.
  374. static const struct {
  375. uint16_t signature_algorithm;
  376. const char name[kMaxSignatureAlgorithmNameLen];
  377. } kSignatureAlgorithmNames[] = {
  378. {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"},
  379. {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"},
  380. {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"},
  381. {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384"},
  382. {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512"},
  383. {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1"},
  384. {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256"},
  385. {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384"},
  386. {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512"},
  387. {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256"},
  388. {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384"},
  389. {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512"},
  390. {SSL_SIGN_ED25519, "ed25519"},
  391. };
  392. const char *SSL_get_signature_algorithm_name(uint16_t sigalg,
  393. int include_curve) {
  394. if (!include_curve) {
  395. switch (sigalg) {
  396. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  397. return "ecdsa_sha256";
  398. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  399. return "ecdsa_sha384";
  400. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  401. return "ecdsa_sha512";
  402. }
  403. }
  404. for (const auto &candidate : kSignatureAlgorithmNames) {
  405. if (candidate.signature_algorithm == sigalg) {
  406. return candidate.name;
  407. }
  408. }
  409. return NULL;
  410. }
  411. int SSL_get_signature_algorithm_key_type(uint16_t sigalg) {
  412. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  413. return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE;
  414. }
  415. const EVP_MD *SSL_get_signature_algorithm_digest(uint16_t sigalg) {
  416. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  417. if (alg == nullptr || alg->digest_func == nullptr) {
  418. return nullptr;
  419. }
  420. return alg->digest_func();
  421. }
  422. int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg) {
  423. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  424. return alg != nullptr && alg->is_rsa_pss;
  425. }
  426. int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  427. size_t num_prefs) {
  428. return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  429. }
  430. int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
  431. size_t num_prefs) {
  432. if (!ssl->config) {
  433. return 0;
  434. }
  435. return ssl->config->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  436. }
  437. static constexpr struct {
  438. int pkey_type;
  439. int hash_nid;
  440. uint16_t signature_algorithm;
  441. } kSignatureAlgorithmsMapping[] = {
  442. {EVP_PKEY_RSA, NID_sha1, SSL_SIGN_RSA_PKCS1_SHA1},
  443. {EVP_PKEY_RSA, NID_sha256, SSL_SIGN_RSA_PKCS1_SHA256},
  444. {EVP_PKEY_RSA, NID_sha384, SSL_SIGN_RSA_PKCS1_SHA384},
  445. {EVP_PKEY_RSA, NID_sha512, SSL_SIGN_RSA_PKCS1_SHA512},
  446. {EVP_PKEY_RSA_PSS, NID_sha256, SSL_SIGN_RSA_PSS_RSAE_SHA256},
  447. {EVP_PKEY_RSA_PSS, NID_sha384, SSL_SIGN_RSA_PSS_RSAE_SHA384},
  448. {EVP_PKEY_RSA_PSS, NID_sha512, SSL_SIGN_RSA_PSS_RSAE_SHA512},
  449. {EVP_PKEY_EC, NID_sha1, SSL_SIGN_ECDSA_SHA1},
  450. {EVP_PKEY_EC, NID_sha256, SSL_SIGN_ECDSA_SECP256R1_SHA256},
  451. {EVP_PKEY_EC, NID_sha384, SSL_SIGN_ECDSA_SECP384R1_SHA384},
  452. {EVP_PKEY_EC, NID_sha512, SSL_SIGN_ECDSA_SECP521R1_SHA512},
  453. {EVP_PKEY_ED25519, NID_undef, SSL_SIGN_ED25519},
  454. };
  455. static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values,
  456. size_t num_values) {
  457. if ((num_values & 1) == 1) {
  458. return false;
  459. }
  460. const size_t num_pairs = num_values / 2;
  461. if (!out->Init(num_pairs)) {
  462. return false;
  463. }
  464. for (size_t i = 0; i < num_values; i += 2) {
  465. const int hash_nid = values[i];
  466. const int pkey_type = values[i+1];
  467. bool found = false;
  468. for (const auto &candidate : kSignatureAlgorithmsMapping) {
  469. if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) {
  470. (*out)[i / 2] = candidate.signature_algorithm;
  471. found = true;
  472. break;
  473. }
  474. }
  475. if (!found) {
  476. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  477. ERR_add_error_dataf("unknown hash:%d pkey:%d", hash_nid, pkey_type);
  478. return false;
  479. }
  480. }
  481. return true;
  482. }
  483. static int compare_uint16_t(const void *p1, const void *p2) {
  484. uint16_t u1 = *((const uint16_t *)p1);
  485. uint16_t u2 = *((const uint16_t *)p2);
  486. if (u1 < u2) {
  487. return -1;
  488. } else if (u1 > u2) {
  489. return 1;
  490. } else {
  491. return 0;
  492. }
  493. }
  494. static bool sigalgs_unique(Span<const uint16_t> in_sigalgs) {
  495. if (in_sigalgs.size() < 2) {
  496. return true;
  497. }
  498. Array<uint16_t> sigalgs;
  499. if (!sigalgs.CopyFrom(in_sigalgs)) {
  500. return false;
  501. }
  502. qsort(sigalgs.data(), sigalgs.size(), sizeof(uint16_t), compare_uint16_t);
  503. for (size_t i = 1; i < sigalgs.size(); i++) {
  504. if (sigalgs[i - 1] == sigalgs[i]) {
  505. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_SIGNATURE_ALGORITHM);
  506. return false;
  507. }
  508. }
  509. return true;
  510. }
  511. int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) {
  512. Array<uint16_t> sigalgs;
  513. if (!parse_sigalg_pairs(&sigalgs, values, num_values) ||
  514. !sigalgs_unique(sigalgs)) {
  515. return 0;
  516. }
  517. if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(),
  518. sigalgs.size()) ||
  519. !ctx->verify_sigalgs.CopyFrom(sigalgs)) {
  520. return 0;
  521. }
  522. return 1;
  523. }
  524. int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) {
  525. if (!ssl->config) {
  526. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  527. return 0;
  528. }
  529. Array<uint16_t> sigalgs;
  530. if (!parse_sigalg_pairs(&sigalgs, values, num_values) ||
  531. !sigalgs_unique(sigalgs)) {
  532. return 0;
  533. }
  534. if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
  535. !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) {
  536. return 0;
  537. }
  538. return 1;
  539. }
  540. static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) {
  541. // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256".
  542. // Count colons to give the number of output elements from any successful
  543. // parse.
  544. size_t num_elements = 1;
  545. size_t len = 0;
  546. for (const char *p = str; *p; p++) {
  547. len++;
  548. if (*p == ':') {
  549. num_elements++;
  550. }
  551. }
  552. if (!out->Init(num_elements)) {
  553. return false;
  554. }
  555. size_t out_i = 0;
  556. enum {
  557. pkey_or_name,
  558. hash_name,
  559. } state = pkey_or_name;
  560. char buf[kMaxSignatureAlgorithmNameLen];
  561. // buf_used is always < sizeof(buf). I.e. it's always safe to write
  562. // buf[buf_used] = 0.
  563. size_t buf_used = 0;
  564. int pkey_type = 0, hash_nid = 0;
  565. // Note that the loop runs to len+1, i.e. it'll process the terminating NUL.
  566. for (size_t offset = 0; offset < len+1; offset++) {
  567. const char c = str[offset];
  568. switch (c) {
  569. case '+':
  570. if (state == hash_name) {
  571. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  572. ERR_add_error_dataf("+ found in hash name at offset %zu", offset);
  573. return false;
  574. }
  575. if (buf_used == 0) {
  576. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  577. ERR_add_error_dataf("empty public key type at offset %zu", offset);
  578. return false;
  579. }
  580. buf[buf_used] = 0;
  581. if (strcmp(buf, "RSA") == 0) {
  582. pkey_type = EVP_PKEY_RSA;
  583. } else if (strcmp(buf, "RSA-PSS") == 0 ||
  584. strcmp(buf, "PSS") == 0) {
  585. pkey_type = EVP_PKEY_RSA_PSS;
  586. } else if (strcmp(buf, "ECDSA") == 0) {
  587. pkey_type = EVP_PKEY_EC;
  588. } else {
  589. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  590. ERR_add_error_dataf("unknown public key type '%s'", buf);
  591. return false;
  592. }
  593. state = hash_name;
  594. buf_used = 0;
  595. break;
  596. case ':':
  597. OPENSSL_FALLTHROUGH;
  598. case 0:
  599. if (buf_used == 0) {
  600. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  601. ERR_add_error_dataf("empty element at offset %zu", offset);
  602. return false;
  603. }
  604. buf[buf_used] = 0;
  605. if (state == pkey_or_name) {
  606. // No '+' was seen thus this is a TLS 1.3-style name.
  607. bool found = false;
  608. for (const auto &candidate : kSignatureAlgorithmNames) {
  609. if (strcmp(candidate.name, buf) == 0) {
  610. assert(out_i < num_elements);
  611. (*out)[out_i++] = candidate.signature_algorithm;
  612. found = true;
  613. break;
  614. }
  615. }
  616. if (!found) {
  617. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  618. ERR_add_error_dataf("unknown signature algorithm '%s'", buf);
  619. return false;
  620. }
  621. } else {
  622. if (strcmp(buf, "SHA1") == 0) {
  623. hash_nid = NID_sha1;
  624. } else if (strcmp(buf, "SHA256") == 0) {
  625. hash_nid = NID_sha256;
  626. } else if (strcmp(buf, "SHA384") == 0) {
  627. hash_nid = NID_sha384;
  628. } else if (strcmp(buf, "SHA512") == 0) {
  629. hash_nid = NID_sha512;
  630. } else {
  631. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  632. ERR_add_error_dataf("unknown hash function '%s'", buf);
  633. return false;
  634. }
  635. bool found = false;
  636. for (const auto &candidate : kSignatureAlgorithmsMapping) {
  637. if (candidate.pkey_type == pkey_type &&
  638. candidate.hash_nid == hash_nid) {
  639. assert(out_i < num_elements);
  640. (*out)[out_i++] = candidate.signature_algorithm;
  641. found = true;
  642. break;
  643. }
  644. }
  645. if (!found) {
  646. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  647. ERR_add_error_dataf("unknown pkey:%d hash:%s", pkey_type, buf);
  648. return false;
  649. }
  650. }
  651. state = pkey_or_name;
  652. buf_used = 0;
  653. break;
  654. default:
  655. if (buf_used == sizeof(buf) - 1) {
  656. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  657. ERR_add_error_dataf("substring too long at offset %zu", offset);
  658. return false;
  659. }
  660. if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
  661. (c >= 'A' && c <= 'Z') || c == '-' || c == '_') {
  662. buf[buf_used++] = c;
  663. } else {
  664. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
  665. ERR_add_error_dataf("invalid character 0x%02x at offest %zu", c,
  666. offset);
  667. return false;
  668. }
  669. }
  670. }
  671. assert(out_i == out->size());
  672. return true;
  673. }
  674. int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str) {
  675. Array<uint16_t> sigalgs;
  676. if (!parse_sigalgs_list(&sigalgs, str) ||
  677. !sigalgs_unique(sigalgs)) {
  678. return 0;
  679. }
  680. if (!SSL_CTX_set_signing_algorithm_prefs(ctx, sigalgs.data(),
  681. sigalgs.size()) ||
  682. !ctx->verify_sigalgs.CopyFrom(sigalgs)) {
  683. return 0;
  684. }
  685. return 1;
  686. }
  687. int SSL_set1_sigalgs_list(SSL *ssl, const char *str) {
  688. if (!ssl->config) {
  689. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  690. return 0;
  691. }
  692. Array<uint16_t> sigalgs;
  693. if (!parse_sigalgs_list(&sigalgs, str) ||
  694. !sigalgs_unique(sigalgs)) {
  695. return 0;
  696. }
  697. if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
  698. !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) {
  699. return 0;
  700. }
  701. return 1;
  702. }
  703. int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  704. size_t num_prefs) {
  705. return ctx->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
  706. }