| 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/evp.h> |
| 57 |
|
| 58 |
#include <limits.h> |
| 59 |
#include <string.h> |
| 60 |
|
| 61 |
#include <openssl/bn.h> |
| 62 |
#include <openssl/buf.h> |
| 63 |
#include <openssl/bytestring.h> |
| 64 |
#include <openssl/digest.h> |
| 65 |
#include <openssl/err.h> |
| 66 |
#include <openssl/mem.h> |
| 67 |
#include <openssl/nid.h> |
| 68 |
#include <openssl/rsa.h> |
| 69 |
|
| 70 |
#include "../rsa/internal.h" |
| 71 |
#include "internal.h" |
| 72 |
|
| 73 |
|
| 74 |
typedef struct { |
| 75 |
/* Key gen parameters */ |
| 76 |
int nbits; |
| 77 |
BIGNUM *pub_exp; |
| 78 |
/* RSA padding mode */ |
| 79 |
int pad_mode; |
| 80 |
/* message digest */ |
| 81 |
const EVP_MD *md; |
| 82 |
/* message digest for MGF1 */ |
| 83 |
const EVP_MD *mgf1md; |
| 84 |
/* PSS salt length */ |
| 85 |
int saltlen; |
| 86 |
/* tbuf is a buffer which is either NULL, or is the size of the RSA modulus. |
| 87 |
* It's used to store the output of RSA operations. */ |
| 88 |
uint8_t *tbuf; |
| 89 |
/* OAEP label */ |
| 90 |
uint8_t *oaep_label; |
| 91 |
size_t oaep_labellen; |
| 92 |
} RSA_PKEY_CTX; |
| 93 |
|
| 94 |
static int pkey_rsa_init(EVP_PKEY_CTX *ctx) { |
| 95 |
RSA_PKEY_CTX *rctx; |
| 96 |
rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX)); |
| 97 |
if (!rctx) { |
| 98 |
return 0; |
| 99 |
} |
| 100 |
memset(rctx, 0, sizeof(RSA_PKEY_CTX)); |
| 101 |
|
| 102 |
rctx->nbits = 2048; |
| 103 |
rctx->pad_mode = RSA_PKCS1_PADDING; |
| 104 |
rctx->saltlen = -2; |
| 105 |
|
| 106 |
ctx->data = rctx; |
| 107 |
|
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { |
| 112 |
RSA_PKEY_CTX *dctx, *sctx; |
| 113 |
if (!pkey_rsa_init(dst)) { |
| 114 |
return 0; |
| 115 |
} |
| 116 |
sctx = src->data; |
| 117 |
dctx = dst->data; |
| 118 |
dctx->nbits = sctx->nbits; |
| 119 |
if (sctx->pub_exp) { |
| 120 |
dctx->pub_exp = BN_dup(sctx->pub_exp); |
| 121 |
if (!dctx->pub_exp) { |
| 122 |
return 0; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
dctx->pad_mode = sctx->pad_mode; |
| 127 |
dctx->md = sctx->md; |
| 128 |
dctx->mgf1md = sctx->mgf1md; |
| 129 |
if (sctx->oaep_label) { |
| 130 |
OPENSSL_free(dctx->oaep_label); |
| 131 |
dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen); |
| 132 |
if (!dctx->oaep_label) { |
| 133 |
return 0; |
| 134 |
} |
| 135 |
dctx->oaep_labellen = sctx->oaep_labellen; |
| 136 |
} |
| 137 |
|
| 138 |
return 1; |
| 139 |
} |
| 140 |
|
| 141 |
static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) { |
| 142 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 143 |
|
| 144 |
if (rctx == NULL) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
BN_free(rctx->pub_exp); |
| 149 |
OPENSSL_free(rctx->tbuf); |
| 150 |
OPENSSL_free(rctx->oaep_label); |
| 151 |
OPENSSL_free(rctx); |
| 152 |
} |
| 153 |
|
| 154 |
static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) { |
| 155 |
if (ctx->tbuf) { |
| 156 |
return 1; |
| 157 |
} |
| 158 |
ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey)); |
| 159 |
if (!ctx->tbuf) { |
| 160 |
return 0; |
| 161 |
} |
| 162 |
return 1; |
| 163 |
} |
| 164 |
|
| 165 |
static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen, |
| 166 |
const uint8_t *tbs, size_t tbslen) { |
| 167 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 168 |
RSA *rsa = ctx->pkey->pkey.rsa; |
| 169 |
const size_t key_len = EVP_PKEY_size(ctx->pkey); |
| 170 |
|
| 171 |
if (!sig) { |
| 172 |
*siglen = key_len; |
| 173 |
return 1; |
| 174 |
} |
| 175 |
|
| 176 |
if (*siglen < key_len) { |
| 177 |
OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
if (rctx->md) { |
| 182 |
unsigned int out_len; |
| 183 |
|
| 184 |
if (tbslen != EVP_MD_size(rctx->md)) { |
| 185 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH); |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
if (EVP_MD_type(rctx->md) == NID_mdc2) { |
| 190 |
OPENSSL_PUT_ERROR(EVP, EVP_R_NO_MDC2_SUPPORT); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
switch (rctx->pad_mode) { |
| 195 |
case RSA_PKCS1_PADDING: |
| 196 |
if (!RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &out_len, rsa)) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
*siglen = out_len; |
| 200 |
return 1; |
| 201 |
|
| 202 |
case RSA_PKCS1_PSS_PADDING: |
| 203 |
if (!setup_tbuf(rctx, ctx) || |
| 204 |
!RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs, rctx->md, |
| 205 |
rctx->mgf1md, rctx->saltlen) || |
| 206 |
!RSA_sign_raw(rsa, siglen, sig, *siglen, rctx->tbuf, key_len, |
| 207 |
RSA_NO_PADDING)) { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
return 1; |
| 211 |
|
| 212 |
default: |
| 213 |
return 0; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode); |
| 218 |
} |
| 219 |
|
| 220 |
static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, |
| 221 |
size_t siglen, const uint8_t *tbs, |
| 222 |
size_t tbslen) { |
| 223 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 224 |
RSA *rsa = ctx->pkey->pkey.rsa; |
| 225 |
size_t rslen; |
| 226 |
const size_t key_len = EVP_PKEY_size(ctx->pkey); |
| 227 |
|
| 228 |
if (rctx->md) { |
| 229 |
switch (rctx->pad_mode) { |
| 230 |
case RSA_PKCS1_PADDING: |
| 231 |
return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa); |
| 232 |
|
| 233 |
case RSA_PKCS1_PSS_PADDING: |
| 234 |
if (!setup_tbuf(rctx, ctx) || |
| 235 |
!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen, |
| 236 |
RSA_NO_PADDING) || |
| 237 |
!RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md, rctx->mgf1md, |
| 238 |
rctx->tbuf, rctx->saltlen)) { |
| 239 |
return 0; |
| 240 |
} |
| 241 |
return 1; |
| 242 |
|
| 243 |
default: |
| 244 |
return 0; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if (!setup_tbuf(rctx, ctx) || |
| 249 |
!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen, |
| 250 |
rctx->pad_mode) || |
| 251 |
rslen != tbslen || |
| 252 |
CRYPTO_memcmp(tbs, rctx->tbuf, rslen) != 0) { |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
return 1; |
| 257 |
} |
| 258 |
|
| 259 |
static int pkey_rsa_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, |
| 260 |
size_t *out_len, const uint8_t *sig, |
| 261 |
size_t sig_len) { |
| 262 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 263 |
RSA *rsa = ctx->pkey->pkey.rsa; |
| 264 |
const size_t key_len = EVP_PKEY_size(ctx->pkey); |
| 265 |
|
| 266 |
if (out == NULL) { |
| 267 |
*out_len = key_len; |
| 268 |
return 1; |
| 269 |
} |
| 270 |
|
| 271 |
if (*out_len < key_len) { |
| 272 |
OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
if (!setup_tbuf(rctx, ctx)) { |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
if (rctx->md == NULL) { |
| 281 |
const int ret = RSA_public_decrypt(sig_len, sig, rctx->tbuf, |
| 282 |
ctx->pkey->pkey.rsa, rctx->pad_mode); |
| 283 |
if (ret < 0) { |
| 284 |
return 0; |
| 285 |
} |
| 286 |
*out_len = ret; |
| 287 |
memcpy(out, rctx->tbuf, *out_len); |
| 288 |
return 1; |
| 289 |
} |
| 290 |
|
| 291 |
if (rctx->pad_mode != RSA_PKCS1_PADDING) { |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
uint8_t *asn1_prefix; |
| 296 |
size_t asn1_prefix_len; |
| 297 |
int asn1_prefix_allocated; |
| 298 |
if (!RSA_add_pkcs1_prefix(&asn1_prefix, &asn1_prefix_len, |
| 299 |
&asn1_prefix_allocated, EVP_MD_type(rctx->md), NULL, |
| 300 |
0)) { |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
size_t rslen; |
| 305 |
int ok = 1; |
| 306 |
if (!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, sig_len, |
| 307 |
RSA_PKCS1_PADDING) || |
| 308 |
rslen < asn1_prefix_len || |
| 309 |
CRYPTO_memcmp(rctx->tbuf, asn1_prefix, asn1_prefix_len) != 0) { |
| 310 |
ok = 0; |
| 311 |
} |
| 312 |
|
| 313 |
if (asn1_prefix_allocated) { |
| 314 |
OPENSSL_free(asn1_prefix); |
| 315 |
} |
| 316 |
|
| 317 |
if (!ok) { |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
const size_t result_len = rslen - asn1_prefix_len; |
| 322 |
if (result_len != EVP_MD_size(rctx->md)) { |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
if (out != NULL) { |
| 327 |
memcpy(out, rctx->tbuf + asn1_prefix_len, result_len); |
| 328 |
} |
| 329 |
*out_len = result_len; |
| 330 |
|
| 331 |
return 1; |
| 332 |
} |
| 333 |
|
| 334 |
static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen, |
| 335 |
const uint8_t *in, size_t inlen) { |
| 336 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 337 |
RSA *rsa = ctx->pkey->pkey.rsa; |
| 338 |
const size_t key_len = EVP_PKEY_size(ctx->pkey); |
| 339 |
|
| 340 |
if (!out) { |
| 341 |
*outlen = key_len; |
| 342 |
return 1; |
| 343 |
} |
| 344 |
|
| 345 |
if (*outlen < key_len) { |
| 346 |
OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
| 351 |
if (!setup_tbuf(rctx, ctx) || |
| 352 |
!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, key_len, in, inlen, |
| 353 |
rctx->oaep_label, rctx->oaep_labellen, |
| 354 |
rctx->md, rctx->mgf1md) || |
| 355 |
!RSA_encrypt(rsa, outlen, out, *outlen, rctx->tbuf, key_len, |
| 356 |
RSA_NO_PADDING)) { |
| 357 |
return 0; |
| 358 |
} |
| 359 |
return 1; |
| 360 |
} |
| 361 |
|
| 362 |
return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode); |
| 363 |
} |
| 364 |
|
| 365 |
static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, |
| 366 |
size_t *outlen, const uint8_t *in, |
| 367 |
size_t inlen) { |
| 368 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 369 |
RSA *rsa = ctx->pkey->pkey.rsa; |
| 370 |
const size_t key_len = EVP_PKEY_size(ctx->pkey); |
| 371 |
|
| 372 |
if (!out) { |
| 373 |
*outlen = key_len; |
| 374 |
return 1; |
| 375 |
} |
| 376 |
|
| 377 |
if (*outlen < key_len) { |
| 378 |
OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
| 383 |
size_t plaintext_len; |
| 384 |
int message_len; |
| 385 |
|
| 386 |
if (!setup_tbuf(rctx, ctx) || |
| 387 |
!RSA_decrypt(rsa, &plaintext_len, rctx->tbuf, key_len, in, inlen, |
| 388 |
RSA_NO_PADDING)) { |
| 389 |
return 0; |
| 390 |
} |
| 391 |
|
| 392 |
message_len = RSA_padding_check_PKCS1_OAEP_mgf1( |
| 393 |
out, key_len, rctx->tbuf, plaintext_len, rctx->oaep_label, |
| 394 |
rctx->oaep_labellen, rctx->md, rctx->mgf1md); |
| 395 |
if (message_len < 0) { |
| 396 |
return 0; |
| 397 |
} |
| 398 |
*outlen = message_len; |
| 399 |
return 1; |
| 400 |
} |
| 401 |
|
| 402 |
return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode); |
| 403 |
} |
| 404 |
|
| 405 |
static int check_padding_md(const EVP_MD *md, int padding) { |
| 406 |
if (!md) { |
| 407 |
return 1; |
| 408 |
} |
| 409 |
|
| 410 |
if (padding == RSA_NO_PADDING) { |
| 411 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE); |
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
return 1; |
| 416 |
} |
| 417 |
|
| 418 |
static int is_known_padding(int padding_mode) { |
| 419 |
switch (padding_mode) { |
| 420 |
case RSA_PKCS1_PADDING: |
| 421 |
case RSA_NO_PADDING: |
| 422 |
case RSA_PKCS1_OAEP_PADDING: |
| 423 |
case RSA_PKCS1_PSS_PADDING: |
| 424 |
return 1; |
| 425 |
default: |
| 426 |
return 0; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { |
| 431 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 432 |
switch (type) { |
| 433 |
case EVP_PKEY_CTRL_RSA_PADDING: |
| 434 |
if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) || |
| 435 |
(p1 == RSA_PKCS1_PSS_PADDING && |
| 436 |
0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) || |
| 437 |
(p1 == RSA_PKCS1_OAEP_PADDING && |
| 438 |
0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) { |
| 439 |
OPENSSL_PUT_ERROR(EVP, EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); |
| 440 |
return 0; |
| 441 |
} |
| 442 |
if ((p1 == RSA_PKCS1_PSS_PADDING || p1 == RSA_PKCS1_OAEP_PADDING) && |
| 443 |
rctx->md == NULL) { |
| 444 |
rctx->md = EVP_sha1(); |
| 445 |
} |
| 446 |
rctx->pad_mode = p1; |
| 447 |
return 1; |
| 448 |
|
| 449 |
case EVP_PKEY_CTRL_GET_RSA_PADDING: |
| 450 |
*(int *)p2 = rctx->pad_mode; |
| 451 |
return 1; |
| 452 |
|
| 453 |
case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: |
| 454 |
case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: |
| 455 |
if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { |
| 456 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN); |
| 457 |
return 0; |
| 458 |
} |
| 459 |
if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) { |
| 460 |
*(int *)p2 = rctx->saltlen; |
| 461 |
} else { |
| 462 |
if (p1 < -2) { |
| 463 |
return 0; |
| 464 |
} |
| 465 |
rctx->saltlen = p1; |
| 466 |
} |
| 467 |
return 1; |
| 468 |
|
| 469 |
case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: |
| 470 |
if (p1 < 256) { |
| 471 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_KEYBITS); |
| 472 |
return 0; |
| 473 |
} |
| 474 |
rctx->nbits = p1; |
| 475 |
return 1; |
| 476 |
|
| 477 |
case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: |
| 478 |
if (!p2) { |
| 479 |
return 0; |
| 480 |
} |
| 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 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE); |
| 489 |
return 0; |
| 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 |
} |
| 496 |
return 1; |
| 497 |
|
| 498 |
case EVP_PKEY_CTRL_MD: |
| 499 |
if (!check_padding_md(p2, rctx->pad_mode)) { |
| 500 |
return 0; |
| 501 |
} |
| 502 |
rctx->md = p2; |
| 503 |
return 1; |
| 504 |
|
| 505 |
case EVP_PKEY_CTRL_GET_MD: |
| 506 |
*(const EVP_MD **)p2 = rctx->md; |
| 507 |
return 1; |
| 508 |
|
| 509 |
case EVP_PKEY_CTRL_RSA_MGF1_MD: |
| 510 |
case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: |
| 511 |
if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING && |
| 512 |
rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
| 513 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_MGF1_MD); |
| 514 |
return 0; |
| 515 |
} |
| 516 |
if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) { |
| 517 |
if (rctx->mgf1md) { |
| 518 |
*(const EVP_MD **)p2 = rctx->mgf1md; |
| 519 |
} else { |
| 520 |
*(const EVP_MD **)p2 = rctx->md; |
| 521 |
} |
| 522 |
} else { |
| 523 |
rctx->mgf1md = p2; |
| 524 |
} |
| 525 |
return 1; |
| 526 |
|
| 527 |
case EVP_PKEY_CTRL_RSA_OAEP_LABEL: |
| 528 |
if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
| 529 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
OPENSSL_free(rctx->oaep_label); |
| 533 |
if (p2 && p1 > 0) { |
| 534 |
rctx->oaep_label = p2; |
| 535 |
rctx->oaep_labellen = p1; |
| 536 |
} else { |
| 537 |
rctx->oaep_label = NULL; |
| 538 |
rctx->oaep_labellen = 0; |
| 539 |
} |
| 540 |
return 1; |
| 541 |
|
| 542 |
case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL: |
| 543 |
if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) { |
| 544 |
OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE); |
| 545 |
return 0; |
| 546 |
} |
| 547 |
CBS_init((CBS *)p2, rctx->oaep_label, rctx->oaep_labellen); |
| 548 |
return 1; |
| 549 |
|
| 550 |
default: |
| 551 |
OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { |
| 557 |
RSA *rsa = NULL; |
| 558 |
RSA_PKEY_CTX *rctx = ctx->data; |
| 559 |
|
| 560 |
if (!rctx->pub_exp) { |
| 561 |
rctx->pub_exp = BN_new(); |
| 562 |
if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) { |
| 563 |
return 0; |
| 564 |
} |
| 565 |
} |
| 566 |
rsa = RSA_new(); |
| 567 |
if (!rsa) { |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) { |
| 572 |
RSA_free(rsa); |
| 573 |
return 0; |
| 574 |
} |
| 575 |
|
| 576 |
EVP_PKEY_assign_RSA(pkey, rsa); |
| 577 |
return 1; |
| 578 |
} |
| 579 |
|
| 580 |
const EVP_PKEY_METHOD rsa_pkey_meth = { |
| 581 |
EVP_PKEY_RSA, |
| 582 |
pkey_rsa_init, |
| 583 |
pkey_rsa_copy, |
| 584 |
pkey_rsa_cleanup, |
| 585 |
pkey_rsa_keygen, |
| 586 |
pkey_rsa_sign, |
| 587 |
pkey_rsa_verify, |
| 588 |
pkey_rsa_verify_recover, |
| 589 |
pkey_rsa_encrypt, |
| 590 |
pkey_rsa_decrypt, |
| 591 |
0 /* derive */, |
| 592 |
pkey_rsa_ctrl, |
| 593 |
}; |
| 594 |
|
| 595 |
int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) { |
| 596 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING, |
| 597 |
padding, NULL); |
| 598 |
} |
| 599 |
|
| 600 |
int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) { |
| 601 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, |
| 602 |
0, out_padding); |
| 603 |
} |
| 604 |
|
| 605 |
int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) { |
| 606 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, |
| 607 |
(EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY), |
| 608 |
EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL); |
| 609 |
} |
| 610 |
|
| 611 |
int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) { |
| 612 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, |
| 613 |
(EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY), |
| 614 |
EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len); |
| 615 |
} |
| 616 |
|
| 617 |
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) { |
| 618 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, |
| 619 |
EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL); |
| 620 |
} |
| 621 |
|
| 622 |
int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) { |
| 623 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, |
| 624 |
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e); |
| 625 |
} |
| 626 |
|
| 627 |
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { |
| 628 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 629 |
EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md); |
| 630 |
} |
| 631 |
|
| 632 |
int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) { |
| 633 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 634 |
EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md); |
| 635 |
} |
| 636 |
|
| 637 |
int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { |
| 638 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, |
| 639 |
EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
| 640 |
EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md); |
| 641 |
} |
| 642 |
|
| 643 |
int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) { |
| 644 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, |
| 645 |
EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
| 646 |
EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md); |
| 647 |
} |
| 648 |
|
| 649 |
int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label, |
| 650 |
size_t label_len) { |
| 651 |
if (label_len > INT_MAX) { |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 656 |
EVP_PKEY_CTRL_RSA_OAEP_LABEL, (int)label_len, |
| 657 |
(void *)label); |
| 658 |
} |
| 659 |
|
| 660 |
int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, |
| 661 |
const uint8_t **out_label) { |
| 662 |
CBS label; |
| 663 |
if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 664 |
EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) { |
| 665 |
return -1; |
| 666 |
} |
| 667 |
if (CBS_len(&label) > INT_MAX) { |
| 668 |
OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW); |
| 669 |
return -1; |
| 670 |
} |
| 671 |
*out_label = CBS_data(&label); |
| 672 |
return (int)CBS_len(&label); |
| 673 |
} |