/src/openssl/crypto/rsa/rsa_pmeth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1t.h> |
13 | | #include <openssl/x509.h> |
14 | | #include <openssl/rsa.h> |
15 | | #include <openssl/bn.h> |
16 | | #include <openssl/evp.h> |
17 | | #include <openssl/x509v3.h> |
18 | | #include <openssl/cms.h> |
19 | | #include "internal/evp_int.h" |
20 | | #include "rsa_locl.h" |
21 | | |
22 | | /* RSA pkey context structure */ |
23 | | |
24 | | typedef struct { |
25 | | /* Key gen parameters */ |
26 | | int nbits; |
27 | | BIGNUM *pub_exp; |
28 | | int primes; |
29 | | /* Keygen callback info */ |
30 | | int gentmp[2]; |
31 | | /* RSA padding mode */ |
32 | | int pad_mode; |
33 | | /* message digest */ |
34 | | const EVP_MD *md; |
35 | | /* message digest for MGF1 */ |
36 | | const EVP_MD *mgf1md; |
37 | | /* PSS salt length */ |
38 | | int saltlen; |
39 | | /* Minimum salt length or -1 if no PSS parameter restriction */ |
40 | | int min_saltlen; |
41 | | /* Temp buffer */ |
42 | | unsigned char *tbuf; |
43 | | /* OAEP label */ |
44 | | unsigned char *oaep_label; |
45 | | size_t oaep_labellen; |
46 | | } RSA_PKEY_CTX; |
47 | | |
48 | | /* True if PSS parameters are restricted */ |
49 | 0 | #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1) |
50 | | |
51 | | static int pkey_rsa_init(EVP_PKEY_CTX *ctx) |
52 | 0 | { |
53 | 0 | RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx)); |
54 | 0 |
|
55 | 0 | if (rctx == NULL) |
56 | 0 | return 0; |
57 | 0 | rctx->nbits = 1024; |
58 | 0 | rctx->primes = RSA_DEFAULT_PRIME_NUM; |
59 | 0 | if (pkey_ctx_is_pss(ctx)) |
60 | 0 | rctx->pad_mode = RSA_PKCS1_PSS_PADDING; |
61 | 0 | else |
62 | 0 | rctx->pad_mode = RSA_PKCS1_PADDING; |
63 | 0 | /* Maximum for sign, auto for verify */ |
64 | 0 | rctx->saltlen = RSA_PSS_SALTLEN_AUTO; |
65 | 0 | rctx->min_saltlen = -1; |
66 | 0 | ctx->data = rctx; |
67 | 0 | ctx->keygen_info = rctx->gentmp; |
68 | 0 | ctx->keygen_info_count = 2; |
69 | 0 |
|
70 | 0 | return 1; |
71 | 0 | } |
72 | | |
73 | | static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
74 | 0 | { |
75 | 0 | RSA_PKEY_CTX *dctx, *sctx; |
76 | 0 |
|
77 | 0 | if (!pkey_rsa_init(dst)) |
78 | 0 | return 0; |
79 | 0 | sctx = src->data; |
80 | 0 | dctx = dst->data; |
81 | 0 | dctx->nbits = sctx->nbits; |
82 | 0 | if (sctx->pub_exp) { |
83 | 0 | dctx->pub_exp = BN_dup(sctx->pub_exp); |
84 | 0 | if (!dctx->pub_exp) |
85 | 0 | return 0; |
86 | 0 | } |
87 | 0 | dctx->pad_mode = sctx->pad_mode; |
88 | 0 | dctx->md = sctx->md; |
89 | 0 | dctx->mgf1md = sctx->mgf1md; |
90 | 0 | if (sctx->oaep_label) { |
91 | 0 | OPENSSL_free(dctx->oaep_label); |
92 | 0 | dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen); |
93 | 0 | if (!dctx->oaep_label) |
94 | 0 | return 0; |
95 | 0 | dctx->oaep_labellen = sctx->oaep_labellen; |
96 | 0 | } |
97 | 0 | return 1; |
98 | 0 | } |
99 | | |
100 | | static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) |
101 | 0 | { |
102 | 0 | if (ctx->tbuf != NULL) |
103 | 0 | return 1; |
104 | 0 | if ((ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey))) == NULL) { |
105 | 0 | RSAerr(RSA_F_SETUP_TBUF, ERR_R_MALLOC_FAILURE); |
106 | 0 | return 0; |
107 | 0 | } |
108 | 0 | return 1; |
109 | 0 | } |
110 | | |
111 | | static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) |
112 | 0 | { |
113 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
114 | 0 | if (rctx) { |
115 | 0 | BN_free(rctx->pub_exp); |
116 | 0 | OPENSSL_free(rctx->tbuf); |
117 | 0 | OPENSSL_free(rctx->oaep_label); |
118 | 0 | OPENSSL_free(rctx); |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, |
123 | | size_t *siglen, const unsigned char *tbs, |
124 | | size_t tbslen) |
125 | 0 | { |
126 | 0 | int ret; |
127 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
128 | 0 | RSA *rsa = ctx->pkey->pkey.rsa; |
129 | 0 |
|
130 | 0 | if (rctx->md) { |
131 | 0 | if (tbslen != (size_t)EVP_MD_size(rctx->md)) { |
132 | 0 | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH); |
133 | 0 | return -1; |
134 | 0 | } |
135 | 0 |
|
136 | 0 | if (EVP_MD_type(rctx->md) == NID_mdc2) { |
137 | 0 | unsigned int sltmp; |
138 | 0 | if (rctx->pad_mode != RSA_PKCS1_PADDING) |
139 | 0 | return -1; |
140 | 0 | ret = RSA_sign_ASN1_OCTET_STRING(0, |
141 | 0 | tbs, tbslen, sig, &sltmp, rsa); |
142 | 0 |
|
143 | 0 | if (ret <= 0) |
144 | 0 | return ret; |
145 | 0 | ret = sltmp; |
146 | 0 | } else if (rctx->pad_mode == RSA_X931_PADDING) { |
147 | 0 | if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) { |
148 | 0 | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL); |
149 | 0 | return -1; |
150 | 0 | } |
151 | 0 | if (!setup_tbuf(rctx, ctx)) { |
152 | 0 | RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE); |
153 | 0 | return -1; |
154 | 0 | } |
155 | 0 | memcpy(rctx->tbuf, tbs, tbslen); |
156 | 0 | rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md)); |
157 | 0 | ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, |
158 | 0 | sig, rsa, RSA_X931_PADDING); |
159 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { |
160 | 0 | unsigned int sltmp; |
161 | 0 | ret = RSA_sign(EVP_MD_type(rctx->md), |
162 | 0 | tbs, tbslen, sig, &sltmp, rsa); |
163 | 0 | if (ret <= 0) |
164 | 0 | return ret; |
165 | 0 | ret = sltmp; |
166 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { |
167 | 0 | if (!setup_tbuf(rctx, ctx)) |
168 | 0 | return -1; |
169 | 0 | if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, |
170 | 0 | rctx->tbuf, tbs, |
171 | 0 | rctx->md, rctx->mgf1md, |
172 | 0 | rctx->saltlen)) |
173 | 0 | return -1; |
174 | 0 | ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf, |
175 | 0 | sig, rsa, RSA_NO_PADDING); |
176 | 0 | } else { |
177 | 0 | return -1; |
178 | 0 | } |
179 | 0 | } else { |
180 | 0 | ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa, |
181 | 0 | rctx->pad_mode); |
182 | 0 | } |
183 | 0 | if (ret < 0) |
184 | 0 | return ret; |
185 | 0 | *siglen = ret; |
186 | 0 | return 1; |
187 | 0 | } |
188 | | |
189 | | static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx, |
190 | | unsigned char *rout, size_t *routlen, |
191 | | const unsigned char *sig, size_t siglen) |
192 | 0 | { |
193 | 0 | int ret; |
194 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
195 | 0 |
|
196 | 0 | if (rctx->md) { |
197 | 0 | if (rctx->pad_mode == RSA_X931_PADDING) { |
198 | 0 | if (!setup_tbuf(rctx, ctx)) |
199 | 0 | return -1; |
200 | 0 | ret = RSA_public_decrypt(siglen, sig, |
201 | 0 | rctx->tbuf, ctx->pkey->pkey.rsa, |
202 | 0 | RSA_X931_PADDING); |
203 | 0 | if (ret < 1) |
204 | 0 | return 0; |
205 | 0 | ret--; |
206 | 0 | if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) { |
207 | 0 | RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, |
208 | 0 | RSA_R_ALGORITHM_MISMATCH); |
209 | 0 | return 0; |
210 | 0 | } |
211 | 0 | if (ret != EVP_MD_size(rctx->md)) { |
212 | 0 | RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, |
213 | 0 | RSA_R_INVALID_DIGEST_LENGTH); |
214 | 0 | return 0; |
215 | 0 | } |
216 | 0 | if (rout) |
217 | 0 | memcpy(rout, rctx->tbuf, ret); |
218 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { |
219 | 0 | size_t sltmp; |
220 | 0 | ret = int_rsa_verify(EVP_MD_type(rctx->md), |
221 | 0 | NULL, 0, rout, &sltmp, |
222 | 0 | sig, siglen, ctx->pkey->pkey.rsa); |
223 | 0 | if (ret <= 0) |
224 | 0 | return 0; |
225 | 0 | ret = sltmp; |
226 | 0 | } else { |
227 | 0 | return -1; |
228 | 0 | } |
229 | 0 | } else { |
230 | 0 | ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa, |
231 | 0 | rctx->pad_mode); |
232 | 0 | } |
233 | 0 | if (ret < 0) |
234 | 0 | return ret; |
235 | 0 | *routlen = ret; |
236 | 0 | return 1; |
237 | 0 | } |
238 | | |
239 | | static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, |
240 | | const unsigned char *sig, size_t siglen, |
241 | | const unsigned char *tbs, size_t tbslen) |
242 | 0 | { |
243 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
244 | 0 | RSA *rsa = ctx->pkey->pkey.rsa; |
245 | 0 | size_t rslen; |
246 | 0 |
|
247 | 0 | if (rctx->md) { |
248 | 0 | if (rctx->pad_mode == RSA_PKCS1_PADDING) |
249 | 0 | return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, |
250 | 0 | sig, siglen, rsa); |
251 | 0 | if (tbslen != (size_t)EVP_MD_size(rctx->md)) { |
252 | 0 | RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH); |
253 | 0 | return -1; |
254 | 0 | } |
255 | 0 | if (rctx->pad_mode == RSA_X931_PADDING) { |
256 | 0 | if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0) |
257 | 0 | return 0; |
258 | 0 | } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { |
259 | 0 | int ret; |
260 | 0 | if (!setup_tbuf(rctx, ctx)) |
261 | 0 | return -1; |
262 | 0 | ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, |
263 | 0 | rsa, RSA_NO_PADDING); |
264 | 0 | if (ret <= 0) |
265 | 0 | return 0; |
266 | 0 | ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, |
267 | 0 | rctx->md, rctx->mgf1md, |
268 | 0 | rctx->tbuf, rctx->saltlen); |
269 | 0 | if (ret <= 0) |
270 | 0 | return 0; |
271 | 0 | return 1; |
272 | 0 | } else { |
273 | 0 | return -1; |
274 | 0 | } |
275 | 0 | } else { |
276 | 0 | if (!setup_tbuf(rctx, ctx)) |
277 | 0 | return -1; |
278 | 0 | rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, |
279 | 0 | rsa, rctx->pad_mode); |
280 | 0 | if (rslen == 0) |
281 | 0 | return 0; |
282 | 0 | } |
283 | 0 | |
284 | 0 | if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen)) |
285 | 0 | return 0; |
286 | 0 | |
287 | 0 | return 1; |
288 | 0 |
|
289 | 0 | } |
290 | | |
291 | | static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, |
292 | | unsigned char *out, size_t *outlen, |
293 | | const unsigned char *in, size_t inlen) |
294 | 0 | { |
295 | 0 | int ret; |
296 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
297 | 0 |
|
298 | 0 | if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
299 | 0 | int klen = RSA_size(ctx->pkey->pkey.rsa); |
300 | 0 | if (!setup_tbuf(rctx, ctx)) |
301 | 0 | return -1; |
302 | 0 | if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen, |
303 | 0 | in, inlen, |
304 | 0 | rctx->oaep_label, |
305 | 0 | rctx->oaep_labellen, |
306 | 0 | rctx->md, rctx->mgf1md)) |
307 | 0 | return -1; |
308 | 0 | ret = RSA_public_encrypt(klen, rctx->tbuf, out, |
309 | 0 | ctx->pkey->pkey.rsa, RSA_NO_PADDING); |
310 | 0 | } else { |
311 | 0 | ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa, |
312 | 0 | rctx->pad_mode); |
313 | 0 | } |
314 | 0 | if (ret < 0) |
315 | 0 | return ret; |
316 | 0 | *outlen = ret; |
317 | 0 | return 1; |
318 | 0 | } |
319 | | |
320 | | static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, |
321 | | unsigned char *out, size_t *outlen, |
322 | | const unsigned char *in, size_t inlen) |
323 | 0 | { |
324 | 0 | int ret; |
325 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
326 | 0 |
|
327 | 0 | if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
328 | 0 | if (!setup_tbuf(rctx, ctx)) |
329 | 0 | return -1; |
330 | 0 | ret = RSA_private_decrypt(inlen, in, rctx->tbuf, |
331 | 0 | ctx->pkey->pkey.rsa, RSA_NO_PADDING); |
332 | 0 | if (ret <= 0) |
333 | 0 | return ret; |
334 | 0 | ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf, |
335 | 0 | ret, ret, |
336 | 0 | rctx->oaep_label, |
337 | 0 | rctx->oaep_labellen, |
338 | 0 | rctx->md, rctx->mgf1md); |
339 | 0 | } else { |
340 | 0 | ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa, |
341 | 0 | rctx->pad_mode); |
342 | 0 | } |
343 | 0 | if (ret < 0) |
344 | 0 | return ret; |
345 | 0 | *outlen = ret; |
346 | 0 | return 1; |
347 | 0 | } |
348 | | |
349 | | static int check_padding_md(const EVP_MD *md, int padding) |
350 | 0 | { |
351 | 0 | int mdnid; |
352 | 0 |
|
353 | 0 | if (!md) |
354 | 0 | return 1; |
355 | 0 | |
356 | 0 | mdnid = EVP_MD_type(md); |
357 | 0 |
|
358 | 0 | if (padding == RSA_NO_PADDING) { |
359 | 0 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE); |
360 | 0 | return 0; |
361 | 0 | } |
362 | 0 |
|
363 | 0 | if (padding == RSA_X931_PADDING) { |
364 | 0 | if (RSA_X931_hash_id(mdnid) == -1) { |
365 | 0 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST); |
366 | 0 | return 0; |
367 | 0 | } |
368 | 0 | } else { |
369 | 0 | switch(mdnid) { |
370 | 0 | /* List of all supported RSA digests */ |
371 | 0 | case NID_sha1: |
372 | 0 | case NID_sha224: |
373 | 0 | case NID_sha256: |
374 | 0 | case NID_sha384: |
375 | 0 | case NID_sha512: |
376 | 0 | case NID_md5: |
377 | 0 | case NID_md5_sha1: |
378 | 0 | case NID_md2: |
379 | 0 | case NID_md4: |
380 | 0 | case NID_mdc2: |
381 | 0 | case NID_ripemd160: |
382 | 0 | case NID_sha3_224: |
383 | 0 | case NID_sha3_256: |
384 | 0 | case NID_sha3_384: |
385 | 0 | case NID_sha3_512: |
386 | 0 | return 1; |
387 | 0 |
|
388 | 0 | default: |
389 | 0 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST); |
390 | 0 | return 0; |
391 | 0 |
|
392 | 0 | } |
393 | 0 | } |
394 | 0 |
|
395 | 0 | return 1; |
396 | 0 | } |
397 | | |
398 | | static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
399 | 0 | { |
400 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
401 | 0 |
|
402 | 0 | switch (type) { |
403 | 0 | case EVP_PKEY_CTRL_RSA_PADDING: |
404 | 0 | if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) { |
405 | 0 | if (!check_padding_md(rctx->md, p1)) |
406 | 0 | return 0; |
407 | 0 | if (p1 == RSA_PKCS1_PSS_PADDING) { |
408 | 0 | if (!(ctx->operation & |
409 | 0 | (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) |
410 | 0 | goto bad_pad; |
411 | 0 | if (!rctx->md) |
412 | 0 | rctx->md = EVP_sha1(); |
413 | 0 | } else if (pkey_ctx_is_pss(ctx)) { |
414 | 0 | goto bad_pad; |
415 | 0 | } |
416 | 0 | if (p1 == RSA_PKCS1_OAEP_PADDING) { |
417 | 0 | if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT)) |
418 | 0 | goto bad_pad; |
419 | 0 | if (!rctx->md) |
420 | 0 | rctx->md = EVP_sha1(); |
421 | 0 | } |
422 | 0 | rctx->pad_mode = p1; |
423 | 0 | return 1; |
424 | 0 | } |
425 | 0 | bad_pad: |
426 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, |
427 | 0 | RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); |
428 | 0 | return -2; |
429 | 0 |
|
430 | 0 | case EVP_PKEY_CTRL_GET_RSA_PADDING: |
431 | 0 | *(int *)p2 = rctx->pad_mode; |
432 | 0 | return 1; |
433 | 0 |
|
434 | 0 | case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: |
435 | 0 | case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: |
436 | 0 | if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { |
437 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN); |
438 | 0 | return -2; |
439 | 0 | } |
440 | 0 | if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) { |
441 | 0 | *(int *)p2 = rctx->saltlen; |
442 | 0 | } else { |
443 | 0 | if (p1 < RSA_PSS_SALTLEN_MAX) |
444 | 0 | return -2; |
445 | 0 | if (rsa_pss_restricted(rctx)) { |
446 | 0 | if (p1 == RSA_PSS_SALTLEN_AUTO |
447 | 0 | && ctx->operation == EVP_PKEY_OP_VERIFY) { |
448 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN); |
449 | 0 | return -2; |
450 | 0 | } |
451 | 0 | if ((p1 == RSA_PSS_SALTLEN_DIGEST |
452 | 0 | && rctx->min_saltlen > EVP_MD_size(rctx->md)) |
453 | 0 | || (p1 >= 0 && p1 < rctx->min_saltlen)) { |
454 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL); |
455 | 0 | return 0; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | rctx->saltlen = p1; |
459 | 0 | } |
460 | 0 | return 1; |
461 | 0 |
|
462 | 0 | case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: |
463 | 0 | if (p1 < RSA_MIN_MODULUS_BITS) { |
464 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL); |
465 | 0 | return -2; |
466 | 0 | } |
467 | 0 | rctx->nbits = p1; |
468 | 0 | return 1; |
469 | 0 |
|
470 | 0 | case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: |
471 | 0 | if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) { |
472 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE); |
473 | 0 | return -2; |
474 | 0 | } |
475 | 0 | BN_free(rctx->pub_exp); |
476 | 0 | rctx->pub_exp = p2; |
477 | 0 | return 1; |
478 | 0 |
|
479 | 0 | case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES: |
480 | 0 | if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) { |
481 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_PRIME_NUM_INVALID); |
482 | 0 | return -2; |
483 | 0 | } |
484 | 0 | rctx->primes = p1; |
485 | 0 | return 1; |
486 | 0 |
|
487 | 0 | case EVP_PKEY_CTRL_RSA_OAEP_MD: |
488 | 0 | case EVP_PKEY_CTRL_GET_RSA_OAEP_MD: |
489 | 0 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
490 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); |
491 | 0 | return -2; |
492 | 0 | } |
493 | 0 | if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) |
494 | 0 | *(const EVP_MD **)p2 = rctx->md; |
495 | 0 | else |
496 | 0 | rctx->md = p2; |
497 | 0 | return 1; |
498 | 0 |
|
499 | 0 | case EVP_PKEY_CTRL_MD: |
500 | 0 | if (!check_padding_md(p2, rctx->pad_mode)) |
501 | 0 | return 0; |
502 | 0 | if (rsa_pss_restricted(rctx)) { |
503 | 0 | if (EVP_MD_type(rctx->md) == EVP_MD_type(p2)) |
504 | 0 | return 1; |
505 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED); |
506 | 0 | return 0; |
507 | 0 | } |
508 | 0 | rctx->md = p2; |
509 | 0 | return 1; |
510 | 0 |
|
511 | 0 | case EVP_PKEY_CTRL_GET_MD: |
512 | 0 | *(const EVP_MD **)p2 = rctx->md; |
513 | 0 | return 1; |
514 | 0 |
|
515 | 0 | case EVP_PKEY_CTRL_RSA_MGF1_MD: |
516 | 0 | case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: |
517 | 0 | if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING |
518 | 0 | && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
519 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD); |
520 | 0 | return -2; |
521 | 0 | } |
522 | 0 | if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) { |
523 | 0 | if (rctx->mgf1md) |
524 | 0 | *(const EVP_MD **)p2 = rctx->mgf1md; |
525 | 0 | else |
526 | 0 | *(const EVP_MD **)p2 = rctx->md; |
527 | 0 | } else { |
528 | 0 | if (rsa_pss_restricted(rctx)) { |
529 | 0 | if (EVP_MD_type(rctx->mgf1md) == EVP_MD_type(p2)) |
530 | 0 | return 1; |
531 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED); |
532 | 0 | return 0; |
533 | 0 | } |
534 | 0 | rctx->mgf1md = p2; |
535 | 0 | } |
536 | 0 | return 1; |
537 | 0 |
|
538 | 0 | case EVP_PKEY_CTRL_RSA_OAEP_LABEL: |
539 | 0 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
540 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); |
541 | 0 | return -2; |
542 | 0 | } |
543 | 0 | OPENSSL_free(rctx->oaep_label); |
544 | 0 | if (p2 && p1 > 0) { |
545 | 0 | rctx->oaep_label = p2; |
546 | 0 | rctx->oaep_labellen = p1; |
547 | 0 | } else { |
548 | 0 | rctx->oaep_label = NULL; |
549 | 0 | rctx->oaep_labellen = 0; |
550 | 0 | } |
551 | 0 | return 1; |
552 | 0 |
|
553 | 0 | case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL: |
554 | 0 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
555 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); |
556 | 0 | return -2; |
557 | 0 | } |
558 | 0 | *(unsigned char **)p2 = rctx->oaep_label; |
559 | 0 | return rctx->oaep_labellen; |
560 | 0 |
|
561 | 0 | case EVP_PKEY_CTRL_DIGESTINIT: |
562 | 0 | case EVP_PKEY_CTRL_PKCS7_SIGN: |
563 | 0 | #ifndef OPENSSL_NO_CMS |
564 | 0 | case EVP_PKEY_CTRL_CMS_SIGN: |
565 | 0 | #endif |
566 | 0 | return 1; |
567 | 0 |
|
568 | 0 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: |
569 | 0 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: |
570 | 0 | #ifndef OPENSSL_NO_CMS |
571 | 0 | case EVP_PKEY_CTRL_CMS_DECRYPT: |
572 | 0 | case EVP_PKEY_CTRL_CMS_ENCRYPT: |
573 | 0 | #endif |
574 | 0 | if (!pkey_ctx_is_pss(ctx)) |
575 | 0 | return 1; |
576 | 0 | /* fall through */ |
577 | 0 | case EVP_PKEY_CTRL_PEER_KEY: |
578 | 0 | RSAerr(RSA_F_PKEY_RSA_CTRL, |
579 | 0 | RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
580 | 0 | return -2; |
581 | 0 |
|
582 | 0 | default: |
583 | 0 | return -2; |
584 | 0 |
|
585 | 0 | } |
586 | 0 | } |
587 | | |
588 | | static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, |
589 | | const char *type, const char *value) |
590 | | { |
591 | | if (value == NULL) { |
592 | | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING); |
593 | | return 0; |
594 | | } |
595 | | if (strcmp(type, "rsa_padding_mode") == 0) { |
596 | | int pm; |
597 | | |
598 | | if (strcmp(value, "pkcs1") == 0) { |
599 | | pm = RSA_PKCS1_PADDING; |
600 | | } else if (strcmp(value, "sslv23") == 0) { |
601 | | pm = RSA_SSLV23_PADDING; |
602 | | } else if (strcmp(value, "none") == 0) { |
603 | | pm = RSA_NO_PADDING; |
604 | | } else if (strcmp(value, "oeap") == 0) { |
605 | | pm = RSA_PKCS1_OAEP_PADDING; |
606 | | } else if (strcmp(value, "oaep") == 0) { |
607 | | pm = RSA_PKCS1_OAEP_PADDING; |
608 | | } else if (strcmp(value, "x931") == 0) { |
609 | | pm = RSA_X931_PADDING; |
610 | | } else if (strcmp(value, "pss") == 0) { |
611 | | pm = RSA_PKCS1_PSS_PADDING; |
612 | | } else { |
613 | | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE); |
614 | | return -2; |
615 | | } |
616 | | return EVP_PKEY_CTX_set_rsa_padding(ctx, pm); |
617 | | } |
618 | | |
619 | | if (strcmp(type, "rsa_pss_saltlen") == 0) { |
620 | | int saltlen; |
621 | | |
622 | | if (!strcmp(value, "digest")) |
623 | | saltlen = RSA_PSS_SALTLEN_DIGEST; |
624 | | else if (!strcmp(value, "max")) |
625 | | saltlen = RSA_PSS_SALTLEN_MAX; |
626 | | else if (!strcmp(value, "auto")) |
627 | | saltlen = RSA_PSS_SALTLEN_AUTO; |
628 | | else |
629 | | saltlen = atoi(value); |
630 | | return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen); |
631 | | } |
632 | | |
633 | | if (strcmp(type, "rsa_keygen_bits") == 0) { |
634 | | int nbits = atoi(value); |
635 | | |
636 | | return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits); |
637 | | } |
638 | | |
639 | | if (strcmp(type, "rsa_keygen_pubexp") == 0) { |
640 | | int ret; |
641 | | |
642 | | BIGNUM *pubexp = NULL; |
643 | | if (!BN_asc2bn(&pubexp, value)) |
644 | | return 0; |
645 | | ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp); |
646 | | if (ret <= 0) |
647 | | BN_free(pubexp); |
648 | | return ret; |
649 | | } |
650 | | |
651 | | if (strcmp(type, "rsa_keygen_primes") == 0) { |
652 | | int nprimes = atoi(value); |
653 | | |
654 | | return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes); |
655 | | } |
656 | | |
657 | | if (strcmp(type, "rsa_mgf1_md") == 0) |
658 | | return EVP_PKEY_CTX_md(ctx, |
659 | | EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
660 | | EVP_PKEY_CTRL_RSA_MGF1_MD, value); |
661 | | |
662 | | if (pkey_ctx_is_pss(ctx)) { |
663 | | |
664 | | if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0) |
665 | | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN, |
666 | | EVP_PKEY_CTRL_RSA_MGF1_MD, value); |
667 | | |
668 | | if (strcmp(type, "rsa_pss_keygen_md") == 0) |
669 | | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN, |
670 | | EVP_PKEY_CTRL_MD, value); |
671 | | |
672 | | if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) { |
673 | | int saltlen = atoi(value); |
674 | | |
675 | | return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen); |
676 | | } |
677 | | } |
678 | | |
679 | | if (strcmp(type, "rsa_oaep_md") == 0) |
680 | | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT, |
681 | | EVP_PKEY_CTRL_RSA_OAEP_MD, value); |
682 | | |
683 | | if (strcmp(type, "rsa_oaep_label") == 0) { |
684 | | unsigned char *lab; |
685 | | long lablen; |
686 | | int ret; |
687 | | |
688 | | lab = OPENSSL_hexstr2buf(value, &lablen); |
689 | | if (!lab) |
690 | | return 0; |
691 | | ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen); |
692 | | if (ret <= 0) |
693 | | OPENSSL_free(lab); |
694 | | return ret; |
695 | | } |
696 | | |
697 | | return -2; |
698 | | } |
699 | | |
700 | | /* Set PSS parameters when generating a key, if necessary */ |
701 | | static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx) |
702 | 0 | { |
703 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
704 | 0 |
|
705 | 0 | if (!pkey_ctx_is_pss(ctx)) |
706 | 0 | return 1; |
707 | 0 | /* If all parameters are default values don't set pss */ |
708 | 0 | if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2) |
709 | 0 | return 1; |
710 | 0 | rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md, |
711 | 0 | rctx->saltlen == -2 ? 0 : rctx->saltlen); |
712 | 0 | if (rsa->pss == NULL) |
713 | 0 | return 0; |
714 | 0 | return 1; |
715 | 0 | } |
716 | | |
717 | | static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
718 | 0 | { |
719 | 0 | RSA *rsa = NULL; |
720 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
721 | 0 | BN_GENCB *pcb; |
722 | 0 | int ret; |
723 | 0 |
|
724 | 0 | if (rctx->pub_exp == NULL) { |
725 | 0 | rctx->pub_exp = BN_new(); |
726 | 0 | if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4)) |
727 | 0 | return 0; |
728 | 0 | } |
729 | 0 | rsa = RSA_new(); |
730 | 0 | if (rsa == NULL) |
731 | 0 | return 0; |
732 | 0 | if (ctx->pkey_gencb) { |
733 | 0 | pcb = BN_GENCB_new(); |
734 | 0 | if (pcb == NULL) { |
735 | 0 | RSA_free(rsa); |
736 | 0 | return 0; |
737 | 0 | } |
738 | 0 | evp_pkey_set_cb_translate(pcb, ctx); |
739 | 0 | } else { |
740 | 0 | pcb = NULL; |
741 | 0 | } |
742 | 0 | ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes, |
743 | 0 | rctx->pub_exp, pcb); |
744 | 0 | BN_GENCB_free(pcb); |
745 | 0 | if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) { |
746 | 0 | RSA_free(rsa); |
747 | 0 | return 0; |
748 | 0 | } |
749 | 0 | if (ret > 0) |
750 | 0 | EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa); |
751 | 0 | else |
752 | 0 | RSA_free(rsa); |
753 | 0 | return ret; |
754 | 0 | } |
755 | | |
756 | | const EVP_PKEY_METHOD rsa_pkey_meth = { |
757 | | EVP_PKEY_RSA, |
758 | | EVP_PKEY_FLAG_AUTOARGLEN, |
759 | | pkey_rsa_init, |
760 | | pkey_rsa_copy, |
761 | | pkey_rsa_cleanup, |
762 | | |
763 | | 0, 0, |
764 | | |
765 | | 0, |
766 | | pkey_rsa_keygen, |
767 | | |
768 | | 0, |
769 | | pkey_rsa_sign, |
770 | | |
771 | | 0, |
772 | | pkey_rsa_verify, |
773 | | |
774 | | 0, |
775 | | pkey_rsa_verifyrecover, |
776 | | |
777 | | 0, 0, 0, 0, |
778 | | |
779 | | 0, |
780 | | pkey_rsa_encrypt, |
781 | | |
782 | | 0, |
783 | | pkey_rsa_decrypt, |
784 | | |
785 | | 0, 0, |
786 | | |
787 | | pkey_rsa_ctrl, |
788 | | pkey_rsa_ctrl_str |
789 | | }; |
790 | | |
791 | | /* |
792 | | * Called for PSS sign or verify initialisation: checks PSS parameter |
793 | | * sanity and sets any restrictions on key usage. |
794 | | */ |
795 | | |
796 | | static int pkey_pss_init(EVP_PKEY_CTX *ctx) |
797 | 0 | { |
798 | 0 | RSA *rsa; |
799 | 0 | RSA_PKEY_CTX *rctx = ctx->data; |
800 | 0 | const EVP_MD *md; |
801 | 0 | const EVP_MD *mgf1md; |
802 | 0 | int min_saltlen, max_saltlen; |
803 | 0 |
|
804 | 0 | /* Should never happen */ |
805 | 0 | if (!pkey_ctx_is_pss(ctx)) |
806 | 0 | return 0; |
807 | 0 | rsa = ctx->pkey->pkey.rsa; |
808 | 0 | /* If no restrictions just return */ |
809 | 0 | if (rsa->pss == NULL) |
810 | 0 | return 1; |
811 | 0 | /* Get and check parameters */ |
812 | 0 | if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen)) |
813 | 0 | return 0; |
814 | 0 | |
815 | 0 | /* See if minimum salt length exceeds maximum possible */ |
816 | 0 | max_saltlen = RSA_size(rsa) - EVP_MD_size(md); |
817 | 0 | if ((RSA_bits(rsa) & 0x7) == 1) |
818 | 0 | max_saltlen--; |
819 | 0 | if (min_saltlen > max_saltlen) { |
820 | 0 | RSAerr(RSA_F_PKEY_PSS_INIT, RSA_R_INVALID_SALT_LENGTH); |
821 | 0 | return 0; |
822 | 0 | } |
823 | 0 |
|
824 | 0 | rctx->min_saltlen = min_saltlen; |
825 | 0 |
|
826 | 0 | /* |
827 | 0 | * Set PSS restrictions as defaults: we can then block any attempt to |
828 | 0 | * use invalid values in pkey_rsa_ctrl |
829 | 0 | */ |
830 | 0 |
|
831 | 0 | rctx->md = md; |
832 | 0 | rctx->mgf1md = mgf1md; |
833 | 0 | rctx->saltlen = min_saltlen; |
834 | 0 |
|
835 | 0 | return 1; |
836 | 0 | } |
837 | | |
838 | | const EVP_PKEY_METHOD rsa_pss_pkey_meth = { |
839 | | EVP_PKEY_RSA_PSS, |
840 | | EVP_PKEY_FLAG_AUTOARGLEN, |
841 | | pkey_rsa_init, |
842 | | pkey_rsa_copy, |
843 | | pkey_rsa_cleanup, |
844 | | |
845 | | 0, 0, |
846 | | |
847 | | 0, |
848 | | pkey_rsa_keygen, |
849 | | |
850 | | pkey_pss_init, |
851 | | pkey_rsa_sign, |
852 | | |
853 | | pkey_pss_init, |
854 | | pkey_rsa_verify, |
855 | | |
856 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
857 | | |
858 | | pkey_rsa_ctrl, |
859 | | pkey_rsa_ctrl_str |
860 | | }; |