| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/rsa.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/bn.h> |
| 22 | #include <openssl/digest.h> |
| 23 | #include <openssl/engine.h> |
| 24 | #include <openssl/err.h> |
| 25 | #include <openssl/ex_data.h> |
| 26 | #include <openssl/md5.h> |
| 27 | #include <openssl/mem.h> |
| 28 | #include <openssl/nid.h> |
| 29 | #include <openssl/thread.h> |
| 30 |
| 31 | #include "../../internal.h" |
| 32 | #include "../bcm_interface.h" |
| 33 | #include "../bn/internal.h" |
| 34 | #include "../delocate.h" |
| 35 | #include "internal.h" |
| 36 |
| 37 |
| 38 | // RSA_R_BLOCK_TYPE_IS_NOT_02 is part of the legacy SSLv23 padding scheme. |
| 39 | // Cryptography.io depends on this error code. |
| 40 | OPENSSL_DECLARE_ERROR_REASON(RSA, BLOCK_TYPE_IS_NOT_02) |
| 41 |
| 42 | DEFINE_STATIC_EX_DATA_CLASS(g_rsa_ex_data_class) |
| 43 |
| 44 | static int bn_dup_into(BIGNUM **dst, const BIGNUM *src) { |
| 45 | if (src == NULL) { |
| 46 | OPENSSL_PUT_ERROR(RSA, ERR_R_PASSED_NULL_PARAMETER); |
| 47 | return 0; |
| 48 | } |
| 49 |
| 50 | BN_free(*dst); |
| 51 | *dst = BN_dup(src); |
| 52 | return *dst != NULL; |
| 53 | } |
| 54 |
| 55 | RSA *RSA_new_public_key(const BIGNUM *n, const BIGNUM *e) { |
| 56 | RSA *rsa = RSA_new(); |
| 57 | if (rsa == NULL || // |
| 58 | !bn_dup_into(&rsa->n, n) || // |
| 59 | !bn_dup_into(&rsa->e, e) || // |
| 60 | !RSA_check_key(rsa)) { |
| 61 | RSA_free(rsa); |
| 62 | return NULL; |
| 63 | } |
| 64 |
| 65 | return rsa; |
| 66 | } |
| 67 |
| 68 | RSA *RSA_new_private_key(const BIGNUM *n, const BIGNUM *e, const BIGNUM *d, |
| 69 | const BIGNUM *p, const BIGNUM *q, const BIGNUM *dmp1, |
| 70 | const BIGNUM *dmq1, const BIGNUM *iqmp) { |
| 71 | RSA *rsa = RSA_new(); |
| 72 | if (rsa == NULL || // |
| 73 | !bn_dup_into(&rsa->n, n) || // |
| 74 | !bn_dup_into(&rsa->e, e) || // |
| 75 | !bn_dup_into(&rsa->d, d) || // |
| 76 | !bn_dup_into(&rsa->p, p) || // |
| 77 | !bn_dup_into(&rsa->q, q) || // |
| 78 | !bn_dup_into(&rsa->dmp1, dmp1) || // |
| 79 | !bn_dup_into(&rsa->dmq1, dmq1) || // |
| 80 | !bn_dup_into(&rsa->iqmp, iqmp) || // |
| 81 | !RSA_check_key(rsa)) { |
| 82 | RSA_free(rsa); |
| 83 | return NULL; |
| 84 | } |
| 85 |
| 86 | return rsa; |
| 87 | } |
| 88 |
| 89 | RSA *RSA_new_private_key_no_crt(const BIGNUM *n, const BIGNUM *e, |
| 90 | const BIGNUM *d) { |
| 91 | RSA *rsa = RSA_new(); |
| 92 | if (rsa == NULL || // |
| 93 | !bn_dup_into(&rsa->n, n) || // |
| 94 | !bn_dup_into(&rsa->e, e) || // |
| 95 | !bn_dup_into(&rsa->d, d) || // |
| 96 | !RSA_check_key(rsa)) { |
| 97 | RSA_free(rsa); |
| 98 | return NULL; |
| 99 | } |
| 100 |
| 101 | return rsa; |
| 102 | } |
| 103 |
| 104 | RSA *RSA_new_private_key_no_e(const BIGNUM *n, const BIGNUM *d) { |
| 105 | RSA *rsa = RSA_new(); |
| 106 | if (rsa == NULL) { |
| 107 | return NULL; |
| 108 | } |
| 109 |
| 110 | rsa->flags |= RSA_FLAG_NO_PUBLIC_EXPONENT; |
| 111 | if (!bn_dup_into(&rsa->n, n) || // |
| 112 | !bn_dup_into(&rsa->d, d) || // |
| 113 | !RSA_check_key(rsa)) { |
| 114 | RSA_free(rsa); |
| 115 | return NULL; |
| 116 | } |
| 117 |
| 118 | return rsa; |
| 119 | } |
| 120 |
| 121 | RSA *RSA_new_public_key_large_e(const BIGNUM *n, const BIGNUM *e) { |
| 122 | RSA *rsa = RSA_new(); |
| 123 | if (rsa == NULL) { |
| 124 | return NULL; |
| 125 | } |
| 126 |
| 127 | rsa->flags |= RSA_FLAG_LARGE_PUBLIC_EXPONENT; |
| 128 | if (!bn_dup_into(&rsa->n, n) || // |
| 129 | !bn_dup_into(&rsa->e, e) || // |
| 130 | !RSA_check_key(rsa)) { |
| 131 | RSA_free(rsa); |
| 132 | return NULL; |
| 133 | } |
| 134 |
| 135 | return rsa; |
| 136 | } |
| 137 |
| 138 | RSA *RSA_new_private_key_large_e(const BIGNUM *n, const BIGNUM *e, |
| 139 | const BIGNUM *d, const BIGNUM *p, |
| 140 | const BIGNUM *q, const BIGNUM *dmp1, |
| 141 | const BIGNUM *dmq1, const BIGNUM *iqmp) { |
| 142 | RSA *rsa = RSA_new(); |
| 143 | if (rsa == NULL) { |
| 144 | return NULL; |
| 145 | } |
| 146 |
| 147 | rsa->flags |= RSA_FLAG_LARGE_PUBLIC_EXPONENT; |
| 148 | if (!bn_dup_into(&rsa->n, n) || // |
| 149 | !bn_dup_into(&rsa->e, e) || // |
| 150 | !bn_dup_into(&rsa->d, d) || // |
| 151 | !bn_dup_into(&rsa->p, p) || // |
| 152 | !bn_dup_into(&rsa->q, q) || // |
| 153 | !bn_dup_into(&rsa->dmp1, dmp1) || // |
| 154 | !bn_dup_into(&rsa->dmq1, dmq1) || // |
| 155 | !bn_dup_into(&rsa->iqmp, iqmp) || // |
| 156 | !RSA_check_key(rsa)) { |
| 157 | RSA_free(rsa); |
| 158 | return NULL; |
| 159 | } |
| 160 |
| 161 | return rsa; |
| 162 | } |
| 163 |
| 164 | RSA *RSA_new(void) { return RSA_new_method(NULL); } |
| 165 |
| 166 | RSA *RSA_new_method(const ENGINE *engine) { |
| 167 | RSA *rsa = reinterpret_cast<RSA *>(OPENSSL_zalloc(sizeof(RSA))); |
| 168 | if (rsa == NULL) { |
| 169 | return NULL; |
| 170 | } |
| 171 |
| 172 | if (engine) { |
| 173 | rsa->meth = ENGINE_get_RSA_method(engine); |
| 174 | } |
| 175 |
| 176 | if (rsa->meth == NULL) { |
| 177 | rsa->meth = (RSA_METHOD *)RSA_default_method(); |
| 178 | } |
| 179 | METHOD_ref(rsa->meth); |
| 180 |
| 181 | rsa->references = 1; |
| 182 | rsa->flags = rsa->meth->flags; |
| 183 | CRYPTO_MUTEX_init(&rsa->lock); |
| 184 | CRYPTO_new_ex_data(&rsa->ex_data); |
| 185 |
| 186 | if (rsa->meth->init && !rsa->meth->init(rsa)) { |
| 187 | CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data); |
| 188 | CRYPTO_MUTEX_cleanup(&rsa->lock); |
| 189 | METHOD_unref(rsa->meth); |
| 190 | OPENSSL_free(rsa); |
| 191 | return NULL; |
| 192 | } |
| 193 |
| 194 | return rsa; |
| 195 | } |
| 196 |
| 197 | RSA *RSA_new_method_no_e(const ENGINE *engine, const BIGNUM *n) { |
| 198 | RSA *rsa = RSA_new_method(engine); |
| 199 | if (rsa == NULL || !bn_dup_into(&rsa->n, n)) { |
| 200 | RSA_free(rsa); |
| 201 | return NULL; |
| 202 | } |
| 203 | rsa->flags |= RSA_FLAG_NO_PUBLIC_EXPONENT; |
| 204 | return rsa; |
| 205 | } |
| 206 |
| 207 | void RSA_free(RSA *rsa) { |
| 208 | if (rsa == NULL) { |
| 209 | return; |
| 210 | } |
| 211 |
| 212 | if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) { |
| 213 | return; |
| 214 | } |
| 215 |
| 216 | if (rsa->meth->finish) { |
| 217 | rsa->meth->finish(rsa); |
| 218 | } |
| 219 | METHOD_unref(rsa->meth); |
| 220 |
| 221 | CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data); |
| 222 |
| 223 | BN_free(rsa->n); |
| 224 | BN_free(rsa->e); |
| 225 | BN_free(rsa->d); |
| 226 | BN_free(rsa->p); |
| 227 | BN_free(rsa->q); |
| 228 | BN_free(rsa->dmp1); |
| 229 | BN_free(rsa->dmq1); |
| 230 | BN_free(rsa->iqmp); |
| 231 | rsa_invalidate_key(rsa); |
| 232 | CRYPTO_MUTEX_cleanup(&rsa->lock); |
| 233 | OPENSSL_free(rsa); |
| 234 | } |
| 235 |
| 236 | int RSA_up_ref(RSA *rsa) { |
| 237 | CRYPTO_refcount_inc(&rsa->references); |
| 238 | return 1; |
| 239 | } |
| 240 |
| 241 | unsigned RSA_bits(const RSA *rsa) { return BN_num_bits(rsa->n); } |
| 242 |
| 243 | const BIGNUM *RSA_get0_n(const RSA *rsa) { return rsa->n; } |
| 244 |
| 245 | const BIGNUM *RSA_get0_e(const RSA *rsa) { return rsa->e; } |
| 246 |
| 247 | const BIGNUM *RSA_get0_d(const RSA *rsa) { return rsa->d; } |
| 248 |
| 249 | const BIGNUM *RSA_get0_p(const RSA *rsa) { return rsa->p; } |
| 250 |
| 251 | const BIGNUM *RSA_get0_q(const RSA *rsa) { return rsa->q; } |
| 252 |
| 253 | const BIGNUM *RSA_get0_dmp1(const RSA *rsa) { return rsa->dmp1; } |
| 254 |
| 255 | const BIGNUM *RSA_get0_dmq1(const RSA *rsa) { return rsa->dmq1; } |
| 256 |
| 257 | const BIGNUM *RSA_get0_iqmp(const RSA *rsa) { return rsa->iqmp; } |
| 258 |
| 259 | void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e, |
| 260 | const BIGNUM **out_d) { |
| 261 | if (out_n != NULL) { |
| 262 | *out_n = rsa->n; |
| 263 | } |
| 264 | if (out_e != NULL) { |
| 265 | *out_e = rsa->e; |
| 266 | } |
| 267 | if (out_d != NULL) { |
| 268 | *out_d = rsa->d; |
| 269 | } |
| 270 | } |
| 271 |
| 272 | void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p, |
| 273 | const BIGNUM **out_q) { |
| 274 | if (out_p != NULL) { |
| 275 | *out_p = rsa->p; |
| 276 | } |
| 277 | if (out_q != NULL) { |
| 278 | *out_q = rsa->q; |
| 279 | } |
| 280 | } |
| 281 |
| 282 | const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *rsa) { |
| 283 | // We do not support the id-RSASSA-PSS key encoding. If we add support later, |
| 284 | // the |maskHash| field should be filled in for OpenSSL compatibility. |
| 285 | return NULL; |
| 286 | } |
| 287 |
| 288 | void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1, |
| 289 | const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) { |
| 290 | if (out_dmp1 != NULL) { |
| 291 | *out_dmp1 = rsa->dmp1; |
| 292 | } |
| 293 | if (out_dmq1 != NULL) { |
| 294 | *out_dmq1 = rsa->dmq1; |
| 295 | } |
| 296 | if (out_iqmp != NULL) { |
| 297 | *out_iqmp = rsa->iqmp; |
| 298 | } |
| 299 | } |
| 300 |
| 301 | int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) { |
| 302 | if ((rsa->n == NULL && n == NULL) || (rsa->e == NULL && e == NULL)) { |
| 303 | return 0; |
| 304 | } |
| 305 |
| 306 | if (n != NULL) { |
| 307 | BN_free(rsa->n); |
| 308 | rsa->n = n; |
| 309 | } |
| 310 | if (e != NULL) { |
| 311 | BN_free(rsa->e); |
| 312 | rsa->e = e; |
| 313 | } |
| 314 | if (d != NULL) { |
| 315 | BN_free(rsa->d); |
| 316 | rsa->d = d; |
| 317 | } |
| 318 |
| 319 | rsa_invalidate_key(rsa); |
| 320 | return 1; |
| 321 | } |
| 322 |
| 323 | int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) { |
| 324 | if ((rsa->p == NULL && p == NULL) || (rsa->q == NULL && q == NULL)) { |
| 325 | return 0; |
| 326 | } |
| 327 |
| 328 | if (p != NULL) { |
| 329 | BN_free(rsa->p); |
| 330 | rsa->p = p; |
| 331 | } |
| 332 | if (q != NULL) { |
| 333 | BN_free(rsa->q); |
| 334 | rsa->q = q; |
| 335 | } |
| 336 |
| 337 | rsa_invalidate_key(rsa); |
| 338 | return 1; |
| 339 | } |
| 340 |
| 341 | int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) { |
| 342 | if ((rsa->dmp1 == NULL && dmp1 == NULL) || |
| 343 | (rsa->dmq1 == NULL && dmq1 == NULL) || |
| 344 | (rsa->iqmp == NULL && iqmp == NULL)) { |
| 345 | return 0; |
| 346 | } |
| 347 |
| 348 | if (dmp1 != NULL) { |
| 349 | BN_free(rsa->dmp1); |
| 350 | rsa->dmp1 = dmp1; |
| 351 | } |
| 352 | if (dmq1 != NULL) { |
| 353 | BN_free(rsa->dmq1); |
| 354 | rsa->dmq1 = dmq1; |
| 355 | } |
| 356 | if (iqmp != NULL) { |
| 357 | BN_free(rsa->iqmp); |
| 358 | rsa->iqmp = iqmp; |
| 359 | } |
| 360 |
| 361 | rsa_invalidate_key(rsa); |
| 362 | return 1; |
| 363 | } |
| 364 |
| 365 | static int rsa_sign_raw_no_self_test(RSA *rsa, size_t *out_len, uint8_t *out, |
| 366 | size_t max_out, const uint8_t *in, |
| 367 | size_t in_len, int padding) { |
| 368 | if (rsa->meth->sign_raw) { |
| 369 | return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding); |
| 370 | } |
| 371 |
| 372 | return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding); |
| 373 | } |
| 374 |
| 375 | int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, |
| 376 | const uint8_t *in, size_t in_len, int padding) { |
| 377 | boringssl_ensure_rsa_self_test(); |
| 378 | return rsa_sign_raw_no_self_test(rsa, out_len, out, max_out, in, in_len, |
| 379 | padding); |
| 380 | } |
| 381 |
| 382 | unsigned RSA_size(const RSA *rsa) { return BN_num_bytes(rsa->n); } |
| 383 |
| 384 | int RSA_is_opaque(const RSA *rsa) { |
| 385 | return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE); |
| 386 | } |
| 387 |
| 388 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
| 389 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
| 390 | return CRYPTO_get_ex_new_index_ex(g_rsa_ex_data_class_bss_get(), argl, argp, |
| 391 | free_func); |
| 392 | } |
| 393 |
| 394 | int RSA_set_ex_data(RSA *rsa, int idx, void *arg) { |
| 395 | return CRYPTO_set_ex_data(&rsa->ex_data, idx, arg); |
| 396 | } |
| 397 |
| 398 | void *RSA_get_ex_data(const RSA *rsa, int idx) { |
| 399 | return CRYPTO_get_ex_data(&rsa->ex_data, idx); |
| 400 | } |
| 401 |
| 402 | // SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's |
| 403 | // the length of an MD5 and SHA1 hash. |
| 404 | static const unsigned SSL_SIG_LENGTH = 36; |
| 405 |
| 406 | // pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is |
| 407 | // to be signed with PKCS#1. |
| 408 | struct pkcs1_sig_prefix { |
| 409 | // nid identifies the hash function. |
| 410 | int nid; |
| 411 | // hash_len is the expected length of the hash function. |
| 412 | uint8_t hash_len; |
| 413 | // len is the number of bytes of |bytes| which are valid. |
| 414 | uint8_t len; |
| 415 | // bytes contains the DER bytes. |
| 416 | uint8_t bytes[19]; |
| 417 | }; |
| 418 |
| 419 | // kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with |
| 420 | // different hash functions. |
| 421 | static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = { |
| 422 | { |
| 423 | NID_md5, |
| 424 | MD5_DIGEST_LENGTH, |
| 425 | 18, |
| 426 | {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
| 427 | 0x02, 0x05, 0x05, 0x00, 0x04, 0x10}, |
| 428 | }, |
| 429 | { |
| 430 | NID_sha1, |
| 431 | BCM_SHA_DIGEST_LENGTH, |
| 432 | 15, |
| 433 | {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, |
| 434 | 0x00, 0x04, 0x14}, |
| 435 | }, |
| 436 | { |
| 437 | NID_sha224, |
| 438 | BCM_SHA224_DIGEST_LENGTH, |
| 439 | 19, |
| 440 | {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 441 | 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c}, |
| 442 | }, |
| 443 | { |
| 444 | NID_sha256, |
| 445 | BCM_SHA256_DIGEST_LENGTH, |
| 446 | 19, |
| 447 | {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 448 | 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}, |
| 449 | }, |
| 450 | { |
| 451 | NID_sha384, |
| 452 | BCM_SHA384_DIGEST_LENGTH, |
| 453 | 19, |
| 454 | {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 455 | 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}, |
| 456 | }, |
| 457 | { |
| 458 | NID_sha512, |
| 459 | BCM_SHA512_DIGEST_LENGTH, |
| 460 | 19, |
| 461 | {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 462 | 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}, |
| 463 | }, |
| 464 | { |
| 465 | NID_undef, |
| 466 | 0, |
| 467 | 0, |
| 468 | {0}, |
| 469 | }, |
| 470 | }; |
| 471 |
| 472 | static int rsa_check_digest_size(int hash_nid, size_t digest_len) { |
| 473 | if (hash_nid == NID_md5_sha1) { |
| 474 | if (digest_len != SSL_SIG_LENGTH) { |
| 475 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 476 | return 0; |
| 477 | } |
| 478 | return 1; |
| 479 | } |
| 480 |
| 481 | for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) { |
| 482 | const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i]; |
| 483 | if (sig_prefix->nid == hash_nid) { |
| 484 | if (digest_len != sig_prefix->hash_len) { |
| 485 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 486 | return 0; |
| 487 | } |
| 488 | return 1; |
| 489 | } |
| 490 | } |
| 491 |
| 492 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE); |
| 493 | return 0; |
| 494 | } |
| 495 |
| 496 | int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len, |
| 497 | int *is_alloced, int hash_nid, const uint8_t *digest, |
| 498 | size_t digest_len) { |
| 499 | if (!rsa_check_digest_size(hash_nid, digest_len)) { |
| 500 | return 0; |
| 501 | } |
| 502 |
| 503 | if (hash_nid == NID_md5_sha1) { |
| 504 | // The length should already have been checked. |
| 505 | assert(digest_len == SSL_SIG_LENGTH); |
| 506 | *out_msg = (uint8_t *)digest; |
| 507 | *out_msg_len = digest_len; |
| 508 | *is_alloced = 0; |
| 509 | return 1; |
| 510 | } |
| 511 |
| 512 | for (size_t i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) { |
| 513 | const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i]; |
| 514 | if (sig_prefix->nid != hash_nid) { |
| 515 | continue; |
| 516 | } |
| 517 |
| 518 | // The length should already have been checked. |
| 519 | assert(digest_len == sig_prefix->hash_len); |
| 520 | const uint8_t *prefix = sig_prefix->bytes; |
| 521 | size_t prefix_len = sig_prefix->len; |
| 522 | size_t signed_msg_len = prefix_len + digest_len; |
| 523 | if (signed_msg_len < prefix_len) { |
| 524 | OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG); |
| 525 | return 0; |
| 526 | } |
| 527 |
| 528 | uint8_t *signed_msg = |
| 529 | reinterpret_cast<uint8_t *>(OPENSSL_malloc(signed_msg_len)); |
| 530 | if (!signed_msg) { |
| 531 | return 0; |
| 532 | } |
| 533 |
| 534 | OPENSSL_memcpy(signed_msg, prefix, prefix_len); |
| 535 | OPENSSL_memcpy(signed_msg + prefix_len, digest, digest_len); |
| 536 |
| 537 | *out_msg = signed_msg; |
| 538 | *out_msg_len = signed_msg_len; |
| 539 | *is_alloced = 1; |
| 540 |
| 541 | return 1; |
| 542 | } |
| 543 |
| 544 | OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE); |
| 545 | return 0; |
| 546 | } |
| 547 |
| 548 | int rsa_sign_no_self_test(int hash_nid, const uint8_t *digest, |
| 549 | size_t digest_len, uint8_t *out, unsigned *out_len, |
| 550 | RSA *rsa) { |
| 551 | if (rsa->meth->sign) { |
| 552 | if (!rsa_check_digest_size(hash_nid, digest_len)) { |
| 553 | return 0; |
| 554 | } |
| 555 | // All supported digest lengths fit in |unsigned|. |
| 556 | assert(digest_len <= EVP_MAX_MD_SIZE); |
| 557 | static_assert(EVP_MAX_MD_SIZE <= UINT_MAX, "digest too long"); |
| 558 | return rsa->meth->sign(hash_nid, digest, (unsigned)digest_len, out, out_len, |
| 559 | rsa); |
| 560 | } |
| 561 |
| 562 | const unsigned rsa_size = RSA_size(rsa); |
| 563 | int ret = 0; |
| 564 | uint8_t *signed_msg = NULL; |
| 565 | size_t signed_msg_len = 0; |
| 566 | int signed_msg_is_alloced = 0; |
| 567 | size_t size_t_out_len; |
| 568 | if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, |
| 569 | &signed_msg_is_alloced, hash_nid, digest, |
| 570 | digest_len) || |
| 571 | !rsa_sign_raw_no_self_test(rsa, &size_t_out_len, out, rsa_size, |
| 572 | signed_msg, signed_msg_len, |
| 573 | RSA_PKCS1_PADDING)) { |
| 574 | goto err; |
| 575 | } |
| 576 |
| 577 | if (size_t_out_len > UINT_MAX) { |
| 578 | OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW); |
| 579 | goto err; |
| 580 | } |
| 581 |
| 582 | *out_len = (unsigned)size_t_out_len; |
| 583 | ret = 1; |
| 584 |
| 585 | err: |
| 586 | if (signed_msg_is_alloced) { |
| 587 | OPENSSL_free(signed_msg); |
| 588 | } |
| 589 | return ret; |
| 590 | } |
| 591 |
| 592 | int RSA_sign(int hash_nid, const uint8_t *digest, size_t digest_len, |
| 593 | uint8_t *out, unsigned *out_len, RSA *rsa) { |
| 594 | boringssl_ensure_rsa_self_test(); |
| 595 |
| 596 | return rsa_sign_no_self_test(hash_nid, digest, digest_len, out, out_len, rsa); |
| 597 | } |
| 598 |
| 599 | int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out, |
| 600 | const uint8_t *digest, size_t digest_len, |
| 601 | const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len) { |
| 602 | if (digest_len != EVP_MD_size(md)) { |
| 603 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 604 | return 0; |
| 605 | } |
| 606 |
| 607 | size_t padded_len = RSA_size(rsa); |
| 608 | uint8_t *padded = reinterpret_cast<uint8_t *>(OPENSSL_malloc(padded_len)); |
| 609 | if (padded == NULL) { |
| 610 | return 0; |
| 611 | } |
| 612 |
| 613 | int ret = RSA_padding_add_PKCS1_PSS_mgf1(rsa, padded, digest, md, mgf1_md, |
| 614 | salt_len) && |
| 615 | RSA_sign_raw(rsa, out_len, out, max_out, padded, padded_len, |
| 616 | RSA_NO_PADDING); |
| 617 | OPENSSL_free(padded); |
| 618 | return ret; |
| 619 | } |
| 620 |
| 621 | int rsa_verify_no_self_test(int hash_nid, const uint8_t *digest, |
| 622 | size_t digest_len, const uint8_t *sig, |
| 623 | size_t sig_len, RSA *rsa) { |
| 624 | if (rsa->n == NULL || rsa->e == NULL) { |
| 625 | OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
| 626 | return 0; |
| 627 | } |
| 628 |
| 629 | const size_t rsa_size = RSA_size(rsa); |
| 630 | uint8_t *buf = NULL; |
| 631 | int ret = 0; |
| 632 | uint8_t *signed_msg = NULL; |
| 633 | size_t signed_msg_len = 0, len; |
| 634 | int signed_msg_is_alloced = 0; |
| 635 |
| 636 | if (hash_nid == NID_md5_sha1 && digest_len != SSL_SIG_LENGTH) { |
| 637 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 638 | return 0; |
| 639 | } |
| 640 |
| 641 | buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(rsa_size)); |
| 642 | if (!buf) { |
| 643 | return 0; |
| 644 | } |
| 645 |
| 646 | if (!rsa_verify_raw_no_self_test(rsa, &len, buf, rsa_size, sig, sig_len, |
| 647 | RSA_PKCS1_PADDING) || |
| 648 | !RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len, |
| 649 | &signed_msg_is_alloced, hash_nid, digest, |
| 650 | digest_len)) { |
| 651 | goto out; |
| 652 | } |
| 653 |
| 654 | // Check that no other information follows the hash value (FIPS 186-4 Section |
| 655 | // 5.5) and it matches the expected hash. |
| 656 | if (len != signed_msg_len || OPENSSL_memcmp(buf, signed_msg, len) != 0) { |
| 657 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE); |
| 658 | goto out; |
| 659 | } |
| 660 |
| 661 | ret = 1; |
| 662 |
| 663 | out: |
| 664 | OPENSSL_free(buf); |
| 665 | if (signed_msg_is_alloced) { |
| 666 | OPENSSL_free(signed_msg); |
| 667 | } |
| 668 | return ret; |
| 669 | } |
| 670 |
| 671 | int RSA_verify(int hash_nid, const uint8_t *digest, size_t digest_len, |
| 672 | const uint8_t *sig, size_t sig_len, RSA *rsa) { |
| 673 | boringssl_ensure_rsa_self_test(); |
| 674 | return rsa_verify_no_self_test(hash_nid, digest, digest_len, sig, sig_len, |
| 675 | rsa); |
| 676 | } |
| 677 |
| 678 | int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *digest, size_t digest_len, |
| 679 | const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len, |
| 680 | const uint8_t *sig, size_t sig_len) { |
| 681 | if (digest_len != EVP_MD_size(md)) { |
| 682 | OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH); |
| 683 | return 0; |
| 684 | } |
| 685 |
| 686 | size_t em_len = RSA_size(rsa); |
| 687 | uint8_t *em = reinterpret_cast<uint8_t *>(OPENSSL_malloc(em_len)); |
| 688 | if (em == NULL) { |
| 689 | return 0; |
| 690 | } |
| 691 |
| 692 | int ret = 0; |
| 693 | if (!RSA_verify_raw(rsa, &em_len, em, em_len, sig, sig_len, RSA_NO_PADDING)) { |
| 694 | goto err; |
| 695 | } |
| 696 |
| 697 | if (em_len != RSA_size(rsa)) { |
| 698 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 699 | goto err; |
| 700 | } |
| 701 |
| 702 | ret = RSA_verify_PKCS1_PSS_mgf1(rsa, digest, md, mgf1_md, em, salt_len); |
| 703 |
| 704 | err: |
| 705 | OPENSSL_free(em); |
| 706 | return ret; |
| 707 | } |
| 708 |
| 709 | static int check_mod_inverse(int *out_ok, const BIGNUM *a, const BIGNUM *ainv, |
| 710 | const BIGNUM *m, unsigned m_min_bits, |
| 711 | BN_CTX *ctx) { |
| 712 | if (BN_is_negative(ainv) || |
| 713 | constant_time_declassify_int(BN_cmp(ainv, m) >= 0)) { |
| 714 | *out_ok = 0; |
| 715 | return 1; |
| 716 | } |
| 717 |
| 718 | // Note |bn_mul_consttime| and |bn_div_consttime| do not scale linearly, but |
| 719 | // checking |ainv| is in range bounds the running time, assuming |m|'s bounds |
| 720 | // were checked by the caller. |
| 721 | BN_CTX_start(ctx); |
| 722 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 723 | int ret = tmp != NULL && bn_mul_consttime(tmp, a, ainv, ctx) && |
| 724 | bn_div_consttime(NULL, tmp, tmp, m, m_min_bits, ctx); |
| 725 | if (ret) { |
| 726 | *out_ok = constant_time_declassify_int(BN_is_one(tmp)); |
| 727 | } |
| 728 | BN_CTX_end(ctx); |
| 729 | return ret; |
| 730 | } |
| 731 |
| 732 | int RSA_check_key(const RSA *key) { |
| 733 | // TODO(davidben): RSA key initialization is spread across |
| 734 | // |rsa_check_public_key|, |RSA_check_key|, |freeze_private_key|, and |
| 735 | // |BN_MONT_CTX_set_locked| as a result of API issues. See |
| 736 | // https://crbug.com/boringssl/316. As a result, we inconsistently check RSA |
| 737 | // invariants. We should fix this and integrate that logic. |
| 738 |
| 739 | if (!rsa_check_public_key(key)) { |
| 740 | return 0; |
| 741 | } |
| 742 |
| 743 | if ((key->p != NULL) != (key->q != NULL)) { |
| 744 | OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN); |
| 745 | return 0; |
| 746 | } |
| 747 |
| 748 | // |key->d| must be bounded by |key->n|. This ensures bounds on |RSA_bits| |
| 749 | // translate to bounds on the running time of private key operations. |
| 750 | if (key->d != NULL && |
| 751 | (BN_is_negative(key->d) || BN_cmp(key->d, key->n) >= 0)) { |
| 752 | OPENSSL_PUT_ERROR(RSA, RSA_R_D_OUT_OF_RANGE); |
| 753 | return 0; |
| 754 | } |
| 755 |
| 756 | if (key->d == NULL || key->p == NULL) { |
| 757 | // For a public key, or without p and q, there's nothing that can be |
| 758 | // checked. |
| 759 | return 1; |
| 760 | } |
| 761 |
| 762 | BN_CTX *ctx = BN_CTX_new(); |
| 763 | if (ctx == NULL) { |
| 764 | return 0; |
| 765 | } |
| 766 |
| 767 | BIGNUM tmp, de, pm1, qm1, dmp1, dmq1; |
| 768 | int ok = 0, has_crt_values; |
| 769 | unsigned pm1_bits, qm1_bits; |
| 770 | BN_init(&tmp); |
| 771 | BN_init(&de); |
| 772 | BN_init(&pm1); |
| 773 | BN_init(&qm1); |
| 774 | BN_init(&dmp1); |
| 775 | BN_init(&dmq1); |
| 776 |
| 777 | // Check that p * q == n. Before we multiply, we check that p and q are in |
| 778 | // bounds, to avoid a DoS vector in |bn_mul_consttime| below. Note that |
| 779 | // n was bound by |rsa_check_public_key|. This also implicitly checks p and q |
| 780 | // are odd, which is a necessary condition for Montgomery reduction. |
| 781 | if (BN_is_negative(key->p) || |
| 782 | constant_time_declassify_int(BN_cmp(key->p, key->n) >= 0) || |
| 783 | BN_is_negative(key->q) || |
| 784 | constant_time_declassify_int(BN_cmp(key->q, key->n) >= 0)) { |
| 785 | OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q); |
| 786 | goto out; |
| 787 | } |
| 788 | if (!bn_mul_consttime(&tmp, key->p, key->q, ctx)) { |
| 789 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 790 | goto out; |
| 791 | } |
| 792 | if (BN_cmp(&tmp, key->n) != 0) { |
| 793 | OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q); |
| 794 | goto out; |
| 795 | } |
| 796 |
| 797 | // d must be an inverse of e mod the Carmichael totient, lcm(p-1, q-1), but it |
| 798 | // may be unreduced because other implementations use the Euler totient. We |
| 799 | // simply check that d * e is one mod p-1 and mod q-1. Note d and e were bound |
| 800 | // by earlier checks in this function. |
| 801 | if (!bn_usub_consttime(&pm1, key->p, BN_value_one()) || |
| 802 | !bn_usub_consttime(&qm1, key->q, BN_value_one())) { |
| 803 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 804 | goto out; |
| 805 | } |
| 806 | pm1_bits = BN_num_bits(&pm1); |
| 807 | qm1_bits = BN_num_bits(&qm1); |
| 808 | if (!bn_mul_consttime(&de, key->d, key->e, ctx) || |
| 809 | !bn_div_consttime(NULL, &tmp, &de, &pm1, pm1_bits, ctx) || |
| 810 | !bn_div_consttime(NULL, &de, &de, &qm1, qm1_bits, ctx)) { |
| 811 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 812 | goto out; |
| 813 | } |
| 814 |
| 815 | if (constant_time_declassify_int(!BN_is_one(&tmp)) || |
| 816 | constant_time_declassify_int(!BN_is_one(&de))) { |
| 817 | OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1); |
| 818 | goto out; |
| 819 | } |
| 820 |
| 821 | has_crt_values = key->dmp1 != NULL; |
| 822 | if (has_crt_values != (key->dmq1 != NULL) || |
| 823 | has_crt_values != (key->iqmp != NULL)) { |
| 824 | OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES); |
| 825 | goto out; |
| 826 | } |
| 827 |
| 828 | if (has_crt_values) { |
| 829 | int dmp1_ok, dmq1_ok, iqmp_ok; |
| 830 | if (!check_mod_inverse(&dmp1_ok, key->e, key->dmp1, &pm1, pm1_bits, ctx) || |
| 831 | !check_mod_inverse(&dmq1_ok, key->e, key->dmq1, &qm1, qm1_bits, ctx) || |
| 832 | // |p| is odd, so |pm1| and |p| have the same bit width. If they didn't, |
| 833 | // we only need a lower bound anyway. |
| 834 | !check_mod_inverse(&iqmp_ok, key->q, key->iqmp, key->p, pm1_bits, |
| 835 | ctx)) { |
| 836 | OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN); |
| 837 | goto out; |
| 838 | } |
| 839 |
| 840 | if (!dmp1_ok || !dmq1_ok || !iqmp_ok) { |
| 841 | OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT); |
| 842 | goto out; |
| 843 | } |
| 844 | } |
| 845 |
| 846 | ok = 1; |
| 847 |
| 848 | out: |
| 849 | BN_free(&tmp); |
| 850 | BN_free(&de); |
| 851 | BN_free(&pm1); |
| 852 | BN_free(&qm1); |
| 853 | BN_free(&dmp1); |
| 854 | BN_free(&dmq1); |
| 855 | BN_CTX_free(ctx); |
| 856 |
| 857 | return ok; |
| 858 | } |
| 859 |
| 860 |
| 861 | // This is the product of the 132 smallest odd primes, from 3 to 751. |
| 862 | static const BN_ULONG kSmallFactorsLimbs[] = {TOBN(0xc4309333, 0x3ef4e3e1), |
| 863 | TOBN(0x71161eb6, 0xcd2d655f), |
| 864 | TOBN(0x95e2238c, 0x0bf94862), |
| 865 | TOBN(0x3eb233d3, 0x24f7912b), |
| 866 | TOBN(0x6b55514b, 0xbf26c483), |
| 867 | TOBN(0x0a84d817, 0x5a144871), |
| 868 | TOBN(0x77d12fee, 0x9b82210a), |
| 869 | TOBN(0xdb5b93c2, 0x97f050b3), |
| 870 | TOBN(0x4acad6b9, 0x4d6c026b), |
| 871 | TOBN(0xeb7751f3, 0x54aec893), |
| 872 | TOBN(0xdba53368, 0x36bc85c4), |
| 873 | TOBN(0xd85a1b28, 0x7f5ec78e), |
| 874 | TOBN(0x2eb072d8, 0x6b322244), |
| 875 | TOBN(0xbba51112, 0x5e2b3aea), |
| 876 | TOBN(0x36ed1a6c, 0x0e2486bf), |
| 877 | TOBN(0x5f270460, 0xec0c5727), |
| 878 | 0x000017b1}; |
| 879 |
| 880 | DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) { |
| 881 | out->d = (BN_ULONG *)kSmallFactorsLimbs; |
| 882 | out->width = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs); |
| 883 | out->dmax = out->width; |
| 884 | out->neg = 0; |
| 885 | out->flags = BN_FLG_STATIC_DATA; |
| 886 | } |
| 887 |
| 888 | int RSA_check_fips(RSA *key) { |
| 889 | if (!RSA_check_key(key)) { |
| 890 | return 0; |
| 891 | } |
| 892 |
| 893 | BN_CTX *ctx = BN_CTX_new(); |
| 894 | if (ctx == NULL) { |
| 895 | return 0; |
| 896 | } |
| 897 |
| 898 | BIGNUM small_gcd; |
| 899 | BN_init(&small_gcd); |
| 900 |
| 901 | int ret = 1; |
| 902 |
| 903 | // Perform partial public key validation of RSA keys (SP 800-89 5.3.3). |
| 904 | // Although this is not for primality testing, SP 800-89 cites an RSA |
| 905 | // primality testing algorithm, so we use |BN_prime_checks_for_generation| to |
| 906 | // match. This is only a plausibility test and we expect the value to be |
| 907 | // composite, so too few iterations will cause us to reject the key, not use |
| 908 | // an implausible one. |
| 909 | // |
| 910 | // |key->e| may be nullptr if created with |RSA_new_private_key_no_e|. |
| 911 | enum bn_primality_result_t primality_result; |
| 912 | if (key->e == nullptr || // |
| 913 | BN_num_bits(key->e) <= 16 || // |
| 914 | BN_num_bits(key->e) > 256 || // |
| 915 | !BN_is_odd(key->n) || // |
| 916 | !BN_is_odd(key->e) || |
| 917 | !BN_gcd(&small_gcd, key->n, g_small_factors(), ctx) || |
| 918 | !BN_is_one(&small_gcd) || |
| 919 | !BN_enhanced_miller_rabin_primality_test(&primality_result, key->n, |
| 920 | BN_prime_checks_for_generation, |
| 921 | ctx, NULL) || |
| 922 | primality_result != bn_non_prime_power_composite) { |
| 923 | OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED); |
| 924 | ret = 0; |
| 925 | } |
| 926 |
| 927 | BN_free(&small_gcd); |
| 928 | BN_CTX_free(ctx); |
| 929 |
| 930 | if (!ret || key->d == NULL || key->p == NULL) { |
| 931 | // On a failure or on only a public key, there's nothing else can be |
| 932 | // checked. |
| 933 | return ret; |
| 934 | } |
| 935 |
| 936 | // FIPS pairwise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG, |
| 937 | // section 9.9, it is not known whether |rsa| will be used for signing or |
| 938 | // encryption, so either pair-wise consistency self-test is acceptable. We |
| 939 | // perform a signing test. |
| 940 | uint8_t data[32] = {0}; |
| 941 | unsigned sig_len = RSA_size(key); |
| 942 | uint8_t *sig = reinterpret_cast<uint8_t *>(OPENSSL_malloc(sig_len)); |
| 943 | if (sig == NULL) { |
| 944 | return 0; |
| 945 | } |
| 946 |
| 947 | if (!RSA_sign(NID_sha256, data, sizeof(data), sig, &sig_len, key)) { |
| 948 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 949 | ret = 0; |
| 950 | goto cleanup; |
| 951 | } |
| 952 | if (boringssl_fips_break_test("RSA_PWCT")) { |
| 953 | data[0] = ~data[0]; |
| 954 | } |
| 955 | if (!RSA_verify(NID_sha256, data, sizeof(data), sig, sig_len, key)) { |
| 956 | OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 957 | ret = 0; |
| 958 | } |
| 959 |
| 960 | cleanup: |
| 961 | OPENSSL_free(sig); |
| 962 |
| 963 | return ret; |
| 964 | } |
| 965 |
| 966 | int rsa_private_transform_no_self_test(RSA *rsa, uint8_t *out, |
| 967 | const uint8_t *in, size_t len) { |
| 968 | if (rsa->meth->private_transform) { |
| 969 | return rsa->meth->private_transform(rsa, out, in, len); |
| 970 | } |
| 971 |
| 972 | return rsa_default_private_transform(rsa, out, in, len); |
| 973 | } |
| 974 |
| 975 | int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, |
| 976 | size_t len) { |
| 977 | boringssl_ensure_rsa_self_test(); |
| 978 | return rsa_private_transform_no_self_test(rsa, out, in, len); |
| 979 | } |
| 980 |
| 981 | int RSA_flags(const RSA *rsa) { return rsa->flags; } |
| 982 |
| 983 | int RSA_test_flags(const RSA *rsa, int flags) { return rsa->flags & flags; } |
| 984 |