s2n_evp_signing.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_evp_signing.h"
  16. #include "crypto/s2n_evp.h"
  17. #include "crypto/s2n_pkey.h"
  18. #include "crypto/s2n_rsa_pss.h"
  19. #include "error/s2n_errno.h"
  20. #include "utils/s2n_safety.h"
  21. DEFINE_POINTER_CLEANUP_FUNC(EVP_PKEY_CTX *, EVP_PKEY_CTX_free);
  22. /*
  23. * FIPS 140-3 requires that we don't pass raw digest bytes to the libcrypto signing methods.
  24. * In order to do that, we need to use signing methods that both calculate the digest and
  25. * perform the signature.
  26. */
  27. static S2N_RESULT s2n_evp_md_ctx_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx)
  28. {
  29. #ifdef S2N_LIBCRYPTO_SUPPORTS_EVP_MD_CTX_SET_PKEY_CTX
  30. EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
  31. return S2N_RESULT_OK;
  32. #else
  33. RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
  34. #endif
  35. }
  36. static S2N_RESULT s2n_evp_pkey_set_rsa_pss_saltlen(EVP_PKEY_CTX *pctx)
  37. {
  38. #if RSA_PSS_SIGNING_SUPPORTED
  39. RESULT_GUARD_OSSL(EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST), S2N_ERR_PKEY_CTX_INIT);
  40. return S2N_RESULT_OK;
  41. #else
  42. RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
  43. #endif
  44. }
  45. bool s2n_evp_signing_supported()
  46. {
  47. #ifdef S2N_LIBCRYPTO_SUPPORTS_EVP_MD_CTX_SET_PKEY_CTX
  48. /* We can only use EVP signing if the hash state has an EVP_MD_CTX
  49. * that we can pass to the EVP signing methods.
  50. */
  51. return s2n_hash_evp_fully_supported();
  52. #else
  53. return false;
  54. #endif
  55. }
  56. /* If using EVP signing, override the sign and verify pkey methods.
  57. * The EVP methods can handle all pkey types / signature algorithms.
  58. */
  59. S2N_RESULT s2n_evp_signing_set_pkey_overrides(struct s2n_pkey *pkey)
  60. {
  61. if (s2n_evp_signing_supported()) {
  62. RESULT_ENSURE_REF(pkey);
  63. pkey->sign = &s2n_evp_sign;
  64. pkey->verify = &s2n_evp_verify;
  65. }
  66. return S2N_RESULT_OK;
  67. }
  68. static S2N_RESULT s2n_evp_signing_validate_hash_alg(s2n_signature_algorithm sig_alg, s2n_hash_algorithm hash_alg)
  69. {
  70. switch (hash_alg) {
  71. case S2N_HASH_NONE:
  72. case S2N_HASH_MD5:
  73. /* MD5 alone is never supported */
  74. RESULT_BAIL(S2N_ERR_HASH_INVALID_ALGORITHM);
  75. break;
  76. case S2N_HASH_MD5_SHA1:
  77. /* Only RSA supports MD5+SHA1.
  78. * This should not be a problem, as we only allow MD5+SHA1 when
  79. * falling back to TLS1.0 or 1.1, which only support RSA.
  80. */
  81. RESULT_ENSURE(sig_alg == S2N_SIGNATURE_RSA, S2N_ERR_HASH_INVALID_ALGORITHM);
  82. break;
  83. default:
  84. break;
  85. }
  86. /* Hash algorithm must be recognized and supported by EVP_MD */
  87. RESULT_ENSURE(s2n_hash_alg_to_evp_md(hash_alg) != NULL, S2N_ERR_HASH_INVALID_ALGORITHM);
  88. return S2N_RESULT_OK;
  89. }
  90. int s2n_evp_sign(const struct s2n_pkey *priv, s2n_signature_algorithm sig_alg,
  91. struct s2n_hash_state *hash_state, struct s2n_blob *signature)
  92. {
  93. POSIX_ENSURE_REF(priv);
  94. POSIX_ENSURE_REF(hash_state);
  95. POSIX_ENSURE_REF(signature);
  96. POSIX_ENSURE(s2n_evp_signing_supported(), S2N_ERR_HASH_NOT_READY);
  97. POSIX_GUARD_RESULT(s2n_evp_signing_validate_hash_alg(sig_alg, hash_state->alg));
  98. DEFER_CLEANUP(EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(priv->pkey, NULL), EVP_PKEY_CTX_free_pointer);
  99. POSIX_ENSURE_REF(pctx);
  100. POSIX_GUARD_OSSL(EVP_PKEY_sign_init(pctx), S2N_ERR_PKEY_CTX_INIT);
  101. POSIX_GUARD_OSSL(S2N_EVP_PKEY_CTX_set_signature_md(pctx, s2n_hash_alg_to_evp_md(hash_state->alg)), S2N_ERR_PKEY_CTX_INIT);
  102. if (sig_alg == S2N_SIGNATURE_RSA_PSS_RSAE || sig_alg == S2N_SIGNATURE_RSA_PSS_PSS) {
  103. POSIX_GUARD_OSSL(EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING), S2N_ERR_PKEY_CTX_INIT);
  104. POSIX_GUARD_RESULT(s2n_evp_pkey_set_rsa_pss_saltlen(pctx));
  105. }
  106. EVP_MD_CTX *ctx = hash_state->digest.high_level.evp.ctx;
  107. POSIX_ENSURE_REF(ctx);
  108. POSIX_GUARD_RESULT(s2n_evp_md_ctx_set_pkey_ctx(ctx, pctx));
  109. size_t signature_size = signature->size;
  110. POSIX_GUARD_OSSL(EVP_DigestSignFinal(ctx, signature->data, &signature_size), S2N_ERR_SIGN);
  111. POSIX_ENSURE(signature_size <= signature->size, S2N_ERR_SIZE_MISMATCH);
  112. signature->size = signature_size;
  113. POSIX_GUARD_RESULT(s2n_evp_md_ctx_set_pkey_ctx(ctx, NULL));
  114. return S2N_SUCCESS;
  115. }
  116. int s2n_evp_verify(const struct s2n_pkey *pub, s2n_signature_algorithm sig_alg,
  117. struct s2n_hash_state *hash_state, struct s2n_blob *signature)
  118. {
  119. POSIX_ENSURE_REF(pub);
  120. POSIX_ENSURE_REF(hash_state);
  121. POSIX_ENSURE_REF(signature);
  122. POSIX_ENSURE(s2n_evp_signing_supported(), S2N_ERR_HASH_NOT_READY);
  123. POSIX_GUARD_RESULT(s2n_evp_signing_validate_hash_alg(sig_alg, hash_state->alg));
  124. DEFER_CLEANUP(EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pub->pkey, NULL), EVP_PKEY_CTX_free_pointer);
  125. POSIX_ENSURE_REF(pctx);
  126. POSIX_GUARD_OSSL(EVP_PKEY_verify_init(pctx), S2N_ERR_PKEY_CTX_INIT);
  127. POSIX_GUARD_OSSL(S2N_EVP_PKEY_CTX_set_signature_md(pctx, s2n_hash_alg_to_evp_md(hash_state->alg)), S2N_ERR_PKEY_CTX_INIT);
  128. if (sig_alg == S2N_SIGNATURE_RSA_PSS_RSAE || sig_alg == S2N_SIGNATURE_RSA_PSS_PSS) {
  129. POSIX_GUARD_OSSL(EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING), S2N_ERR_PKEY_CTX_INIT);
  130. POSIX_GUARD_RESULT(s2n_evp_pkey_set_rsa_pss_saltlen(pctx));
  131. }
  132. EVP_MD_CTX *ctx = hash_state->digest.high_level.evp.ctx;
  133. POSIX_ENSURE_REF(ctx);
  134. POSIX_GUARD_RESULT(s2n_evp_md_ctx_set_pkey_ctx(ctx, pctx));
  135. POSIX_GUARD_OSSL(EVP_DigestVerifyFinal(ctx, signature->data, signature->size), S2N_ERR_VERIFY_SIGNATURE);
  136. POSIX_GUARD_RESULT(s2n_evp_md_ctx_set_pkey_ctx(ctx, NULL));
  137. return S2N_SUCCESS;
  138. }