1
0

securitycheck_fips.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/deprecated.h"
  10. #include <openssl/rsa.h>
  11. #include <openssl/dsa.h>
  12. #include <openssl/dh.h>
  13. #include <openssl/ec.h>
  14. #include <openssl/err.h>
  15. #include <openssl/proverr.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/obj_mac.h>
  18. #include "prov/securitycheck.h"
  19. int ossl_fips_config_securitycheck_enabled(OSSL_LIB_CTX *libctx)
  20. {
  21. #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
  22. return ossl_fips_config_security_checks(libctx);
  23. #else
  24. return 0;
  25. #endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
  26. }
  27. int ossl_digest_rsa_sign_get_md_nid(const EVP_MD *md)
  28. {
  29. return ossl_digest_get_approved_nid(md);
  30. }
  31. int ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND *ind, int id,
  32. OSSL_LIB_CTX *libctx,
  33. const RSA *rsa, const char *desc, int protect)
  34. {
  35. int key_approved = ossl_rsa_check_key_size(rsa, protect);
  36. if (!key_approved) {
  37. if (!ossl_FIPS_IND_on_unapproved(ind, id, libctx, desc, "Key size",
  38. ossl_fips_config_securitycheck_enabled)) {
  39. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH,
  40. "operation: %s", desc);
  41. return 0;
  42. }
  43. }
  44. return 1;
  45. }
  46. # ifndef OPENSSL_NO_EC
  47. int ossl_fips_ind_ec_key_check(OSSL_FIPS_IND *ind, int id,
  48. OSSL_LIB_CTX *libctx,
  49. const EC_GROUP *group, const char *desc,
  50. int protect)
  51. {
  52. int curve_allowed, strength_allowed;
  53. if (group == NULL)
  54. return 0;
  55. curve_allowed = ossl_ec_check_curve_allowed(group);
  56. strength_allowed = ossl_ec_check_security_strength(group, protect);
  57. if (!strength_allowed || !curve_allowed) {
  58. if (!ossl_FIPS_IND_on_unapproved(ind, id, libctx, desc, "EC Key",
  59. ossl_fips_config_securitycheck_enabled)) {
  60. if (!curve_allowed)
  61. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
  62. if (!strength_allowed)
  63. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  64. return 0;
  65. }
  66. }
  67. return 1;
  68. }
  69. #endif
  70. int ossl_fips_ind_digest_exch_check(OSSL_FIPS_IND *ind, int id,
  71. OSSL_LIB_CTX *libctx,
  72. const EVP_MD *md, const char *desc)
  73. {
  74. int nid = ossl_digest_get_approved_nid(md);
  75. int approved = (nid != NID_undef && nid != NID_sha1);
  76. if (!approved) {
  77. if (!ossl_FIPS_IND_on_unapproved(ind, id, libctx, desc, "Digest",
  78. ossl_fips_config_securitycheck_enabled)) {
  79. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
  80. return 0;
  81. }
  82. }
  83. return 1;
  84. }
  85. int ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND *ind, int id,
  86. OSSL_LIB_CTX *libctx,
  87. int nid, int sha1_allowed,
  88. int sha512_trunc_allowed,
  89. const char *desc,
  90. OSSL_FIPS_IND_CHECK_CB *config_check_f)
  91. {
  92. int approved;
  93. const char *op = "none";
  94. switch (nid) {
  95. case NID_undef:
  96. approved = 0;
  97. break;
  98. case NID_sha512_224:
  99. case NID_sha512_256:
  100. approved = sha512_trunc_allowed;
  101. op = "Digest Truncated SHA512";
  102. break;
  103. case NID_sha1:
  104. approved = sha1_allowed;
  105. op = "Digest SHA1";
  106. break;
  107. default:
  108. approved = 1;
  109. break;
  110. }
  111. if (!approved) {
  112. if (!ossl_FIPS_IND_on_unapproved(ind, id, libctx, desc, op,
  113. config_check_f)) {
  114. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
  115. return 0;
  116. }
  117. }
  118. return 1;
  119. }