Coverage Report

Created: 2026-08-08 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/evp/p_rsa.cc
Line
Count
Source
1
// Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
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
#include <openssl/span.h>
28
29
#include "../fipsmodule/rsa/internal.h"
30
#include "../internal.h"
31
#include "../mem_internal.h"
32
#include "../rsa/internal.h"
33
#include "internal.h"
34
35
36
using namespace bssl;
37
38
namespace {
39
40
struct EVP_PKEY_ALG_RSA_PSS : public EVP_PKEY_ALG {
41
  rsa_pss_params_t pss_params;
42
};
43
44
extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
45
extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
46
extern const EVP_PKEY_CTX_METHOD rsa_pkey_meth;
47
extern const EVP_PKEY_CTX_METHOD rsa_pss_pkey_meth;
48
49
1.15k
static int rsa_pub_encode(CBB *out, const EvpPkey *key) {
50
  // See RFC 3279, section 2.3.1.
51
1.15k
  const RSA *rsa = reinterpret_cast<const RSA *>(key->pkey);
52
1.15k
  CBB spki, algorithm, null, key_bitstring;
53
1.15k
  if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
54
1.15k
      !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
55
1.15k
      !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, rsa_asn1_meth.oid,
56
1.15k
                            rsa_asn1_meth.oid_len) ||
57
1.15k
      !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
58
1.15k
      !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
59
1.15k
      !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
60
1.15k
      !RSA_marshal_public_key(&key_bitstring, rsa) ||  //
61
1.15k
      !CBB_flush(out)) {
62
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
63
0
    return 0;
64
0
  }
65
66
1.15k
  return 1;
67
1.15k
}
68
69
static bssl::evp_decode_result_t rsa_pub_decode(const EVP_PKEY_ALG *alg,
70
                                                EvpPkey *out, CBS *params,
71
132k
                                                CBS *key) {
72
  // See RFC 3279, section 2.3.1.
73
74
  // The parameters must be NULL.
75
132k
  CBS null;
76
132k
  if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) || CBS_len(&null) != 0 ||
77
131k
      CBS_len(params) != 0) {
78
651
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
79
651
    return evp_decode_error;
80
651
  }
81
82
131k
  UniquePtr<RSA> rsa(RSA_public_key_from_bytes(CBS_data(key), CBS_len(key)));
83
131k
  if (rsa == nullptr) {
84
23.1k
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
85
23.1k
    return evp_decode_error;
86
23.1k
  }
87
88
108k
  EVP_PKEY_assign_RSA(out, rsa.release());
89
108k
  return evp_decode_ok;
90
131k
}
91
92
11.8k
static bool rsa_pub_equal(const EvpPkey *a, const EvpPkey *b) {
93
  // We currently assume that all `EVP_PKEY_RSA_PSS` keys have the same
94
  // parameters, so this vacuously compares parameters. If we ever support
95
  // multiple PSS parameter sets, we probably should compare them too. Note,
96
  // however, that OpenSSL does not compare parameters here.
97
11.8k
  const RSA *a_rsa = reinterpret_cast<const RSA *>(a->pkey);
98
11.8k
  const RSA *b_rsa = reinterpret_cast<const RSA *>(b->pkey);
99
11.8k
  return BN_cmp(RSA_get0_n(b_rsa), RSA_get0_n(a_rsa)) == 0 &&
100
11.8k
         BN_cmp(RSA_get0_e(b_rsa), RSA_get0_e(a_rsa)) == 0;
101
11.8k
}
102
103
0
static bool rsa_pub_present(const EvpPkey *pk) {
104
0
  const RSA *pk_rsa = reinterpret_cast<const RSA *>(pk->pkey);
105
  // An RSA public key should always have n and e. It's possible for a (private)
106
  // key to have n and d, but not e, so we must explicitly check for the
107
  // presence of e.
108
0
  return RSA_get0_n(pk_rsa) != nullptr && RSA_get0_e(pk_rsa) != nullptr;
109
0
}
110
111
0
static bool rsa_pub_copy(EvpPkey *out, const EvpPkey *pkey) {
112
0
  const RSAImpl *pk_rsa = reinterpret_cast<const RSAImpl *>(pkey->pkey);
113
0
  const BIGNUM *pk_n = RSA_get0_n(pk_rsa);
114
0
  const BIGNUM *pk_e = RSA_get0_e(pk_rsa);
115
0
  if (pk_n == nullptr || pk_e == nullptr) {
116
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PUBLIC_KEY);
117
0
    return false;
118
0
  }
119
0
  UniquePtr<RSA> public_copy_rsa(RSA_new_public_key(pk_n, pk_e));
120
0
  if (!public_copy_rsa) {
121
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
122
0
    return false;
123
0
  }
124
0
  FromOpaque(public_copy_rsa.get())->pss_params = pk_rsa->pss_params;
125
0
  evp_pkey_set0(out, pkey->ameth, public_copy_rsa.release());
126
0
  return true;
127
0
}
128
129
2
static int rsa_priv_encode(CBB *out, const EvpPkey *key) {
130
2
  const RSA *rsa = reinterpret_cast<const RSA *>(key->pkey);
131
2
  CBB pkcs8, algorithm, null, private_key;
132
2
  if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
133
2
      !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
134
2
      !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
135
2
      !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, rsa_asn1_meth.oid,
136
2
                            rsa_asn1_meth.oid_len) ||
137
2
      !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
138
2
      !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
139
2
      !RSA_marshal_private_key(&private_key, rsa) ||  //
140
2
      !CBB_flush(out)) {
141
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
142
0
    return 0;
143
0
  }
144
145
2
  return 1;
146
2
}
147
148
static bssl::evp_decode_result_t rsa_priv_decode(const EVP_PKEY_ALG *alg,
149
                                                 EvpPkey *out, CBS *params,
150
946
                                                 CBS *key) {
151
  // Per RFC 8017, A.1, the parameters have type NULL.
152
946
  CBS null;
153
946
  if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) || CBS_len(&null) != 0 ||
154
921
      CBS_len(params) != 0) {
155
48
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
156
48
    return evp_decode_error;
157
48
  }
158
159
898
  UniquePtr<RSA> rsa(RSA_private_key_from_bytes(CBS_data(key), CBS_len(key)));
