您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ssl_privkey.c 18 KiB

Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
Simplify ssl_private_key_* state machine points. The original motivation behind the sign/complete split was to avoid needlessly hashing the input on each pass through the state machine, but we're payload-based now and, in all cases, the payload is either cheap to compute or readily available. (Even the hashing worry was probably unnecessary.) Tweak ssl_private_key_{sign,decrypt} to automatically call ssl_private_key_complete as needed and take advantage of this in the handshake state machines: - TLS 1.3 signing now computes the payload each pass. The payload is small and we're already allocating a comparable-sized buffer each iteration to hold the signature. This shouldn't be a big deal. - TLS 1.2 decryption code still needs two states due to reading the message (fixed in new state machine style), but otherwise it just performs cheap idempotent tasks again. The PSK code is reshuffled to guarantee the callback is not called twice (though this was impossible anyway because we don't support RSA_PSK). - TLS 1.2 CertificateVerify signing is easy as the transcript is readily available. The buffer is released very slightly later, but it shouldn't matter. - TLS 1.2 ServerKeyExchange signing required some reshuffling. Assembling the ServerKeyExchange parameters is moved to the previous state. The signing payload has some randoms prepended. This is cheap enough, but a nuisance in C. Pre-prepend the randoms in hs->server_params. With this change, we are *nearly* rid of the A/B => same function pattern. BUG=128 Change-Id: Iec4fe0be7cfc88a6de027ba2760fae70794ea810 Reviewed-on: https://boringssl-review.googlesource.com/17265 Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Steven Valdez <svaldez@google.com> Reviewed-by: Steven Valdez <svaldez@google.com>
7 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 <openssl/type_check.h>
  65. #include "internal.h"
  66. #include "../crypto/internal.h"
  67. int ssl_is_key_type_supported(int key_type) {
  68. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC ||
  69. key_type == EVP_PKEY_ED25519;
  70. }
  71. static int ssl_set_pkey(CERT *cert, EVP_PKEY *pkey) {
  72. if (!ssl_is_key_type_supported(pkey->type)) {
  73. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  74. return 0;
  75. }
  76. if (cert->chain != NULL &&
  77. sk_CRYPTO_BUFFER_value(cert->chain, 0) != NULL &&
  78. /* Sanity-check that the private key and the certificate match. */
  79. !ssl_cert_check_private_key(cert, pkey)) {
  80. return 0;
  81. }
  82. EVP_PKEY_free(cert->privatekey);
  83. EVP_PKEY_up_ref(pkey);
  84. cert->privatekey = pkey;
  85. return 1;
  86. }
  87. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  88. EVP_PKEY *pkey;
  89. int ret;
  90. if (rsa == NULL) {
  91. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  92. return 0;
  93. }
  94. pkey = EVP_PKEY_new();
  95. if (pkey == NULL) {
  96. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  97. return 0;
  98. }
  99. RSA_up_ref(rsa);
  100. EVP_PKEY_assign_RSA(pkey, rsa);
  101. ret = ssl_set_pkey(ssl->cert, pkey);
  102. EVP_PKEY_free(pkey);
  103. return ret;
  104. }
  105. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  106. if (pkey == NULL) {
  107. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  108. return 0;
  109. }
  110. return ssl_set_pkey(ssl->cert, pkey);
  111. }
  112. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
  113. size_t der_len) {
  114. if (der_len > LONG_MAX) {
  115. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  116. return 0;
  117. }
  118. const uint8_t *p = der;
  119. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  120. if (pkey == NULL || p != der + der_len) {
  121. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  122. EVP_PKEY_free(pkey);
  123. return 0;
  124. }
  125. int ret = SSL_use_PrivateKey(ssl, pkey);
  126. EVP_PKEY_free(pkey);
  127. return ret;
  128. }
  129. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  130. int ret;
  131. EVP_PKEY *pkey;
  132. if (rsa == NULL) {
  133. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  134. return 0;
  135. }
  136. pkey = EVP_PKEY_new();
  137. if (pkey == NULL) {
  138. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  139. return 0;
  140. }
  141. RSA_up_ref(rsa);
  142. EVP_PKEY_assign_RSA(pkey, rsa);
  143. ret = ssl_set_pkey(ctx->cert, pkey);
  144. EVP_PKEY_free(pkey);
  145. return ret;
  146. }
  147. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  148. size_t der_len) {
  149. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  150. if (rsa == NULL) {
  151. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  152. return 0;
  153. }
  154. int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  155. RSA_free(rsa);
  156. return ret;
  157. }
  158. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  159. if (pkey == NULL) {
  160. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  161. return 0;
  162. }
  163. return ssl_set_pkey(ctx->cert, pkey);
  164. }
  165. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
  166. size_t der_len) {
  167. if (der_len > LONG_MAX) {
  168. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  169. return 0;
  170. }
  171. const uint8_t *p = der;
  172. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  173. if (pkey == NULL || p != der + der_len) {
  174. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  175. EVP_PKEY_free(pkey);
  176. return 0;
  177. }
  178. int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  179. EVP_PKEY_free(pkey);
  180. return ret;
  181. }
  182. void SSL_set_private_key_method(SSL *ssl,
  183. const SSL_PRIVATE_KEY_METHOD *key_method) {
  184. ssl->cert->key_method = key_method;
  185. }
  186. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  187. const SSL_PRIVATE_KEY_METHOD *key_method) {
  188. ctx->cert->key_method = key_method;
  189. }
  190. static int set_algorithm_prefs(uint16_t **out_prefs, size_t *out_num_prefs,
  191. const uint16_t *prefs, size_t num_prefs) {
  192. OPENSSL_free(*out_prefs);
  193. *out_num_prefs = 0;
  194. *out_prefs = BUF_memdup(prefs, num_prefs * sizeof(prefs[0]));
  195. if (*out_prefs == NULL) {
  196. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  197. return 0;
  198. }
  199. *out_num_prefs = num_prefs;
  200. return 1;
  201. }
  202. int SSL_CTX_set_signing_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  203. size_t num_prefs) {
  204. return set_algorithm_prefs(&ctx->cert->sigalgs, &ctx->cert->num_sigalgs,
  205. prefs, num_prefs);
  206. }
  207. int SSL_set_signing_algorithm_prefs(SSL *ssl, const uint16_t *prefs,
  208. size_t num_prefs) {
  209. return set_algorithm_prefs(&ssl->cert->sigalgs, &ssl->cert->num_sigalgs,
  210. prefs, num_prefs);
  211. }
  212. int SSL_CTX_set_verify_algorithm_prefs(SSL_CTX *ctx, const uint16_t *prefs,
  213. size_t num_prefs) {
  214. return set_algorithm_prefs(&ctx->verify_sigalgs, &ctx->num_verify_sigalgs,
  215. prefs, num_prefs);
  216. }
  217. int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
  218. size_t num_digests) {
  219. OPENSSL_free(ssl->cert->sigalgs);
  220. OPENSSL_COMPILE_ASSERT(sizeof(int) >= 2 * sizeof(uint16_t),
  221. digest_list_conversion_cannot_overflow);
  222. ssl->cert->num_sigalgs = 0;
  223. ssl->cert->sigalgs = OPENSSL_malloc(sizeof(uint16_t) * 2 * num_digests);
  224. if (ssl->cert->sigalgs == NULL) {
  225. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  226. return 0;
  227. }
  228. /* Convert the digest list to a signature algorithms list.
  229. *
  230. * TODO(davidben): Replace this API with one that can express RSA-PSS, etc. */
  231. for (size_t i = 0; i < num_digests; i++) {
  232. switch (digest_nids[i]) {
  233. case NID_sha1:
  234. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA1;
  235. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] = SSL_SIGN_ECDSA_SHA1;
  236. ssl->cert->num_sigalgs += 2;
  237. break;
  238. case NID_sha256:
  239. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA256;
  240. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  241. SSL_SIGN_ECDSA_SECP256R1_SHA256;
  242. ssl->cert->num_sigalgs += 2;
  243. break;
  244. case NID_sha384:
  245. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA384;
  246. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  247. SSL_SIGN_ECDSA_SECP384R1_SHA384;
  248. ssl->cert->num_sigalgs += 2;
  249. break;
  250. case NID_sha512:
  251. ssl->cert->sigalgs[ssl->cert->num_sigalgs] = SSL_SIGN_RSA_PKCS1_SHA512;
  252. ssl->cert->sigalgs[ssl->cert->num_sigalgs + 1] =
  253. SSL_SIGN_ECDSA_SECP521R1_SHA512;
  254. ssl->cert->num_sigalgs += 2;
  255. break;
  256. }
  257. }
  258. return 1;
  259. }
  260. typedef struct {
  261. uint16_t sigalg;
  262. int pkey_type;
  263. int curve;
  264. const EVP_MD *(*digest_func)(void);
  265. char is_rsa_pss;
  266. } SSL_SIGNATURE_ALGORITHM;
  267. static const SSL_SIGNATURE_ALGORITHM kSignatureAlgorithms[] = {
  268. {SSL_SIGN_RSA_PKCS1_MD5_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_md5_sha1, 0},
  269. {SSL_SIGN_RSA_PKCS1_SHA1, EVP_PKEY_RSA, NID_undef, &EVP_sha1, 0},
  270. {SSL_SIGN_RSA_PKCS1_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 0},
  271. {SSL_SIGN_RSA_PKCS1_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 0},
  272. {SSL_SIGN_RSA_PKCS1_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 0},
  273. {SSL_SIGN_RSA_PSS_SHA256, EVP_PKEY_RSA, NID_undef, &EVP_sha256, 1},
  274. {SSL_SIGN_RSA_PSS_SHA384, EVP_PKEY_RSA, NID_undef, &EVP_sha384, 1},
  275. {SSL_SIGN_RSA_PSS_SHA512, EVP_PKEY_RSA, NID_undef, &EVP_sha512, 1},
  276. {SSL_SIGN_ECDSA_SHA1, EVP_PKEY_EC, NID_undef, &EVP_sha1, 0},
  277. {SSL_SIGN_ECDSA_SECP256R1_SHA256, EVP_PKEY_EC, NID_X9_62_prime256v1,
  278. &EVP_sha256, 0},
  279. {SSL_SIGN_ECDSA_SECP384R1_SHA384, EVP_PKEY_EC, NID_secp384r1, &EVP_sha384,
  280. 0},
  281. {SSL_SIGN_ECDSA_SECP521R1_SHA512, EVP_PKEY_EC, NID_secp521r1, &EVP_sha512,
  282. 0},
  283. {SSL_SIGN_ED25519, EVP_PKEY_ED25519, NID_undef, NULL, 0},
  284. };
  285. static const SSL_SIGNATURE_ALGORITHM *get_signature_algorithm(uint16_t sigalg) {
  286. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kSignatureAlgorithms); i++) {
  287. if (kSignatureAlgorithms[i].sigalg == sigalg) {
  288. return &kSignatureAlgorithms[i];
  289. }
  290. }
  291. return NULL;
  292. }
  293. int ssl_has_private_key(const SSL *ssl) {
  294. return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
  295. }
  296. static int pkey_supports_algorithm(const SSL *ssl, EVP_PKEY *pkey,
  297. uint16_t sigalg) {
  298. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  299. if (alg == NULL ||
  300. EVP_PKEY_id(pkey) != alg->pkey_type) {
  301. return 0;
  302. }
  303. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  304. /* RSA keys may only be used with RSA-PSS. */
  305. if (alg->pkey_type == EVP_PKEY_RSA && !alg->is_rsa_pss) {
  306. return 0;
  307. }
  308. /* EC keys have a curve requirement. */
  309. if (alg->pkey_type == EVP_PKEY_EC &&
  310. (alg->curve == NID_undef ||
  311. EC_GROUP_get_curve_name(
  312. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey))) != alg->curve)) {
  313. return 0;
  314. }
  315. }
  316. return 1;
  317. }
  318. static int setup_ctx(SSL *ssl, EVP_MD_CTX *ctx, EVP_PKEY *pkey, uint16_t sigalg,
  319. int is_verify) {
  320. if (!pkey_supports_algorithm(ssl, pkey, sigalg)) {
  321. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  322. return 0;
  323. }
  324. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  325. const EVP_MD *digest = alg->digest_func != NULL ? alg->digest_func() : NULL;
  326. EVP_PKEY_CTX *pctx;
  327. if (is_verify) {
  328. if (!EVP_DigestVerifyInit(ctx, &pctx, digest, NULL, pkey)) {
  329. return 0;
  330. }
  331. } else if (!EVP_DigestSignInit(ctx, &pctx, digest, NULL, pkey)) {
  332. return 0;
  333. }
  334. if (alg->is_rsa_pss) {
  335. if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
  336. !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1 /* salt len = hash len */)) {
  337. return 0;
  338. }
  339. }
  340. return 1;
  341. }
  342. static int legacy_sign_digest_supported(const SSL_SIGNATURE_ALGORITHM *alg) {
  343. return (alg->pkey_type == EVP_PKEY_EC || alg->pkey_type == EVP_PKEY_RSA) &&
  344. !alg->is_rsa_pss;
  345. }
  346. static enum ssl_private_key_result_t legacy_sign(
  347. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, uint16_t sigalg,
  348. const uint8_t *in, size_t in_len) {
  349. /* TODO(davidben): Remove support for |sign_digest|-only
  350. * |SSL_PRIVATE_KEY_METHOD|s. */
  351. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  352. if (alg == NULL || !legacy_sign_digest_supported(alg)) {
  353. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  354. return ssl_private_key_failure;
  355. }
  356. const EVP_MD *md = alg->digest_func();
  357. uint8_t hash[EVP_MAX_MD_SIZE];
  358. unsigned hash_len;
  359. if (!EVP_Digest(in, in_len, hash, &hash_len, md, NULL)) {
  360. return ssl_private_key_failure;
  361. }
  362. return ssl->cert->key_method->sign_digest(ssl, out, out_len, max_out, md,
  363. hash, hash_len);
  364. }
  365. enum ssl_private_key_result_t ssl_private_key_sign(
  366. SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
  367. uint16_t sigalg, const uint8_t *in, size_t in_len) {
  368. SSL *const ssl = hs->ssl;
  369. if (ssl->cert->key_method != NULL) {
  370. enum ssl_private_key_result_t ret;
  371. if (hs->pending_private_key_op) {
  372. ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
  373. } else {
  374. ret = (ssl->cert->key_method->sign != NULL
  375. ? ssl->cert->key_method->sign
  376. : legacy_sign)(ssl, out, out_len, max_out, sigalg, in, in_len);
  377. }
  378. hs->pending_private_key_op = ret == ssl_private_key_retry;
  379. return ret;
  380. }
  381. *out_len = max_out;
  382. EVP_MD_CTX ctx;
  383. EVP_MD_CTX_init(&ctx);
  384. int ret = setup_ctx(ssl, &ctx, ssl->cert->privatekey, sigalg, 0 /* sign */) &&
  385. EVP_DigestSign(&ctx, out, out_len, in, in_len);
  386. EVP_MD_CTX_cleanup(&ctx);
  387. return ret ? ssl_private_key_success : ssl_private_key_failure;
  388. }
  389. int ssl_public_key_verify(SSL *ssl, const uint8_t *signature,
  390. size_t signature_len, uint16_t sigalg, EVP_PKEY *pkey,
  391. const uint8_t *in, size_t in_len) {
  392. EVP_MD_CTX ctx;
  393. EVP_MD_CTX_init(&ctx);
  394. int ret = setup_ctx(ssl, &ctx, pkey, sigalg, 1 /* verify */) &&
  395. EVP_DigestVerify(&ctx, signature, signature_len, in, in_len);
  396. EVP_MD_CTX_cleanup(&ctx);
  397. return ret;
  398. }
  399. enum ssl_private_key_result_t ssl_private_key_decrypt(
  400. SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, size_t max_out,
  401. const uint8_t *in, size_t in_len) {
  402. SSL *const ssl = hs->ssl;
  403. if (ssl->cert->key_method != NULL) {
  404. enum ssl_private_key_result_t ret;
  405. if (hs->pending_private_key_op) {
  406. ret = ssl->cert->key_method->complete(ssl, out, out_len, max_out);
  407. } else {
  408. ret = ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
  409. in_len);
  410. }
  411. hs->pending_private_key_op = ret == ssl_private_key_retry;
  412. return ret;
  413. }
  414. RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey);
  415. if (rsa == NULL) {
  416. /* Decrypt operations are only supported for RSA keys. */
  417. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  418. return ssl_private_key_failure;
  419. }
  420. /* Decrypt with no padding. PKCS#1 padding will be removed as part
  421. * of the timing-sensitive code by the caller. */
  422. if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
  423. return ssl_private_key_failure;
  424. }
  425. return ssl_private_key_success;
  426. }
  427. int ssl_private_key_supports_signature_algorithm(SSL_HANDSHAKE *hs,
  428. uint16_t sigalg) {
  429. SSL *const ssl = hs->ssl;
  430. if (!pkey_supports_algorithm(ssl, hs->local_pubkey, sigalg)) {
  431. return 0;
  432. }
  433. /* Ensure the RSA key is large enough for the hash. RSASSA-PSS requires that
  434. * emLen be at least hLen + sLen + 2. Both hLen and sLen are the size of the
  435. * hash in TLS. Reasonable RSA key sizes are large enough for the largest
  436. * defined RSASSA-PSS algorithm, but 1024-bit RSA is slightly too small for
  437. * SHA-512. 1024-bit RSA is sometimes used for test credentials, so check the
  438. * size so that we can fall back to another algorithm in that case. */
  439. const SSL_SIGNATURE_ALGORITHM *alg = get_signature_algorithm(sigalg);
  440. if (alg->is_rsa_pss &&
  441. (size_t)EVP_PKEY_size(hs->local_pubkey) <
  442. 2 * EVP_MD_size(alg->digest_func()) + 2) {
  443. return 0;
  444. }
  445. /* Newer algorithms require message-based private keys.
  446. * TODO(davidben): Remove this check when sign_digest is gone. */
  447. if (ssl->cert->key_method != NULL &&
  448. ssl->cert->key_method->sign == NULL &&
  449. !legacy_sign_digest_supported(alg)) {
  450. return 0;
  451. }
  452. return 1;
  453. }