s2n_rsa.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "crypto/s2n_rsa.h"
  16. #include <openssl/evp.h>
  17. #include <openssl/rsa.h>
  18. #include <stdint.h>
  19. #include "crypto/s2n_drbg.h"
  20. #include "crypto/s2n_evp_signing.h"
  21. #include "crypto/s2n_hash.h"
  22. #include "crypto/s2n_pkey.h"
  23. #include "crypto/s2n_rsa_signing.h"
  24. #include "error/s2n_errno.h"
  25. #include "stuffer/s2n_stuffer.h"
  26. #include "utils/s2n_blob.h"
  27. #include "utils/s2n_compiler.h"
  28. #include "utils/s2n_random.h"
  29. #include "utils/s2n_result.h"
  30. #include "utils/s2n_safety.h"
  31. RSA *s2n_unsafe_rsa_get_non_const(const struct s2n_rsa_key *rsa_key)
  32. {
  33. PTR_ENSURE_REF(rsa_key);
  34. #ifdef S2N_DIAGNOSTICS_PUSH_SUPPORTED
  35. #pragma GCC diagnostic push
  36. #pragma GCC diagnostic ignored "-Wcast-qual"
  37. #endif
  38. RSA *out_rsa_key = (RSA *) rsa_key->rsa;
  39. #ifdef S2N_DIAGNOSTICS_POP_SUPPORTED
  40. #pragma GCC diagnostic pop
  41. #endif
  42. return out_rsa_key;
  43. }
  44. static S2N_RESULT s2n_rsa_modulus_check(const RSA *rsa)
  45. {
  46. /* RSA was made opaque starting in Openssl 1.1.0 */
  47. #if S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0)
  48. const BIGNUM *n = NULL;
  49. /* RSA still owns the memory for n */
  50. RSA_get0_key(rsa, &n, NULL, NULL);
  51. RESULT_ENSURE_REF(n);
  52. #else
  53. RESULT_ENSURE_REF(rsa->n);
  54. #endif
  55. return S2N_RESULT_OK;
  56. }
  57. static S2N_RESULT s2n_rsa_encrypted_size(const struct s2n_pkey *pkey, uint32_t *size_out)
  58. {
  59. RESULT_ENSURE_REF(pkey);
  60. RESULT_ENSURE_REF(size_out);
  61. const struct s2n_rsa_key *rsa_key = &pkey->key.rsa_key;
  62. RESULT_ENSURE_REF(rsa_key->rsa);
  63. RESULT_GUARD(s2n_rsa_modulus_check(rsa_key->rsa));
  64. const int size = RSA_size(rsa_key->rsa);
  65. RESULT_GUARD_POSIX(size);
  66. *size_out = size;
  67. return S2N_RESULT_OK;
  68. }
  69. static int s2n_rsa_sign(const struct s2n_pkey *priv, s2n_signature_algorithm sig_alg, struct s2n_hash_state *digest,
  70. struct s2n_blob *signature)
  71. {
  72. switch (sig_alg) {
  73. case S2N_SIGNATURE_RSA:
  74. return s2n_rsa_pkcs1v15_sign(priv, digest, signature);
  75. case S2N_SIGNATURE_RSA_PSS_RSAE:
  76. return s2n_rsa_pss_sign(priv, digest, signature);
  77. default:
  78. POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
  79. }
  80. return S2N_SUCCESS;
  81. }
  82. static int s2n_rsa_verify(const struct s2n_pkey *pub, s2n_signature_algorithm sig_alg, struct s2n_hash_state *digest,
  83. struct s2n_blob *signature)
  84. {
  85. switch (sig_alg) {
  86. case S2N_SIGNATURE_RSA:
  87. return s2n_rsa_pkcs1v15_verify(pub, digest, signature);
  88. case S2N_SIGNATURE_RSA_PSS_RSAE:
  89. return s2n_rsa_pss_verify(pub, digest, signature);
  90. default:
  91. POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
  92. }
  93. return S2N_SUCCESS;
  94. }
  95. static int s2n_rsa_encrypt(const struct s2n_pkey *pub, struct s2n_blob *in, struct s2n_blob *out)
  96. {
  97. uint32_t size = 0;
  98. POSIX_GUARD_RESULT(s2n_rsa_encrypted_size(pub, &size));
  99. S2N_ERROR_IF(out->size < size, S2N_ERR_NOMEM);
  100. const s2n_rsa_public_key *pub_key = &pub->key.rsa_key;
  101. /* Safety: RSA_public_encrypt does not mutate the key */
  102. int r = RSA_public_encrypt(in->size, (unsigned char *) in->data, (unsigned char *) out->data,
  103. s2n_unsafe_rsa_get_non_const(pub_key), RSA_PKCS1_PADDING);
  104. POSIX_ENSURE((int64_t) r == (int64_t) out->size, S2N_ERR_SIZE_MISMATCH);
  105. return 0;
  106. }
  107. static int s2n_rsa_decrypt(const struct s2n_pkey *priv, struct s2n_blob *in, struct s2n_blob *out)
  108. {
  109. unsigned char intermediate[4096];
  110. uint32_t expected_size = 0;
  111. POSIX_GUARD_RESULT(s2n_rsa_encrypted_size(priv, &expected_size));
  112. S2N_ERROR_IF(expected_size > sizeof(intermediate), S2N_ERR_NOMEM);
  113. S2N_ERROR_IF(out->size > sizeof(intermediate), S2N_ERR_NOMEM);
  114. POSIX_GUARD_RESULT(s2n_get_public_random_data(out));
  115. const s2n_rsa_private_key *priv_key = &priv->key.rsa_key;
  116. /* Safety: RSA_private_decrypt does not mutate the key */
  117. int r = RSA_private_decrypt(in->size, (unsigned char *) in->data, intermediate,
  118. s2n_unsafe_rsa_get_non_const(priv_key), RSA_NO_PADDING);
  119. POSIX_ENSURE((int64_t) r == (int64_t) expected_size, S2N_ERR_SIZE_MISMATCH);
  120. s2n_constant_time_pkcs1_unpad_or_dont(out->data, intermediate, r, out->size);
  121. return 0;
  122. }
  123. static int s2n_rsa_keys_match(const struct s2n_pkey *pub, const struct s2n_pkey *priv)
  124. {
  125. uint8_t plain_inpad[36] = { 1 }, plain_outpad[36] = { 0 }, encpad[8192];
  126. struct s2n_blob plain_in = { 0 }, plain_out = { 0 }, enc = { 0 };
  127. plain_in.data = plain_inpad;
  128. plain_in.size = sizeof(plain_inpad);
  129. enc.data = encpad;
  130. POSIX_GUARD_RESULT(s2n_rsa_encrypted_size(pub, &enc.size));
  131. POSIX_ENSURE_LTE(enc.size, sizeof(encpad));
  132. POSIX_GUARD(s2n_rsa_encrypt(pub, &plain_in, &enc));
  133. plain_out.data = plain_outpad;
  134. plain_out.size = sizeof(plain_outpad);
  135. POSIX_GUARD(s2n_rsa_decrypt(priv, &enc, &plain_out));
  136. S2N_ERROR_IF(memcmp(plain_in.data, plain_out.data, plain_in.size), S2N_ERR_KEY_MISMATCH);
  137. return 0;
  138. }
  139. static int s2n_rsa_key_free(struct s2n_pkey *pkey)
  140. {
  141. POSIX_ENSURE_REF(pkey);
  142. struct s2n_rsa_key *rsa_key = &pkey->key.rsa_key;
  143. if (rsa_key->rsa == NULL) {
  144. return S2N_SUCCESS;
  145. }
  146. /* Safety: freeing the key owned by this object */
  147. RSA_free(s2n_unsafe_rsa_get_non_const(rsa_key));
  148. rsa_key->rsa = NULL;
  149. return S2N_SUCCESS;
  150. }
  151. static int s2n_rsa_check_key_exists(const struct s2n_pkey *pkey)
  152. {
  153. const struct s2n_rsa_key *rsa_key = &pkey->key.rsa_key;
  154. POSIX_ENSURE_REF(rsa_key->rsa);
  155. return 0;
  156. }
  157. int s2n_evp_pkey_to_rsa_public_key(s2n_rsa_public_key *rsa_key, EVP_PKEY *evp_public_key)
  158. {
  159. const RSA *rsa = EVP_PKEY_get1_RSA(evp_public_key);
  160. S2N_ERROR_IF(rsa == NULL, S2N_ERR_DECODE_CERTIFICATE);
  161. rsa_key->rsa = rsa;
  162. return 0;
  163. }
  164. int s2n_evp_pkey_to_rsa_private_key(s2n_rsa_private_key *rsa_key, EVP_PKEY *evp_private_key)
  165. {
  166. const RSA *rsa = EVP_PKEY_get1_RSA(evp_private_key);
  167. S2N_ERROR_IF(rsa == NULL, S2N_ERR_DECODE_PRIVATE_KEY);
  168. rsa_key->rsa = rsa;
  169. return 0;
  170. }
  171. int s2n_rsa_pkey_init(struct s2n_pkey *pkey)
  172. {
  173. pkey->size = &s2n_rsa_encrypted_size;
  174. pkey->sign = &s2n_rsa_sign;
  175. pkey->verify = &s2n_rsa_verify;
  176. pkey->encrypt = &s2n_rsa_encrypt;
  177. pkey->decrypt = &s2n_rsa_decrypt;
  178. pkey->match = &s2n_rsa_keys_match;
  179. pkey->free = &s2n_rsa_key_free;
  180. pkey->check_key = &s2n_rsa_check_key_exists;
  181. POSIX_GUARD_RESULT(s2n_evp_signing_set_pkey_overrides(pkey));
  182. return 0;
  183. }