160
898
  if (rsa == nullptr) {
161
888
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
162
888
    return evp_decode_error;
163
888
  }
164
165
10
  EVP_PKEY_assign_RSA(out, rsa.release());
166
10
  return evp_decode_ok;
167
898
}
168
169
0
static bool rsa_priv_present(const EvpPkey *pk) {
170
0
  const RSA *pk_rsa = reinterpret_cast<const RSA *>(pk->pkey);
171
0
  return RSA_get0_n(pk_rsa) != nullptr && RSA_get0_d(pk_rsa) != nullptr;
172
0
}
173
174
static bssl::evp_decode_result_t rsa_decode_pss_params(
175
0
    rsa_pss_params_t expected, CBS *params) {
176
0
  if (CBS_len(params) == 0) {
177
0
    return evp_decode_unsupported;
178
0
  }
179
0
  rsa_pss_params_t pss_params;
180
0
  if (!rsa_parse_pss_params(params, &pss_params,
181
0
                            /*allow_explicit_trailer=*/false) ||
182
0
      CBS_len(params) != 0) {
183
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
184
0
    return evp_decode_error;
185
0
  }
186
0
  return pss_params == expected ? evp_decode_ok : evp_decode_unsupported;
187
0
}
188
189
0
static int rsa_pub_encode_pss(CBB *out, const EvpPkey *key) {
190
0
  const RSAImpl *rsa = reinterpret_cast<const RSAImpl *>(key->pkey);
191
0
  CBB spki, algorithm, key_bitstring;
192
0
  if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
193
0
      !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
194
0
      !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, rsa_pss_asn1_meth.oid,
195
0
                            rsa_pss_asn1_meth.oid_len) ||
196
0
      !rsa_marshal_pss_params(&algorithm, rsa->pss_params) ||
197
0
      !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
198
0
      !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
199
0
      !RSA_marshal_public_key(&key_bitstring, rsa) ||  //
200
0
      !CBB_flush(out)) {
201
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
202
0
    return 0;
203
0
  }
204
205
0
  return 1;
206
0
}
207
208
static void evp_pkey_set0_pss(EvpPkey *out, const EVP_PKEY_ALG *alg,
209
0
                              UniquePtr<RSA> rsa) {
210
0
  BSSL_CHECK(alg->pkey_method->pkey_id == EVP_PKEY_RSA_PSS);
211
0
  const auto *alg_pss = static_cast<const EVP_PKEY_ALG_RSA_PSS *>(alg);
212
0
  FromOpaque(rsa.get())->pss_params = alg_pss->pss_params;
213
0
  evp_pkey_set0(out, alg->method, rsa.release());
214
0
}
215
216
static bssl::evp_decode_result_t rsa_pub_decode_pss(const EVP_PKEY_ALG *alg,
217
                                                    EvpPkey *out, CBS *params,
218
0
                                                    CBS *key) {
219
0
  const auto *alg_pss = static_cast<const EVP_PKEY_ALG_RSA_PSS *>(alg);
220
0
  evp_decode_result_t ret = rsa_decode_pss_params(alg_pss->pss_params, params);
221
0
  if (ret != evp_decode_ok) {
222
0
    return ret;
223
0
  }
224
225
0
  UniquePtr<RSA> rsa(RSA_public_key_from_bytes(CBS_data(key), CBS_len(key)));
226
0
  if (rsa == nullptr) {
227
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
228
0
    return evp_decode_error;
229
0
  }
230
231
0
  evp_pkey_set0_pss(out, alg, std::move(rsa));
232
0
  return evp_decode_ok;
233
0
}
234
235
0
static int rsa_priv_encode_pss(CBB *out, const EvpPkey *key) {
236
0
  const RSAImpl *rsa = reinterpret_cast<const RSAImpl *>(key->pkey);
237
0
  CBB pkcs8, algorithm, private_key;
238
0
  if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
239
0
      !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
240
0
      !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
241
0
      !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, rsa_pss_asn1_meth.oid,
242
0
                            rsa_pss_asn1_meth.oid_len) ||
243
0
      !rsa_marshal_pss_params(&algorithm, rsa->pss_params) ||
244
0
      !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
245
0
      !RSA_marshal_private_key(&private_key, rsa) ||  //
246
0
      !CBB_flush(out)) {
247
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
248
0
    return 0;
249
0
  }
250
251
0
  return 1;
252
0
}
253
254
static bssl::evp_decode_result_t rsa_priv_decode_pss(const EVP_PKEY_ALG *alg,
255
                                                     EvpPkey *out, CBS *params,
256
0
                                                     CBS *key) {
257
0
  const auto *alg_pss = static_cast<const EVP_PKEY_ALG_RSA_PSS *>(alg);
258
0
  evp_decode_result_t ret = rsa_decode_pss_params(alg_pss->pss_params, params);
259
0
  if (ret != evp_decode_ok) {
260
0
    return ret;
261
0
  }
262
263
0
  UniquePtr<RSA> rsa(RSA_private_key_from_bytes(CBS_data(key), CBS_len(key)));
264
0
  if (rsa == nullptr) {
265
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
266
0
    return evp_decode_error;
267
0
  }
268
269
0
  evp_pkey_set0_pss(out, alg, std::move(rsa));
270
0
  return evp_decode_ok;
271
0
}
272
273
11.8k
static int rsa_opaque(const EvpPkey *pkey) {
274
11.8k
  const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
275
11.8k
  return RSA_is_opaque(rsa);
276
11.8k
}
277
278
114k
static int int_rsa_size(const EvpPkey *pkey) {
279
114k
  const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
280
114k
  return RSA_size(rsa);
281
114k
}
282
283
0
static int rsa_bits(const EvpPkey *pkey) {
284
0
  const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
285
0
  return RSA_bits(rsa);
286
0
}
287
288
108k
static void int_rsa_free(EvpPkey *pkey) {
289
108k
  RSA_free(reinterpret_cast<RSA *>(pkey->pkey));
290
108k
  pkey->pkey = nullptr;
291
108k
}
292
293
0
static int rsa_pss_params_missing(const EvpPkey *pkey) {
294
0
  const RSA *rsa = reinterpret_cast<const RSA *>(pkey->pkey);
295
0
  return rsa == nullptr || FromOpaque(rsa)->pss_params == rsa_pss_none;
296
0
}
297
298
0
static int rsa_pss_params_copy(EvpPkey *to, const EvpPkey *from) {
299
0
  const RSA *from_key = reinterpret_cast<const RSA *>(from->pkey);
300
0
  if (from_key == nullptr) {
301
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
302
0
    return 0;
303
0
  }
304
0
  rsa_pss_params_t pss_params = FromOpaque(from_key)->pss_params;
305
0
  if (pss_params == rsa_pss_none) {
306
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
307
0
    return 0;
308
0
  }
309
0
  if (to->pkey == nullptr) {
310
0
    to->pkey = RSA_new();
311
0
    if (to->pkey == nullptr) {
312
0
      return 0;
313
0
    }
314
0
  }
315
0
  FromOpaque(reinterpret_cast<RSA *>(to->pkey))->pss_params = pss_params;
316
0
  return 1;
317
0
}
318
319
0
static bool rsa_pss_params_equal(const EvpPkey *a, const EvpPkey *b) {
320
0
  const RSA *a_rsa = reinterpret_cast<const RSA *>(a->pkey);
321
0
  const RSA *b_rsa = reinterpret_cast<const RSA *>(b->pkey);
322
0
  if (a_rsa == nullptr || b_rsa == nullptr) {
323
0
    return false;
324
0
  }
325
0
  rsa_pss_params_t a_pss_params = FromOpaque(a_rsa)->pss_params;
326
0
  rsa_pss_params_t b_pss_params = FromOpaque(b_rsa)->pss_params;
327
0
  if (a_pss_params == rsa_pss_none || b_pss_params == rsa_pss_none) {
328
0
    return false;
329
0
  }
330
0
  return a_pss_params == b_pss_params;
331
0
}
332
333
const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
334
    EVP_PKEY_RSA,
