1 /* Written by Dr Stephen N Henson (steve@openssl.org) 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 * licensing@OpenSSL.org.
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 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com). */
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 X509_ALGOR_set_md(*palg, md);
129 return 1;
130 }
131
132 // rsa_md_to_mgf1 sets |*palg| to an |X509_ALGOR| describing MGF-1 with the
133 // digest |mgf1md|, which must be an allowed PSS digest.
rsa_md_to_mgf1(X509_ALGOR ** palg,const EVP_MD * mgf1md)134 static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) {
135 // SHA-1 should be omitted (DEFAULT), but we do not allow SHA-1.
136 assert(is_allowed_pss_md(mgf1md));
137 X509_ALGOR *algtmp = NULL;
138 ASN1_STRING *stmp = NULL;
139 // need to embed algorithm ID inside another
140 if (!rsa_md_to_algor(&algtmp, mgf1md) ||
141 !ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) {
142 goto err;
143 }
144 *palg = X509_ALGOR_new();
145 if (!*palg) {
146 goto err;
147 }
148 if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) {
149 goto err;
150 }
151 stmp = NULL;
152
153 err:
154 ASN1_STRING_free(stmp);
155 X509_ALGOR_free(algtmp);
156 if (*palg) {
157 return 1;
158 }
159
160 return 0;
161 }
162
rsa_algor_to_md(const X509_ALGOR * alg)163 static const EVP_MD *rsa_algor_to_md(const X509_ALGOR *alg) {
164 if (!alg) {
165 // If omitted, PSS defaults to SHA-1, which we do not allow.
166 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
167 return NULL;
168 }
169 const EVP_MD *md = EVP_get_digestbyobj(alg->algorithm);
170 if (md == NULL || !is_allowed_pss_md(md)) {
171 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
172 return NULL;
173 }
174 return md;
175 }
176
rsa_mgf1_to_md(const X509_ALGOR * alg)177 static const EVP_MD *rsa_mgf1_to_md(const X509_ALGOR *alg) {
178 if (!alg) {
179 // If omitted, PSS defaults to MGF-1 with SHA-1, which we do not allow.
180 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
181 return NULL;
182 }
183 // Check mask and lookup mask hash algorithm.
184 X509_ALGOR *maskHash = rsa_mgf1_decode(alg);
185 if (maskHash == NULL) {
186 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
187 return NULL;
188 }
189 const EVP_MD *ret = rsa_algor_to_md(maskHash);
190 X509_ALGOR_free(maskHash);
191 return ret;
192 }
193
x509_rsa_ctx_to_pss(EVP_MD_CTX * ctx,X509_ALGOR * algor)194 int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor) {
195 const EVP_MD *sigmd, *mgf1md;
196 int saltlen;
197 if (!EVP_PKEY_CTX_get_signature_md(ctx->pctx, &sigmd) ||
198 !EVP_PKEY_CTX_get_rsa_mgf1_md(ctx->pctx, &mgf1md) ||
199 !EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx->pctx, &saltlen)) {
200 return 0;
201 }
202
203 if (sigmd != mgf1md || !is_allowed_pss_md(sigmd)) {
204 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
205 return 0;
206 }
207 int md_len = (int)EVP_MD_size(sigmd);
208 if (saltlen == -1) {
209 saltlen = md_len;
210 } else if (saltlen != md_len) {
211 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
212 return 0;
213 }
214
215 int ret = 0;
216 ASN1_STRING *os = NULL;
217 RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
218 if (!pss) {
219 goto err;
220 }
221
222 // The DEFAULT value is 20, but this does not match any supported digest.
223 assert(saltlen != 20);
224 pss->saltLength = ASN1_INTEGER_new();
225 if (!pss->saltLength || //
226 !ASN1_INTEGER_set_int64(pss->saltLength, saltlen)) {
227 goto err;
228 }
229
230 if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd) ||
231 !rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) {
232 goto err;
233 }
234
235 // Finally create string with pss parameter encoding.
236 if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) {
237 goto err;
238 }
239
240 if (!X509_ALGOR_set0(algor, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os)) {
241 goto err;
242 }
243 os = NULL;
244 ret = 1;
245
246 err:
247 RSA_PSS_PARAMS_free(pss);
248 ASN1_STRING_free(os);
249 return ret;
250 }
251
x509_rsa_pss_to_ctx(EVP_MD_CTX * ctx,const X509_ALGOR * sigalg,EVP_PKEY * pkey)252 int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
253 EVP_PKEY *pkey) {
254 assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
255
256 // Decode PSS parameters
257 int ret = 0;
258 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
259 if (pss == NULL) {
260 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
261 goto err;
262 }
263
264 const EVP_MD *mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm);
265 const EVP_MD *md = rsa_algor_to_md(pss->hashAlgorithm);
266 if (mgf1md == NULL || md == NULL) {
267 goto err;
268 }
269
270 // We require the MGF-1 and signing hashes to match.
271 if (mgf1md != md) {
272 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
273 goto err;
274 }
275
276 // We require the salt length be the hash length. The DEFAULT value is 20, but
277 // this does not match any supported salt length.
278 uint64_t salt_len = 0;
279 if (pss->saltLength == NULL ||
280 !ASN1_INTEGER_get_uint64(&salt_len, pss->saltLength) ||
281 salt_len != EVP_MD_size(md)) {
282 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
283 goto err;
284 }
285 assert(salt_len <= INT_MAX);
286
287 // The trailer field must be 1 (0xbc). This value is DEFAULT, so the structure
288 // is required to omit it in DER. Although a syntax error, we also tolerate an
289 // explicitly-encoded value. See the certificates in cl/362617931.
290 if (pss->trailerField != NULL && ASN1_INTEGER_get(pss->trailerField) != 1) {
291 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PSS_PARAMETERS);
292 goto err;
293 }
294
295 EVP_PKEY_CTX *pctx;
296 if (!EVP_DigestVerifyInit(ctx, &pctx, md, NULL, pkey) ||
297 !EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) ||
298 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, (int)salt_len) ||
299 !EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1md)) {
300 goto err;
301 }
302
303 ret = 1;
304
305 err:
306 RSA_PSS_PARAMS_free(pss);
307 return ret;
308 }
309
x509_print_rsa_pss_params(BIO * bp,const X509_ALGOR * sigalg,int indent,ASN1_PCTX * pctx)310 int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent,
311 ASN1_PCTX *pctx) {
312 assert(OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss);
313
314 int rv = 0;
315 X509_ALGOR *maskHash = NULL;
316 RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
317 if (!pss) {
318 if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) {
319 goto err;
320 }
321 rv = 1;
322 goto err;
323 }
324
325 if (BIO_puts(bp, "\n") <= 0 || //
326 !BIO_indent(bp, indent, 128) || //
327 BIO_puts(bp, "Hash Algorithm: ") <= 0) {
328 goto err;
329 }
330
331 if (pss->hashAlgorithm) {
332 if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) {
333 goto err;
334 }
335 } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
336 goto err;
337 }
338
339 if (BIO_puts(bp, "\n") <= 0 || //
340 !BIO_indent(bp, indent, 128) || //
341 BIO_puts(bp, "Mask Algorithm: ") <= 0) {
342 goto err;
343 }
344
345 if (pss->maskGenAlgorithm) {
346 maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
347 if (maskHash == NULL) {
348 if (BIO_puts(bp, "INVALID") <= 0) {
349 goto err;
350 }
351 } else {
352 if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 ||
353 BIO_puts(bp, " with ") <= 0 ||
354 i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) {
355 goto err;
356 }
357 }
358 } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
359 goto err;
360 }
361 BIO_puts(bp, "\n");
362
363 if (!BIO_indent(bp, indent, 128) || //
364 BIO_puts(bp, "Salt Length: 0x") <= 0) {
365 goto err;
366 }
367
368 if (pss->saltLength) {
369 if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) {
370 goto err;
371 }
372 } else if (BIO_puts(bp, "14 (default)") <= 0) {
373 goto err;
374 }
375 BIO_puts(bp, "\n");
376
377 if (!BIO_indent(bp, indent, 128) || //
378 BIO_puts(bp, "Trailer Field: 0x") <= 0) {
379 goto err;
380 }
381
382 if (pss->trailerField) {
383 if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) {
384 goto err;
385 }
386 } else if (BIO_puts(bp, "BC (default)") <= 0) {
387 goto err;
388 }
389 BIO_puts(bp, "\n");
390
391 rv = 1;
392
393 err:
394 RSA_PSS_PARAMS_free(pss);
395 X509_ALGOR_free(maskHash);
396 return rv;
397 }
398