2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/core_names.h>
11 #include <openssl/params.h>
12 #include "crypto/rsa.h"
15 * The intention with the "backend" source file is to offer backend support
16 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
17 * implementations alike.
20 DEFINE_STACK_OF(BIGNUM)
22 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
23 const OSSL_PARAM params[], const char *key)
25 const OSSL_PARAM *p = NULL;
30 for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
33 if (!OSSL_PARAM_get_BN(p, &tmp)
34 || sk_BIGNUM_push(numbers, tmp) == 0)
41 int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
43 const OSSL_PARAM *param_n, *param_e, *param_d;
44 BIGNUM *n = NULL, *e = NULL, *d = NULL;
45 STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
51 param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
52 param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
53 param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
55 if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
56 || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
57 || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
60 is_private = (d != NULL);
62 if (!RSA_set0_key(rsa, n, e, d))
67 if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
68 OSSL_PKEY_PARAM_RSA_FACTOR)
69 || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
70 OSSL_PKEY_PARAM_RSA_EXPONENT)
71 || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
72 OSSL_PKEY_PARAM_RSA_COEFFICIENT))
75 /* It's ok if this private key just has n, e and d */
76 if (sk_BIGNUM_num(factors) != 0
77 && !rsa_set0_all_params(rsa, factors, exps, coeffs))
81 sk_BIGNUM_free(factors);
83 sk_BIGNUM_free(coeffs);
90 sk_BIGNUM_pop_free(factors, BN_free);
91 sk_BIGNUM_pop_free(exps, BN_free);
92 sk_BIGNUM_pop_free(coeffs, BN_free);