335
    // 1.2.840.113549.1.1.1
336
    {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01},
337
    9,
338
339
    &rsa_pkey_meth,
340
341
    rsa_pub_decode,
342
    rsa_pub_encode,
343
    rsa_pub_equal,
344
    rsa_pub_present,
345
    rsa_pub_copy,
346
347
    rsa_priv_decode,
348
    rsa_priv_encode,
349
    rsa_priv_present,
350
351
    /*set_priv_raw=*/nullptr,
352
    /*set_priv_seed=*/nullptr,
353
    /*set_pub_raw=*/nullptr,
354
    /*get_priv_raw=*/nullptr,
355
    /*get_priv_seed=*/nullptr,
356
    /*get_pub_raw=*/nullptr,
357
    /*set1_tls_encodedpoint=*/nullptr,
358
    /*get1_tls_encodedpoint=*/nullptr,
359
360
    rsa_opaque,
361
362
    int_rsa_size,
363
    rsa_bits,
364
365
    /*param_missing=*/nullptr,
366
    /*param_copy=*/nullptr,
367
    /*param_equal=*/nullptr,
368
369
    int_rsa_free,
370
};
371
372
const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
373
    EVP_PKEY_RSA_PSS,
374
    // 1.2.840.113549.1.1.10
375
    {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a},
376
    9,
377
378
    &rsa_pss_pkey_meth,
379
380
    rsa_pub_decode_pss,
381
    rsa_pub_encode_pss,
382
    rsa_pub_equal,
383
    rsa_pub_present,
384
    rsa_pub_copy,
385
386
    rsa_priv_decode_pss,
387
    rsa_priv_encode_pss,
388
    rsa_priv_present,
389
390
    /*set_priv_raw=*/nullptr,
391
    /*set_priv_seed=*/nullptr,
392
    /*set_pub_raw=*/nullptr,
393
    /*get_priv_raw=*/nullptr,
394
    /*get_priv_seed=*/nullptr,
395
    /*get_pub_raw=*/nullptr,
396
    /*set1_tls_encodedpoint=*/nullptr,
397
    /*get1_tls_encodedpoint=*/nullptr,
398
399
    rsa_opaque,
400
401
    int_rsa_size,
402
    rsa_bits,
403
404
    rsa_pss_params_missing,
405
    rsa_pss_params_copy,
406
    rsa_pss_params_equal,
407
408
    int_rsa_free,
