xref: /netbsd-src/crypto/external/bsd/openssl/dist/providers/common/securitycheck.c (revision 4778aede4608a995eaeedca856a7a71a2fa5c675)
1b0d17251Schristos /*
2*4778aedeSchristos  * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos  *
4b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6b0d17251Schristos  * in the file LICENSE in the source distribution or at
7b0d17251Schristos  * https://www.openssl.org/source/license.html
8b0d17251Schristos  */
9b0d17251Schristos 
10b0d17251Schristos #include "internal/deprecated.h"
11b0d17251Schristos 
12b0d17251Schristos #include <openssl/rsa.h>
13b0d17251Schristos #include <openssl/dsa.h>
14b0d17251Schristos #include <openssl/dh.h>
15b0d17251Schristos #include <openssl/ec.h>
16b0d17251Schristos #include <openssl/evp.h>
17b0d17251Schristos #include <openssl/err.h>
18b0d17251Schristos #include <openssl/proverr.h>
19b0d17251Schristos #include <openssl/core_names.h>
20b0d17251Schristos #include <openssl/obj_mac.h>
21b0d17251Schristos #include "prov/securitycheck.h"
22b0d17251Schristos 
23b0d17251Schristos /*
24b0d17251Schristos  * FIPS requires a minimum security strength of 112 bits (for encryption or
25b0d17251Schristos  * signing), and for legacy purposes 80 bits (for decryption or verifying).
26b0d17251Schristos  * Set protect = 1 for encryption or signing operations, or 0 otherwise. See
27b0d17251Schristos  * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf.
28b0d17251Schristos  */
ossl_rsa_check_key(OSSL_LIB_CTX * ctx,const RSA * rsa,int operation)29b0d17251Schristos int ossl_rsa_check_key(OSSL_LIB_CTX *ctx, const RSA *rsa, int operation)
30b0d17251Schristos {
31b0d17251Schristos     int protect = 0;
32b0d17251Schristos 
33b0d17251Schristos     switch (operation) {
34b0d17251Schristos         case EVP_PKEY_OP_SIGN:
35b0d17251Schristos             protect = 1;
36b0d17251Schristos             /* fallthrough */
37b0d17251Schristos         case EVP_PKEY_OP_VERIFY:
38b0d17251Schristos             break;
39b0d17251Schristos         case EVP_PKEY_OP_ENCAPSULATE:
40b0d17251Schristos         case EVP_PKEY_OP_ENCRYPT:
41b0d17251Schristos             protect = 1;
42b0d17251Schristos             /* fallthrough */
43b0d17251Schristos         case EVP_PKEY_OP_VERIFYRECOVER:
44b0d17251Schristos         case EVP_PKEY_OP_DECAPSULATE:
45b0d17251Schristos         case EVP_PKEY_OP_DECRYPT:
46b0d17251Schristos             if (RSA_test_flags(rsa,
47b0d17251Schristos                                RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSASSAPSS) {
48b0d17251Schristos                 ERR_raise_data(ERR_LIB_PROV,
49b0d17251Schristos                                PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
50b0d17251Schristos                                "operation: %d", operation);
51b0d17251Schristos                 return 0;
52b0d17251Schristos             }
53b0d17251Schristos             break;
54b0d17251Schristos         default:
55b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
56b0d17251Schristos                            "invalid operation: %d", operation);
57b0d17251Schristos             return 0;
58b0d17251Schristos     }
59b0d17251Schristos 
60b0d17251Schristos #if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
61b0d17251Schristos     if (ossl_securitycheck_enabled(ctx)) {
62b0d17251Schristos         int sz = RSA_bits(rsa);
63b0d17251Schristos 
64b0d17251Schristos         if (protect ? (sz < 2048) : (sz < 1024)) {
65b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH,
66b0d17251Schristos                            "operation: %d", operation);
67b0d17251Schristos             return 0;
68b0d17251Schristos         }
69b0d17251Schristos     }
70b0d17251Schristos #else
71b0d17251Schristos     /* make protect used */
72b0d17251Schristos     (void)protect;
73b0d17251Schristos #endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
74b0d17251Schristos     return 1;
75b0d17251Schristos }
76b0d17251Schristos 
77b0d17251Schristos #ifndef OPENSSL_NO_EC
78b0d17251Schristos /*
79b0d17251Schristos  * In FIPS mode:
80b0d17251Schristos  * protect should be 1 for any operations that need 112 bits of security
81b0d17251Schristos  * strength (such as signing, and key exchange), or 0 for operations that allow
82b0d17251Schristos  * a lower security strength (such as verify).
83b0d17251Schristos  *
84b0d17251Schristos  * For ECDH key agreement refer to SP800-56A
85b0d17251Schristos  * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
86b0d17251Schristos  * "Appendix D"
87b0d17251Schristos  *
88b0d17251Schristos  * For ECDSA signatures refer to
89b0d17251Schristos  * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
90b0d17251Schristos  * "Table 2"
91b0d17251Schristos  */
ossl_ec_check_key(OSSL_LIB_CTX * ctx,const EC_KEY * ec,int protect)92b0d17251Schristos int ossl_ec_check_key(OSSL_LIB_CTX *ctx, const EC_KEY *ec, int protect)
93b0d17251Schristos {
94b0d17251Schristos # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
95b0d17251Schristos     if (ossl_securitycheck_enabled(ctx)) {
96b0d17251Schristos         int nid, strength;
97b0d17251Schristos         const char *curve_name;
98b0d17251Schristos         const EC_GROUP *group = EC_KEY_get0_group(ec);
99b0d17251Schristos 
100b0d17251Schristos         if (group == NULL) {
101b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE, "No group");
102b0d17251Schristos             return 0;
103b0d17251Schristos         }
104b0d17251Schristos         nid = EC_GROUP_get_curve_name(group);
105b0d17251Schristos         if (nid == NID_undef) {
106b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE,
107b0d17251Schristos                            "Explicit curves are not allowed in fips mode");
108b0d17251Schristos             return 0;
109b0d17251Schristos         }
110b0d17251Schristos 
111b0d17251Schristos         curve_name = EC_curve_nid2nist(nid);
112b0d17251Schristos         if (curve_name == NULL) {
113b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE,
114b0d17251Schristos                            "Curve %s is not approved in FIPS mode", curve_name);
115b0d17251Schristos             return 0;
116b0d17251Schristos         }
117b0d17251Schristos 
118b0d17251Schristos         /*
119b0d17251Schristos          * For EC the security strength is the (order_bits / 2)
120b0d17251Schristos          * e.g. P-224 is 112 bits.
121b0d17251Schristos          */
122b0d17251Schristos         strength = EC_GROUP_order_bits(group) / 2;
123b0d17251Schristos         /* The min security strength allowed for legacy verification is 80 bits */
124b0d17251Schristos         if (strength < 80) {
125b0d17251Schristos             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
126b0d17251Schristos             return 0;
127b0d17251Schristos         }
128b0d17251Schristos 
129b0d17251Schristos         /*
130b0d17251Schristos          * For signing or key agreement only allow curves with at least 112 bits of
131b0d17251Schristos          * security strength
132b0d17251Schristos          */
133b0d17251Schristos         if (protect && strength < 112) {
134b0d17251Schristos             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_CURVE,
135b0d17251Schristos                            "Curve %s cannot be used for signing", curve_name);
136b0d17251Schristos             return 0;
137b0d17251Schristos         }
138b0d17251Schristos     }
139b0d17251Schristos # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
140b0d17251Schristos     return 1;
141b0d17251Schristos }
142b0d17251Schristos #endif /* OPENSSL_NO_EC */
143b0d17251Schristos 
144b0d17251Schristos #ifndef OPENSSL_NO_DSA
145b0d17251Schristos /*
146b0d17251Schristos  * Check for valid key sizes if fips mode. Refer to
147b0d17251Schristos  * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
148b0d17251Schristos  * "Table 2"
149b0d17251Schristos  */
ossl_dsa_check_key(OSSL_LIB_CTX * ctx,const DSA * dsa,int sign)150b0d17251Schristos int ossl_dsa_check_key(OSSL_LIB_CTX *ctx, const DSA *dsa, int sign)
151b0d17251Schristos {
152b0d17251Schristos # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
153b0d17251Schristos     if (ossl_securitycheck_enabled(ctx)) {
154b0d17251Schristos         size_t L, N;
155b0d17251Schristos         const BIGNUM *p, *q;
156b0d17251Schristos 
157b0d17251Schristos         if (dsa == NULL)
158b0d17251Schristos             return 0;
159b0d17251Schristos 
160b0d17251Schristos         p = DSA_get0_p(dsa);
161b0d17251Schristos         q = DSA_get0_q(dsa);
162b0d17251Schristos         if (p == NULL || q == NULL)
163b0d17251Schristos             return 0;
164b0d17251Schristos 
165b0d17251Schristos         L = BN_num_bits(p);
166b0d17251Schristos         N = BN_num_bits(q);
167b0d17251Schristos 
168b0d17251Schristos         /*
169b0d17251Schristos          * For Digital signature verification DSA keys with < 112 bits of
170*4778aedeSchristos          * security strength, are still allowed for legacy
171*4778aedeSchristos          * use. The bounds given in SP 800-131Ar2 - Table 2 are
172*4778aedeSchristos          * (512 <= L < 2048 or 160 <= N < 224).
173*4778aedeSchristos          *
174*4778aedeSchristos          * We are a little stricter and insist that both minimums are met.
175*4778aedeSchristos          * For example a L = 256, N = 160 key *would* be allowed by SP 800-131Ar2
176*4778aedeSchristos          * but we don't.
177b0d17251Schristos          */
178*4778aedeSchristos         if (!sign) {
179*4778aedeSchristos             if (L < 512 || N < 160)
180*4778aedeSchristos                 return 0;
181*4778aedeSchristos             if (L < 2048 || N < 224)
182*4778aedeSchristos                 return 1;
183*4778aedeSchristos         }
184b0d17251Schristos 
185b0d17251Schristos          /* Valid sizes for both sign and verify */
186*4778aedeSchristos         if (L == 2048 && (N == 224 || N == 256))    /* 112 bits */
187b0d17251Schristos             return 1;
188*4778aedeSchristos         return (L == 3072 && N == 256);             /* 128 bits */
189b0d17251Schristos     }
190b0d17251Schristos # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
191b0d17251Schristos     return 1;
192b0d17251Schristos }
193b0d17251Schristos #endif /* OPENSSL_NO_DSA */
194b0d17251Schristos 
195b0d17251Schristos #ifndef OPENSSL_NO_DH
196b0d17251Schristos /*
197b0d17251Schristos  * For DH key agreement refer to SP800-56A
198b0d17251Schristos  * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf
199b0d17251Schristos  * "Section 5.5.1.1FFC Domain Parameter Selection/Generation" and
200b0d17251Schristos  * "Appendix D" FFC Safe-prime Groups
201b0d17251Schristos  */
ossl_dh_check_key(OSSL_LIB_CTX * ctx,const DH * dh)202b0d17251Schristos int ossl_dh_check_key(OSSL_LIB_CTX *ctx, const DH *dh)
203b0d17251Schristos {
204b0d17251Schristos # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
205b0d17251Schristos     if (ossl_securitycheck_enabled(ctx)) {
206b0d17251Schristos         size_t L, N;
207b0d17251Schristos         const BIGNUM *p, *q;
208b0d17251Schristos 
209b0d17251Schristos         if (dh == NULL)
210b0d17251Schristos             return 0;
211b0d17251Schristos 
212b0d17251Schristos         p = DH_get0_p(dh);
213b0d17251Schristos         q = DH_get0_q(dh);
214b0d17251Schristos         if (p == NULL || q == NULL)
215b0d17251Schristos             return 0;
216b0d17251Schristos 
217b0d17251Schristos         L = BN_num_bits(p);
218b0d17251Schristos         if (L < 2048)
219b0d17251Schristos             return 0;
220b0d17251Schristos 
221b0d17251Schristos         /* If it is a safe prime group then it is ok */
222b0d17251Schristos         if (DH_get_nid(dh))
223b0d17251Schristos             return 1;
224b0d17251Schristos 
225b0d17251Schristos         /* If not then it must be FFC, which only allows certain sizes. */
226b0d17251Schristos         N = BN_num_bits(q);
227b0d17251Schristos 
228b0d17251Schristos         return (L == 2048 && (N == 224 || N == 256));
229b0d17251Schristos     }
230b0d17251Schristos # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
231b0d17251Schristos     return 1;
232b0d17251Schristos }
233b0d17251Schristos #endif /* OPENSSL_NO_DH */
234b0d17251Schristos 
ossl_digest_get_approved_nid_with_sha1(OSSL_LIB_CTX * ctx,const EVP_MD * md,int sha1_allowed)235b0d17251Schristos int ossl_digest_get_approved_nid_with_sha1(OSSL_LIB_CTX *ctx, const EVP_MD *md,
236b0d17251Schristos                                            int sha1_allowed)
237b0d17251Schristos {
238b0d17251Schristos     int mdnid = ossl_digest_get_approved_nid(md);
239b0d17251Schristos 
240b0d17251Schristos # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
241b0d17251Schristos     if (ossl_securitycheck_enabled(ctx)) {
242b0d17251Schristos         if (mdnid == NID_undef || (mdnid == NID_sha1 && !sha1_allowed))
243b0d17251Schristos             mdnid = -1; /* disallowed by security checks */
244b0d17251Schristos     }
245b0d17251Schristos # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
246b0d17251Schristos     return mdnid;
247b0d17251Schristos }
248b0d17251Schristos 
ossl_digest_is_allowed(OSSL_LIB_CTX * ctx,const EVP_MD * md)249b0d17251Schristos int ossl_digest_is_allowed(OSSL_LIB_CTX *ctx, const EVP_MD *md)
250b0d17251Schristos {
251b0d17251Schristos # if !defined(OPENSSL_NO_FIPS_SECURITYCHECKS)
252b0d17251Schristos     if (ossl_securitycheck_enabled(ctx))
253b0d17251Schristos         return ossl_digest_get_approved_nid(md) != NID_undef;
254b0d17251Schristos # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
255b0d17251Schristos     return 1;
256b0d17251Schristos }
257