1 /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
2 * project 2006.
3 */
4 /* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * [email protected].
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * ([email protected]). This product includes software written by Tim
54 * Hudson ([email protected]). */
55
56 #include <openssl/x509.h>
57
58 #include <assert.h>
59 #include <limits.h>
60
61 #include <openssl/asn1.h>
62 #include <openssl/asn1t.h>
63 #include <openssl/bio.h>
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/obj.h>
67
68 #include "internal.h"
69
70
rsa_pss_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)71 static int rsa_pss_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
72 void *exarg) {
73 if (operation == ASN1_OP_FREE_PRE) {
74 RSA_PSS_PARAMS *pss = (RSA_PSS_PARAMS *)*pval;
75 X509_ALGOR_free(pss->maskHash);
76 }
77 return 1;
78 }
79
80 ASN1_SEQUENCE_cb(RSA_PSS_PARAMS, rsa_pss_cb) = {
81 ASN1_EXP_OPT(RSA_PSS_PARAMS, hashAlgorithm, X509_ALGOR, 0),
82 ASN1_EXP_OPT(RSA_PSS_PARAMS, maskGenAlgorithm, X509_ALGOR, 1),
83 ASN1_EXP_OPT(RSA_PSS_PARAMS, saltLength, ASN1_INTEGER, 2),
84 ASN1_EXP_OPT(RSA_PSS_PARAMS, trailerField, ASN1_INTEGER, 3),
85 } ASN1_SEQUENCE_END_cb(RSA_PSS_PARAMS, RSA_PSS_PARAMS)
86
87 IMPLEMENT_ASN1_FUNCTIONS_const(RSA_PSS_PARAMS)
88
89
90 // Given an MGF1 Algorithm ID decode to an Algorithm Identifier
91 static X509_ALGOR *rsa_mgf1_decode(const X509_ALGOR *alg) {
92 if (OBJ_obj2nid(alg->algorithm) != NID_mgf1 ||
93 alg->parameter == NULL ||
94 alg->parameter->type != V_ASN1_SEQUENCE) {
95 return NULL;
96 }
97
98 const uint8_t *p = alg->parameter->value.sequence->data;
99 int plen = alg->parameter->value.sequence->length;
100 return d2i_X509_ALGOR(NULL, &p, plen);
101 }
102
rsa_pss_decode(const X509_ALGOR * alg)103 static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg) {
104 if (alg->parameter == NULL || alg->parameter->type != V_ASN1_SEQUENCE) {
105 return NULL;
106 }
107
108 const uint8_t *p = alg->parameter->value.sequence->data;
109 int plen = alg->parameter->value.sequence->length;
110 return d2i_RSA_PSS_PARAMS(NULL, &p, plen);
111 }
112
is_allowed_pss_md(const EVP_MD * md)113 static int is_allowed_pss_md(const EVP_MD *md) {
114 int md_type = EVP_MD_type(md);
115 return md_type == NID_sha256 || md_type == NID_sha384 ||
116 md_type == NID_sha512;
117 }
118
119 // rsa_md_to_algor sets |*palg| to an |X509_ALGOR| describing the digest |md|,
120 // which must be an allowed PSS digest.
rsa_md_to_algor(X509_ALGOR ** palg,const EVP_MD * md)121 static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md) {
122 // SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1.
123 assert(is_allowed_pss_md(md));
124 *palg = X509_ALGOR_new();
125 if (*palg == NULL) {
126 return 0;
127 }
128 if (!X509_ALGOR_set_md(*palg, md)) {
129 X509_ALGOR_free(*palg);
130 *palg = NULL;
131 return 0;
132 }
133 return 1;
134 }
135
136 // rsa_md_to_mgf1 sets |*palg| to an |X509_ALGOR| describing MGF-1 with the
137 // digest |mgf1md|, which must be an allowed PSS digest.
rsa_md_to_mgf1(X509_ALGOR ** palg,const EVP_MD * mgf1md)138 static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) {
139 // SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1.
140 assert(is_allowed_pss_md(mgf1md));
141 X509_ALGOR *algtmp = NULL;
142 ASN1_STRING *stmp = NULL;
143 // need to embed algorithm ID inside another
144 if (!rsa_md_to_algor(&algtmp, mgf1md) ||
145 !ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) {
146 goto err;
147 }
148 *palg = X509_ALGOR_new();
149 if (!*palg) {
150 goto err;
151 }
152 if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) {
153 goto err;
154 }
155 stmp = NULL;
156
157 err:
158 ASN1_STRING_free(stmp);
159 X509_ALGOR_free(algtmp);
160 if (*palg) {
161 return 1;
162 }
163
164 return 0;
165 }
166
rsa_algor_to_md(const X509_ALGOR * alg)167 static const EVP_MD *rsa_algor_to_md(const X509_ALGOR *alg) {
168 if (!alg) {
169 // If omitted, PSS defaults to SHA-1, which we do not allow.
170 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
171 return NULL;
172 }
173 const EVP_MD *md = EVP_get_digestbyobj(alg->algorithm);
174 if (md == NULL || !is_allowed_pss_md(md)) {
175 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
176 return NULL;
177 }
178 return md;
179 }
180
rsa_mgf1_to_md(const X509_ALGOR * alg)181 static const EVP_MD *rsa_mgf1_to_md(const X509_ALGOR *alg) {
182 if (!alg) {
183 // If omitted, PSS defaults to MGF-1 with SHA-1, which we do not allow.
184 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
185 return NULL;
186 }
187 // Check mask and lookup mask hash algorithm.
188 X509_ALGOR *maskHash = rsa_mgf1_decode(alg);
189 if (maskHash == NULL) {
190 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
191 return NULL;
192 }
193 const EVP_MD *ret = rsa_algor_to_md(maskHash);
194 X509_ALGOR_free(maskHash);
195 return ret;
196 }
197
x509_rsa_ctx_to_pss(EVP_MD_CTX * ctx,X509_ALGOR * algor)198 int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
199 const EVP_MD *sigmd, *mgf1md;
200 int saltlen;
201 if (!EVP_PKEY_CTX_get_signature_md(ctx->pctx, &sigmd) ||
202 !EVP_PKEY_CTX_get_rsa_mgf1_md(ctx->pctx, &mgf1md) ||
203 !EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx->pctx, &saltlen)) {
204 return 0;
205 }
206
207 if (sigmd != mgf1md || !is_allowed_pss_md(sigmd)) {
208 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
209 return 0;
210 }
211 int md_len = (int)EVP_MD_size(sigmd);
212 if (saltlen == -1) {
213 saltlen = md_len;
214 } else if (saltlen != md_len) {
215 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
216 return 0;
217 }
218
219 int ret = 0;
220 ASN1_STRING *os = NULL;
221 RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
222 if (!pss) {
223 goto err;
224 }
225
226 // The DEFAULT value is 20, but this does not match any supported digest.
227 assert(saltlen != 20);
228 pss->saltLength = ASN1_INTEGER_new();
229 if (!pss->saltLength || //
230 !ASN1_INTEGER_set_int64(pss->saltLength, saltlen)) {
231 goto err;
232 }
233
234 if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd) ||
235 !rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) {
236 goto err;
237 }
238
239 // Finally create string with pss parameter encoding.
240 if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) {
241 goto err;
242 }
243
244 if (!X509_ALGOR_set0(algor, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os)) {
245 goto err;
246 }
247 os = NULL;
248 ret = 1;
249
250 err:
251 RSA_PSS_PARAMS_free(pss);
252 ASN1_STRING_free(os);
253 return ret;
254 }
255
x509_rsa_pss_to_ctx(EVP_MD_CTX * ctx,const X509_ALGOR * sigalg,EVP_PKEY * pkey)256 int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
257 EVP_PKEY *pkey) {
258 assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
259
260 // Decode PSS parameters
261 int ret = 0;
262 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
263 if (pss == NULL) {
264 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
265 goto err;
266 }
267
268 const EVP_MD *mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm);
269 const EVP_MD *md = rsa_algor_to_md(pss->hashAlgorithm);
270 if (mgf1md == NULL || md == NULL) {
271 goto err;
272 }
273
274 // We require the MGF-1 and signing hashes to match.
275 if (mgf1md != md) {
276 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
277 goto err;
278 }
279
280 // We require the salt length be the hash length. The DEFAULT value is 20, but
281 // this does not match any supported salt length.
282 uint64_t salt_len = 0;
283 if (pss->saltLength == NULL ||
284 !ASN1_INTEGER_get_uint64(&salt_len, pss->saltLength) ||
285 salt_len != EVP_MD_size(md)) {
286 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
287 goto err;
288 }
289 assert(salt_len <= INT_MAX);
290
291 // The trailer field must be 1 (0xbc). This value is DEFAULT, so the structure
292 // is required to omit it in DER. Although a syntax error, we also tolerate an
293 // explicitly-encoded value. See the certificates in cl/362617931.
294 if (pss->trailerField != NULL && ASN1_INTEGER_get(pss->trailerField) != 1) {
295 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
296 goto err;
297 }
298
299 EVP_PKEY_CTX *pctx;
300 if (!EVP_DigestVerifyInit(ctx, &pctx, md, NULL, pkey) ||
301 !EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
302 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, (int)salt_len) ||
303 !EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1md)) {
304 goto err;
305 }
306
307 ret = 1;
308
309 err:
310 RSA_PSS_PARAMS_free(pss);
311 return ret;
312 }
313
x509_print_rsa_pss_params(BIO * bp,const X509_ALGOR * sigalg,int indent,ASN1_PCTX * pctx)314 int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent,
315 ASN1_PCTX *pctx) {
316 assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
317
318 int rv = 0;
319 X509_ALGOR *maskHash = NULL;
320 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
321 if (!pss) {
322 if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) {
323 goto err;
324 }
325 rv = 1;
326 goto err;
327 }
328
329 if (BIO_puts(bp, "\n") <= 0 || //
330 !BIO_indent(bp, indent, 128) || //
331 BIO_puts(bp, "Hash Algorithm: ") <= 0) {
332 goto err;
333 }
334
335 if (pss->hashAlgorithm) {
336 if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) {
337 goto err;
338 }
339 } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
340 goto err;
341 }
342
343 if (BIO_puts(bp, "\n") <= 0 || //
344 !BIO_indent(bp, indent, 128) || //
345 BIO_puts(bp, "Mask Algorithm: ") <= 0) {
346 goto err;
347 }
348
349 if (pss->maskGenAlgorithm) {
350 maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
351 if (maskHash == NULL) {
352 if (BIO_puts(bp, "INVALID") <= 0) {
353 goto err;
354 }
355 } else {
356 if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 ||
357 BIO_puts(bp, " with ") <= 0 ||
358 i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) {
359 goto err;
360 }
361 }
362 } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
363 goto err;
364 }
365 BIO_puts(bp, "\n");
366
367 if (!BIO_indent(bp, indent, 128) || //
368 BIO_puts(bp, "Salt Length: 0x") <= 0) {
369 goto err;
370 }
371
372 if (pss->saltLength) {
373 if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) {
374 goto err;
375 }
376 } else if (BIO_puts(bp, "14 (default)") <= 0) {
377 goto err;
378 }
379 BIO_puts(bp, "\n");
380
381 if (!BIO_indent(bp, indent, 128) || //
382 BIO_puts(bp, "Trailer Field: 0x") <= 0) {
383 goto err;
384 }
385
386 if (pss->trailerField) {
387 if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) {
388 goto err;
389 }
390 } else if (BIO_puts(bp, "BC (default)") <= 0) {
391 goto err;
392 }
393 BIO_puts(bp, "\n");
394
395 rv = 1;
396
397 err:
398 RSA_PSS_PARAMS_free(pss);
399 X509_ALGOR_free(maskHash);
400 return rv;
401 }
402