]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_pmeth.c
Rename some BUF_xxx to OPENSSL_xxx
[thirdparty/openssl.git] / crypto / rsa / rsa_pmeth.c
CommitLineData
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>
b39fc560 61#include "internal/cryptlib.h"
0b6f3c66
DSH
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
76typedef 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
97static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
0f113f3e
MC
98{
99 RSA_PKEY_CTX *rctx;
64b25758 100 rctx = OPENSSL_zalloc(sizeof(*rctx));
90945fa3 101 if (rctx == NULL)
0f113f3e
MC
102 return 0;
103 rctx->nbits = 1024;
0f113f3e 104 rctx->pad_mode = RSA_PKCS1_PADDING;
0f113f3e 105 rctx->saltlen = -2;
0f113f3e
MC
106 ctx->data = rctx;
107 ctx->keygen_info = rctx->gentmp;
108 ctx->keygen_info_count = 2;
109
110 return 1;
111}
b2a97be7 112
8bdcef40 113static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
0f113f3e
MC
114{
115 RSA_PKEY_CTX *dctx, *sctx;
116 if (!pkey_rsa_init(dst))
117 return 0;
118 sctx = src->data;
119 dctx = dst->data;
120 dctx->nbits = sctx->nbits;
121 if (sctx->pub_exp) {
122 dctx->pub_exp = BN_dup(sctx->pub_exp);
123 if (!dctx->pub_exp)
124 return 0;
125 }
126 dctx->pad_mode = sctx->pad_mode;
127 dctx->md = sctx->md;
128 dctx->mgf1md = sctx->mgf1md;
129 if (sctx->oaep_label) {
b548a1f1 130 OPENSSL_free(dctx->oaep_label);
7644a9ae 131 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
0f113f3e
MC
132 if (!dctx->oaep_label)
133 return 0;
134 dctx->oaep_labellen = sctx->oaep_labellen;
135 }
136 return 1;
137}
8bdcef40 138
b2a97be7 139static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
0f113f3e
MC
140{
141 if (ctx->tbuf)
142 return 1;
143 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
90945fa3 144 if (ctx->tbuf == NULL)
0f113f3e
MC
145 return 0;
146 return 1;
147}
07e970c7
DSH
148
149static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
0f113f3e
MC
150{
151 RSA_PKEY_CTX *rctx = ctx->data;
152 if (rctx) {
23a1d5e9 153 BN_free(rctx->pub_exp);
b548a1f1
RS
154 OPENSSL_free(rctx->tbuf);
155 OPENSSL_free(rctx->oaep_label);
0f113f3e
MC
156 OPENSSL_free(rctx);
157 }
158}
159
160static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
161 size_t *siglen, const unsigned char *tbs,
162 size_t tbslen)
163{
164 int ret;
165 RSA_PKEY_CTX *rctx = ctx->data;
166 RSA *rsa = ctx->pkey->pkey.rsa;
167
168 if (rctx->md) {
169 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
170 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
171 return -1;
172 }
173
174 if (EVP_MD_type(rctx->md) == NID_mdc2) {
175 unsigned int sltmp;
176 if (rctx->pad_mode != RSA_PKCS1_PADDING)
177 return -1;
178 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
179 tbs, tbslen, sig, &sltmp, rsa);
180
181 if (ret <= 0)
182 return ret;
183 ret = sltmp;
184 } else if (rctx->pad_mode == RSA_X931_PADDING) {
34166d41
MC
185 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
186 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
0f113f3e 187 return -1;
34166d41
MC
188 }
189 if (!setup_tbuf(rctx, ctx)) {
190 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
191 return -1;
192 }
0f113f3e
MC
193 memcpy(rctx->tbuf, tbs, tbslen);
194 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
195 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
196 sig, rsa, RSA_X931_PADDING);
197 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
198 unsigned int sltmp;
199 ret = RSA_sign(EVP_MD_type(rctx->md),
200 tbs, tbslen, sig, &sltmp, rsa);
201 if (ret <= 0)
202 return ret;
203 ret = sltmp;
204 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
205 if (!setup_tbuf(rctx, ctx))
206 return -1;
207 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
208 rctx->tbuf, tbs,
209 rctx->md, rctx->mgf1md,
210 rctx->saltlen))
211 return -1;
212 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
213 sig, rsa, RSA_NO_PADDING);
214 } else
215 return -1;
216 } else
217 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
218 rctx->pad_mode);
219 if (ret < 0)
220 return ret;
221 *siglen = ret;
222 return 1;
223}
07e970c7
DSH
224
225static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
0f113f3e
MC
226 unsigned char *rout, size_t *routlen,
227 const unsigned char *sig, size_t siglen)
228{
229 int ret;
230 RSA_PKEY_CTX *rctx = ctx->data;
231
232 if (rctx->md) {
233 if (rctx->pad_mode == RSA_X931_PADDING) {
234 if (!setup_tbuf(rctx, ctx))
235 return -1;
236 ret = RSA_public_decrypt(siglen, sig,
237 rctx->tbuf, ctx->pkey->pkey.rsa,
238 RSA_X931_PADDING);
239 if (ret < 1)
240 return 0;
241 ret--;
242 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
243 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
244 RSA_R_ALGORITHM_MISMATCH);
245 return 0;
246 }
247 if (ret != EVP_MD_size(rctx->md)) {
248 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
249 RSA_R_INVALID_DIGEST_LENGTH);
250 return 0;
251 }
252 if (rout)
253 memcpy(rout, rctx->tbuf, ret);
254 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
255 size_t sltmp;
256 ret = int_rsa_verify(EVP_MD_type(rctx->md),
257 NULL, 0, rout, &sltmp,
258 sig, siglen, ctx->pkey->pkey.rsa);
259 if (ret <= 0)
260 return 0;
261 ret = sltmp;
262 } else
263 return -1;
264 } else
265 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
266 rctx->pad_mode);
267 if (ret < 0)
268 return ret;
269 *routlen = ret;
270 return 1;
271}
07e970c7 272
4f59b658 273static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
0f113f3e
MC
274 const unsigned char *sig, size_t siglen,
275 const unsigned char *tbs, size_t tbslen)
276{
277 RSA_PKEY_CTX *rctx = ctx->data;
278 RSA *rsa = ctx->pkey->pkey.rsa;
279 size_t rslen;
280 if (rctx->md) {
281 if (rctx->pad_mode == RSA_PKCS1_PADDING)
282 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
283 sig, siglen, rsa);
284 if (rctx->pad_mode == RSA_X931_PADDING) {
285 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
286 return 0;
287 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
288 int ret;
289 if (!setup_tbuf(rctx, ctx))
290 return -1;
291 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
292 rsa, RSA_NO_PADDING);
293 if (ret <= 0)
294 return 0;
295 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
296 rctx->md, rctx->mgf1md,
297 rctx->tbuf, rctx->saltlen);
298 if (ret <= 0)
299 return 0;
300 return 1;
301 } else
302 return -1;
303 } else {
304 if (!setup_tbuf(rctx, ctx))
305 return -1;
306 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
307 rsa, rctx->pad_mode);
308 if (rslen == 0)
309 return 0;
310 }
311
312 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
313 return 0;
314
315 return 1;
316
317}
4f59b658 318
eaff5a14 319static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
0f113f3e
MC
320 unsigned char *out, size_t *outlen,
321 const unsigned char *in, size_t inlen)
322{
323 int ret;
324 RSA_PKEY_CTX *rctx = ctx->data;
325 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
326 int klen = RSA_size(ctx->pkey->pkey.rsa);
327 if (!setup_tbuf(rctx, ctx))
328 return -1;
329 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
330 in, inlen,
331 rctx->oaep_label,
332 rctx->oaep_labellen,
333 rctx->md, rctx->mgf1md))
334 return -1;
335 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
336 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
337 } else
338 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
339 rctx->pad_mode);
340 if (ret < 0)
341 return ret;
342 *outlen = ret;
343 return 1;
344}
8cd44e36 345
eaff5a14 346static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
0f113f3e
MC
347 unsigned char *out, size_t *outlen,
348 const unsigned char *in, size_t inlen)
349{
350 int ret;
351 RSA_PKEY_CTX *rctx = ctx->data;
352 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
353 int i;
354 if (!setup_tbuf(rctx, ctx))
355 return -1;
356 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
357 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
358 if (ret <= 0)
359 return ret;
360 for (i = 0; i < ret; i++) {
361 if (rctx->tbuf[i])
362 break;
363 }
364 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
365 ret - i, ret,
366 rctx->oaep_label,
367 rctx->oaep_labellen,
368 rctx->md, rctx->mgf1md);
369 } else
370 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
371 rctx->pad_mode);
372 if (ret < 0)
373 return ret;
374 *outlen = ret;
375 return 1;
376}
07e970c7 377
75d44c04 378static int check_padding_md(const EVP_MD *md, int padding)
0f113f3e 379{
7f572e95 380 int mdnid;
0f113f3e
MC
381 if (!md)
382 return 1;
383
7f572e95
DSH
384 mdnid = EVP_MD_type(md);
385
0f113f3e
MC
386 if (padding == RSA_NO_PADDING) {
387 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
388 return 0;
389 }
390
391 if (padding == RSA_X931_PADDING) {
7f572e95 392 if (RSA_X931_hash_id(mdnid) == -1) {
0f113f3e
MC
393 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
394 return 0;
395 }
7f572e95
DSH
396 } else {
397 switch(mdnid) {
398 /* List of all supported RSA digests */
399 case NID_sha1:
400 case NID_sha224:
401 case NID_sha256:
402 case NID_sha384:
403 case NID_sha512:
404 case NID_md5:
405 case NID_md5_sha1:
406 case NID_md2:
407 case NID_md4:
408 case NID_mdc2:
409 case NID_ripemd160:
410 return 1;
411
412 default:
413 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
414 return 0;
415
416 }
0f113f3e
MC
417 }
418
419 return 1;
420}
b2a97be7 421
4a3dc3c0 422static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
0f113f3e
MC
423{
424 RSA_PKEY_CTX *rctx = ctx->data;
425 switch (type) {
426 case EVP_PKEY_CTRL_RSA_PADDING:
427 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
428 if (!check_padding_md(rctx->md, p1))
429 return 0;
430 if (p1 == RSA_PKCS1_PSS_PADDING) {
431 if (!(ctx->operation &
432 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
433 goto bad_pad;
434 if (!rctx->md)
435 rctx->md = EVP_sha1();
436 }
437 if (p1 == RSA_PKCS1_OAEP_PADDING) {
438 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
439 goto bad_pad;
440 if (!rctx->md)
441 rctx->md = EVP_sha1();
442 }
443 rctx->pad_mode = p1;
444 return 1;
445 }
446 bad_pad:
447 RSAerr(RSA_F_PKEY_RSA_CTRL,
448 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
449 return -2;
450
451 case EVP_PKEY_CTRL_GET_RSA_PADDING:
452 *(int *)p2 = rctx->pad_mode;
453 return 1;
454
455 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
456 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
457 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
458 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
459 return -2;
460 }
461 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
462 *(int *)p2 = rctx->saltlen;
463 else {
464 if (p1 < -2)
465 return -2;
466 rctx->saltlen = p1;
467 }
468 return 1;
469
470 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
471 if (p1 < 512) {
472 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
473 return -2;
474 }
475 rctx->nbits = p1;
476 return 1;
477
478 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
479 if (!p2)
480 return -2;
481 BN_free(rctx->pub_exp);
482 rctx->pub_exp = p2;
483 return 1;
484
485 case EVP_PKEY_CTRL_RSA_OAEP_MD:
486 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
487 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
488 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
489 return -2;
490 }
491 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
492 *(const EVP_MD **)p2 = rctx->md;
493 else
494 rctx->md = p2;
495 return 1;
496
497 case EVP_PKEY_CTRL_MD:
498 if (!check_padding_md(p2, rctx->pad_mode))
499 return 0;
500 rctx->md = p2;
501 return 1;
502
503 case EVP_PKEY_CTRL_GET_MD:
504 *(const EVP_MD **)p2 = rctx->md;
505 return 1;
506
507 case EVP_PKEY_CTRL_RSA_MGF1_MD:
508 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
509 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
510 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
511 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
512 return -2;
513 }
514 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
515 if (rctx->mgf1md)
516 *(const EVP_MD **)p2 = rctx->mgf1md;
517 else
518 *(const EVP_MD **)p2 = rctx->md;
519 } else
520 rctx->mgf1md = p2;
521 return 1;
522
523 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
524 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
525 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
526 return -2;
527 }
b548a1f1 528 OPENSSL_free(rctx->oaep_label);
0f113f3e
MC
529 if (p2 && p1 > 0) {
530 rctx->oaep_label = p2;
531 rctx->oaep_labellen = p1;
532 } else {
533 rctx->oaep_label = NULL;
534 rctx->oaep_labellen = 0;
535 }
536 return 1;
537
538 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
539 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
540 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
541 return -2;
542 }
543 *(unsigned char **)p2 = rctx->oaep_label;
544 return rctx->oaep_labellen;
545
546 case EVP_PKEY_CTRL_DIGESTINIT:
547 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
548 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
549 case EVP_PKEY_CTRL_PKCS7_SIGN:
550 return 1;
8931b30d 551#ifndef OPENSSL_NO_CMS
0f113f3e
MC
552 case EVP_PKEY_CTRL_CMS_DECRYPT:
553 case EVP_PKEY_CTRL_CMS_ENCRYPT:
554 case EVP_PKEY_CTRL_CMS_SIGN:
555 return 1;
b3339050 556#endif
0f113f3e
MC
557 case EVP_PKEY_CTRL_PEER_KEY:
558 RSAerr(RSA_F_PKEY_RSA_CTRL,
559 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
560 return -2;
561
562 default:
563 return -2;
399a6f0b 564
0f113f3e
MC
565 }
566}
4a3dc3c0 567
4a3dc3c0 568static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
0f113f3e
MC
569 const char *type, const char *value)
570{
571 if (!value) {
572 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
573 return 0;
574 }
86885c28 575 if (strcmp(type, "rsa_padding_mode") == 0) {
0f113f3e 576 int pm;
86885c28 577 if (strcmp(value, "pkcs1") == 0)
0f113f3e 578 pm = RSA_PKCS1_PADDING;
86885c28 579 else if (strcmp(value, "sslv23") == 0)
0f113f3e 580 pm = RSA_SSLV23_PADDING;
86885c28 581 else if (strcmp(value, "none") == 0)
0f113f3e 582 pm = RSA_NO_PADDING;
86885c28 583 else if (strcmp(value, "oeap") == 0)
0f113f3e 584 pm = RSA_PKCS1_OAEP_PADDING;
86885c28 585 else if (strcmp(value, "oaep") == 0)
0f113f3e 586 pm = RSA_PKCS1_OAEP_PADDING;
86885c28 587 else if (strcmp(value, "x931") == 0)
0f113f3e 588 pm = RSA_X931_PADDING;
86885c28 589 else if (strcmp(value, "pss") == 0)
0f113f3e
MC
590 pm = RSA_PKCS1_PSS_PADDING;
591 else {
592 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
593 return -2;
594 }
595 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
596 }
597
86885c28 598 if (strcmp(type, "rsa_pss_saltlen") == 0) {
0f113f3e
MC
599 int saltlen;
600 saltlen = atoi(value);
601 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
602 }
603
86885c28 604 if (strcmp(type, "rsa_keygen_bits") == 0) {
0f113f3e
MC
605 int nbits;
606 nbits = atoi(value);
607 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
608 }
609
86885c28 610 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
0f113f3e
MC
611 int ret;
612 BIGNUM *pubexp = NULL;
613 if (!BN_asc2bn(&pubexp, value))
614 return 0;
615 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
616 if (ret <= 0)
617 BN_free(pubexp);
618 return ret;
619 }
620
86885c28 621 if (strcmp(type, "rsa_mgf1_md") == 0) {
0f113f3e 622 const EVP_MD *md;
75ebbd9a 623 if ((md = EVP_get_digestbyname(value)) == NULL) {
0f113f3e
MC
624 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
625 return 0;
626 }
627 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
628 }
629
86885c28 630 if (strcmp(type, "rsa_oaep_md") == 0) {
0f113f3e 631 const EVP_MD *md;
75ebbd9a 632 if ((md = EVP_get_digestbyname(value)) == NULL) {
0f113f3e
MC
633 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
634 return 0;
635 }
636 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
637 }
86885c28 638 if (strcmp(type, "rsa_oaep_label") == 0) {
0f113f3e
MC
639 unsigned char *lab;
640 long lablen;
641 int ret;
642 lab = string_to_hex(value, &lablen);
643 if (!lab)
644 return 0;
645 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
646 if (ret <= 0)
647 OPENSSL_free(lab);
648 return ret;
649 }
650
651 return -2;
652}
4a3dc3c0 653
f5cda4cb 654static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
0f113f3e
MC
655{
656 RSA *rsa = NULL;
657 RSA_PKEY_CTX *rctx = ctx->data;
658 BN_GENCB *pcb;
659 int ret;
90945fa3 660 if (rctx->pub_exp == NULL) {
0f113f3e 661 rctx->pub_exp = BN_new();
90945fa3 662 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
0f113f3e
MC
663 return 0;
664 }
665 rsa = RSA_new();
90945fa3 666 if (rsa == NULL)
0f113f3e
MC
667 return 0;
668 if (ctx->pkey_gencb) {
669 pcb = BN_GENCB_new();
90945fa3 670 if (pcb == NULL) {
0f113f3e
MC
671 RSA_free(rsa);
672 return 0;
673 }
674 evp_pkey_set_cb_translate(pcb, ctx);
675 } else
676 pcb = NULL;
677 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
678 BN_GENCB_free(pcb);
679 if (ret > 0)
680 EVP_PKEY_assign_RSA(pkey, rsa);
681 else
682 RSA_free(rsa);
683 return ret;
684}
685
686const EVP_PKEY_METHOD rsa_pkey_meth = {
687 EVP_PKEY_RSA,
688 EVP_PKEY_FLAG_AUTOARGLEN,
689 pkey_rsa_init,
690 pkey_rsa_copy,
691 pkey_rsa_cleanup,
692
693 0, 0,
694
695 0,
696 pkey_rsa_keygen,
697
698 0,
699 pkey_rsa_sign,
700
701 0,
702 pkey_rsa_verify,
703
704 0,
705 pkey_rsa_verifyrecover,
706
707 0, 0, 0, 0,
708
709 0,
710 pkey_rsa_encrypt,
711
712 0,
713 pkey_rsa_decrypt,
714
715 0, 0,
716
717 pkey_rsa_ctrl,
718 pkey_rsa_ctrl_str
719};