s2n_evp.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.h"
  16. #include "crypto/s2n_fips.h"
  17. #include "error/s2n_errno.h"
  18. #include "utils/s2n_safety.h"
  19. int s2n_digest_allow_md5_for_fips(struct s2n_evp_digest *evp_digest)
  20. {
  21. POSIX_ENSURE_REF(evp_digest);
  22. /* This is only to be used for EVP digests that will require MD5 to be used
  23. * to comply with the TLS 1.0 and 1.1 RFC's for the PRF. MD5 cannot be used
  24. * outside of the TLS 1.0 and 1.1 PRF when in FIPS mode.
  25. */
  26. S2N_ERROR_IF(!s2n_is_in_fips_mode() || (evp_digest->ctx == NULL), S2N_ERR_ALLOW_MD5_FOR_FIPS_FAILED);
  27. #if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_IS_AWSLC)
  28. EVP_MD_CTX_set_flags(evp_digest->ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  29. #endif
  30. return S2N_SUCCESS;
  31. }
  32. S2N_RESULT s2n_digest_is_md5_allowed_for_fips(struct s2n_evp_digest *evp_digest, bool *out)
  33. {
  34. RESULT_ENSURE_REF(out);
  35. *out = false;
  36. #if !defined(OPENSSL_IS_BORINGSSL) && !defined(OPENSSL_IS_AWSLC)
  37. if (s2n_is_in_fips_mode() && evp_digest && evp_digest->ctx && EVP_MD_CTX_test_flags(evp_digest->ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW)) {
  38. /* s2n is in FIPS mode and the EVP digest allows MD5. */
  39. *out = true;
  40. }
  41. #else
  42. if (s2n_is_in_fips_mode()) {
  43. /* If s2n is in FIPS mode and built with AWS-LC or BoringSSL, there are no flags to check in the EVP digest to allow MD5. */
  44. *out = true;
  45. }
  46. #endif
  47. return S2N_RESULT_OK;
  48. }