409
};
410
411
412
struct RSA_PKEY_CTX {
413
  // Key gen parameters
414
  int nbits = 2048;
415
  UniquePtr<BIGNUM> pub_exp;
416
  // RSA padding mode
417
  int pad_mode = RSA_PKCS1_PADDING;
418
  // message digest
419
  const EVP_MD *md = nullptr;
420
  // message digest for MGF1
421
  const EVP_MD *mgf1md = nullptr;
422
  // PSS salt length
423
  int saltlen = RSA_PSS_SALTLEN_DIGEST;
424
  // restrict_pss_params, if true, indicates that the PSS signing/verifying
425
  // parameters are restricted by the key's parameters. `md` and `mgf1md` may
426
  // not change, and `saltlen` must be at least `md`'s hash length.
427
  bool restrict_pss_params = false;
428
  Array<uint8_t> oaep_label;
429
};
430
431
105k
static bool is_pss_only(const EvpPkeyCtx *ctx) {
432
105k
  return ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS;
433
105k
}
434
435
88.7k
static int pkey_rsa_init(EvpPkeyCtx *ctx, const EVP_PKEY_ALG *alg) {
436
88.7k
  RSA_PKEY_CTX *rctx = New<RSA_PKEY_CTX>();
437
88.7k
  if (!rctx) {
438
0
    return 0;
439
0
  }
440
441
88.7k
  if (is_pss_only(ctx)) {
442
0
    rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
443
    // Pick up PSS parameters from the key or algorithm. We don't currently
444
    // support keygen from PSS, so the algorithm does not currently do anything.
445
0
    rsa_pss_params_t pss_params = rsa_pss_none;
446
0
    const auto *alg_pss = static_cast<const EVP_PKEY_ALG_RSA_PSS *>(alg);
447
0
    if (alg_pss != nullptr) {
448
0
      pss_params = alg_pss->pss_params;
449
0
    } else if (ctx->pkey != nullptr && ctx->pkey->pkey != nullptr) {
450
0
      pss_params = static_cast<const RSAImpl *>(ctx->pkey->pkey)->pss_params;
451
0
    }
452
0
    const EVP_MD *md = rsa_pss_params_get_md(pss_params);
453
0
    if (md != nullptr) {
454
0
      rctx->md = rctx->mgf1md = md;
455
      // All our supported modes use the digest length as the salt length.
456
0
      rctx->saltlen = EVP_MD_size(rctx->md);
457
0
      rctx->restrict_pss_params = true;
458
0
    }
459
0
  }
460
461
88.7k
  ctx->data = rctx;
462
88.7k
  return 1;
463
88.7k
}
464
465
44.3k
static int pkey_rsa_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) {
466
44.3k
  RSA_PKEY_CTX *dctx, *sctx;
467
44.3k
  if (!pkey_rsa_init(dst, nullptr)) {
468
0
    return 0;
469
0
  }
470
44.3k
  sctx = reinterpret_cast<RSA_PKEY_CTX *>(src->data);
471
44.3k
  dctx = reinterpret_cast<RSA_PKEY_CTX *>(dst->data);
472
44.3k
  dctx->nbits = sctx->nbits;
473
44.3k
  if (sctx->pub_exp) {
474
0
    dctx->pub_exp.reset(BN_dup(sctx->pub_exp.get()));
475
0
    if (!dctx->pub_exp) {
476
0
      return 0;
477
0
    }
478
0
  }
479
480
44.3k
  dctx->pad_mode = sctx->pad_mode;
481
44.3k
  dctx->md = sctx->md;
482
44.3k
  dctx->mgf1md = sctx->mgf1md;
483
44.3k
  dctx->saltlen = sctx->saltlen;
484
44.3k
  dctx->restrict_pss_params = sctx->restrict_pss_params;
485
44.3k
  if (!dctx->oaep_label.CopyFrom(sctx->oaep_label)) {
486
0
    return 0;
487
0
  }
488
489
44.3k
  return 1;
490
44.3k
}
491
492
88.7k
static void pkey_rsa_cleanup(EvpPkeyCtx *ctx) {
493
88.7k
  Delete(reinterpret_cast<RSA_PKEY_CTX *>(ctx->data));
494
88.7k
}
495
496
static int pkey_rsa_sign(EvpPkeyCtx *ctx, uint8_t *sig, size_t *siglen,
497
20.6k
                         const uint8_t *tbs, size_t tbslen) {
498
20.6k
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
499
20.6k
  RSA *rsa = reinterpret_cast<RSA *>(ctx->pkey->pkey);
500
20.6k
  const size_t key_len = EVP_PKEY_size(ctx->pkey.get());
501
502
20.6k
  if (!sig) {
503
0
    *siglen = key_len;
504
0
    return 1;
505
0
  }
506
507
20.6k
  if (*siglen < key_len) {
508
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
509
0
    return 0;
510
0
  }
511
512
20.6k
  if (rctx->md) {
513
20.6k
    unsigned out_len;
514
20.6k
    switch (rctx->pad_mode) {
515
5.81k
      case RSA_PKCS1_PADDING:
516
5.81k
        if (!RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &out_len, rsa)) {
517
0
          return 0;
518
0
        }
519
5.81k
        *siglen = out_len;
520
5.81k
        return 1;
521
522
14.7k
      case RSA_PKCS1_PSS_PADDING:
523
14.7k
        return RSA_sign_pss_mgf1(rsa, siglen, sig, *siglen, tbs, tbslen,
524
14.7k
                                 rctx->md, rctx->mgf1md, rctx->saltlen);
525
526
0
      default:
527
0
        return 0;
528
20.6k
    }
529
20.6k
  }
530
531
0
  return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode);
532
20.6k
}
533
534
static int pkey_rsa_verify(EvpPkeyCtx *ctx, const uint8_t *sig, size_t siglen,
535
23.7k
                           const uint8_t *tbs, size_t tbslen) {
536
23.7k
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
537
23.7k
  RSA *rsa = reinterpret_cast<RSA *>(ctx->pkey->pkey);
538
539
23.7k
  if (rctx->md) {
540
23.7k
    switch (rctx->pad_mode) {
541
21.5k
      case RSA_PKCS1_PADDING:
542
21.5k
        return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa);
543
544
2.26k
      case RSA_PKCS1_PSS_PADDING:
545
2.26k
        return RSA_verify_pss_mgf1(rsa, tbs, tbslen, rctx->md, rctx->mgf1md,
546
2.26k
                                   rctx->saltlen, sig, siglen);
547
548
0
      default:
549
0
        return 0;
550
23.7k
    }
551
23.7k
  }
552
553
0
  size_t rslen;
554
0
  const size_t key_len = EVP_PKEY_size(ctx->pkey.get());
555
0
  Array<uint8_t> tbuf;
556
0
  if (!tbuf.InitForOverwrite(key_len) ||
557
0
      !RSA_verify_raw(rsa, &rslen, tbuf.data(), tbuf.size(), sig, siglen,
558
0
                      rctx->pad_mode)) {
559
0
    return 0;
560
0
  }
561
0
  if (rslen != tbslen || CRYPTO_memcmp(tbs, tbuf.data(), rslen) != 0) {
562
0
    OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
563
0
    return 0;
564
0
  }
565
566
0
  return 1;
