| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/rsa.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/bn.h> |
| 11 |
#include <openssl/digest.h> |
| 12 |
#include <openssl/engine.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
#include <openssl/ex_data.h> |
| 15 |
#include <openssl/md5.h> |
| 16 |
#include <openssl/mem.h> |
| 17 |
#include <openssl/nid.h> |
| 18 |
#include <openssl/sha.h> |
| 19 |
#include <openssl/thread.h> |
| 20 |
|
| 21 |
#include "../../internal.h" |
| 22 |
//#include "../../rsa_extra/internal.h" |
| 23 |
#include "../bn/internal.h" |
| 24 |
#include "../delocate.h" |
| 25 |
#include "internal.h" |
| 26 |
|
| 27 |
|
| 28 |
// RSA_R_BLOCK_TYPE_IS_NOT_02 is part of the legacy SSLv23 padding scheme. |
| 29 |
// Cryptography.io depends on this error code. |
| 30 |
OPENSSL_DECLARE_ERROR_REASON(RSA, BLOCK_TYPE_IS_NOT_02) |
| 31 |
|
| 32 |
DEFINE_STATIC_EX_DATA_CLASS(g_rsa_ex_data_class) |
| 33 |
|
| 34 |
static int bn_dup_into(BIGNUM **dst, const BIGNUM *src) { |
| 35 |
if (src == NULL) { |
| 36 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
if (*dst == src) { |
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
BN_free(*dst); |
| 45 |
*dst = BN_dup(src); |
| 46 |
return *dst != NULL; |
| 47 |
} |
| 48 |
|
| 49 |
RSA *RSA_new_public_key(const BIGNUM *n, const BIGNUM *e) { |
| 50 |
RSA *rsa = RSA_new(); |
| 51 |
if (rsa == NULL || // |
| 52 |
!bn_dup_into(&rsa->n, n) || // |
| 53 |
!bn_dup_into(&rsa->e, e) || // |
| 54 |
!RSA_check_key(rsa)) { |
| 55 |
RSA_free(rsa); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
return rsa; |
| 60 |
} |
| 61 |
|
| 62 |
RSA *RSA_new_private_key(const BIGNUM *n, const BIGNUM *e, const BIGNUM *d, |
| 63 |
const BIGNUM *p, const BIGNUM *q, const BIGNUM *dmp1, |
| 64 |
const BIGNUM *dmq1, const BIGNUM *iqmp) { |
| 65 |
SET_DIT_AUTO_RESET; |
| 66 |
RSA *rsa = RSA_new(); |
| 67 |
if (rsa == NULL || // |
| 68 |
!bn_dup_into(&rsa->n, n) || // |
| 69 |
!bn_dup_into(&rsa->e, e) || // |
| 70 |
!bn_dup_into(&rsa->d, d) || // |
| 71 |
!bn_dup_into(&rsa->p, p) || // |
| 72 |
!bn_dup_into(&rsa->q, q) || // |
| 73 |
!bn_dup_into(&rsa->dmp1, dmp1) || // |
| 74 |
!bn_dup_into(&rsa->dmq1, dmq1) || // |
| 75 |
!bn_dup_into(&rsa->iqmp, iqmp) || // |
| 76 |
!RSA_check_key(rsa)) { |
| 77 |
RSA_free(rsa); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
return rsa; |
| 82 |
} |
| 83 |
|
| 84 |
RSA *RSA_new_private_key_no_crt(const BIGNUM *n, const BIGNUM *e, |
| 85 |
const BIGNUM *d) { |
| 86 |
SET_DIT_AUTO_RESET; |
| 87 |
RSA *rsa = RSA_new(); |
| 88 |
if (rsa == NULL || // |
| 89 |
!bn_dup_into(&rsa->n, n) || // |
| 90 |
!bn_dup_into(&rsa->e, e) || // |
| 91 |
!bn_dup_into(&rsa->d, d) || // |
| 92 |
!RSA_check_key(rsa)) { |
| 93 |
RSA_free(rsa); |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
return rsa; |
| 98 |
} |
| 99 |
|
| 100 |
RSA *RSA_new_private_key_no_e(const BIGNUM *n, const BIGNUM *d) { |
| 101 |
SET_DIT_AUTO_RESET; |
| 102 |
RSA *rsa = RSA_new(); |
| 103 |
if (rsa == NULL) { |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
rsa->flags |= RSA_FLAG_NO_PUBLIC_EXPONENT; |
| 108 |
if (!bn_dup_into(&rsa->n, n) || // |
| 109 |
!bn_dup_into(&rsa->d, d) || // |
| 110 |
!RSA_check_key(rsa)) { |
| 111 |
RSA_free(rsa); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
return rsa; |
| 116 |
} |
| 117 |
|
| 118 |
RSA *RSA_new_public_key_large_e(const BIGNUM *n, const BIGNUM *e) { |
| 119 |
RSA *rsa = RSA_new(); |
| 120 |
if (rsa == NULL) { |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
rsa->flags |= RSA_FLAG_LARGE_PUBLIC_EXPONENT; |
| 125 |
if (!bn_dup_into(&rsa->n, n) || // |
| 126 |
!bn_dup_into(&rsa->e, e) || // |
| 127 |
!RSA_check_key(rsa)) { |
| 128 |
RSA_free(rsa); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
return rsa; |
| 133 |
} |
| 134 |
|
| 135 |
RSA *RSA_new_private_key_large_e(const BIGNUM *n, const BIGNUM *e, |
| 136 |
const BIGNUM *d, const BIGNUM *p, |
| 137 |
const BIGNUM *q, const BIGNUM *dmp1, |
| 138 |
const BIGNUM *dmq1, const BIGNUM *iqmp) { |
| 139 |
SET_DIT_AUTO_RESET; |
| 140 |
RSA *rsa = RSA_new(); |
| 141 |
if (rsa == NULL) { |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
|
| 145 |
rsa->flags |= RSA_FLAG_LARGE_PUBLIC_EXPONENT; |
| 146 |
if (!bn_dup_into(&rsa->n, n) || // |
| 147 |
!bn_dup_into(&rsa->e, e) || // |
| 148 |
!bn_dup_into(&rsa->d, d) || // |
| 149 |
!bn_dup_into(&rsa->p, p) || // |
| 150 |
!bn_dup_into(&rsa->q, q) || // |
| 151 |
!bn_dup_into(&rsa->dmp1, dmp1) || // |
| 152 |
!bn_dup_into(&rsa->dmq1, dmq1) || // |
| 153 |
!bn_dup_into(&rsa->iqmp, iqmp) || // |
| 154 |
!RSA_check_key(rsa)) { |
| 155 |
RSA_free(rsa); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
return rsa; |
| 160 |
} |
| 161 |
|
| 162 |
RSA *RSA_new(void) { return RSA_new_method(NULL); } |
| 163 |
|
| 164 |
RSA *RSA_new_method(const ENGINE *engine) { |
| 165 |
RSA *rsa = OPENSSL_zalloc(sizeof(RSA)); |
| 166 |
if (rsa == NULL) { |
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
if (engine) { |
| 171 |
rsa->meth = ENGINE_get_RSA(engine); |
| 172 |
} |
| 173 |
|
| 174 |
if (rsa->meth == NULL) { |
| 175 |
rsa->meth = (RSA_METHOD *) RSA_get_default_method(); |
| 176 |
} |
| 177 |
|
| 178 |
rsa->references = 1; |
| 179 |
rsa->flags = rsa->meth->flags; |
| 180 |
CRYPTO_MUTEX_init(&rsa->lock); |
| 181 |
CRYPTO_new_ex_data(&rsa->ex_data); |
| 182 |
|
| 183 |
if (rsa->meth->init && !rsa->meth->init(rsa)) { |
| 184 |
CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data); |
| 185 |
CRYPTO_MUTEX_cleanup(&rsa->lock); |
| 186 |
OPENSSL_free(rsa); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
return rsa; |
| 191 |
} |
| 192 |
|
| 193 |
RSA *RSA_new_method_no_e(const ENGINE *engine, const BIGNUM *n) { |
| 194 |
RSA *rsa = RSA_new_method(engine); |
| 195 |
if (rsa == NULL || |
| 196 |
!bn_dup_into(&rsa->n, n)) { |
| 197 |
RSA_free(rsa); |
| 198 |
return NULL; |
| 199 |
} |
| 200 |
rsa->flags |= RSA_FLAG_NO_PUBLIC_EXPONENT; |
| 201 |
return rsa; |
| 202 |
} |
| 203 |
|
| 204 |
void RSA_free(RSA *rsa) { |
| 205 |
SET_DIT_AUTO_RESET; |
| 206 |
if (rsa == NULL) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
if (rsa->meth && rsa->meth->finish) { |
| 216 |
rsa->meth->finish(rsa); |
| 217 |
} |
| 218 |
|
| 219 |
CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data); |
| 220 |
|
| 221 |
BN_free(rsa->n); |
| 222 |
BN_free(rsa->e); |
| 223 |
BN_free(rsa->d); |
| 224 |
BN_free(rsa->p); |
| 225 |
BN_free(rsa->q); |
| 226 |
BN_free(rsa->dmp1); |
| 227 |
BN_free(rsa->dmq1); |
| 228 |
BN_free(rsa->iqmp); |
| 229 |
RSASSA_PSS_PARAMS_free(rsa->pss); |
| 230 |
rsa_invalidate_key(rsa); |
| 231 |
CRYPTO_MUTEX_cleanup(&rsa->lock); |
| 232 |
OPENSSL_free(rsa); |
| 233 |
} |
| 234 |
|
| 235 |
int RSA_up_ref(RSA *rsa) { |
| 236 |
SET_DIT_AUTO_RESET; |
| 237 |
CRYPTO_refcount_inc(&rsa->references); |
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
unsigned RSA_bits(const RSA *rsa) { |
| 242 |
SET_DIT_AUTO_RESET; |
| 243 |
return BN_num_bits(rsa->n); |
| 244 |
} |
| 245 |
|
| 246 |
const BIGNUM *RSA_get0_n(const RSA *rsa) { |
| 247 |
SET_DIT_AUTO_RESET; |
| 248 |
return rsa->n; |
| 249 |
} |
| 250 |
|
| 251 |
const BIGNUM *RSA_get0_e(const RSA *rsa) { |
| 252 |
SET_DIT_AUTO_RESET; |
| 253 |
return rsa->e; |
| 254 |
} |
| 255 |
|
| 256 |
const BIGNUM *RSA_get0_d(const RSA *rsa) { |
| 257 |
SET_DIT_AUTO_RESET; |
| 258 |
return rsa->d; |
| 259 |
} |
| 260 |
|
| 261 |
const BIGNUM *RSA_get0_p(const RSA *rsa) { |
| 262 |
SET_DIT_AUTO_RESET; |
| 263 |
return rsa->p; |
| 264 |
} |
| 265 |
|
| 266 |
const BIGNUM *RSA_get0_q(const RSA *rsa) { |
| 267 |
SET_DIT_AUTO_RESET; |
| 268 |
return rsa->q; |
| 269 |
} |
| 270 |
|
| 271 |
const BIGNUM *RSA_get0_dmp1(const RSA *rsa) { |
| 272 |
SET_DIT_AUTO_RESET; |
| 273 |
return rsa->dmp1; |
| 274 |
} |
| 275 |
|
| 276 |
const BIGNUM *RSA_get0_dmq1(const RSA *rsa) { |
| 277 |
SET_DIT_AUTO_RESET; |
| 278 |
return rsa->dmq1; |
| 279 |
} |
| 280 |
|
| 281 |
const BIGNUM *RSA_get0_iqmp(const RSA *rsa) { |
| 282 |
SET_DIT_AUTO_RESET; |
| 283 |
return rsa->iqmp; |
| 284 |
} |
| 285 |
|
| 286 |
void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e, |
| 287 |
const BIGNUM **out_d) { |
| 288 |
SET_DIT_AUTO_RESET; |
| 289 |
if (out_n != NULL) { |
| 290 |
*out_n = rsa->n; |
| 291 |
} |
| 292 |
if (out_e != NULL) { |
| 293 |
*out_e = rsa->e; |
| 294 |
} |
| 295 |
if (out_d != NULL) { |
| 296 |
*out_d = rsa->d; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p, |
| 301 |
const BIGNUM **out_q) { |
| 302 |
SET_DIT_AUTO_RESET; |
| 303 |
if (out_p != NULL) { |
| 304 |
*out_p = rsa->p; |
| 305 |
} |
| 306 |
if (out_q != NULL) { |
| 307 |
*out_q = rsa->q; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *rsa) { |
| 312 |
// We do not support the id-RSASSA-PSS key encoding. If we add support later, |
| 313 |
// the |maskHash| field should be filled in for OpenSSL compatibility. |
| 314 |
SET_DIT_AUTO_RESET; |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
const RSASSA_PSS_PARAMS *RSA_get0_ssa_pss_params(const RSA *rsa) { |
| 319 |
SET_DIT_AUTO_RESET; |
| 320 |
return rsa->pss; |
| 321 |
} |
| 322 |
|
| 323 |
void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, |
| 324 |
const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) { |
| 325 |
SET_DIT_AUTO_RESET; |
| 326 |
if (out_dmp1 != NULL) { |
| 327 |
*out_dmp1 = rsa->dmp1; |
| 328 |
} |
| 329 |
if (out_dmq1 != NULL) { |
| 330 |
*out_dmq1 = rsa->dmq1; |
| 331 |
} |
| 332 |
if (out_iqmp != NULL) { |
| 333 |
*out_iqmp = rsa->iqmp; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) { |
| 338 |
SET_DIT_AUTO_RESET; |
| 339 |
if ((rsa->n == NULL && n == NULL) || |
| 340 |
(rsa->e == NULL && e == NULL && rsa->d == NULL && d == NULL)) { |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
if (n != NULL) { |
| 345 |
BN_free(rsa->n); |
| 346 |
rsa->n = n; |
| 347 |
} |
| 348 |
if (e != NULL) { |
| 349 |
BN_free(rsa->e); |
| 350 |
rsa->e = e; |
| 351 |
} |
| 352 |
if (d != NULL) { |
| 353 |
BN_free(rsa->d); |
| 354 |
rsa->d = d; |
| 355 |
} |
| 356 |
|
| 357 |
rsa_invalidate_key(rsa); |
| 358 |
return 1; |
| 359 |
} |
| 360 |
|
| 361 |
int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) { |
| 362 |
SET_DIT_AUTO_RESET; |
| 363 |
if ((rsa->p == NULL && p == NULL) || |
| 364 |
(rsa->q == NULL && q == NULL)) { |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
if (p != NULL) { |
| 370 |
BN_free(rsa->p); |
| 371 |
rsa->p = p; |
| 372 |
} |
| 373 |
if (q != NULL) { |
| 374 |
BN_free(rsa->q); |
| 375 |
rsa->q = q; |
| 376 |
} |
| 377 |
|
| 378 |
rsa_invalidate_key(rsa); |
| 379 |
return 1; |
| 380 |
} |
| 381 |
|
| 382 |
int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) { |
| 383 |
SET_DIT_AUTO_RESET; |
| 384 |
if ((rsa->dmp1 == NULL && dmp1 == NULL) || |
| 385 |
(rsa->dmq1 == NULL && dmq1 == NULL) || |
| 386 |
(rsa->iqmp == NULL && iqmp == NULL)) { |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
if (dmp1 != NULL) { |
| 391 |
BN_free(rsa->dmp1); |
| 392 |
rsa->dmp1 = dmp1; |
| 393 |
} |
| 394 |
if (dmq1 != NULL) { |
| 395 |
BN_free(rsa->dmq1); |
| 396 |
rsa->dmq1 = dmq1; |
| 397 |
} |
| 398 |
if (iqmp != NULL) { |
| 399 |
BN_free(rsa->iqmp); |
| 400 |
rsa->iqmp = iqmp; |
| 401 |
} |
| 402 |
|
| 403 |
rsa_invalidate_key(rsa); |
| 404 |
return 1; |
| 405 |
} |
| 406 |
|
| 407 |
RSA_METHOD *RSA_meth_new(const char *name, int flags) { |
| 408 |
RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth)); |
| 409 |
|
| 410 |
if (meth == NULL) { |
| 411 |
return NULL; |
| 412 |
} |
| 413 |
|
| 414 |
if (flags == RSA_FLAG_OPAQUE) { |
| 415 |
meth->flags = flags; |
| 416 |
} |
| 417 |
return meth; |
| 418 |
} |
| 419 |
|
| 420 |
int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) { |
| 421 |
if(rsa == NULL || meth == NULL) { |
| 422 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
rsa->meth = meth; |
| 427 |
return 1; |
| 428 |
} |
| 429 |
|
| 430 |
const RSA_METHOD *RSA_get_method(const RSA *rsa) { |
| 431 |
if(rsa == NULL) { |
| 432 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 433 |
return NULL; |
| 434 |
} |
| 435 |
|
| 436 |
return rsa->meth; |
| 437 |
} |
| 438 |
|
| 439 |
void RSA_meth_free(RSA_METHOD *meth) |
| 440 |
{ |
| 441 |
if (meth != NULL) { |
| 442 |
OPENSSL_free(meth); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
int RSA_meth_set_init(RSA_METHOD *meth, int (*init) (RSA *rsa)) { |
| 447 |
if(meth == NULL) { |
| 448 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
meth->init = init; |
| 453 |
return 1; |
| 454 |
} |
| 455 |
|
| 456 |
int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa)) { |
| 457 |
if(meth == NULL) { |
| 458 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
meth->finish = finish; |
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
int RSA_meth_set_priv_dec(RSA_METHOD *meth, |
| 467 |
int (*priv_dec) (int max_out, const uint8_t *from, |
| 468 |
uint8_t *to, RSA *rsa, |
| 469 |
int padding)) { |
| 470 |
if(meth == NULL) { |
| 471 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
meth->decrypt = priv_dec; |
| 476 |
return 1; |
| 477 |
} |
| 478 |
|
| 479 |
int RSA_meth_set_priv_enc(RSA_METHOD *meth, |
| 480 |
int (*priv_enc) (int max_out, const uint8_t *from, |
| 481 |
uint8_t *to, RSA *rsa, |
| 482 |
int padding)) { |
| 483 |
if(meth == NULL) { |
| 484 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 485 |
return 0; |
| 486 |
} |
| 487 |
|
| 488 |
meth->sign_raw = priv_enc; |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
int RSA_meth_set_pub_dec(RSA_METHOD *meth, |
| 493 |
int (*pub_dec) (int max_out, const uint8_t *from, |
| 494 |
uint8_t *to, RSA *rsa, |
| 495 |
int padding)) { |
| 496 |
if(meth == NULL) { |
| 497 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
meth->verify_raw = pub_dec; |
| 502 |
return 1; |
| 503 |
} |
| 504 |
|
| 505 |
int RSA_meth_set_pub_enc(RSA_METHOD *meth, |
| 506 |
int (*pub_enc) (int max_out, const uint8_t *from, |
| 507 |
uint8_t *to, RSA *rsa, |
| 508 |
int padding)) { |
| 509 |
if(meth == NULL) { |
| 510 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
meth->encrypt = pub_enc; |
| 515 |
return 1; |
| 516 |
} |
| 517 |
|
| 518 |
int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data) { |
| 519 |
if(meth == NULL) { |
| 520 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
meth->app_data = app_data; |
| 525 |
return 1; |
| 526 |
} |
| 527 |
|
| 528 |
int RSA_meth_set_sign(RSA_METHOD *meth, int (*sign) (int type, |
| 529 |
const unsigned char *m, unsigned int m_length, unsigned char *sigret, |
| 530 |
unsigned int *siglen, const RSA *rsa)) { |
| 531 |
if(meth == NULL) { |
| 532 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 533 |
return 0; |
| 534 |
} |
| 535 |
|
| 536 |
meth->sign = sign; |
| 537 |
return 1; |
| 538 |
} |
| 539 |
|
| 540 |
static int rsa_sign_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out, |
| 541 |
size_t max_out, const uint8_t *in, |
| 542 |
size_t in_len, int padding) { |
| 543 |
SET_DIT_AUTO_RESET; |
| 544 |
if (rsa->meth && rsa->meth->sign_raw) { |
| 545 |
// In OpenSSL, the RSA_METHOD |sign_raw| or |priv_enc| operation does |
| 546 |
// not directly take and initialize an |out_len| parameter. Instead, it |
| 547 |
// returns the size of the encrypted data or a negative number for error. |
| 548 |
// Our wrapping functions like |RSA_sign_raw| diverge from this paradigm |
| 549 |
// and expect an |out_len| parameter. To remain compatible with this new |
| 550 |
// paradigm and OpenSSL, we initialize |out_len| based on the return value |
| 551 |
// here. |
| 552 |
if (in_len > INT_MAX) { |
| 553 |
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW); |
| 554 |
*out_len = 0; |
| 555 |
return 0; |
| 556 |
} |
| 557 |
int ret = rsa->meth->sign_raw((int)in_len, in, out, rsa, padding); |
| 558 |
if(ret < 0) { |
| 559 |
*out_len = 0; |
| 560 |
return 0; |
| 561 |
} |
| 562 |
*out_len = ret; |
| 563 |
return 1; |
| 564 |
} |
| 565 |
|
| 566 |
return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding); |
| 567 |
} |
| 568 |
|
| 569 |
int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, |
| 570 |
const uint8_t *in, size_t in_len, int padding) { |
| 571 |
boringssl_ensure_rsa_self_test(); |
| 572 |
SET_DIT_AUTO_RESET; |
| 573 |
|
| 574 |
return rsa_sign_raw_no_self_test(rsa, out_len, out, max_out, in, in_len, |
| 575 |
padding); |
| 576 |
} |
| 577 |
|
| 578 |
unsigned RSA_size(const RSA *rsa) { |
| 579 |
SET_DIT_AUTO_RESET; |
| 580 |
size_t ret = (rsa->meth && rsa->meth->size) ? |
| 581 |
rsa->meth->size(rsa) : rsa_default_size(rsa); |
| 582 |
// RSA modulus sizes are bounded by |BIGNUM|, which must fit in |unsigned|. |
| 583 |
// |
| 584 |
// TODO(https://crbug.com/boringssl/516): Should we make this return |size_t|? |
| 585 |
assert(ret < UINT_MAX); |
| 586 |
return (unsigned)ret; |
| 587 |
} |
| 588 |
|
| 589 |
int RSA_is_opaque(const RSA *rsa) { |
| 590 |
SET_DIT_AUTO_RESET; |
| 591 |
return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE); |
| 592 |
} |
| 593 |
|
| 594 |
int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 595 |
CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
| 596 |
SET_DIT_AUTO_RESET; |
| 597 |
int index; |
| 598 |
if (!CRYPTO_get_ex_new_index(g_rsa_ex_data_class_bss_get(), &index, argl, |
| 599 |
argp, free_func)) { |
| 600 |
return -1; |
| 601 |
} |
| 602 |
return index; |
| 603 |
} |
| 604 |
|
| 605 |
int RSA_set_ex_data(RSA *rsa, int idx, void *arg) { |
| 606 |
SET_DIT_AUTO_RESET; |
| 607 |
return CRYPTO_set_ex_data(&rsa->ex_data, idx, arg); |
| 608 |
} |
| 609 |
|
| 610 |
void *RSA_get_ex_data(const RSA *rsa, int idx) { |
| 611 |
SET_DIT_AUTO_RESET; |
| 612 |
return CRYPTO_get_ex_data(&rsa->ex_data, idx); |
| 613 |
} |
| 614 |
|
| 615 |
// SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's |
| 616 |
// the length of an MD5 and SHA1 hash. |
| 617 |
static const unsigned SSL_SIG_LENGTH = 36; |
| 618 |
|
| 619 |
// pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is |
| 620 |
// to be signed with PKCS#1. |
| 621 |
struct pkcs1_sig_prefix { |
| 622 |
// nid identifies the hash function. |
| 623 |
int nid; |
| 624 |
// hash_len is the expected length of the hash function. |
| 625 |
uint8_t hash_len; |
| 626 |
// len is the number of bytes of |bytes| which are valid. |
| 627 |
uint8_t len; |
| 628 |
// bytes contains the DER bytes. |
| 629 |
uint8_t bytes[19]; |
| 630 |
}; |
| 631 |
|
| 632 |
// kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with |
| 633 |
// different hash functions. These are defined in RFC-8017 Section 9.2 |
| 634 |
// https://datatracker.ietf.org/doc/html/rfc8017#section-9.2 |
| 635 |
static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = { |
| 636 |
{ |
| 637 |
NID_md5, |
| 638 |
MD5_DIGEST_LENGTH, |
| 639 |
18, |
| 640 |
{0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
| 641 |
0x02, 0x05, 0x05, 0x00, 0x04, 0x10}, |
| 642 |
}, |
| 643 |
{ |
| 644 |
NID_sha1, |
| 645 |
SHA_DIGEST_LENGTH, |
| 646 |
15, |
| 647 |
{0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, |
| 648 |
0x00, 0x04, 0x14}, |
| 649 |
}, |
| 650 |
{ |
| 651 |
NID_sha224, |
| 652 |
SHA224_DIGEST_LENGTH, |
| 653 |
19, |
| 654 |
{0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 655 |
0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c}, |
| 656 |
}, |
| 657 |
{ |
| 658 |
NID_sha256, |
| 659 |
SHA256_DIGEST_LENGTH, |
| 660 |
19, |
| 661 |
{0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 662 |
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}, |
| 663 |
}, |
| 664 |
{ |
| 665 |
NID_sha384, |
| 666 |
SHA384_DIGEST_LENGTH, |
| 667 |
19, |
| 668 |
{0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 669 |
0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}, |
| 670 |
}, |
| 671 |
{ |
| 672 |
NID_sha512, |
| 673 |
SHA512_DIGEST_LENGTH, |
| 674 |
19, |
| 675 |
{0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 676 |
0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}, |
| 677 |
}, |
| 678 |
{ |
| 679 |
NID_sha512_224, |
| 680 |
SHA512_224_DIGEST_LENGTH, |
| 681 |
19, |
| 682 |
{0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 683 |
0x04, 0x02, 0x05, 0x05, 0x00, 0x04, 0x1c}, |
| 684 |
}, |
| 685 |
{ |
| 686 |
NID_sha512_256, |
| 687 |
SHA512_256_DIGEST_LENGTH, |
| 688 |
19, |
| 689 |
{0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 690 |
0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20}, |
| 691 |
}, |
| 692 |
{ |
| 693 |
NID_sha3_224, |
| 694 |
28, |
| 695 |
19, |
| 696 |
{0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 697 |
0x04, 0x02, 0x07, 0x05, 0x00, 0x04, 0x1c}, |
| 698 |
}, |
| 699 |
{ |
| 700 |
NID_sha3_256, |
| 701 |
32, |
| 702 |
19, |
| 703 |
{0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 704 |
0x04, 0x02, 0x08, 0x05, 0x00, 0x04, 0x20}, |
| 705 |
}, |
| 706 |
{ |
| 707 |
NID_sha3_384, |
| 708 |
48, |
| 709 |
19, |
| 710 |
{0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 711 |
0x04, 0x02, 0x09, 0x05, 0x00, 0x04, 0x30}, |
| 712 |
}, |
| 713 |
{ |
| 714 |
NID_sha3_512, |
| 715 |
64, |
| 716 |
19, |
| 717 |
{0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 718 |
0x04, 0x02, 0x0a, 0x05, 0x00, 0x04, 0x40}, |
| 719 |
}, |
| 720 |
{ |
| 721 |
NID_undef, 0, 0, {0}, |
| 722 |
}, |
| 723 |
}; |
| 724 |
|
| 725 |
static int rsa_check_digest_size(int hash_nid, size_t digest_len) { |
| 726 |
if (hash_nid == NID_md5_sha1) { |
| 727 |
if (digest_len != SSL_SIG_LENGTH) { |
| 728 |
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 729 |
return 0; |
| 730 |
} |
| 731 |
return 1; |
| 732 |
} |
| 733 |
|
| 734 |
for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) { |
| 735 |
const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i]; |
| 736 |
if (sig_prefix->nid == hash_nid) { |
| 737 |
if (digest_len != sig_prefix->hash_len) { |
| 738 |
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 739 |
return 0; |
| 740 |
} |
| 741 |
return 1; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE); |
| 746 |
return 0; |
| 747 |
|
| 748 |
} |
| 749 |
|
| 750 |
int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, |
| 751 |
int *is_alloced, int hash_nid, const uint8_t *digest, |
| 752 |
size_t digest_len) { |
| 753 |
if (!rsa_check_digest_size(hash_nid, digest_len)) { |
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
if (hash_nid == NID_md5_sha1) { |
| 758 |
// The length should already have been checked. |
| 759 |
assert(digest_len == SSL_SIG_LENGTH); |
| 760 |
*out_msg = (uint8_t *)digest; |
| 761 |
*out_msg_len = digest_len; |
| 762 |
*is_alloced = 0; |
| 763 |
return 1; |
| 764 |
} |
| 765 |
|
| 766 |
for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) { |
| 767 |
const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i]; |
| 768 |
if (sig_prefix->nid != hash_nid) { |
| 769 |
continue; |
| 770 |
} |
| 771 |
|
| 772 |
// The length should already have been checked. |
| 773 |
assert(digest_len == sig_prefix->hash_len); |
| 774 |
const uint8_t* prefix = sig_prefix->bytes; |
| 775 |
size_t prefix_len = sig_prefix->len; |
| 776 |
size_t signed_msg_len = prefix_len + digest_len; |
| 777 |
if (signed_msg_len < prefix_len) { |
| 778 |
OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG); |
| 779 |
return 0; |
| 780 |
} |
| 781 |
|
| 782 |
uint8_t *signed_msg = OPENSSL_malloc(signed_msg_len); |
| 783 |
if (!signed_msg) { |
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
OPENSSL_memcpy(signed_msg, prefix, prefix_len); |
| 788 |
OPENSSL_memcpy(signed_msg + prefix_len, digest, digest_len); |
| 789 |
|
| 790 |
*out_msg = signed_msg; |
| 791 |
*out_msg_len = signed_msg_len; |
| 792 |
*is_alloced = 1; |
| 793 |
|
| 794 |
return 1; |
| 795 |
} |
| 796 |
|
| 797 |
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE); |
| 798 |
return 0; |
| 799 |
} |
| 800 |
|
| 801 |
int rsa_sign_no_self_test(int hash_nid, const uint8_t *digest, |
| 802 |
size_t digest_len, uint8_t *out, unsigned *out_len, |
| 803 |
RSA *rsa) { |
| 804 |
if (rsa->meth && rsa->meth->sign) { |
| 805 |
if (!rsa_check_digest_size(hash_nid, digest_len)) { |
| 806 |
return 0; |
| 807 |
} |
| 808 |
// All supported digest lengths fit in |unsigned|. |
| 809 |
assert(digest_len <= EVP_MAX_MD_SIZE); |
| 810 |
OPENSSL_STATIC_ASSERT(EVP_MAX_MD_SIZE <= UINT_MAX, digest_too_long); |
| 811 |
return rsa->meth->sign(hash_nid, digest, (unsigned)digest_len, out, out_len, |
| 812 |
rsa); |
| 813 |
} |
| 814 |
|
| 815 |
const unsigned rsa_size = RSA_size(rsa); |
| 816 |
int ret = 0; |
| 817 |
uint8_t *signed_msg = NULL; |
| 818 |
size_t signed_msg_len = 0; |
| 819 |
int signed_msg_is_alloced = 0; |
| 820 |
size_t size_t_out_len; |
| 821 |
if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, |
| 822 |
&signed_msg_is_alloced, hash_nid, digest, |
| 823 |
digest_len) || |
| 824 |
!rsa_sign_raw_no_self_test(rsa, &size_t_out_len, out, rsa_size, |
| 825 |
signed_msg, signed_msg_len, |
| 826 |
RSA_PKCS1_PADDING)) { |
| 827 |
goto err; |
| 828 |
} |
| 829 |
|
| 830 |
if (size_t_out_len > UINT_MAX) { |
| 831 |
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW); |
| 832 |
goto err; |
| 833 |
} |
| 834 |
|
| 835 |
*out_len = (unsigned)size_t_out_len; |
| 836 |
ret = 1; |
| 837 |
|
| 838 |
err: |
| 839 |
if (signed_msg_is_alloced) { |
| 840 |
OPENSSL_free(signed_msg); |
| 841 |
} |
| 842 |
return ret; |
| 843 |
} |
| 844 |
|
| 845 |
int RSA_sign(int hash_nid, const uint8_t *digest, size_t digest_len, |
| 846 |
uint8_t *out, unsigned *out_len, RSA *rsa) { |
| 847 |
boringssl_ensure_rsa_self_test(); |
| 848 |
SET_DIT_AUTO_RESET; |
| 849 |
|
| 850 |
return rsa_sign_no_self_test(hash_nid, digest, digest_len, out, out_len, rsa); |
| 851 |
} |
| 852 |
|
| 853 |
int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, |
| 854 |
const uint8_t *digest, size_t digest_len, |
| 855 |
const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len) { |
| 856 |
SET_DIT_AUTO_RESET; |
| 857 |
if (digest_len != EVP_MD_size(md)) { |
| 858 |
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
size_t padded_len = RSA_size(rsa); |
| 863 |
uint8_t *padded = OPENSSL_malloc(padded_len); |
| 864 |
if (padded == NULL) { |
| 865 |
return 0; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
int ret = RSA_padding_add_PKCS1_PSS_mgf1(rsa, padded, digest, md, mgf1_md, |
| 870 |
salt_len) && |
| 871 |
RSA_sign_raw(rsa, out_len, out, max_out, padded, padded_len, |
| 872 |
RSA_NO_PADDING); |
| 873 |
OPENSSL_free(padded); |
| 874 |
return ret; |
| 875 |
} |
| 876 |
|
| 877 |
int rsa_digestsign_no_self_test(const EVP_MD *md, const uint8_t *input, |
| 878 |
size_t in_len, uint8_t *out, unsigned *out_len, |
| 879 |
RSA *rsa) { |
| 880 |
SET_DIT_AUTO_RESET; |
| 881 |
uint8_t digest[EVP_MAX_MD_SIZE]; |
| 882 |
unsigned int digest_len = EVP_MAX_MD_SIZE; |
| 883 |
if (!EVP_Digest(input, in_len, digest, &digest_len, md, NULL)) { |
| 884 |
return 0; |
| 885 |
} |
| 886 |
|
| 887 |
return rsa_sign_no_self_test(EVP_MD_type(md), digest, digest_len, out, |
| 888 |
out_len, rsa); |
| 889 |
} |
| 890 |
|
| 891 |
int rsa_verify_no_self_test(int hash_nid, const uint8_t *digest, |
| 892 |
size_t digest_len, const uint8_t *sig, |
| 893 |
size_t sig_len, RSA *rsa) { |
| 894 |
if (rsa->n == NULL || rsa->e == NULL) { |
| 895 |
OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
| 896 |
return 0; |
| 897 |
} |
| 898 |
|
| 899 |
const size_t rsa_size = RSA_size(rsa); |
| 900 |
uint8_t *buf = NULL; |
| 901 |
int ret = 0; |
| 902 |
uint8_t *signed_msg = NULL; |
| 903 |
size_t signed_msg_len = 0, len; |
| 904 |
int signed_msg_is_alloced = 0; |
| 905 |
|
| 906 |
if (hash_nid == NID_md5_sha1 && digest_len != SSL_SIG_LENGTH) { |
| 907 |
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 908 |
return 0; |
| 909 |
} |
| 910 |
|
| 911 |
buf = OPENSSL_malloc(rsa_size); |
| 912 |
if (!buf) { |
| 913 |
return 0; |
| 914 |
} |
| 915 |
|
| 916 |
if (!rsa_verify_raw_no_self_test(rsa, &len, buf, rsa_size, sig, sig_len, |
| 917 |
RSA_PKCS1_PADDING) || |
| 918 |
!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, |
| 919 |
&signed_msg_is_alloced, hash_nid, digest, |
| 920 |
digest_len)) { |
| 921 |
goto out; |
| 922 |
} |
| 923 |
|
| 924 |
// Check that no other information follows the hash value (FIPS 186-4 Section 5.5) |
| 925 |
if (len != signed_msg_len) { |
| 926 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE); |
| 927 |
goto out; |
| 928 |
} |
| 929 |
|
| 930 |
// Check that the computed hash matches the expected hash |
| 931 |
if (OPENSSL_memcmp(buf, signed_msg, len) != 0) { |
| 932 |
OPENSSL_PUT_ERROR(RSA, RSA_R_MISMATCHED_SIGNATURE); |
| 933 |
goto out; |
| 934 |
} |
| 935 |
|
| 936 |
ret = 1; |
| 937 |
|
| 938 |
out: |
| 939 |
OPENSSL_free(buf); |
| 940 |
if (signed_msg_is_alloced) { |
| 941 |
OPENSSL_free(signed_msg); |
| 942 |
} |
| 943 |
return ret; |
| 944 |
} |
| 945 |
|
| 946 |
int rsa_digestverify_no_self_test(const EVP_MD *md, const uint8_t *input, |
| 947 |
size_t in_len, const uint8_t *sig, |
| 948 |
size_t sig_len, RSA *rsa) { |
| 949 |
uint8_t digest[EVP_MAX_MD_SIZE]; |
| 950 |
unsigned int digest_len = EVP_MAX_MD_SIZE; |
| 951 |
if (!EVP_Digest(input, in_len, digest, &digest_len, md, NULL)) { |
| 952 |
return 0; |
| 953 |
} |
| 954 |
|
| 955 |
return rsa_verify_no_self_test(EVP_MD_type(md), digest, digest_len, sig, |
| 956 |
sig_len, rsa); |
| 957 |
} |
| 958 |
|
| 959 |
int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, |
| 960 |
const uint8_t *sig, size_t sig_len, RSA *rsa) { |
| 961 |
boringssl_ensure_rsa_self_test(); |
| 962 |
SET_DIT_AUTO_RESET; |
| 963 |
return rsa_verify_no_self_test(hash_nid, digest, digest_len, sig, sig_len, |
| 964 |
rsa); |
| 965 |
} |
| 966 |
|
| 967 |
int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest, size_t digest_len, |
| 968 |
const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len, |
| 969 |
const uint8_t *sig, size_t sig_len) { |
| 970 |
SET_DIT_AUTO_RESET; |
| 971 |
if (digest_len != EVP_MD_size(md)) { |
| 972 |
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 973 |
return 0; |
| 974 |
} |
| 975 |
|
| 976 |
size_t em_len = RSA_size(rsa); |
| 977 |
uint8_t *em = OPENSSL_malloc(em_len); |
| 978 |
if (em == NULL) { |
| 979 |
return 0; |
| 980 |
} |
| 981 |
|
| 982 |
int ret = 0; |
| 983 |
if (!RSA_verify_raw(rsa, &em_len, em, em_len, sig, sig_len, RSA_NO_PADDING)) { |
| 984 |
goto err; |
| 985 |
} |
| 986 |
|
| 987 |
if (em_len != RSA_size(rsa)) { |
| 988 |
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 989 |
goto err; |
| 990 |
} |
| 991 |
|
| 992 |
ret = RSA_verify_PKCS1_PSS_mgf1(rsa, digest, md, mgf1_md, em, salt_len); |
| 993 |
|
| 994 |
err: |
| 995 |
OPENSSL_free(em); |
| 996 |
return ret; |
| 997 |
} |
| 998 |
|
| 999 |
int rsa_private_transform_no_self_test(RSA *rsa, uint8_t *out, |
| 1000 |
const uint8_t *in, size_t len) { |
| 1001 |
if (rsa->meth && rsa->meth->private_transform) { |
| 1002 |
return rsa->meth->private_transform(rsa, out, in, len); |
| 1003 |
} |
| 1004 |
|
| 1005 |
return rsa_default_private_transform(rsa, out, in, len); |
| 1006 |
} |
| 1007 |
|
| 1008 |
int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, |
| 1009 |
size_t len) { |
| 1010 |
boringssl_ensure_rsa_self_test(); |
| 1011 |
SET_DIT_AUTO_RESET; |
| 1012 |
return rsa_private_transform_no_self_test(rsa, out, in, len); |
| 1013 |
} |
| 1014 |
|
| 1015 |
int RSA_flags(const RSA *rsa) { |
| 1016 |
SET_DIT_AUTO_RESET; |
| 1017 |
if (rsa == NULL) { |
| 1018 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 1019 |
return 0; |
| 1020 |
} |
| 1021 |
|
| 1022 |
return rsa->flags; |
| 1023 |
} |
| 1024 |
|
| 1025 |
void RSA_set_flags(RSA *rsa, int flags) { |
| 1026 |
SET_DIT_AUTO_RESET; |
| 1027 |
if (rsa == NULL) { |
| 1028 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 1029 |
return; |
| 1030 |
} |
| 1031 |
|
| 1032 |
rsa->flags |= flags; |
| 1033 |
} |
| 1034 |
|
| 1035 |
int RSA_test_flags(const RSA *rsa, int flags) { |
| 1036 |
SET_DIT_AUTO_RESET; |
| 1037 |
if (rsa) { |
| 1038 |
return rsa->flags & flags; |
| 1039 |
} |
| 1040 |
|
| 1041 |
OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
|
| 1045 |
int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) { |
| 1046 |
SET_DIT_AUTO_RESET; |
| 1047 |
return (rsa != NULL && ((rsa->flags & RSA_FLAG_NO_BLINDING) == 0)) ? 1 : 0; |
| 1048 |
} |
| 1049 |
|
| 1050 |
void RSA_blinding_off_temp_for_accp_compatibility(RSA *rsa) { |
| 1051 |
SET_DIT_AUTO_RESET; |
| 1052 |
if (rsa != NULL) { |
| 1053 |
rsa->flags |= RSA_FLAG_NO_BLINDING; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2) { |
| 1058 |
SET_DIT_AUTO_RESET; |
| 1059 |
if (ctx != NULL && ctx->pmeth != NULL) { |
| 1060 |
if (ctx->pmeth->pkey_id == EVP_PKEY_RSA || |
| 1061 |
ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS) { |
| 1062 |
return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2); |
| 1063 |
} |
| 1064 |
return -1; |
| 1065 |
} |
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
|
| 1069 |
// ------------- KEY CHECKING FUNCTIONS ---------------- |
| 1070 |
// |
| 1071 |
// Performs several checks on the public component of the given RSA key. |
| 1072 |
// The key must have at least the public modulus n, the public exponent e is |
| 1073 |
// optional (this is to support the special case of JCA stripped private keys |
| 1074 |
// that are missing e). |
| 1075 |
// |
| 1076 |
// The checks: |
| 1077 |
// - n is positive, odd, and fits in 16k bits, |
| 1078 |
// - e is positive and odd (if present), |
| 1079 |
// - e is either <= 2^33 in default case, |
| 1080 |
// or <= n when RSA_FLAG_LARGE_PUBLIC_EXPONENT is set. |
| 1081 |
// |
| 1082 |
int is_public_component_of_rsa_key_good(const RSA *key) { |
| 1083 |
SET_DIT_AUTO_RESET; |
| 1084 |
if (key->n == NULL) { |
| 1085 |
OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
| 1086 |
return 0; |
| 1087 |
} |
| 1088 |
|
| 1089 |
unsigned int n_bits = BN_num_bits(key->n); |
| 1090 |
if (n_bits > 16 * 1024) { |
| 1091 |
OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE); |
| 1092 |
return 0; |
| 1093 |
} |
| 1094 |
|
| 1095 |
// RSA moduli n must be positive and odd because it is |
| 1096 |
// a product of positive odd prime numbers. |
| 1097 |
if (!BN_is_odd(key->n) || BN_is_negative(key->n)) { |
| 1098 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 1099 |
return 0; |
| 1100 |
} |
| 1101 |
|
| 1102 |
// Stripped private keys do not have the public exponent e, so the remaining |
| 1103 |
// checks in this function are not applicable. However, such keys should have |
| 1104 |
// the RSA_FLAG_NO_PUBLIC_EXPONENT flag set. |
| 1105 |
if (key->e == NULL) { |
| 1106 |
if (!(key->flags & RSA_FLAG_NO_PUBLIC_EXPONENT)) { |
| 1107 |
OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
| 1108 |
return 0; |
| 1109 |
} |
| 1110 |
return 1; |
| 1111 |
} |
| 1112 |
|
| 1113 |
unsigned int e_bits = BN_num_bits(key->e); |
| 1114 |
|
| 1115 |
// RSA public exponent e must be odd because it is a multiplicative inverse |
| 1116 |
// of the corresponding private exponent modulo phi(n). To be invertible |
| 1117 |
// modulo phi(n), e has to be realtively prime to phi(n). Since |
| 1118 |
// phi(n) = (p-1)(q-1) and p and q are odd prime numbers, it follows that |
| 1119 |
// phi(n) is even. Therefore, for e to be relatively prime to phi(n) it is |
| 1120 |
// necessary that e is odd. Additionally, reject e = 1 and negative e. |
| 1121 |
if (!BN_is_odd(key->e) || BN_is_negative(key->e) || e_bits < 2) { |
| 1122 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE); |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (key->flags & RSA_FLAG_LARGE_PUBLIC_EXPONENT) { |
| 1127 |
// The caller has requested disabling DoS protections. |
| 1128 |
// Still, e must be less than n. |
| 1129 |
if (BN_ucmp(key->n, key->e) <= 0) { |
| 1130 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE); |
| 1131 |
return 0; |
| 1132 |
} |
| 1133 |
|
| 1134 |
} else { |
| 1135 |
// Mitigate DoS attacks by limiting the exponent size. 33 bits was chosen as |
| 1136 |
// the limit based on the recommendations in: |
| 1137 |
// - https://www.imperialviolet.org/2012/03/16/rsae.html |
| 1138 |
// - https://www.imperialviolet.org/2012/03/17/rsados.html |
| 1139 |
if (e_bits > 33) { |
| 1140 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE); |
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
return 1; |
| 1146 |
} |
| 1147 |
|
| 1148 |
// The RSA key checking function works with five different types of keys: |
| 1149 |
// - public: (n, e), |
| 1150 |
// - private_min: (n, e, d), |
| 1151 |
// - private: (n, e, d, p, q), |
| 1152 |
// - private_crt: (n, e, d, p, q, dmp1, dmq1, iqmp), |
| 1153 |
// - private_strip: (n, d). |
| 1154 |
enum rsa_key_type_for_checking { |
| 1155 |
RSA_KEY_TYPE_FOR_CHECKING_PUBLIC, |
| 1156 |
RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_MIN, |
| 1157 |
RSA_KEY_TYPE_FOR_CHECKING_PRIVATE, |
| 1158 |
RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_CRT, |
| 1159 |
RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_STRIP, |
| 1160 |
RSA_KEY_TYPE_FOR_CHECKING_INVALID, |
| 1161 |
}; |
| 1162 |
|
| 1163 |
static enum rsa_key_type_for_checking determine_key_type_for_checking(const RSA *key) { |
| 1164 |
// The key must have the modulus n. |
| 1165 |
SET_DIT_AUTO_RESET; |
| 1166 |
if (key->n == NULL) { |
| 1167 |
return RSA_KEY_TYPE_FOR_CHECKING_INVALID; |
| 1168 |
} |
| 1169 |
|
| 1170 |
// (n, e) |
| 1171 |
if (key->e != NULL && key->d == NULL && key->p == NULL && key->q == NULL && |
| 1172 |
key->dmp1 == NULL && key->dmq1 == NULL && key->iqmp == NULL) { |
| 1173 |
return RSA_KEY_TYPE_FOR_CHECKING_PUBLIC; |
| 1174 |
} |
| 1175 |
|
| 1176 |
// (n, e, d) |
| 1177 |
if (key->e != NULL && key->d != NULL && key->p == NULL && key->q == NULL && |
| 1178 |
key->dmp1 == NULL && key->dmq1 == NULL && key->iqmp == NULL) { |
| 1179 |
return RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_MIN; |
| 1180 |
} |
| 1181 |
|
| 1182 |
// (n, e, d, p, q) |
| 1183 |
if (key->e != NULL && key->d != NULL && key->p != NULL && key->q != NULL && |
| 1184 |
key->dmp1 == NULL && key->dmq1 == NULL && key->iqmp == NULL) { |
| 1185 |
return RSA_KEY_TYPE_FOR_CHECKING_PRIVATE; |
| 1186 |
} |
| 1187 |
|
| 1188 |
// (n, e, d, p, q, dmp1, dmq1, iqmp) |
| 1189 |
if (key->e != NULL && key->d != NULL && key->p != NULL && key->q != NULL && |
| 1190 |
key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) { |
| 1191 |
return RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_CRT; |
| 1192 |
} |
| 1193 |
|
| 1194 |
// (n, d) |
| 1195 |
if (key->e == NULL && key->d != NULL && key->p == NULL && key->q == NULL && |
| 1196 |
key->dmp1 == NULL && key->dmq1 == NULL && key->iqmp == NULL) { |
| 1197 |
return RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_STRIP; |
| 1198 |
} |
| 1199 |
|
| 1200 |
return RSA_KEY_TYPE_FOR_CHECKING_INVALID; |
| 1201 |
} |
| 1202 |
|
| 1203 |
// Performs certain checks on the given RSA key. The key can be a key pair |
| 1204 |
// consisting of public and private component, but it can also be only the |
| 1205 |
// public component. The public component is |
| 1206 |
// (n, e), |
| 1207 |
// the modulus n and the public exponent e. A private key contains at minimum |
| 1208 |
// the private exponent e in addition to the public part: |
| 1209 |
// (n, e, d), |
| 1210 |
// while normally a private key would consist of |
| 1211 |
// (n, e, d, p, q) |
| 1212 |
// where p and q are the prime factors of n. Some keys store additional |
| 1213 |
// precomputed private parameters |
| 1214 |
// (dmp1, dmq1, iqmp). |
| 1215 |
// Additionally, we support checking stripped private keys that JCA supports |
| 1216 |
// that consist of (n, d). |
| 1217 |
// |
| 1218 |
// The function performs the following checks (when possible): |
| 1219 |
// - n fits in 16k bits, |
| 1220 |
// - 1 < log(e, 2) <= 33, |
| 1221 |
// - n and e are odd, |
| 1222 |
// - n > e, |
| 1223 |
// - p * q = n, |
| 1224 |
// - (d * e) mod (p - 1) = 1, |
| 1225 |
// - (d * e) mod (q - 1) = 1, |
| 1226 |
// - dmp1 = d mod (p - 1), |
| 1227 |
// - dmq1 = d mod (q - 1), |
| 1228 |
// - (q * iqmp) mod p = 1. |
| 1229 |
// |
| 1230 |
// Note: see the rsa_key_type_for_checking enum for details on types of keys |
| 1231 |
// the function can work with. |
| 1232 |
int RSA_check_key(const RSA *key) { |
| 1233 |
SET_DIT_AUTO_RESET; |
| 1234 |
GUARD_PTR(key); |
| 1235 |
|
| 1236 |
enum rsa_key_type_for_checking key_type = determine_key_type_for_checking(key); |
| 1237 |
if (key_type == RSA_KEY_TYPE_FOR_CHECKING_INVALID) { |
| 1238 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 1239 |
return 0; |
| 1240 |
} |
| 1241 |
|
| 1242 |
// We check the public component for every key type. |
| 1243 |
if (!is_public_component_of_rsa_key_good(key)) { |
| 1244 |
return 0; |
| 1245 |
} |
| 1246 |
|
| 1247 |
// Nothing else to check for public keys (n, e) and private keys in minimal |
| 1248 |
// or stripped format, (n, e, d) and (n, d), resp. |
| 1249 |
if (key_type == RSA_KEY_TYPE_FOR_CHECKING_PUBLIC || |
| 1250 |
key_type == RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_MIN || |
| 1251 |
key_type == RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_STRIP) { |
| 1252 |
return 1; |
| 1253 |
} |
| 1254 |
|
| 1255 |
// Keys that reach this point are either private keys (n, e, p, q, d), |
| 1256 |
// or CRT keys with (dmp1, dmq1, iqmp) values precomputed. |
| 1257 |
if (key_type != RSA_KEY_TYPE_FOR_CHECKING_PRIVATE && |
| 1258 |
key_type != RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_CRT) { |
| 1259 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
|
| 1263 |
int ret = 0; |
| 1264 |
|
| 1265 |
BN_CTX *ctx = BN_CTX_new(); |
| 1266 |
if (ctx == NULL) { |
| 1267 |
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 1268 |
return 0; |
| 1269 |
} |
| 1270 |
|
| 1271 |
BIGNUM tmp, de, pm1, qm1, dmp1, dmq1; |
| 1272 |
BN_init(&tmp); |
| 1273 |
BN_init(&de); |
| 1274 |
BN_init(&pm1); |
| 1275 |
BN_init(&qm1); |
| 1276 |
BN_init(&dmp1); |
| 1277 |
BN_init(&dmq1); |
| 1278 |
|
| 1279 |
// Check that p * q == n. Before we multiply, we check that p and q are in |
| 1280 |
// bounds, to avoid a DoS vector in |bn_mul_consttime| below. Note that |
| 1281 |
// n was bound by |is_public_component_of_rsa_key_good|. This also implicitly |
| 1282 |
// checks p and q are odd, which is a necessary condition for Montgomery |
| 1283 |
// reduction. |
| 1284 |
if (BN_is_negative(key->p) || |
| 1285 |
constant_time_declassify_int(BN_cmp(key->p, key->n) >= 0) || |
| 1286 |
BN_is_negative(key->q) || |
| 1287 |
constant_time_declassify_int(BN_cmp(key->q, key->n) >= 0)) { |
| 1288 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 1289 |
goto out; |
| 1290 |
} |
| 1291 |
if (!bn_mul_consttime(&tmp, key->p, key->q, ctx)) { |
| 1292 |
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 1293 |
goto out; |
| 1294 |
} |
| 1295 |
if (BN_cmp(&tmp, key->n) != 0) { |
| 1296 |
OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q); |
| 1297 |
goto out; |
| 1298 |
} |
| 1299 |
|
| 1300 |
// d must be an inverse of e mod the Carmichael totient, lcm(p-1, q-1), but it |
| 1301 |
// may be unreduced because other implementations use the Euler totient. We |
| 1302 |
// simply check that d * e is one mod p-1 and mod q-1. Note d and e were bound |
| 1303 |
// by earlier checks in this function. |
| 1304 |
if (!bn_usub_consttime(&pm1, key->p, BN_value_one()) || |
| 1305 |
!bn_usub_consttime(&qm1, key->q, BN_value_one())) { |
| 1306 |
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 1307 |
goto out; |
| 1308 |
} |
| 1309 |
const unsigned pm1_bits = BN_num_bits(&pm1); |
| 1310 |
const unsigned qm1_bits = BN_num_bits(&qm1); |
| 1311 |
if (!bn_mul_consttime(&de, key->d, key->e, ctx) || |
| 1312 |
!bn_div_consttime(NULL, &tmp, &de, &pm1, pm1_bits, ctx) || |
| 1313 |
!bn_div_consttime(NULL, &de, &de, &qm1, qm1_bits, ctx)) { |
| 1314 |
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 1315 |
goto out; |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (constant_time_declassify_int(!BN_is_one(&tmp)) || |
| 1319 |
constant_time_declassify_int(!BN_is_one(&de))) { |
| 1320 |
OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1); |
| 1321 |
goto out; |
| 1322 |
} |
| 1323 |
|
| 1324 |
// No more checks for a basic private key without CRT parameters. |
| 1325 |
if (key_type == RSA_KEY_TYPE_FOR_CHECKING_PRIVATE) { |
| 1326 |
ret = 1; |
| 1327 |
goto out; |
| 1328 |
} |
| 1329 |
|
| 1330 |
// Keys that reach this point are RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_CRT, |
| 1331 |
// so check that the CRT params are correct: |
| 1332 |
// - dmp1 == d mod (p - 1), |
| 1333 |
// - dmq1 == d mod (q - 1), |
| 1334 |
// - (iqmp * q) mod (p) == 1. |
| 1335 |
|
| 1336 |
if (!bn_div_consttime(NULL, &tmp, key->d, &pm1, pm1_bits, ctx) || |
| 1337 |
!bn_div_consttime(NULL, &de, key->d, &qm1, qm1_bits, ctx)) { |
| 1338 |
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 1339 |
goto out; |
| 1340 |
} |
| 1341 |
|
| 1342 |
// dmp1 == d mod (p - 1) and dmq1 == d mod (q - 1). |
| 1343 |
if (BN_cmp(&tmp, key->dmp1) != 0 || BN_cmp(&de, key->dmq1) != 0) { |
| 1344 |
OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT); |
| 1345 |
goto out; |
| 1346 |
} |
| 1347 |
|
| 1348 |
// Check that iqmp is fully reduced modulo p. |
| 1349 |
if (BN_cmp(key->iqmp, key->p) >= 0) { |
| 1350 |
OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT); |
| 1351 |
goto out; |
| 1352 |
} |
| 1353 |
|
| 1354 |
if (!bn_mul_consttime(&tmp, key->q, key->iqmp, ctx) || |
| 1355 |
// p is odd, so pm1 and p have the same bit width. |
| 1356 |
!bn_div_consttime(NULL, &tmp, &tmp, key->p, pm1_bits, ctx)) { |
| 1357 |
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 1358 |
goto out; |
| 1359 |
} |
| 1360 |
|
| 1361 |
// (iqmp * q) mod p = 1. |
| 1362 |
if (BN_cmp(&tmp, BN_value_one()) != 0) { |
| 1363 |
OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT); |
| 1364 |
goto out; |
| 1365 |
} |
| 1366 |
|
| 1367 |
ret = 1; |
| 1368 |
|
| 1369 |
out: |
| 1370 |
|
| 1371 |
BN_free(&tmp); |
| 1372 |
BN_free(&de); |
| 1373 |
BN_free(&pm1); |
| 1374 |
BN_free(&qm1); |
| 1375 |
BN_free(&dmp1); |
| 1376 |
BN_free(&dmq1); |
| 1377 |
BN_CTX_free(ctx); |
| 1378 |
|
| 1379 |
return ret; |
| 1380 |
} |
| 1381 |
|
| 1382 |
// Performs Pair-Wise Consistency Test (PWCT) with the given RSA key |
| 1383 |
// by signing and verifying a message. This is required for RSA_check_fips |
| 1384 |
// function further below. According to our FIPS lab we have to do the test |
| 1385 |
// with EVP_DigestSign/Verify API. |
| 1386 |
static int rsa_key_fips_pairwise_consistency_test_signing(RSA *key) { |
| 1387 |
int ret = 0; |
| 1388 |
|
| 1389 |
uint8_t msg[1] = {0}; |
| 1390 |
size_t msg_len = 1; |
| 1391 |
uint8_t *sig_der = NULL; |
| 1392 |
size_t sig_len = 0; |
| 1393 |
|
| 1394 |
EVP_PKEY *evp_pkey = NULL; |
| 1395 |
EVP_MD_CTX md_ctx; |
| 1396 |
EVP_MD_CTX_init(&md_ctx); |
| 1397 |
const EVP_MD *md = EVP_sha256(); |
| 1398 |
|
| 1399 |
evp_pkey = EVP_PKEY_new(); |
| 1400 |
if (!evp_pkey || !EVP_PKEY_set1_RSA(evp_pkey, key)) { |
| 1401 |
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 1402 |
goto end; |
| 1403 |
} |
| 1404 |
|
| 1405 |
// Grab the expected signature length. |
| 1406 |
if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, evp_pkey) || |
| 1407 |
!EVP_DigestSign(&md_ctx, NULL, &sig_len, msg, msg_len)) { |
| 1408 |
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 1409 |
goto end; |
| 1410 |
} |
| 1411 |
|
| 1412 |
sig_der = OPENSSL_malloc(sig_len); |
| 1413 |
if (!sig_der || |
| 1414 |
!EVP_DigestSign(&md_ctx, sig_der, &sig_len, msg, msg_len)) { |
| 1415 |
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 1416 |
goto end; |
| 1417 |
} |
| 1418 |
if (boringssl_fips_break_test("RSA_PWCT")) { |
| 1419 |
msg[0] = ~msg[0]; |
| 1420 |
} |
| 1421 |
if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, evp_pkey) || |
| 1422 |
!EVP_DigestVerify(&md_ctx, sig_der, sig_len, msg, msg_len)) { |
| 1423 |
goto end; |
| 1424 |
} |
| 1425 |
|
| 1426 |
ret = 1; |
| 1427 |
|
| 1428 |
end: |
| 1429 |
EVP_PKEY_free(evp_pkey); |
| 1430 |
EVP_MD_CTX_cleanse(&md_ctx); |
| 1431 |
OPENSSL_free(sig_der); |
| 1432 |
return ret; |
| 1433 |
} |
| 1434 |
|
| 1435 |
// This is the product of the 132 smallest odd primes, from 3 to 751, |
| 1436 |
// as defined in SP 800-89 5.3.3. |
| 1437 |
static const BN_ULONG kSmallFactorsLimbs[] = { |
| 1438 |
TOBN(0xc4309333, 0x3ef4e3e1), TOBN(0x71161eb6, 0xcd2d655f), |
| 1439 |
TOBN(0x95e2238c, 0x0bf94862), TOBN(0x3eb233d3, 0x24f7912b), |
| 1440 |
TOBN(0x6b55514b, 0xbf26c483), TOBN(0x0a84d817, 0x5a144871), |
| 1441 |
TOBN(0x77d12fee, 0x9b82210a), TOBN(0xdb5b93c2, 0x97f050b3), |
| 1442 |
TOBN(0x4acad6b9, 0x4d6c026b), TOBN(0xeb7751f3, 0x54aec893), |
| 1443 |
TOBN(0xdba53368, 0x36bc85c4), TOBN(0xd85a1b28, 0x7f5ec78e), |
| 1444 |
TOBN(0x2eb072d8, 0x6b322244), TOBN(0xbba51112, 0x5e2b3aea), |
| 1445 |
TOBN(0x36ed1a6c, 0x0e2486bf), TOBN(0x5f270460, 0xec0c5727), |
| 1446 |
0x000017b1 |
| 1447 |
}; |
| 1448 |
|
| 1449 |
DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) { |
| 1450 |
out->d = (BN_ULONG *) kSmallFactorsLimbs; |
| 1451 |
out->width = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs); |
| 1452 |
out->dmax = out->width; |
| 1453 |
out->neg = 0; |
| 1454 |
out->flags = BN_FLG_STATIC_DATA; |
| 1455 |
} |
| 1456 |
|
| 1457 |
// |RSA_check_fips| function: |
| 1458 |
// - validates basic properties of the key (by calling RSA_check_key), |
| 1459 |
// - performs partial public key validation (SP 800-89 5.3.3), |
| 1460 |
// - performs a pair-wise consistency test, if possible. |
| 1461 |
// The reason this function offers only key checks that are relevant for |
| 1462 |
// RSA signatures (SP 800-89) and not RSA key establishment (SP 800-56B) is |
| 1463 |
// that the AWS-LC FIPS module offers only RSA signing and verification as |
| 1464 |
// approved FIPS services. |
| 1465 |
int RSA_check_fips(RSA *key) { |
| 1466 |
SET_DIT_AUTO_RESET; |
| 1467 |
GUARD_PTR(key); |
| 1468 |
|
| 1469 |
enum rsa_key_type_for_checking key_type = determine_key_type_for_checking(key); |
| 1470 |
// In addition to invalid key type, stripped private keys can not be checked |
| 1471 |
// with this function because they lack the public component which is |
| 1472 |
// necessary for both FIPS checks performed here. |
| 1473 |
if (key_type == RSA_KEY_TYPE_FOR_CHECKING_INVALID || |
| 1474 |
key_type == RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_STRIP) { |
| 1475 |
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 1476 |
return 0; |
| 1477 |
} |
| 1478 |
|
| 1479 |
// Validate basic properties of the key. |
| 1480 |
if (!RSA_check_key(key)) { |
| 1481 |
return 0; |
| 1482 |
} |
| 1483 |
|
| 1484 |
BN_CTX *ctx = BN_CTX_new(); |
| 1485 |
if (ctx == NULL) { |
| 1486 |
return 0; |
| 1487 |
} |
| 1488 |
|
| 1489 |
BIGNUM small_gcd; |
| 1490 |
BN_init(&small_gcd); |
| 1491 |
|
| 1492 |
int ret = 0; |
| 1493 |
|
| 1494 |
// Perform partial public key validation of RSA keys (SP 800-89 5.3.3). |
| 1495 |
// Although this is not for primality testing, SP 800-89 cites an RSA |
| 1496 |
// primality testing algorithm, so we use |BN_prime_checks_for_generation| to |
| 1497 |
// match. This is only a plausibility test and we expect the value to be |
| 1498 |
// composite, so too few iterations will cause us to reject the key, not use |
| 1499 |
// an implausible one. |
| 1500 |
enum bn_primality_result_t primality_result; |
| 1501 |
if (BN_num_bits(key->e) <= 16 || |
| 1502 |
BN_num_bits(key->e) > 256 || |
| 1503 |
!BN_is_odd(key->n) || |
| 1504 |
!BN_is_odd(key->e) || |
| 1505 |
!BN_gcd(&small_gcd, key->n, g_small_factors(), ctx) || |
| 1506 |
!BN_is_one(&small_gcd) || |
| 1507 |
!BN_enhanced_miller_rabin_primality_test(&primality_result, key->n, |
| 1508 |
BN_prime_checks_for_generation, |
| 1509 |
ctx, NULL) || |
| 1510 |
primality_result != bn_non_prime_power_composite) { |
| 1511 |
OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED); |
| 1512 |
goto end; |
| 1513 |
} |
| 1514 |
|
| 1515 |
// For public keys the check is over because the public key validation is |
| 1516 |
// the only thing we can test, we can't perform the pair-wise consistency |
| 1517 |
// test without the private key. |
| 1518 |
if (key_type == RSA_KEY_TYPE_FOR_CHECKING_PUBLIC) { |
| 1519 |
ret = 1; |
| 1520 |
goto end; |
| 1521 |
} |
| 1522 |
|
| 1523 |
// Only private keys (that contain the public component as well) can be |
| 1524 |
// tested with a pair-wise consistency test. |
| 1525 |
if (key_type != RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_MIN && |
| 1526 |
key_type != RSA_KEY_TYPE_FOR_CHECKING_PRIVATE && |
| 1527 |
key_type != RSA_KEY_TYPE_FOR_CHECKING_PRIVATE_CRT) { |
| 1528 |
goto end; |
| 1529 |
} |
| 1530 |
|
| 1531 |
// FIPS pair-wise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG, |
| 1532 |
// section 9.9, it is not known whether |rsa| will be used for signing or |
| 1533 |
// encryption, so either pair-wise consistency self-test is acceptable. We |
| 1534 |
// perform a signing test. The same guidance can be found in FIPS 140-3 IG |
| 1535 |
// in Section 7.10.3.3, sub-section Additional comments. |
| 1536 |
if (!rsa_key_fips_pairwise_consistency_test_signing(key)) { |
| 1537 |
OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED); |
| 1538 |
goto end; |
| 1539 |
} |
| 1540 |
|
| 1541 |
ret = 1; |
| 1542 |
|
| 1543 |
end: |
| 1544 |
BN_free(&small_gcd); |
| 1545 |
BN_CTX_free(ctx); |
| 1546 |
|
| 1547 |
return ret; |
| 1548 |
} |