ssl_privkey.cc
Go to the documentation of this file.
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 
57 #include <openssl/ssl.h>
58 
59 #include <assert.h>
60 #include <limits.h>
61 
62 #include <openssl/ec.h>
63 #include <openssl/ec_key.h>
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/mem.h>
67 
68 #include "internal.h"
69 #include "../crypto/internal.h"
70 
71 
73 
75  return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
77 }
78 
79 static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
80  if (!ssl_is_key_type_supported(pkey->type)) {
82  return false;
83  }
84 
85  if (cert->chain != nullptr &&
86  sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) != nullptr &&
87  // Sanity-check that the private key and the certificate match.
88  !ssl_cert_check_private_key(cert, pkey)) {
89  return false;
90  }
91 
92  cert->privatekey = UpRef(pkey);
93  return true;
94 }
95 
96 typedef struct {
98  int pkey_type;
99  int curve;
100  const EVP_MD *(*digest_func)(void);
103 
106  false},
111 
115 
118  &EVP_sha256, false},
120  false},
122  false},
123 
124  {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, nullptr, false},
125 };
126 
128  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
129  if (kSignatureAlgorithms[i].sigalg == sigalg) {
130  return &kSignatureAlgorithms[i];
131  }
132  }
133  return NULL;
134 }
135 
137  if (hs->config->cert->privatekey != nullptr ||
138  hs->config->cert->key_method != nullptr ||
139  ssl_signing_with_dc(hs)) {
140  return true;
141  }
142 
143  return false;
144 }
145 
146 static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
147  uint16_t sigalg) {
149  if (alg == NULL ||
150  EVP_PKEY_id(pkey) != alg->pkey_type) {
151  return false;
152  }
153 
154  if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
155  // RSA keys may only be used with RSA-PSS.
156  if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
157  return false;
158  }
159 
160  // EC keys have a curve requirement.
161  if (alg->pkey_type == EVP_PKEY_EC &&
162  (alg->curve == NID_undef ||
164  EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
165  return false;
166  }
167  }
168 
169  return true;
170 }
171 
172 static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey,
173  uint16_t sigalg, bool is_verify) {
174  if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
176  return false;
177  }
178 
180  const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
181  EVP_PKEY_CTX *pctx;
182  if (is_verify) {
183  if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
184  return false;
185  }
186  } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
187  return false;
188  }
189 
190  if (alg->is_rsa_pss) {
192  !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
193  return false;
194  }
195  }
196 
197  return true;
198 }
199 
200 enum ssl_private_key_result_t ssl_private_key_sign(
201  SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
202  uint16_t sigalg, Span<const uint8_t> in) {
203  SSL *const ssl = hs->ssl;
204  const SSL_PRIVATE_KEY_METHOD *key_method = hs->config->cert->key_method;
205  EVP_PKEY *privatekey = hs->config->cert->privatekey.get();
206  assert(!hs->can_release_private_key);
207  if (ssl_signing_with_dc(hs)) {
208  key_method = hs->config->cert->dc_key_method;
209  privatekey = hs->config->cert->dc_privatekey.get();
210  }
211 
212  if (key_method != NULL) {
213  enum ssl_private_key_result_t ret;
214  if (hs->pending_private_key_op) {
215  ret = key_method->complete(ssl, out, out_len, max_out);
216  } else {
217  ret = key_method->sign(ssl, out, out_len, max_out,
218  sigalg, in.data(), in.size());
219  }
220  if (ret == ssl_private_key_failure) {
222  }
224  return ret;
225  }
226 
227  *out_len = max_out;
228  ScopedEVP_MD_CTX ctx;
229  if (!setup_ctx(ssl, ctx.get(), privatekey, sigalg, false /* sign */) ||
230  !EVP_DigestSign(ctx.get(), out, out_len, in.data(), in.size())) {
232  }
234 }
235 
237  uint16_t sigalg, EVP_PKEY *pkey,
239  ScopedEVP_MD_CTX ctx;
240  if (!setup_ctx(ssl, ctx.get(), pkey, sigalg, true /* verify */)) {
241  return false;
242  }
243  bool ok = EVP_DigestVerify(ctx.get(), signature.data(), signature.size(),
244  in.data(), in.size());
245 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
246  ok = true;
247  ERR_clear_error();
248 #endif
249  return ok;
250 }
251 
252 enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs,
253  uint8_t *out,
254  size_t *out_len,
255  size_t max_out,
257  SSL *const ssl = hs->ssl;
258  assert(!hs->can_release_private_key);
259  if (hs->config->cert->key_method != NULL) {
260  enum ssl_private_key_result_t ret;
261  if (hs->pending_private_key_op) {
262  ret = hs->config->cert->key_method->complete(ssl, out, out_len, max_out);
263  } else {
264  ret = hs->config->cert->key_method->decrypt(ssl, out, out_len, max_out,
265  in.data(), in.size());
266  }
267  if (ret == ssl_private_key_failure) {
269  }
271  return ret;
272  }
273 
274  RSA *rsa = EVP_PKEY_get0_RSA(hs->config->cert->privatekey.get());
275  if (rsa == NULL) {
276  // Decrypt operations are only supported for RSA keys.
279  }
280 
281  // Decrypt with no padding. PKCS#1 padding will be removed as part of the
282  // timing-sensitive code by the caller.
283  if (!RSA_decrypt(rsa, out_len, out, max_out, in.data(), in.size(),
284  RSA_NO_PADDING)) {
286  }
288 }
289 
291  uint16_t sigalg) {
292  SSL *const ssl = hs->ssl;
293  if (!pkey_supports_algorithm(ssl, hs->local_pubkey.get(), sigalg)) {
294  return false;
295  }
296 
297  // Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
298  // emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
299  // hash in TLS. Reasonable RSA key sizes are large enough for the largest
300  // defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
301  // SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
302  // size so that we can fall back to another algorithm in that case.
304  if (alg->is_rsa_pss && (size_t)EVP_PKEY_size(hs->local_pubkey.get()) <
305  2 * EVP_MD_size(alg->digest_func()) + 2) {
306  return false;
307  }
308 
309  return true;
310 }
311 
313 
314 using namespace bssl;
315 
316 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
317  if (rsa == NULL || ssl->config == NULL) {
319  return 0;
320  }
321 
322  UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
323  if (!pkey ||
324  !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
326  return 0;
327  }
328 
329  return ssl_set_pkey(ssl->config->cert.get(), pkey.get());
330 }
331 
332 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
333  UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
334  if (!rsa) {
336  return 0;
337  }
338 
339  return SSL_use_RSAPrivateKey(ssl, rsa.get());
340 }
341 
342 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
343  if (pkey == NULL || ssl->config == NULL) {
345  return 0;
346  }
347 
348  return ssl_set_pkey(ssl->config->cert.get(), pkey);
349 }
350 
351 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
352  size_t der_len) {
353  if (der_len > LONG_MAX) {
355  return 0;
356  }
357 
358  const uint8_t *p = der;
359  UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
360  if (!pkey || p != der + der_len) {
362  return 0;
363  }
364 
365  return SSL_use_PrivateKey(ssl, pkey.get());
366 }
367 
369  if (rsa == NULL) {
371  return 0;
372  }
373 
374  UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
375  if (!pkey ||
376  !EVP_PKEY_set1_RSA(pkey.get(), rsa)) {
378  return 0;
379  }
380 
381  return ssl_set_pkey(ctx->cert.get(), pkey.get());
382 }
383 
385  size_t der_len) {
386  UniquePtr<RSA> rsa(RSA_private_key_from_bytes(der, der_len));
387  if (!rsa) {
389  return 0;
390  }
391 
392  return SSL_CTX_use_RSAPrivateKey(ctx, rsa.get());
393 }
394 
396  if (pkey == NULL) {
398  return 0;
399  }
400 
401  return ssl_set_pkey(ctx->cert.get(), pkey);
402 }
403 
405  size_t der_len) {
406  if (der_len > LONG_MAX) {
408  return 0;
409  }
410 
411  const uint8_t *p = der;
412  UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, NULL, &p, (long)der_len));
413  if (!pkey || p != der + der_len) {
415  return 0;
416  }
417 
418  return SSL_CTX_use_PrivateKey(ctx, pkey.get());
419 }
420 
422  const SSL_PRIVATE_KEY_METHOD *key_method) {
423  if (!ssl->config) {
424  return;
425  }
426  ssl->config->cert->key_method = key_method;
427 }
428 
430  const SSL_PRIVATE_KEY_METHOD *key_method) {
431  ctx->cert->key_method = key_method;
432 }
433 
434 static constexpr size_t kMaxSignatureAlgorithmNameLen = 23;
435 
436 // This was "constexpr" rather than "const", but that triggered a bug in MSVC
437 // where it didn't pad the strings to the correct length.
438 static const struct {
442  {SSL_SIGN_RSA_PKCS1_MD5_SHA1, "rsa_pkcs1_md5_sha1"},
443  {SSL_SIGN_RSA_PKCS1_SHA1, "rsa_pkcs1_sha1"},
444  {SSL_SIGN_RSA_PKCS1_SHA256, "rsa_pkcs1_sha256"},
445  {SSL_SIGN_RSA_PKCS1_SHA384, "rsa_pkcs1_sha384"},
446  {SSL_SIGN_RSA_PKCS1_SHA512, "rsa_pkcs1_sha512"},
447  {SSL_SIGN_ECDSA_SHA1, "ecdsa_sha1"},
448  {SSL_SIGN_ECDSA_SECP256R1_SHA256, "ecdsa_secp256r1_sha256"},
449  {SSL_SIGN_ECDSA_SECP384R1_SHA384, "ecdsa_secp384r1_sha384"},
450  {SSL_SIGN_ECDSA_SECP521R1_SHA512, "ecdsa_secp521r1_sha512"},
451  {SSL_SIGN_RSA_PSS_RSAE_SHA256, "rsa_pss_rsae_sha256"},
452  {SSL_SIGN_RSA_PSS_RSAE_SHA384, "rsa_pss_rsae_sha384"},
453  {SSL_SIGN_RSA_PSS_RSAE_SHA512, "rsa_pss_rsae_sha512"},
454  {SSL_SIGN_ED25519, "ed25519"},
455 };
456 
458  int include_curve) {
459  if (!include_curve) {
460  switch (sigalg) {
462  return "ecdsa_sha256";
464  return "ecdsa_sha384";
466  return "ecdsa_sha512";
467  }
468  }
469 
470  for (const auto &candidate : kSignatureAlgorithmNames) {
471  if (candidate.signature_algorithm == sigalg) {
472  return candidate.name;
473  }
474  }
475 
476  return NULL;
477 }
478 
481  return alg != nullptr ? alg->pkey_type : EVP_PKEY_NONE;
482 }
483 
486  if (alg == nullptr || alg->digest_func == nullptr) {
487  return nullptr;
488  }
489  return alg->digest_func();
490 }
491 
494  return alg != nullptr && alg->is_rsa_pss;
495 }
496 
498  size_t num_prefs) {
499  return ctx->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
500 }
501 
503  size_t num_prefs) {
504  if (!ssl->config) {
505  return 0;
506  }
507  return ssl->config->cert->sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
508 }
509 
510 static constexpr struct {
512  int hash_nid;
527 };
528 
529 static bool parse_sigalg_pairs(Array<uint16_t> *out, const int *values,
530  size_t num_values) {
531  if ((num_values & 1) == 1) {
532  return false;
533  }
534 
535  const size_t num_pairs = num_values / 2;
536  if (!out->Init(num_pairs)) {
537  return false;
538  }
539 
540  for (size_t i = 0; i < num_values; i += 2) {
541  const int hash_nid = values[i];
542  const int pkey_type = values[i+1];
543 
544  bool found = false;
545  for (const auto &candidate : kSignatureAlgorithmsMapping) {
546  if (candidate.pkey_type == pkey_type && candidate.hash_nid == hash_nid) {
547  (*out)[i / 2] = candidate.signature_algorithm;
548  found = true;
549  break;
550  }
551  }
552 
553  if (!found) {
555  ERR_add_error_dataf("unknown hash:%d pkey:%d", hash_nid, pkey_type);
556  return false;
557  }
558  }
559 
560  return true;
561 }
562 
563 static int compare_uint16_t(const void *p1, const void *p2) {
564  uint16_t u1 = *((const uint16_t *)p1);
565  uint16_t u2 = *((const uint16_t *)p2);
566  if (u1 < u2) {
567  return -1;
568  } else if (u1 > u2) {
569  return 1;
570  } else {
571  return 0;
572  }
573 }
574 
575 static bool sigalgs_unique(Span<const uint16_t> in_sigalgs) {
576  if (in_sigalgs.size() < 2) {
577  return true;
578  }
579 
580  Array<uint16_t> sigalgs;
581  if (!sigalgs.CopyFrom(in_sigalgs)) {
582  return false;
583  }
584 
585  qsort(sigalgs.data(), sigalgs.size(), sizeof(uint16_t), compare_uint16_t);
586 
587  for (size_t i = 1; i < sigalgs.size(); i++) {
588  if (sigalgs[i - 1] == sigalgs[i]) {
590  return false;
591  }
592  }
593 
594  return true;
595 }
596 
597 int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values) {
598  Array<uint16_t> sigalgs;
599  if (!parse_sigalg_pairs(&sigalgs, values, num_values) ||
600  !sigalgs_unique(sigalgs)) {
601  return 0;
602  }
603 
605  sigalgs.size()) ||
606  !ctx->verify_sigalgs.CopyFrom(sigalgs)) {
607  return 0;
608  }
609 
610  return 1;
611 }
612 
613 int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values) {
614  if (!ssl->config) {
616  return 0;
617  }
618 
619  Array<uint16_t> sigalgs;
620  if (!parse_sigalg_pairs(&sigalgs, values, num_values) ||
621  !sigalgs_unique(sigalgs)) {
622  return 0;
623  }
624 
625  if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
626  !ssl->config->verify_sigalgs.CopyFrom(sigalgs)) {
627  return 0;
628  }
629 
630  return 1;
631 }
632 
633 static bool parse_sigalgs_list(Array<uint16_t> *out, const char *str) {
634  // str looks like "RSA+SHA1:ECDSA+SHA256:ecdsa_secp256r1_sha256".
635 
636  // Count colons to give the number of output elements from any successful
637  // parse.
638  size_t num_elements = 1;
639  size_t len = 0;
640  for (const char *p = str; *p; p++) {
641  len++;
642  if (*p == ':') {
643  num_elements++;
644  }
645  }
646 
647  if (!out->Init(num_elements)) {
648  return false;
649  }
650  size_t out_i = 0;
651 
652  enum {
653  pkey_or_name,
654  hash_name,
655  } state = pkey_or_name;
656 
658  // buf_used is always < sizeof(buf). I.e. it's always safe to write
659  // buf[buf_used] = 0.
660  size_t buf_used = 0;
661 
662  int pkey_type = 0, hash_nid = 0;
663 
664  // Note that the loop runs to len+1, i.e. it'll process the terminating NUL.
665  for (size_t offset = 0; offset < len+1; offset++) {
666  const char c = str[offset];
667 
668  switch (c) {
669  case '+':
670  if (state == hash_name) {
672  ERR_add_error_dataf("+ found in hash name at offset %zu", offset);
673  return false;
674  }
675  if (buf_used == 0) {
677  ERR_add_error_dataf("empty public key type at offset %zu", offset);
678  return false;
679  }
680  buf[buf_used] = 0;
681 
682  if (strcmp(buf, "RSA") == 0) {
684  } else if (strcmp(buf, "RSA-PSS") == 0 ||
685  strcmp(buf, "PSS") == 0) {
687  } else if (strcmp(buf, "ECDSA") == 0) {
689  } else {
691  ERR_add_error_dataf("unknown public key type '%s'", buf);
692  return false;
693  }
694 
695  state = hash_name;
696  buf_used = 0;
697  break;
698 
699  case ':':
701  case 0:
702  if (buf_used == 0) {
704  ERR_add_error_dataf("empty element at offset %zu", offset);
705  return false;
706  }
707 
708  buf[buf_used] = 0;
709 
710  if (state == pkey_or_name) {
711  // No '+' was seen thus this is a TLS 1.3-style name.
712  bool found = false;
713  for (const auto &candidate : kSignatureAlgorithmNames) {
714  if (strcmp(candidate.name, buf) == 0) {
715  assert(out_i < num_elements);
716  (*out)[out_i++] = candidate.signature_algorithm;
717  found = true;
718  break;
719  }
720  }
721 
722  if (!found) {
724  ERR_add_error_dataf("unknown signature algorithm '%s'", buf);
725  return false;
726  }
727  } else {
728  if (strcmp(buf, "SHA1") == 0) {
729  hash_nid = NID_sha1;
730  } else if (strcmp(buf, "SHA256") == 0) {
732  } else if (strcmp(buf, "SHA384") == 0) {
734  } else if (strcmp(buf, "SHA512") == 0) {
736  } else {
738  ERR_add_error_dataf("unknown hash function '%s'", buf);
739  return false;
740  }
741 
742  bool found = false;
743  for (const auto &candidate : kSignatureAlgorithmsMapping) {
744  if (candidate.pkey_type == pkey_type &&
745  candidate.hash_nid == hash_nid) {
746  assert(out_i < num_elements);
747  (*out)[out_i++] = candidate.signature_algorithm;
748  found = true;
749  break;
750  }
751  }
752 
753  if (!found) {
755  ERR_add_error_dataf("unknown pkey:%d hash:%s", pkey_type, buf);
756  return false;
757  }
758  }
759 
760  state = pkey_or_name;
761  buf_used = 0;
762  break;
763 
764  default:
765  if (buf_used == sizeof(buf) - 1) {
767  ERR_add_error_dataf("substring too long at offset %zu", offset);
768  return false;
769  }
770 
771  if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
772  (c >= 'A' && c <= 'Z') || c == '-' || c == '_') {
773  buf[buf_used++] = c;
774  } else {
776  ERR_add_error_dataf("invalid character 0x%02x at offest %zu", c,
777  offset);
778  return false;
779  }
780  }
781  }
782 
783  assert(out_i == out->size());
784  return true;
785 }
786 
788  Array<uint16_t> sigalgs;
789  if (!parse_sigalgs_list(&sigalgs, str) ||
790  !sigalgs_unique(sigalgs)) {
791  return 0;
792  }
793 
795  sigalgs.size()) ||
797  sigalgs.size())) {
798  return 0;
799  }
800 
801  return 1;
802 }
803 
804 int SSL_set1_sigalgs_list(SSL *ssl, const char *str) {
805  if (!ssl->config) {
807  return 0;
808  }
809 
810  Array<uint16_t> sigalgs;
811  if (!parse_sigalgs_list(&sigalgs, str) ||
812  !sigalgs_unique(sigalgs)) {
813  return 0;
814  }
815 
816  if (!SSL_set_signing_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size()) ||
817  !SSL_set_verify_algorithm_prefs(ssl, sigalgs.data(), sigalgs.size())) {
818  return 0;
819  }
820 
821  return 1;
822 }
823 
825  size_t num_prefs) {
826  return ctx->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
827 }
828 
830  size_t num_prefs) {
831  if (!ssl->config) {
833  return 0;
834  }
835 
836  return ssl->config->verify_sigalgs.CopyFrom(MakeConstSpan(prefs, num_prefs));
837 }
CERT::privatekey
UniquePtr< EVP_PKEY > privatekey
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2351
xds_interop_client.str
str
Definition: xds_interop_client.py:487
SSL_get_signature_algorithm_name
const char * SSL_get_signature_algorithm_name(uint16_t sigalg, int include_curve)
Definition: ssl_privkey.cc:457
SSL_CTX_use_PrivateKey
int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
Definition: ssl_privkey.cc:395
SSL_SIGN_RSA_PSS_RSAE_SHA512
#define SSL_SIGN_RSA_PSS_RSAE_SHA512
Definition: ssl.h:1075
EVP_PKEY_id
#define EVP_PKEY_id
Definition: boringssl_prefix_symbols.h:1638
EVP_PKEY_EC
#define EVP_PKEY_EC
Definition: evp.h:178
EVP_PKEY_new
#define EVP_PKEY_new
Definition: boringssl_prefix_symbols.h:1643
EVP_md5_sha1
const OPENSSL_EXPORT EVP_MD * EVP_md5_sha1(void)
SSL_R_PRIVATE_KEY_OPERATION_FAILED
#define SSL_R_PRIVATE_KEY_OPERATION_FAILED
Definition: ssl.h:5554
EVP_DigestSignInit
#define EVP_DigestSignInit
Definition: boringssl_prefix_symbols.h:1514
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
EVP_sha512
const OPENSSL_EXPORT EVP_MD * EVP_sha512(void)
sigalgs_unique
static bool sigalgs_unique(Span< const uint16_t > in_sigalgs)
Definition: ssl_privkey.cc:575
SSL_SIGN_RSA_PSS_RSAE_SHA256
#define SSL_SIGN_RSA_PSS_RSAE_SHA256
Definition: ssl.h:1073
Span::size
size_t size() const
Definition: boringssl-with-bazel/src/include/openssl/span.h:133
EVP_DigestVerifyInit
#define EVP_DigestVerifyInit
Definition: boringssl_prefix_symbols.h:1519
SSL_SIGNATURE_ALGORITHM
Definition: ssl_privkey.cc:96
compare_uint16_t
static int compare_uint16_t(const void *p1, const void *p2)
Definition: ssl_privkey.cc:563
ctx
Definition: benchmark-async.c:30
NID_X9_62_prime256v1
#define NID_X9_62_prime256v1
Definition: nid.h:1914
setup_ctx
static bool setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, uint16_t sigalg, bool is_verify)
Definition: ssl_privkey.cc:172
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
Array::data
const T * data() const
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:274
evp.h
SSL_HANDSHAKE::local_pubkey
UniquePtr< EVP_PKEY > local_pubkey
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1909
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
EVP_PKEY_get0_RSA
#define EVP_PKEY_get0_RSA
Definition: boringssl_prefix_symbols.h:1630
EVP_sha384
const OPENSSL_EXPORT EVP_MD * EVP_sha384(void)
SSL_R_INVALID_SIGNATURE_ALGORITHM
#define SSL_R_INVALID_SIGNATURE_ALGORITHM
Definition: ssl.h:5562
kSignatureAlgorithmsMapping
static constexpr struct @375 kSignatureAlgorithmsMapping[]
EVP_PKEY_RSA_PSS
#define EVP_PKEY_RSA_PSS
Definition: evp.h:176
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
internal.h
ssl_private_key_method_st::sign
enum ssl_private_key_result_t(* sign)(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, uint16_t signature_algorithm, const uint8_t *in, size_t in_len)
Definition: ssl.h:1265
ssl_private_key_failure
ssl_private_key_failure
Definition: ssl.h:1236
OPENSSL_ARRAY_SIZE
#define OPENSSL_ARRAY_SIZE(array)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:179
SSL_CTX_set_signing_algorithm_prefs
int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, size_t num_prefs)
Definition: ssl_privkey.cc:497
ssl_st::config
bssl::UniquePtr< bssl::SSL_CONFIG > config
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3712
SSL_HANDSHAKE::ssl
SSL * ssl
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1726
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
SSL_SIGNATURE_ALGORITHM::digest_func
const EVP_MD *(* digest_func)(void)
Definition: ssl_privkey.cc:100
SSL_SIGNATURE_ALGORITHM::pkey_type
int pkey_type
Definition: ssl_privkey.cc:98
SSL_CONFIG::cert
UniquePtr< CERT > cert
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2986
ssl_private_key_method_st
Definition: ssl.h:1248
ssl_private_key_sign
enum ssl_private_key_result_t ssl_private_key_sign(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, uint16_t sigalg, Span< const uint8_t > in)
Definition: ssl_privkey.cc:200
bssl
Definition: hpke_test.cc:37
SSL_SIGN_RSA_PSS_RSAE_SHA384
#define SSL_SIGN_RSA_PSS_RSAE_SHA384
Definition: ssl.h:1074
SSL_set_signing_algorithm_prefs
int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs, size_t num_prefs)
Definition: ssl_privkey.cc:502
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
SSL_R_DUPLICATE_SIGNATURE_ALGORITHM
#define SSL_R_DUPLICATE_SIGNATURE_ALGORITHM
Definition: ssl.h:5563
EVP_PKEY_RSA
#define EVP_PKEY_RSA
Definition: evp.h:175
num_elements
static size_t num_elements(const uint8_t *in, size_t in_len)
Definition: evp_asn1.c:283
ssl_cert_check_private_key
bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey)
Definition: ssl_cert.cc:518
SSL_SIGN_ECDSA_SECP521R1_SHA512
#define SSL_SIGN_ECDSA_SECP521R1_SHA512
Definition: ssl.h:1072
NID_sha384
#define NID_sha384
Definition: nid.h:2998
EVP_sha256
const OPENSSL_EXPORT EVP_MD * EVP_sha256(void)
SSL_SIGN_RSA_PKCS1_SHA256
#define SSL_SIGN_RSA_PKCS1_SHA256
Definition: ssl.h:1066
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
SSL_use_PrivateKey_ASN1
int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der, size_t der_len)
Definition: ssl_privkey.cc:351
EC_KEY_get0_group
#define EC_KEY_get0_group
Definition: boringssl_prefix_symbols.h:1344
ssl_ctx_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3404
parse_sigalgs_list
static bool parse_sigalgs_list(Array< uint16_t > *out, const char *str)
Definition: ssl_privkey.cc:633
Array::CopyFrom
bool CopyFrom(Span< const T > in)
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:338
SSL_get_signature_algorithm_key_type
int SSL_get_signature_algorithm_key_type(uint16_t sigalg)
Definition: ssl_privkey.cc:479
SSL_is_signature_algorithm_rsa_pss
int SSL_is_signature_algorithm_rsa_pss(uint16_t sigalg)
Definition: ssl_privkey.cc:492
EVP_PKEY_ED25519
#define EVP_PKEY_ED25519
Definition: evp.h:179
env_md_ctx_st
Definition: digest.h:306
CERT
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2345
SSL_SIGN_RSA_PKCS1_SHA384
#define SSL_SIGN_RSA_PKCS1_SHA384
Definition: ssl.h:1067
SSL_SIGN_RSA_PKCS1_SHA512
#define SSL_SIGN_RSA_PKCS1_SHA512
Definition: ssl.h:1068
RSA_private_key_from_bytes
#define RSA_private_key_from_bytes
Definition: boringssl_prefix_symbols.h:2125
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
evp_pkey_ctx_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:182
CERT::chain
UniquePtr< STACK_OF(CRYPTO_BUFFER)> chain
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2359
SSL_set_verify_algorithm_prefs
int SSL_set_verify_algorithm_prefs(SSL *ssl, const uint16_t *prefs, size_t num_prefs)
Definition: ssl_privkey.cc:829
ssl_has_private_key
bool ssl_has_private_key(const SSL_HANDSHAKE *hs)
Definition: ssl_privkey.cc:136
EVP_PKEY_NONE
#define EVP_PKEY_NONE
Definition: evp.h:174
evp_pkey_st
Definition: evp.h:1046
SSL_HANDSHAKE
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1720
Array< uint16_t >
NID_secp521r1
#define NID_secp521r1
Definition: nid.h:3172
gen_stats_data.found
bool found
Definition: gen_stats_data.py:61
SSL_CTX_use_RSAPrivateKey_ASN1
int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der, size_t der_len)
Definition: ssl_privkey.cc:384
SSL_set_private_key_method
void SSL_set_private_key_method(SSL *ssl, const SSL_PRIVATE_KEY_METHOD *key_method)
Definition: ssl_privkey.cc:421
ERR_R_PASSED_NULL_PARAMETER
#define ERR_R_PASSED_NULL_PARAMETER
Definition: err.h:373
SSL_HANDSHAKE::pending_private_key_op
bool pending_private_key_op
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2015
SSL_CTX_use_RSAPrivateKey
int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
Definition: ssl_privkey.cc:368
Array::size
size_t size() const
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:276
NID_sha256
#define NID_sha256
Definition: nid.h:2993
ssl_public_key_verify
bool ssl_public_key_verify(SSL *ssl, Span< const uint8_t > signature, uint16_t sigalg, EVP_PKEY *pkey, Span< const uint8_t > in)
Definition: ssl_privkey.cc:236
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
ERR_add_error_dataf
#define ERR_add_error_dataf
Definition: boringssl_prefix_symbols.h:1412
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
#define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
Definition: err.h:372
EC_GROUP_get_curve_name
#define EC_GROUP_get_curve_name
Definition: boringssl_prefix_symbols.h:1327
EVP_MD_size
#define EVP_MD_size
Definition: boringssl_prefix_symbols.h:1579
SSL_SIGN_RSA_PKCS1_SHA1
#define SSL_SIGN_RSA_PKCS1_SHA1
Definition: ssl.h:1065
get_signature_algorithm
static const SSL_SIGNATURE_ALGORITHM * get_signature_algorithm(uint16_t sigalg)
Definition: ssl_privkey.cc:127
ssl_private_key_method_st::complete
enum ssl_private_key_result_t(* complete)(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out)
Definition: ssl.h:1294
SSL_R_WRONG_SIGNATURE_TYPE
#define SSL_R_WRONG_SIGNATURE_TYPE
Definition: ssl.h:5512
BSSL_NAMESPACE_END
#define BSSL_NAMESPACE_END
Definition: base.h:480
SSL_use_RSAPrivateKey_ASN1
int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len)
Definition: ssl_privkey.cc:332
err.h
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
ssl_protocol_version
uint16_t ssl_protocol_version(const SSL *ssl)
Definition: ssl_versions.cc:251
ec_key.h
NID_undef
#define NID_undef
Definition: nid.h:85
EVP_PKEY_size
#define EVP_PKEY_size
Definition: boringssl_prefix_symbols.h:1658
NID_sha1
#define NID_sha1
Definition: nid.h:372
Span< const uint8_t >
qsort
void qsort(void *a, size_t n, size_t es, int(*cmp)(const void *, const void *))
Definition: qsort.h:130
EVP_DigestSign
#define EVP_DigestSign
Definition: boringssl_prefix_symbols.h:1512
SSL_HANDSHAKE::can_release_private_key
bool can_release_private_key
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2037
RSA_NO_PADDING
#define RSA_NO_PADDING
Definition: rsa.h:213
NID_sha512
#define NID_sha512
Definition: nid.h:3003
ERR_R_OVERFLOW
#define ERR_R_OVERFLOW
Definition: err.h:375
evp_pkey_st::type
int type
Definition: evp.h:1051
ssl.h
SSL_SIGNATURE_ALGORITHM::sigalg
uint16_t sigalg
Definition: ssl_privkey.cc:97
SSL_CTX_set1_sigalgs_list
int SSL_CTX_set1_sigalgs_list(SSL_CTX *ctx, const char *str)
Definition: ssl_privkey.cc:787
ERR_R_ASN1_LIB
#define ERR_R_ASN1_LIB
Definition: err.h:340
SSL_set1_sigalgs
int SSL_set1_sigalgs(SSL *ssl, const int *values, size_t num_values)
Definition: ssl_privkey.cc:613
ssl_is_key_type_supported
BSSL_NAMESPACE_BEGIN bool ssl_is_key_type_supported(int key_type)
Definition: ssl_privkey.cc:74
TLS1_3_VERSION
#define TLS1_3_VERSION
Definition: ssl.h:653
sk_CRYPTO_BUFFER_value
#define sk_CRYPTO_BUFFER_value
Definition: boringssl_prefix_symbols.h:561
EVP_DigestVerify
#define EVP_DigestVerify
Definition: boringssl_prefix_symbols.h:1517
SSL_SIGN_ED25519
#define SSL_SIGN_ED25519
Definition: ssl.h:1076
EVP_PKEY_set1_RSA
#define EVP_PKEY_set1_RSA
Definition: boringssl_prefix_symbols.h:1653
BSSL_NAMESPACE_BEGIN
Definition: trust_token_test.cc:45
SSL_SIGN_RSA_PKCS1_MD5_SHA1
#define SSL_SIGN_RSA_PKCS1_MD5_SHA1
Definition: ssl.h:1081
SSL_HANDSHAKE::config
SSL_CONFIG * config
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1729
EVP_PKEY_get0_EC_KEY
#define EVP_PKEY_get0_EC_KEY
Definition: boringssl_prefix_symbols.h:1629
d2i_PrivateKey
#define d2i_PrivateKey
Definition: boringssl_prefix_symbols.h:3016
ssl_private_key_success
ssl_private_key_success
Definition: ssl.h:1234
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
parse_sigalg_pairs
static bool parse_sigalg_pairs(Array< uint16_t > *out, const int *values, size_t num_values)
Definition: ssl_privkey.cc:529
SSL_use_RSAPrivateKey
int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
Definition: ssl_privkey.cc:316
ssl_private_key_decrypt
enum ssl_private_key_result_t ssl_private_key_decrypt(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out, Span< const uint8_t > in)
Definition: ssl_privkey.cc:252
ssl_set_pkey
static bool ssl_set_pkey(CERT *cert, EVP_PKEY *pkey)
Definition: ssl_privkey.cc:79
values
std::array< int64_t, Size > values
Definition: abseil-cpp/absl/container/btree_benchmark.cc:608
SSL_get_signature_algorithm_digest
const EVP_MD * SSL_get_signature_algorithm_digest(uint16_t sigalg)
Definition: ssl_privkey.cc:484
kSignatureAlgorithms
static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[]
Definition: ssl_privkey.cc:104
kMaxSignatureAlgorithmNameLen
static constexpr size_t kMaxSignatureAlgorithmNameLen
Definition: ssl_privkey.cc:434
ok
bool ok
Definition: async_end2end_test.cc:197
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
ERR_clear_error
#define ERR_clear_error
Definition: boringssl_prefix_symbols.h:1413
EVP_sha1
const OPENSSL_EXPORT EVP_MD * EVP_sha1(void)
RSA_PKCS1_PSS_PADDING
#define RSA_PKCS1_PSS_PADDING
Definition: rsa.h:221
SSL_CTX_use_PrivateKey_ASN1
int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der, size_t der_len)
Definition: ssl_privkey.cc:404
EVP_PKEY_CTX_set_rsa_padding
#define EVP_PKEY_CTX_set_rsa_padding
Definition: boringssl_prefix_symbols.h:1603
ERR_R_EVP_LIB
#define ERR_R_EVP_LIB
Definition: err.h:334
name
const char name[kMaxSignatureAlgorithmNameLen]
Definition: ssl_privkey.cc:440
RSA_decrypt
#define RSA_decrypt
Definition: boringssl_prefix_symbols.h:2086
SSL_R_UNKNOWN_CERTIFICATE_TYPE
#define SSL_R_UNKNOWN_CERTIFICATE_TYPE
Definition: ssl.h:5495
kSignatureAlgorithmNames
static const struct @374 kSignatureAlgorithmNames[]
pkey_type
int pkey_type
Definition: ssl_privkey.cc:511
Span::data
T * data() const
Definition: boringssl-with-bazel/src/include/openssl/span.h:132
SSL_SIGN_ECDSA_SHA1
#define SSL_SIGN_ECDSA_SHA1
Definition: ssl.h:1069
mem.h
SSL_CTX_set_private_key_method
void SSL_CTX_set_private_key_method(SSL_CTX *ctx, const SSL_PRIVATE_KEY_METHOD *key_method)
Definition: ssl_privkey.cc:429
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
rsa_st
Definition: rsa.h:732
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
EVP_PKEY_CTX_set_rsa_pss_saltlen
#define EVP_PKEY_CTX_set_rsa_pss_saltlen
Definition: boringssl_prefix_symbols.h:1607
NID_secp384r1
#define NID_secp384r1
Definition: nid.h:3168
ssl_private_key_supports_signature_algorithm
bool ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t sigalg)
Definition: ssl_privkey.cc:290
key_type
upb_fieldtype_t key_type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1071
SSL_use_PrivateKey
int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
Definition: ssl_privkey.cc:342
ssl_signing_with_dc
bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs)
Definition: ssl_cert.cc:831
SSL_CTX_set1_sigalgs
int SSL_CTX_set1_sigalgs(SSL_CTX *ctx, const int *values, size_t num_values)
Definition: ssl_privkey.cc:597
ec.h
hash_nid
int hash_nid
Definition: ssl_privkey.cc:512
OPENSSL_FALLTHROUGH
#define OPENSSL_FALLTHROUGH
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:201
SSL_SIGNATURE_ALGORITHM::is_rsa_pss
bool is_rsa_pss
Definition: ssl_privkey.cc:101
SSL_SIGNATURE_ALGORITHM::curve
int curve
Definition: ssl_privkey.cc:99
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
pkey_supports_algorithm
static bool pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey, uint16_t sigalg)
Definition: ssl_privkey.cc:146
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142
signature_algorithm
uint16_t signature_algorithm
Definition: ssl_privkey.cc:439
SSL_set1_sigalgs_list
int SSL_set1_sigalgs_list(SSL *ssl, const char *str)
Definition: ssl_privkey.cc:804
ssl_private_key_retry
ssl_private_key_retry
Definition: ssl.h:1235
SSL_SIGN_ECDSA_SECP384R1_SHA384
#define SSL_SIGN_ECDSA_SECP384R1_SHA384
Definition: ssl.h:1071
absl::MakeConstSpan
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
Definition: abseil-cpp/absl/types/span.h:707
SSL_SIGN_ECDSA_SECP256R1_SHA256
#define SSL_SIGN_ECDSA_SECP256R1_SHA256
Definition: ssl.h:1070
SSL_CTX_set_verify_algorithm_prefs
int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs, size_t num_prefs)
Definition: ssl_privkey.cc:824


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:16