567
0
}
568
569
static int pkey_rsa_verify_recover(EvpPkeyCtx *ctx, uint8_t *out,
570
                                   size_t *out_len, const uint8_t *sig,
571
0
                                   size_t sig_len) {
572
0
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
573
0
  RSA *rsa = reinterpret_cast<RSA *>(ctx->pkey->pkey);
574
0
  const size_t key_len = EVP_PKEY_size(ctx->pkey.get());
575
576
0
  if (out == nullptr) {
577
0
    *out_len = key_len;
578
0
    return 1;
579
0
  }
580
581
0
  if (*out_len < key_len) {
582
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
583
0
    return 0;
584
0
  }
585
586
0
  if (rctx->md == nullptr) {
587
0
    return RSA_verify_raw(rsa, out_len, out, *out_len, sig, sig_len,
588
0
                          rctx->pad_mode);
589
0
  }
590
591
0
  if (rctx->pad_mode != RSA_PKCS1_PADDING) {
592
0
    return 0;
593
0
  }
594
595
  // Assemble the encoded hash, using a placeholder hash value.
596
0
  static const uint8_t kDummyHash[EVP_MAX_MD_SIZE] = {0};
597
0
  const size_t hash_len = EVP_MD_size(rctx->md);
598
0
  uint8_t *asn1_prefix;
599
0
  size_t asn1_prefix_len;
600
0
  int asn1_prefix_allocated;
601
0
  if (!RSA_add_pkcs1_prefix(&asn1_prefix, &asn1_prefix_len,
602
0
                            &asn1_prefix_allocated, EVP_MD_type(rctx->md),
603
0
                            kDummyHash, hash_len)) {
604
0
    return 0;
605
0
  }
606
0
  UniquePtr<uint8_t> free_asn1_prefix(asn1_prefix_allocated ? asn1_prefix
607
0
                                                            : nullptr);
608
609
0
  Array<uint8_t> tbuf;
610
0
  size_t rslen;
611
0
  if (!tbuf.InitForOverwrite(key_len) ||
612
0
      !RSA_verify_raw(rsa, &rslen, tbuf.data(), tbuf.size(), sig, sig_len,
613
0
                      RSA_PKCS1_PADDING) ||
614
0
      rslen != asn1_prefix_len ||
615
      // Compare all but the hash suffix.
616
0
      CRYPTO_memcmp(tbuf.data(), asn1_prefix, asn1_prefix_len - hash_len) !=
617
0
          0) {
618
0
    return 0;
619
0
  }
620
621
0
  if (out != nullptr) {
622
0
    OPENSSL_memcpy(out, tbuf.data() + rslen - hash_len, hash_len);
623
0
  }
624
0
  *out_len = hash_len;
625
626
0
  return 1;
627
0
}
628
629
static int pkey_rsa_encrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen,
630
0
                            const uint8_t *in, size_t inlen) {
631
0
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
632
0
  RSA *rsa = reinterpret_cast<RSA *>(ctx->pkey->pkey);
633
0
  const size_t key_len = EVP_PKEY_size(ctx->pkey.get());
634
635
0
  if (!out) {
636
0
    *outlen = key_len;
637
0
    return 1;
638
0
  }
639
640
0
  if (*outlen < key_len) {
641
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
642
0
    return 0;
643
0
  }
644
645
0
  if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
646
0
    Array<uint8_t> tbuf;
647
0
    if (!tbuf.InitForOverwrite(key_len) ||
648
0
        !RSA_padding_add_PKCS1_OAEP_mgf1(
649
0
            tbuf.data(), tbuf.size(), in, inlen, rctx->oaep_label.data(),
650
0
            rctx->oaep_label.size(), rctx->md, rctx->mgf1md) ||
651
0
        !RSA_encrypt(rsa, outlen, out, *outlen, tbuf.data(), tbuf.size(),
652
0
                     RSA_NO_PADDING)) {
653
0
      return 0;
654
0
    }
655
0
    return 1;
656
0
  }
657
658
0
  return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode);
659
0
}
660
661
static int pkey_rsa_decrypt(EvpPkeyCtx *ctx, uint8_t *out, size_t *outlen,
662
0
                            const uint8_t *in, size_t inlen) {
663
0
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
664
0
  RSA *rsa = reinterpret_cast<RSA *>(ctx->pkey->pkey);
665
0
  const size_t key_len = EVP_PKEY_size(ctx->pkey.get());
666
667
0
  if (!out) {
668
0
    *outlen = key_len;
669
0
    return 1;
670
0
  }
671
672
0
  if (*outlen < key_len) {
673
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
674
0
    return 0;
675
0
  }
676
677
0
  if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
678
0
    Array<uint8_t> tbuf;
679
0
    size_t padded_len;
680
0
    if (!tbuf.InitForOverwrite(key_len) ||
681
0
        !RSA_decrypt(rsa, &padded_len, tbuf.data(), tbuf.size(), in, inlen,
682
0
                     RSA_NO_PADDING) ||
683
0
        !RSA_padding_check_PKCS1_OAEP_mgf1(out, outlen, key_len, tbuf.data(),
684
0
                                           padded_len, rctx->oaep_label.data(),
685
0
                                           rctx->oaep_label.size(), rctx->md,
686
0
                                           rctx->mgf1md)) {
687
0
      return 0;
688
0
    }
689
0
    return 1;
690
0
  }
691
692
0
  return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode);
693
0
}
694
695
61.4k
static int check_padding_md(const EVP_MD *md, int padding) {
696
61.4k
  if (!md) {
697
0
    return 1;
698
0
  }
699
700
61.4k
  if (padding == RSA_NO_PADDING) {
701
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
702
0
    return 0;
703
0
  }
704
705
61.4k
  return 1;
706
61.4k
}
707
708
17.0k
static int is_known_padding(int padding_mode) {
709
17.0k
  switch (padding_mode) {
710
0
    case RSA_PKCS1_PADDING:
711
0
    case RSA_NO_PADDING:
712
0
    case RSA_PKCS1_OAEP_PADDING:
713
17.0k
    case RSA_PKCS1_PSS_PADDING:
714
17.0k
      return 1;
715
0
    default:
716
0
      return 0;
717
17.0k
  }
718
17.0k
}
719
720
78.4k
static int pkey_rsa_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) {
721
78.4k
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
722
78.4k
  switch (type) {
723
17.0k
    case EVP_PKEY_CTRL_RSA_PADDING:
724
      // PSS keys cannot be switched to other padding types.
725
17.0k
      if (is_pss_only(ctx) && p1 != RSA_PKCS1_PSS_PADDING) {
726
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
727
0
        return 0;
728
0
      }
729
17.0k
      if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) ||
730
17.0k
          (p1 == RSA_PKCS1_PSS_PADDING &&
731
17.0k
           0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) ||
732
17.0k
          (p1 == RSA_PKCS1_OAEP_PADDING &&
733
0
           0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) {
734
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
735
0
        return 0;
736
0
      }
737
17.0k
      if (p1 == RSA_PKCS1_OAEP_PADDING && rctx->md == nullptr) {
738
0
        rctx->md = EVP_sha1();
739
0
      }
740
17.0k
      rctx->pad_mode = p1;
741
17.0k
      return 1;
742
743
0
    case EVP_PKEY_CTRL_GET_RSA_PADDING:
744
0
      *(int *)p2 = rctx->pad_mode;
745
0
      return 1;
746
747
17.0k
    case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
748
17.0k
    case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
749
17.0k
      if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
750
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN);
751
0
        return 0;
