Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/rsa_pss.c

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2006.
2 // Copyright (c) 2006 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/x509.h>
6
7 #include <assert.h>
8
9 #include <openssl/asn1.h>
10 #include <openssl/asn1t.h>
11 #include <openssl/bio.h>
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
14 #include <openssl/obj.h>
15
16 #include "internal.h"
17
18
19 static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
20 void *exarg) {
21 if (operation == ASN1_OP_FREE_PRE) {
22 RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval;
23 X509_ALGOR_free(pss->maskHash);
24 }
25 return 1;
26 }
27
28 ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
29 ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR, 0),
30 ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR, 1),
31 ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER, 2),
32 ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3),
33 } ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
34
35 IMPLEMENT_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS)
36
37
38 // Given an MGF1 Algorithm ID decode to an Algorithm Identifier
39 static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg) {
40 if (alg == NULL || alg->parameter == NULL ||
41 OBJ_obj2nid(alg->algorithm) != NID_mgf1 ||
42 alg->parameter->type != V_ASN1_SEQUENCE) {
43 return NULL;
44 }
45
46 const uint8_t *p = alg->parameter->value.sequence->data;
47 int plen = alg->parameter->value.sequence->length;
48 return d2i_X509_ALGOR(NULL, &p, plen);
49 }
50
51 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
52 X509_ALGOR **pmaskHash) {
53 *pmaskHash = NULL;
54
55 if (alg->parameter == NULL || alg->parameter->type != V_ASN1_SEQUENCE) {
56 return NULL;
57 }
58
59 const uint8_t *p = alg->parameter->value.sequence->data;
60 int plen = alg->parameter->value.sequence->length;
61 RSA_PSS_PARAMS *pss = d2i_RSA_PSS_PARAMS(NULL, &p, plen);
62 if (pss == NULL) {
63 return NULL;
64 }
65
66 *pmaskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
67 return pss;
68 }
69
70 // allocate and set algorithm ID from EVP_MD, default SHA1
71 static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md) {
72 if (EVP_MD_type(md) == NID_sha1) {
73 return 1;
74 }
75 *palg = X509_ALGOR_new();
76 if (*palg == NULL) {
77 return 0;
78 }
79 if (!X509_ALGOR_set_md(*palg, md)) {
80 X509_ALGOR_free(*palg);
81 *palg = NULL;
82 return 0;
83 }
84 return 1;
85 }
86
87 // Allocate and set MGF1 algorithm ID from EVP_MD
88 static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) {
89 X509_ALGOR *algtmp = NULL;
90 ASN1_STRING *stmp = NULL;
91 *palg = NULL;
92
93 if (EVP_MD_type(mgf1md) == NID_sha1) {
94 return 1;
95 }
96 // need to embed algorithm ID inside another
97 if (!rsa_md_to_algor(&algtmp, mgf1md) ||
98 !ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) {
99 goto err;
100 }
101 *palg = X509_ALGOR_new();
102 if (!*palg) {
103 goto err;
104 }
105 if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) {
106 goto err;
107 }
108 stmp = NULL;
109
110 err:
111 ASN1_STRING_free(stmp);
112 X509_ALGOR_free(algtmp);
113 if (*palg) {
114 return 1;
115 }
116
117 return 0;
118 }
119
120 // convert algorithm ID to EVP_MD, default SHA1
121 static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg) {
122 const EVP_MD *md;
123 if (!alg) {
124 return EVP_sha1();
125 }
126 md = EVP_get_digestbyobj(alg->algorithm);
127 if (md == NULL) {
128 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
129 }
130 return md;
131 }
132
133 // convert MGF1 algorithm ID to EVP_MD, default SHA1
134 static const EVP_MD *rsa_mgf1_to_md(const X509_ALGOR *alg,
135 X509_ALGOR *maskHash) {
136 const EVP_MD *md;
137 if (!alg) {
138 return EVP_sha1();
139 }
140 // Check mask and lookup mask hash algorithm
141 if (OBJ_obj2nid(alg->algorithm) != NID_mgf1 || maskHash == NULL) {
142 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
143 return NULL;
144 }
145 md = EVP_get_digestbyobj(maskHash->algorithm);
146 if (md == NULL) {
147 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
148 return NULL;
149 }
150 return md;
151 }
152
153 int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
154 const EVP_MD *sigmd, *mgf1md;
155 int saltlen;
156 if (!EVP_PKEY_CTX_get_signature_md(ctx->pctx, &sigmd) ||
157 !EVP_PKEY_CTX_get_rsa_mgf1_md(ctx->pctx, &mgf1md) ||
158 !EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx->pctx, &saltlen)) {
159 return 0;
160 }
161
162 EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(ctx->pctx);
163 if (saltlen == -1) {
164 saltlen = EVP_MD_size(sigmd);
165 } else if (saltlen == -2) {
166 // TODO(davidben): Forbid this mode. The world has largely standardized on
167 // salt length matching hash length.
168 saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
169 if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0) {
170 saltlen--;
171 }
172 } else if (saltlen != (int)EVP_MD_size(sigmd)) {
173 // We only allow salt length matching hash length and, for now, the -2 case.
174 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
175 return 0;
176 }
177
178 int ret = 0;
179 ASN1_STRING *os = NULL;
180 RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
181 if (!pss) {
182 goto err;
183 }
184
185 if (saltlen != 20) {
186 pss->saltLength = ASN1_INTEGER_new();
187 if (!pss->saltLength || !ASN1_INTEGER_set_int64(pss->saltLength, saltlen)) {
188 goto err;
189 }
190 }
191
192 if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd) ||
193 !rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) {
194 goto err;
195 }
196
197 // Finally create string with pss parameter encoding.
198 if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) {
199 goto err;
200 }
201
202 if (!X509_ALGOR_set0(algor, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os)) {
203 goto err;
204 }
205 os = NULL;
206 ret = 1;
207
208 err:
209 RSA_PSS_PARAMS_free(pss);
210 ASN1_STRING_free(os);
211 return ret;
212 }
213
214 int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
215 EVP_PKEY *pkey) {
216 assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
217
218 // Decode PSS parameters
219 int ret = 0;
220 X509_ALGOR *maskHash;
221 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg, &maskHash);
222 if (pss == NULL) {
223 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
224 goto err;
225 }
226
227 const EVP_MD *mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm, maskHash);
228 const EVP_MD *md = rsa_algor_to_md(pss->hashAlgorithm);
229 if (mgf1md == NULL || md == NULL) {
230 goto err;
231 }
232
233 // Check that both signature algorithms are not weak
234 if(!x509_digest_nid_ok(EVP_MD_type(md)) || !x509_digest_nid_ok(EVP_MD_type(mgf1md))) {
235 OPENSSL_PUT_ERROR(X509, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED);
236 goto err;
237 }
238
239 int saltlen = 20;
240 if (pss->saltLength != NULL) {
241 saltlen = ASN1_INTEGER_get(pss->saltLength);
242
243 // Could perform more salt length sanity checks but the main
244 // RSA routines will trap other invalid values anyway.
245 if (saltlen < 0) {
246 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
247 goto err;
248 }
249 }
250
251 // low-level routines support only trailer field 0xbc (value 1)
252 // and PKCS#1 says we should reject any other value anyway.
253 if (pss->trailerField != NULL && ASN1_INTEGER_get(pss->trailerField) != 1) {
254 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
255 goto err;
256 }
257
258 EVP_PKEY_CTX *pctx;
259 if (!EVP_DigestVerifyInit(ctx, &pctx, md, NULL, pkey) ||
260 !EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
261 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, saltlen) ||
262 !EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1md)) {
263 goto err;
264 }
265
266 ret = 1;
267
268 err:
269 RSA_PSS_PARAMS_free(pss);
270 X509_ALGOR_free(maskHash);
271 return ret;
272 }
273
274 int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent,
275 ASN1_PCTX *pctx) {
276 assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
277
278 int rv = 0;
279 X509_ALGOR *maskHash;
280 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg, &maskHash);
281 if (!pss) {
282 if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) {
283 goto err;
284 }
285 rv = 1;
286 goto err;
287 }
288
289 if (BIO_puts(bp, "\n") <= 0 || //
290 !BIO_indent(bp, indent, 128) || //
291 BIO_puts(bp, "Hash Algorithm: ") <= 0) {
292 goto err;
293 }
294
295 if (pss->hashAlgorithm) {
296 if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) {
297 goto err;
298 }
299 } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
300 goto err;
301 }
302
303 if (BIO_puts(bp, "\n") <= 0 || //
304 !BIO_indent(bp, indent, 128) || //
305 BIO_puts(bp, "Mask Algorithm: ") <= 0) {
306 goto err;
307 }
308
309 if (pss->maskGenAlgorithm) {
310 if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 ||
311 BIO_puts(bp, " with ") <= 0) {
312 goto err;
313 }
314
315 if (maskHash) {
316 if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) {
317 goto err;
318 }
319 } else if (BIO_puts(bp, "INVALID") <= 0) {
320 goto err;
321 }
322 } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
323 goto err;
324 }
325 BIO_puts(bp, "\n");
326
327 if (!BIO_indent(bp, indent, 128) || //
328 BIO_puts(bp, "Salt Length: 0x") <= 0) {
329 goto err;
330 }
331
332 if (pss->saltLength) {
333 if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) {
334 goto err;
335 }
336 } else if (BIO_puts(bp, "14 (default)") <= 0) {
337 goto err;
338 }
339 BIO_puts(bp, "\n");
340
341 if (!BIO_indent(bp, indent, 128) || //
342 BIO_puts(bp, "Trailer Field: 0x") <= 0) {
343 goto err;
344 }
345
346 if (pss->trailerField) {
347 if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) {
348 goto err;
349 }
350 } else if (BIO_puts(bp, "BC (default)") <= 0) {
351 goto err;
352 }
353 BIO_puts(bp, "\n");
354
355 rv = 1;
356
357 err:
358 RSA_PSS_PARAMS_free(pss);
359 X509_ALGOR_free(maskHash);
360 return rv;
361 }