]>
Commit | Line | Data |
---|---|---|
09b88a4a | 1 | /* crypto/rsa/rsa_pmeth.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 2006. | |
0b6f3c66 DSH |
5 | */ |
6 | /* ==================================================================== | |
7 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
0b6f3c66 DSH |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include "cryptlib.h" | |
62 | #include <openssl/asn1t.h> | |
63 | #include <openssl/x509.h> | |
64 | #include <openssl/rsa.h> | |
1e26a8ba | 65 | #include <openssl/bn.h> |
b2a97be7 | 66 | #include <openssl/evp.h> |
271fef0e | 67 | #include <openssl/x509v3.h> |
b3339050 | 68 | #ifndef OPENSSL_NO_CMS |
0f113f3e | 69 | # include <openssl/cms.h> |
b3339050 | 70 | #endif |
27af42f9 | 71 | #include "internal/evp_int.h" |
777c47ac | 72 | #include "rsa_locl.h" |
b2a97be7 | 73 | |
07e970c7 DSH |
74 | /* RSA pkey context structure */ |
75 | ||
0f113f3e MC |
76 | typedef struct { |
77 | /* Key gen parameters */ | |
78 | int nbits; | |
79 | BIGNUM *pub_exp; | |
80 | /* Keygen callback info */ | |
81 | int gentmp[2]; | |
82 | /* RSA padding mode */ | |
83 | int pad_mode; | |
84 | /* message digest */ | |
85 | const EVP_MD *md; | |
86 | /* message digest for MGF1 */ | |
87 | const EVP_MD *mgf1md; | |
88 | /* PSS salt length */ | |
89 | int saltlen; | |
90 | /* Temp buffer */ | |
91 | unsigned char *tbuf; | |
92 | /* OAEP label */ | |
93 | unsigned char *oaep_label; | |
94 | size_t oaep_labellen; | |
95 | } RSA_PKEY_CTX; | |
07e970c7 DSH |
96 | |
97 | static int pkey_rsa_init(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
98 | { |
99 | RSA_PKEY_CTX *rctx; | |
b4faea50 | 100 | rctx = OPENSSL_malloc(sizeof(*rctx)); |
0f113f3e MC |
101 | if (!rctx) |
102 | return 0; | |
103 | rctx->nbits = 1024; | |
104 | rctx->pub_exp = NULL; | |
105 | rctx->pad_mode = RSA_PKCS1_PADDING; | |
106 | rctx->md = NULL; | |
107 | rctx->mgf1md = NULL; | |
108 | rctx->tbuf = NULL; | |
109 | ||
110 | rctx->saltlen = -2; | |
111 | ||
112 | rctx->oaep_label = NULL; | |
113 | rctx->oaep_labellen = 0; | |
114 | ||
115 | ctx->data = rctx; | |
116 | ctx->keygen_info = rctx->gentmp; | |
117 | ctx->keygen_info_count = 2; | |
118 | ||
119 | return 1; | |
120 | } | |
b2a97be7 | 121 | |
8bdcef40 | 122 | static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
0f113f3e MC |
123 | { |
124 | RSA_PKEY_CTX *dctx, *sctx; | |
125 | if (!pkey_rsa_init(dst)) | |
126 | return 0; | |
127 | sctx = src->data; | |
128 | dctx = dst->data; | |
129 | dctx->nbits = sctx->nbits; | |
130 | if (sctx->pub_exp) { | |
131 | dctx->pub_exp = BN_dup(sctx->pub_exp); | |
132 | if (!dctx->pub_exp) | |
133 | return 0; | |
134 | } | |
135 | dctx->pad_mode = sctx->pad_mode; | |
136 | dctx->md = sctx->md; | |
137 | dctx->mgf1md = sctx->mgf1md; | |
138 | if (sctx->oaep_label) { | |
b548a1f1 | 139 | OPENSSL_free(dctx->oaep_label); |
0f113f3e MC |
140 | dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen); |
141 | if (!dctx->oaep_label) | |
142 | return 0; | |
143 | dctx->oaep_labellen = sctx->oaep_labellen; | |
144 | } | |
145 | return 1; | |
146 | } | |
8bdcef40 | 147 | |
b2a97be7 | 148 | static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) |
0f113f3e MC |
149 | { |
150 | if (ctx->tbuf) | |
151 | return 1; | |
152 | ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey)); | |
153 | if (!ctx->tbuf) | |
154 | return 0; | |
155 | return 1; | |
156 | } | |
07e970c7 DSH |
157 | |
158 | static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
159 | { |
160 | RSA_PKEY_CTX *rctx = ctx->data; | |
161 | if (rctx) { | |
23a1d5e9 | 162 | BN_free(rctx->pub_exp); |
b548a1f1 RS |
163 | OPENSSL_free(rctx->tbuf); |
164 | OPENSSL_free(rctx->oaep_label); | |
0f113f3e MC |
165 | OPENSSL_free(rctx); |
166 | } | |
167 | } | |
168 | ||
169 | static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, | |
170 | size_t *siglen, const unsigned char *tbs, | |
171 | size_t tbslen) | |
172 | { | |
173 | int ret; | |
174 | RSA_PKEY_CTX *rctx = ctx->data; | |
175 | RSA *rsa = ctx->pkey->pkey.rsa; | |
176 | ||
177 | if (rctx->md) { | |
178 | if (tbslen != (size_t)EVP_MD_size(rctx->md)) { | |
179 | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH); | |
180 | return -1; | |
181 | } | |
182 | ||
183 | if (EVP_MD_type(rctx->md) == NID_mdc2) { | |
184 | unsigned int sltmp; | |
185 | if (rctx->pad_mode != RSA_PKCS1_PADDING) | |
186 | return -1; | |
187 | ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, | |
188 | tbs, tbslen, sig, &sltmp, rsa); | |
189 | ||
190 | if (ret <= 0) | |
191 | return ret; | |
192 | ret = sltmp; | |
193 | } else if (rctx->pad_mode == RSA_X931_PADDING) { | |
34166d41 MC |
194 | if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) { |
195 | RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL); | |
0f113f3e | 196 | return -1; |
34166d41 MC |
197 | } |
198 | if (!setup_tbuf(rctx, ctx)) { | |
199 | RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE); | |
200 | return -1; | |
201 | } | |
0f113f3e MC |
202 | memcpy(rctx->tbuf, tbs, tbslen); |
203 | rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md)); | |
204 | ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, | |
205 | sig, rsa, RSA_X931_PADDING); | |
206 | } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { | |
207 | unsigned int sltmp; | |
208 | ret = RSA_sign(EVP_MD_type(rctx->md), | |
209 | tbs, tbslen, sig, &sltmp, rsa); | |
210 | if (ret <= 0) | |
211 | return ret; | |
212 | ret = sltmp; | |
213 | } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { | |
214 | if (!setup_tbuf(rctx, ctx)) | |
215 | return -1; | |
216 | if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, | |
217 | rctx->tbuf, tbs, | |
218 | rctx->md, rctx->mgf1md, | |
219 | rctx->saltlen)) | |
220 | return -1; | |
221 | ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf, | |
222 | sig, rsa, RSA_NO_PADDING); | |
223 | } else | |
224 | return -1; | |
225 | } else | |
226 | ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa, | |
227 | rctx->pad_mode); | |
228 | if (ret < 0) | |
229 | return ret; | |
230 | *siglen = ret; | |
231 | return 1; | |
232 | } | |
07e970c7 DSH |
233 | |
234 | static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
235 | unsigned char *rout, size_t *routlen, |
236 | const unsigned char *sig, size_t siglen) | |
237 | { | |
238 | int ret; | |
239 | RSA_PKEY_CTX *rctx = ctx->data; | |
240 | ||
241 | if (rctx->md) { | |
242 | if (rctx->pad_mode == RSA_X931_PADDING) { | |
243 | if (!setup_tbuf(rctx, ctx)) | |
244 | return -1; | |
245 | ret = RSA_public_decrypt(siglen, sig, | |
246 | rctx->tbuf, ctx->pkey->pkey.rsa, | |
247 | RSA_X931_PADDING); | |
248 | if (ret < 1) | |
249 | return 0; | |
250 | ret--; | |
251 | if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) { | |
252 | RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, | |
253 | RSA_R_ALGORITHM_MISMATCH); | |
254 | return 0; | |
255 | } | |
256 | if (ret != EVP_MD_size(rctx->md)) { | |
257 | RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, | |
258 | RSA_R_INVALID_DIGEST_LENGTH); | |
259 | return 0; | |
260 | } | |
261 | if (rout) | |
262 | memcpy(rout, rctx->tbuf, ret); | |
263 | } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { | |
264 | size_t sltmp; | |
265 | ret = int_rsa_verify(EVP_MD_type(rctx->md), | |
266 | NULL, 0, rout, &sltmp, | |
267 | sig, siglen, ctx->pkey->pkey.rsa); | |
268 | if (ret <= 0) | |
269 | return 0; | |
270 | ret = sltmp; | |
271 | } else | |
272 | return -1; | |
273 | } else | |
274 | ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa, | |
275 | rctx->pad_mode); | |
276 | if (ret < 0) | |
277 | return ret; | |
278 | *routlen = ret; | |
279 | return 1; | |
280 | } | |
07e970c7 | 281 | |
4f59b658 | 282 | static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
283 | const unsigned char *sig, size_t siglen, |
284 | const unsigned char *tbs, size_t tbslen) | |
285 | { | |
286 | RSA_PKEY_CTX *rctx = ctx->data; | |
287 | RSA *rsa = ctx->pkey->pkey.rsa; | |
288 | size_t rslen; | |
289 | if (rctx->md) { | |
290 | if (rctx->pad_mode == RSA_PKCS1_PADDING) | |
291 | return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, | |
292 | sig, siglen, rsa); | |
293 | if (rctx->pad_mode == RSA_X931_PADDING) { | |
294 | if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0) | |
295 | return 0; | |
296 | } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { | |
297 | int ret; | |
298 | if (!setup_tbuf(rctx, ctx)) | |
299 | return -1; | |
300 | ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, | |
301 | rsa, RSA_NO_PADDING); | |
302 | if (ret <= 0) | |
303 | return 0; | |
304 | ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, | |
305 | rctx->md, rctx->mgf1md, | |
306 | rctx->tbuf, rctx->saltlen); | |
307 | if (ret <= 0) | |
308 | return 0; | |
309 | return 1; | |
310 | } else | |
311 | return -1; | |
312 | } else { | |
313 | if (!setup_tbuf(rctx, ctx)) | |
314 | return -1; | |
315 | rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, | |
316 | rsa, rctx->pad_mode); | |
317 | if (rslen == 0) | |
318 | return 0; | |
319 | } | |
320 | ||
321 | if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen)) | |
322 | return 0; | |
323 | ||
324 | return 1; | |
325 | ||
326 | } | |
4f59b658 | 327 | |
eaff5a14 | 328 | static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
329 | unsigned char *out, size_t *outlen, |
330 | const unsigned char *in, size_t inlen) | |
331 | { | |
332 | int ret; | |
333 | RSA_PKEY_CTX *rctx = ctx->data; | |
334 | if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { | |
335 | int klen = RSA_size(ctx->pkey->pkey.rsa); | |
336 | if (!setup_tbuf(rctx, ctx)) | |
337 | return -1; | |
338 | if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen, | |
339 | in, inlen, | |
340 | rctx->oaep_label, | |
341 | rctx->oaep_labellen, | |
342 | rctx->md, rctx->mgf1md)) | |
343 | return -1; | |
344 | ret = RSA_public_encrypt(klen, rctx->tbuf, out, | |
345 | ctx->pkey->pkey.rsa, RSA_NO_PADDING); | |
346 | } else | |
347 | ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa, | |
348 | rctx->pad_mode); | |
349 | if (ret < 0) | |
350 | return ret; | |
351 | *outlen = ret; | |
352 | return 1; | |
353 | } | |
8cd44e36 | 354 | |
eaff5a14 | 355 | static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
356 | unsigned char *out, size_t *outlen, |
357 | const unsigned char *in, size_t inlen) | |
358 | { | |
359 | int ret; | |
360 | RSA_PKEY_CTX *rctx = ctx->data; | |
361 | if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { | |
362 | int i; | |
363 | if (!setup_tbuf(rctx, ctx)) | |
364 | return -1; | |
365 | ret = RSA_private_decrypt(inlen, in, rctx->tbuf, | |
366 | ctx->pkey->pkey.rsa, RSA_NO_PADDING); | |
367 | if (ret <= 0) | |
368 | return ret; | |
369 | for (i = 0; i < ret; i++) { | |
370 | if (rctx->tbuf[i]) | |
371 | break; | |
372 | } | |
373 | ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i, | |
374 | ret - i, ret, | |
375 | rctx->oaep_label, | |
376 | rctx->oaep_labellen, | |
377 | rctx->md, rctx->mgf1md); | |
378 | } else | |
379 | ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa, | |
380 | rctx->pad_mode); | |
381 | if (ret < 0) | |
382 | return ret; | |
383 | *outlen = ret; | |
384 | return 1; | |
385 | } | |
07e970c7 | 386 | |
75d44c04 | 387 | static int check_padding_md(const EVP_MD *md, int padding) |
0f113f3e MC |
388 | { |
389 | if (!md) | |
390 | return 1; | |
391 | ||
392 | if (padding == RSA_NO_PADDING) { | |
393 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE); | |
394 | return 0; | |
395 | } | |
396 | ||
397 | if (padding == RSA_X931_PADDING) { | |
398 | if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) { | |
399 | RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST); | |
400 | return 0; | |
401 | } | |
402 | return 1; | |
403 | } | |
404 | ||
405 | return 1; | |
406 | } | |
b2a97be7 | 407 | |
4a3dc3c0 | 408 | static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) |
0f113f3e MC |
409 | { |
410 | RSA_PKEY_CTX *rctx = ctx->data; | |
411 | switch (type) { | |
412 | case EVP_PKEY_CTRL_RSA_PADDING: | |
413 | if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) { | |
414 | if (!check_padding_md(rctx->md, p1)) | |
415 | return 0; | |
416 | if (p1 == RSA_PKCS1_PSS_PADDING) { | |
417 | if (!(ctx->operation & | |
418 | (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) | |
419 | goto bad_pad; | |
420 | if (!rctx->md) | |
421 | rctx->md = EVP_sha1(); | |
422 | } | |
423 | if (p1 == RSA_PKCS1_OAEP_PADDING) { | |
424 | if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT)) | |
425 | goto bad_pad; | |
426 | if (!rctx->md) | |
427 | rctx->md = EVP_sha1(); | |
428 | } | |
429 | rctx->pad_mode = p1; | |
430 | return 1; | |
431 | } | |
432 | bad_pad: | |
433 | RSAerr(RSA_F_PKEY_RSA_CTRL, | |
434 | RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); | |
435 | return -2; | |
436 | ||
437 | case EVP_PKEY_CTRL_GET_RSA_PADDING: | |
438 | *(int *)p2 = rctx->pad_mode; | |
439 | return 1; | |
440 | ||
441 | case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: | |
442 | case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: | |
443 | if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { | |
444 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN); | |
445 | return -2; | |
446 | } | |
447 | if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) | |
448 | *(int *)p2 = rctx->saltlen; | |
449 | else { | |
450 | if (p1 < -2) | |
451 | return -2; | |
452 | rctx->saltlen = p1; | |
453 | } | |
454 | return 1; | |
455 | ||
456 | case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: | |
457 | if (p1 < 512) { | |
458 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL); | |
459 | return -2; | |
460 | } | |
461 | rctx->nbits = p1; | |
462 | return 1; | |
463 | ||
464 | case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: | |
465 | if (!p2) | |
466 | return -2; | |
467 | BN_free(rctx->pub_exp); | |
468 | rctx->pub_exp = p2; | |
469 | return 1; | |
470 | ||
471 | case EVP_PKEY_CTRL_RSA_OAEP_MD: | |
472 | case EVP_PKEY_CTRL_GET_RSA_OAEP_MD: | |
473 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | |
474 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); | |
475 | return -2; | |
476 | } | |
477 | if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) | |
478 | *(const EVP_MD **)p2 = rctx->md; | |
479 | else | |
480 | rctx->md = p2; | |
481 | return 1; | |
482 | ||
483 | case EVP_PKEY_CTRL_MD: | |
484 | if (!check_padding_md(p2, rctx->pad_mode)) | |
485 | return 0; | |
486 | rctx->md = p2; | |
487 | return 1; | |
488 | ||
489 | case EVP_PKEY_CTRL_GET_MD: | |
490 | *(const EVP_MD **)p2 = rctx->md; | |
491 | return 1; | |
492 | ||
493 | case EVP_PKEY_CTRL_RSA_MGF1_MD: | |
494 | case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: | |
495 | if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING | |
496 | && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | |
497 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD); | |
498 | return -2; | |
499 | } | |
500 | if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) { | |
501 | if (rctx->mgf1md) | |
502 | *(const EVP_MD **)p2 = rctx->mgf1md; | |
503 | else | |
504 | *(const EVP_MD **)p2 = rctx->md; | |
505 | } else | |
506 | rctx->mgf1md = p2; | |
507 | return 1; | |
508 | ||
509 | case EVP_PKEY_CTRL_RSA_OAEP_LABEL: | |
510 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | |
511 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); | |
512 | return -2; | |
513 | } | |
b548a1f1 | 514 | OPENSSL_free(rctx->oaep_label); |
0f113f3e MC |
515 | if (p2 && p1 > 0) { |
516 | rctx->oaep_label = p2; | |
517 | rctx->oaep_labellen = p1; | |
518 | } else { | |
519 | rctx->oaep_label = NULL; | |
520 | rctx->oaep_labellen = 0; | |
521 | } | |
522 | return 1; | |
523 | ||
524 | case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL: | |
525 | if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { | |
526 | RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE); | |
527 | return -2; | |
528 | } | |
529 | *(unsigned char **)p2 = rctx->oaep_label; | |
530 | return rctx->oaep_labellen; | |
531 | ||
532 | case EVP_PKEY_CTRL_DIGESTINIT: | |
533 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: | |
534 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: | |
535 | case EVP_PKEY_CTRL_PKCS7_SIGN: | |
536 | return 1; | |
8931b30d | 537 | #ifndef OPENSSL_NO_CMS |
0f113f3e MC |
538 | case EVP_PKEY_CTRL_CMS_DECRYPT: |
539 | case EVP_PKEY_CTRL_CMS_ENCRYPT: | |
540 | case EVP_PKEY_CTRL_CMS_SIGN: | |
541 | return 1; | |
b3339050 | 542 | #endif |
0f113f3e MC |
543 | case EVP_PKEY_CTRL_PEER_KEY: |
544 | RSAerr(RSA_F_PKEY_RSA_CTRL, | |
545 | RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
546 | return -2; | |
547 | ||
548 | default: | |
549 | return -2; | |
399a6f0b | 550 | |
0f113f3e MC |
551 | } |
552 | } | |
4a3dc3c0 | 553 | |
4a3dc3c0 | 554 | static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
555 | const char *type, const char *value) |
556 | { | |
557 | if (!value) { | |
558 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING); | |
559 | return 0; | |
560 | } | |
86885c28 | 561 | if (strcmp(type, "rsa_padding_mode") == 0) { |
0f113f3e | 562 | int pm; |
86885c28 | 563 | if (strcmp(value, "pkcs1") == 0) |
0f113f3e | 564 | pm = RSA_PKCS1_PADDING; |
86885c28 | 565 | else if (strcmp(value, "sslv23") == 0) |
0f113f3e | 566 | pm = RSA_SSLV23_PADDING; |
86885c28 | 567 | else if (strcmp(value, "none") == 0) |
0f113f3e | 568 | pm = RSA_NO_PADDING; |
86885c28 | 569 | else if (strcmp(value, "oeap") == 0) |
0f113f3e | 570 | pm = RSA_PKCS1_OAEP_PADDING; |
86885c28 | 571 | else if (strcmp(value, "oaep") == 0) |
0f113f3e | 572 | pm = RSA_PKCS1_OAEP_PADDING; |
86885c28 | 573 | else if (strcmp(value, "x931") == 0) |
0f113f3e | 574 | pm = RSA_X931_PADDING; |
86885c28 | 575 | else if (strcmp(value, "pss") == 0) |
0f113f3e MC |
576 | pm = RSA_PKCS1_PSS_PADDING; |
577 | else { | |
578 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE); | |
579 | return -2; | |
580 | } | |
581 | return EVP_PKEY_CTX_set_rsa_padding(ctx, pm); | |
582 | } | |
583 | ||
86885c28 | 584 | if (strcmp(type, "rsa_pss_saltlen") == 0) { |
0f113f3e MC |
585 | int saltlen; |
586 | saltlen = atoi(value); | |
587 | return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen); | |
588 | } | |
589 | ||
86885c28 | 590 | if (strcmp(type, "rsa_keygen_bits") == 0) { |
0f113f3e MC |
591 | int nbits; |
592 | nbits = atoi(value); | |
593 | return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits); | |
594 | } | |
595 | ||
86885c28 | 596 | if (strcmp(type, "rsa_keygen_pubexp") == 0) { |
0f113f3e MC |
597 | int ret; |
598 | BIGNUM *pubexp = NULL; | |
599 | if (!BN_asc2bn(&pubexp, value)) | |
600 | return 0; | |
601 | ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp); | |
602 | if (ret <= 0) | |
603 | BN_free(pubexp); | |
604 | return ret; | |
605 | } | |
606 | ||
86885c28 | 607 | if (strcmp(type, "rsa_mgf1_md") == 0) { |
0f113f3e MC |
608 | const EVP_MD *md; |
609 | if (!(md = EVP_get_digestbyname(value))) { | |
610 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST); | |
611 | return 0; | |
612 | } | |
613 | return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md); | |
614 | } | |
615 | ||
86885c28 | 616 | if (strcmp(type, "rsa_oaep_md") == 0) { |
0f113f3e MC |
617 | const EVP_MD *md; |
618 | if (!(md = EVP_get_digestbyname(value))) { | |
619 | RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST); | |
620 | return 0; | |
621 | } | |
622 | return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md); | |
623 | } | |
86885c28 | 624 | if (strcmp(type, "rsa_oaep_label") == 0) { |
0f113f3e MC |
625 | unsigned char *lab; |
626 | long lablen; | |
627 | int ret; | |
628 | lab = string_to_hex(value, &lablen); | |
629 | if (!lab) | |
630 | return 0; | |
631 | ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen); | |
632 | if (ret <= 0) | |
633 | OPENSSL_free(lab); | |
634 | return ret; | |
635 | } | |
636 | ||
637 | return -2; | |
638 | } | |
4a3dc3c0 | 639 | |
f5cda4cb | 640 | static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) |
0f113f3e MC |
641 | { |
642 | RSA *rsa = NULL; | |
643 | RSA_PKEY_CTX *rctx = ctx->data; | |
644 | BN_GENCB *pcb; | |
645 | int ret; | |
646 | if (!rctx->pub_exp) { | |
647 | rctx->pub_exp = BN_new(); | |
648 | if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) | |
649 | return 0; | |
650 | } | |
651 | rsa = RSA_new(); | |
652 | if (!rsa) | |
653 | return 0; | |
654 | if (ctx->pkey_gencb) { | |
655 | pcb = BN_GENCB_new(); | |
656 | if (!pcb) { | |
657 | RSA_free(rsa); | |
658 | return 0; | |
659 | } | |
660 | evp_pkey_set_cb_translate(pcb, ctx); | |
661 | } else | |
662 | pcb = NULL; | |
663 | ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb); | |
664 | BN_GENCB_free(pcb); | |
665 | if (ret > 0) | |
666 | EVP_PKEY_assign_RSA(pkey, rsa); | |
667 | else | |
668 | RSA_free(rsa); | |
669 | return ret; | |
670 | } | |
671 | ||
672 | const EVP_PKEY_METHOD rsa_pkey_meth = { | |
673 | EVP_PKEY_RSA, | |
674 | EVP_PKEY_FLAG_AUTOARGLEN, | |
675 | pkey_rsa_init, | |
676 | pkey_rsa_copy, | |
677 | pkey_rsa_cleanup, | |
678 | ||
679 | 0, 0, | |
680 | ||
681 | 0, | |
682 | pkey_rsa_keygen, | |
683 | ||
684 | 0, | |
685 | pkey_rsa_sign, | |
686 | ||
687 | 0, | |
688 | pkey_rsa_verify, | |
689 | ||
690 | 0, | |
691 | pkey_rsa_verifyrecover, | |
692 | ||
693 | 0, 0, 0, 0, | |
694 | ||
695 | 0, | |
696 | pkey_rsa_encrypt, | |
697 | ||
698 | 0, | |
699 | pkey_rsa_decrypt, | |
700 | ||
701 | 0, 0, | |
702 | ||
703 | pkey_rsa_ctrl, | |
704 | pkey_rsa_ctrl_str | |
705 | }; |