752
0
      }
753
17.0k
      if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
754
0
        *(int *)p2 = rctx->saltlen;
755
17.0k
      } else {
756
        // Negative salt lengths are special values.
757
17.0k
        if (p1 < 0) {
758
17.0k
          if (p1 != RSA_PSS_SALTLEN_DIGEST && p1 != RSA_PSS_SALTLEN_AUTO) {
759
0
            return 0;
760
0
          }
761
          // All our PSS restrictions accept saltlen == hashlen, so allow
762
          // `RSA_PSS_SALTLEN_DIGEST`. Reject `RSA_PSS_SALTLEN_AUTO` for
763
          // simplicity.
764
17.0k
          if (rctx->restrict_pss_params && p1 != RSA_PSS_SALTLEN_DIGEST) {
765
0
            OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN);
766
0
            return 0;
767
0
          }
768
17.0k
        } else if (rctx->restrict_pss_params &&
769
0
                   static_cast<size_t>(p1) < EVP_MD_size(rctx->md)) {
770
0
          OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN);
771
0
          return 0;
772
0
        }
773
17.0k
        rctx->saltlen = p1;
774
17.0k
      }
775
17.0k
      return 1;
776
777
0
    case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
778
0
      if (p1 < 256) {
779
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_KEYBITS);
780
0
        return 0;
781
0
      }
782
0
      rctx->nbits = p1;
783
0
      return 1;
784
785
0
    case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
786
0
      if (!p2) {
787
0
        return 0;
788
0
      }
789
0
      rctx->pub_exp.reset(reinterpret_cast<BIGNUM *>(p2));
790
0
      return 1;
791
792
0
    case EVP_PKEY_CTRL_RSA_OAEP_MD:
793
0
    case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
794
0
      if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
795
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
796
0
        return 0;
797
0
      }
798
0
      if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) {
799
0
        *(const EVP_MD **)p2 = rctx->md;
800
0
      } else {
801
0
        rctx->md = reinterpret_cast<EVP_MD *>(p2);
802
0
      }
803
0
      return 1;
804
805
44.3k
    case EVP_PKEY_CTRL_MD: {
806
44.3k
      const EVP_MD *md = reinterpret_cast<EVP_MD *>(p2);
807
44.3k
      if (!check_padding_md(md, rctx->pad_mode)) {
808
0
        return 0;
809
0
      }
810
44.3k
      if (rctx->restrict_pss_params &&
811
0
          EVP_MD_type(rctx->md) != EVP_MD_type(md)) {
812
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_TYPE);
813
0
        return 0;
814
0
      }
815
44.3k
      rctx->md = md;
816
44.3k
      return 1;
817
44.3k
    }
818
819
0
    case EVP_PKEY_CTRL_GET_MD:
820
0
      *(const EVP_MD **)p2 = rctx->md;
821
0
      return 1;
822
823
0
    case EVP_PKEY_CTRL_RSA_MGF1_MD:
824
0
    case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
825
0
      if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
826
0
          rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
827
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_MGF1_MD);
828
0
        return 0;
829
0
      }
830
0
      if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
831
0
        if (rctx->mgf1md) {
832
0
          *(const EVP_MD **)p2 = rctx->mgf1md;
833
0
        } else {
834
0
          *(const EVP_MD **)p2 = rctx->md;
835
0
        }
836
0
      } else {
837
0
        const EVP_MD *md = reinterpret_cast<EVP_MD *>(p2);
838
0
        if (rctx->restrict_pss_params &&
839
0
            EVP_MD_type(rctx->mgf1md) != EVP_MD_type(md)) {
840
0
          OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_MGF1_MD);
841
0
          return 0;
842
0
        }
843
0
        rctx->mgf1md = md;
844
0
      }
845
0
      return 1;
846
847
0
    case EVP_PKEY_CTRL_RSA_OAEP_LABEL: {
848
0
      if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
849
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
850
0
        return 0;
851
0
      }
852
      // `EVP_PKEY_CTRL_RSA_OAEP_LABEL` takes ownership of `label`'s underlying
853
      // buffer (via `Reset`), but only on success.
854
0
      auto *label = reinterpret_cast<Span<uint8_t> *>(p2);
855
0
      rctx->oaep_label.Reset(label->data(), label->size());
856
0
      return 1;
857
0
    }
858
859
0
    case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
860
0
      if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
861
0
        OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
862
0
        return 0;
863
0
      }
864
0
      *reinterpret_cast<CBS *>(p2) = CBS(rctx->oaep_label);
865
0
      return 1;
866
867
0
    default:
868
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
869
0
      return 0;
870
78.4k
  }
871
78.4k
}
872
873
0
static int pkey_rsa_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) {
874
0
  RSA_PKEY_CTX *rctx = reinterpret_cast<RSA_PKEY_CTX *>(ctx->data);
875
0
  if (!rctx->pub_exp) {
876
0
    rctx->pub_exp.reset(BN_new());
877
0
    if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp.get(), RSA_F4)) {
878
0
      return 0;
879
0
    }
880
0
  }
881
0
  UniquePtr<RSA> rsa(RSA_new());
882
0
  if (!rsa) {
883
0
    return 0;
884
0
  }
885
886
0
  if (!RSA_generate_key_ex(rsa.get(), rctx->nbits, rctx->pub_exp.get(),
887
0
                           nullptr)) {
888
0
    return 0;
889
0
  }
890
891
0
  EVP_PKEY_assign_RSA(pkey, rsa.release());
892
0
  return 1;
