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