893
0
}
894
895
const EVP_PKEY_CTX_METHOD rsa_pkey_meth = {
896
    EVP_PKEY_RSA,
897
    pkey_rsa_init,
898
    pkey_rsa_copy,
899
    pkey_rsa_cleanup,
900
    pkey_rsa_keygen,
901
    pkey_rsa_sign,
902
    /*sign_message=*/nullptr,
903
    pkey_rsa_verify,
904
    /*verify_message=*/nullptr,
905
    pkey_rsa_verify_recover,
906
    pkey_rsa_encrypt,
907
    pkey_rsa_decrypt,
908
    /*derive=*/nullptr,
909
    /*paramgen=*/nullptr,
910
    /*encap=*/nullptr,
911
    /*decap=*/nullptr,
912
    pkey_rsa_ctrl,
913
};
914
915
const EVP_PKEY_CTX_METHOD rsa_pss_pkey_meth = {
916
    EVP_PKEY_RSA_PSS,
917
    pkey_rsa_init,
918
    pkey_rsa_copy,
919
    pkey_rsa_cleanup,
920
    // In OpenSSL, `EVP_PKEY_RSA_PSS` supports key generation and fills in PSS
921
    // parameters based on a separate set of keygen-targetted setters:
922
    // `EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen`,
923
    // `EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md`, and
924
    // `EVP_PKEY_CTX_rsa_pss_key_digest`. We do not currently implement this
925
    // because we only support one parameter set.
926
    /*keygen=*/nullptr,
927
    pkey_rsa_sign,
928
    /*sign_message=*/nullptr,
929
    pkey_rsa_verify,
930
    /*verify_message=*/nullptr,
931
    /*verify_recover=*/nullptr,
932
    /*encrypt=*/nullptr,
933
    /*decrypt=*/nullptr,
934
    /*derive=*/nullptr,
935
    /*paramgen=*/nullptr,
936
    /*encap=*/nullptr,
937
    /*decap=*/nullptr,
938
    pkey_rsa_ctrl,
939
};
940
941
}  // namespace
942
943
267k
const EVP_PKEY_ALG *EVP_pkey_rsa() {
944
267k
  static const EVP_PKEY_ALG kAlg = {&rsa_asn1_meth, &rsa_pkey_meth};
945
267k
  return &kAlg;
946
267k
}
947
948
0
const EVP_PKEY_ALG *EVP_pkey_rsa_pss_sha256() {
949
0
  static const EVP_PKEY_ALG_RSA_PSS kAlg = {
950
0
      {&rsa_pss_asn1_meth, &rsa_pss_pkey_meth}, rsa_pss_sha256};
951
0
  return &kAlg;
952
0
}
953
954
0
const EVP_PKEY_ALG *EVP_pkey_rsa_pss_sha384() {
955
0
  static const EVP_PKEY_ALG_RSA_PSS kAlg = {
956
0
      {&rsa_pss_asn1_meth, &rsa_pss_pkey_meth}, rsa_pss_sha384};
957
0
  return &kAlg;
958
0
}
959
960
0
const EVP_PKEY_ALG *EVP_pkey_rsa_pss_sha512() {
961
0
  static const EVP_PKEY_ALG_RSA_PSS kAlg = {
962
0
      {&rsa_pss_asn1_meth, &rsa_pss_pkey_meth}, rsa_pss_sha512};
963
0
  return &kAlg;
964
0
}
965
966
0
EVP_PKEY *EVP_RSA_gen(unsigned bits) {
967
  // TODO(crbug.com/487376811): After EVP_PKEY_CTX is switched to C++
968
  // subclassing, it should be possible to stack-allocate enough the
969
  // RSA-specific subclass.
970
0
  UniquePtr<EvpPkeyCtx> ctx = evp_pkey_ctx_new_alg(EVP_pkey_rsa());
971
0
  EVP_PKEY *pkey = nullptr;
972
0
  if (ctx == nullptr ||  //
973
0
      !EVP_PKEY_keygen_init(ctx.get()) ||
974
0
      !EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), bits) ||
975
0
      !EVP_PKEY_keygen(ctx.get(), &pkey)) {
976
0
    return nullptr;
977
0
  }
978
0
  return pkey;
979
0
}
980
981
EVP_PKEY *EVP_PKEY_from_rsa_public_key(const EVP_PKEY_ALG *alg,
982
0
                                       const uint8_t *in, size_t len) {
983
0
  if (alg->pkey_method->pkey_id != EVP_PKEY_RSA &&
984
0
      alg->pkey_method->pkey_id != EVP_PKEY_RSA_PSS) {
985
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
986
0
    return nullptr;
987
0
  }
988
0
  UniquePtr<RSA> rsa(RSA_public_key_from_bytes(in, len));
989
0
  if (rsa == nullptr) {
990
0
    return nullptr;
991
0
  }
992
0
  UniquePtr<EVP_PKEY> ret(EVP_PKEY_new());
993
0
  if (ret == nullptr) {
994
0
    return nullptr;
995
0
  }
996
  // Use the PSS-specific setter if needed, to fill in `rsa->pss_params`.
997
0
  if (alg->pkey_method->pkey_id == EVP_PKEY_RSA) {
998
0
    evp_pkey_set0(FromOpaque(ret.get()), alg->method, rsa.release());
999
0
  } else {
1000
0
    evp_pkey_set0_pss(FromOpaque(ret.get()), alg, std::move(rsa));
1001
0
  }
1002
0
  return ret.release();
1003
0
}
1004
1005
EVP_PKEY *EVP_PKEY_from_rsa_private_key(const EVP_PKEY_ALG *alg,
1006
12
                                        const uint8_t *in, size_t len) {
1007
12
  if (alg->pkey_method->pkey_id != EVP_PKEY_RSA &&
1008
0
      alg->pkey_method->pkey_id != EVP_PKEY_RSA_PSS) {
1009
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1010
0
    return nullptr;
1011
0
  }
1012
12
  UniquePtr<RSA> rsa(RSA_private_key_from_bytes(in, len));
1013
12
  if (rsa == nullptr) {
1014
0
    return nullptr;
1015
0
  }
1016
12
  UniquePtr<EVP_PKEY> ret(EVP_PKEY_new());
1017
12
  if (ret == nullptr) {
1018
0
    return nullptr;
1019
0
  }
1020
  // Use the PSS-specific setter if needed, to fill in `rsa->pss_params`.
1021
12
  if (alg->pkey_method->pkey_id == EVP_PKEY_RSA) {
1022
12
    evp_pkey_set0(FromOpaque(ret.get()), alg->method, rsa.release());
1023
12
  } else {
1024
0
    evp_pkey_set0_pss(FromOpaque(ret.get()), alg, std::move(rsa));
1025
0
  }
1026
12
  return ret.release();
1027
12
}
1028
1029
0
int EVP_PKEY_marshal_rsa_public_key(CBB *cbb, const EVP_PKEY *key) {
1030
0
  const RSA *rsa = EVP_PKEY_get0_RSA(key);
1031
0
  if (rsa == nullptr) {
1032
0
    return 0;
1033
0
  }
1034
0
  return RSA_marshal_public_key(cbb, rsa);
1035
0
}
1036
1037
0
int EVP_PKEY_marshal_rsa_private_key(CBB *cbb, const EVP_PKEY *key) {
1038
0
  const RSA *rsa = EVP_PKEY_get0_RSA(key);
1039
0
  if (rsa == nullptr) {
1040
0
    return 0;
1041
0
  }
1042
0
  return RSA_marshal_private_key(cbb, rsa);
1043
0
}
1044
1045
0
int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
1046
0
  if (EVP_PKEY_assign_RSA(pkey, key)) {
1047
0
    RSA_up_ref(key);
1048
0
    return 1;
1049
0
  }
1050
0
  return 0;
1051
0
}
1052
1053
108k
int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
1054
108k
  if (key == nullptr) {
1055
0
    return 0;
1056
0
  }
1057
108k
  evp_pkey_set0(FromOpaque(pkey), &rsa_asn1_meth, key);
1058
108k
  return 1;
1059
108k
}
1060
1061
3.56k
RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
1062
3.56k
  int pkey_id = EVP_PKEY_id(pkey);
1063
3.56k
  if (pkey_id != EVP_PKEY_RSA && pkey_id != EVP_PKEY_RSA_PSS) {
1064
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
1065
0
    return nullptr;
1066
0
  }
1067
3.56k
  return reinterpret_cast<RSA *>(FromOpaque(pkey)->pkey);
1068
3.56k
}
1069
1070
0
RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
1071
0
  RSA *rsa = EVP_PKEY_get0_RSA(pkey);
1072
0
  if (rsa != nullptr) {
1073
0
    RSA_up_ref(rsa);
1074
0
  }
1075
0
  return rsa;
1076
0
}
1077
1078
static int rsa_or_rsa_pss_ctrl(EvpPkeyCtx *ctx, int optype, int cmd, int p1,
1079
34.1k
                               void *p2) {
1080
34.1k
  if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
1081
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
1082
0
    return 0;
1083
0
  }
1084
34.1k
  if (ctx->pmeth->pkey_id != EVP_PKEY_RSA &&
1085
0
      ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) {
1086
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1087
0
    return 0;
1088
0
  }
1089
34.1k
  return EVP_PKEY_CTX_ctrl(ctx, /*keytype=*/-1, optype, cmd, p1, p2);
1090
34.1k
}
1091
1092
17.0k
int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
1093
17.0k
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx), -1, EVP_PKEY_CTRL_RSA_PADDING,
1094
17.0k
                             padding, nullptr);
1095
17.0k
}
1096
1097
0
int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
1098
0
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx), -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
1099
0
                             0, out_padding);
1100
0
}
1101
1102
0
int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
1103
  // We currently do not support keygen with `EVP_PKEY_RSA_PSS`.
1104
0
  return 0;
1105
0
}
1106
1107
0
int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
1108
  // We currently do not support keygen with `EVP_PKEY_RSA_PSS`.
1109
0
  return 0;
1110
0
}
1111
1112
int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx,
1113
0
                                            const EVP_MD *md) {
1114
  // We currently do not support keygen with `EVP_PKEY_RSA_PSS`.
1115
0
  return 0;
1116
0
}
1117
1118
17.0k
int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
1119
17.0k
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx),
1120
17.0k
                             (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
1121
17.0k
                             EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, nullptr);
1122
17.0k
}
1123
1124
0
int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
1125
0
  return rsa_or_rsa_pss_ctrl(
1126
0
      FromOpaque(ctx), (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
1127
0
      EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
1128
0
}
1129
1130
0
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
1131
0
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx), EVP_PKEY_OP_KEYGEN,
1132
0
                             EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, nullptr);
1133
0
}
1134
1135
0
int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
1136
0
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx), EVP_PKEY_OP_KEYGEN,
1137
0
                             EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
1138
0
}
1139
1140
0
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
1141
0
  return EVP_PKEY_CTX_ctrl(FromOpaque(ctx), EVP_PKEY_RSA,
1142
0
                           EVP_PKEY_OP_TYPE_CRYPT, EVP_PKEY_CTRL_RSA_OAEP_MD, 0,
1143
0
                           (void *)md);
1144
0
}
1145
1146
0
int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
1147
0
  return EVP_PKEY_CTX_ctrl(FromOpaque(ctx), EVP_PKEY_RSA,
1148
0
                           EVP_PKEY_OP_TYPE_CRYPT,
1149
0
                           EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)out_md);
1150
0
}
1151
1152
0
int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
1153
0
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx),
1154
0
                             EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1155
0
                             EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
1156
0
}
1157
1158
0
int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
1159
0
  return rsa_or_rsa_pss_ctrl(FromOpaque(ctx),
1160
0
                             EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
1161
0
                             EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)out_md);
1162
0
}
1163
1164
int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label,
1165
0
                                     size_t label_len) {
1166
0
  Span span(label, label_len);
1167
0
  return EVP_PKEY_CTX_ctrl(FromOpaque(ctx), EVP_PKEY_RSA,
1168
0
                           EVP_PKEY_OP_TYPE_CRYPT, EVP_PKEY_CTRL_RSA_OAEP_LABEL,
1169
0
                           0, &span);
1170
0
}
1171
1172
int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
1173
0
                                     const uint8_t **out_label) {
1174
0
  CBS label;
1175
0
  if (!EVP_PKEY_CTX_ctrl(FromOpaque(ctx), EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
1176
0
                         EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
1177
0
    return -1;
1178
0
  }
1179
0
  if (CBS_len(&label) > INT_MAX) {
1180
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
1181
0
    return -1;
1182
0
  }
1183
0
  *out_label = CBS_data(&label);
1184
0
  return (int)CBS_len(&label);
1185
0
}