2 * Copyright 2019 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_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/err.h>
13 #include <openssl/pem.h>
14 #include <openssl/rsa.h>
15 #include <openssl/types.h>
16 #include <openssl/params.h>
17 #include <openssl/safestack.h>
19 #include "prov/implementations.h"
20 #include "prov/providercommonerr.h"
21 #include "serializer_local.h"
23 static OSSL_OP_serializer_newctx_fn rsa_priv_newctx;
24 static OSSL_OP_serializer_freectx_fn rsa_priv_freectx;
25 static OSSL_OP_serializer_set_ctx_params_fn rsa_priv_set_ctx_params;
26 static OSSL_OP_serializer_settable_ctx_params_fn rsa_priv_settable_ctx_params;
27 static OSSL_OP_serializer_serialize_data_fn rsa_priv_der_data;
28 static OSSL_OP_serializer_serialize_object_fn rsa_priv_der;
29 static OSSL_OP_serializer_serialize_data_fn rsa_pem_priv_data;
30 static OSSL_OP_serializer_serialize_object_fn rsa_pem_priv;
32 static OSSL_OP_serializer_newctx_fn rsa_print_newctx;
33 static OSSL_OP_serializer_freectx_fn rsa_print_freectx;
34 static OSSL_OP_serializer_serialize_data_fn rsa_priv_print_data;
35 static OSSL_OP_serializer_serialize_object_fn rsa_priv_print;
38 * Context used for private key serialization.
40 struct rsa_priv_ctx_st {
43 struct pkcs8_encrypt_ctx_st sc;
46 /* Helper functions to prepare RSA-PSS params for serialization */
48 static int prepare_rsa_params(const void *rsa, int nid,
49 ASN1_STRING **pstr, int *pstrtype)
51 const RSA_PSS_PARAMS *pss = RSA_get0_pss_params(rsa);
54 /* If RSA it's just NULL type */
55 if (nid != EVP_PKEY_RSA_PSS) {
56 *pstrtype = V_ASN1_NULL;
59 /* If no PSS parameters we omit parameters entirely */
61 *pstrtype = V_ASN1_UNDEF;
64 /* Encode PSS parameters */
65 if (ASN1_item_pack((void *)pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr)
69 *pstrtype = V_ASN1_SEQUENCE;
73 /* Private key : context */
74 static void *rsa_priv_newctx(void *provctx)
76 struct rsa_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
79 ctx->provctx = provctx;
80 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
86 static void rsa_priv_freectx(void *vctx)
88 struct rsa_priv_ctx_st *ctx = vctx;
90 EVP_CIPHER_free(ctx->sc.cipher);
91 OPENSSL_free(ctx->sc.cipher_pass);
95 static const OSSL_PARAM *rsa_priv_settable_ctx_params(void)
97 static const OSSL_PARAM settables[] = {
98 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
99 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
106 static int rsa_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
108 struct rsa_priv_ctx_st *ctx = vctx;
111 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
113 const OSSL_PARAM *propsp =
114 OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
115 const char *props = NULL;
117 if (p->data_type != OSSL_PARAM_UTF8_STRING)
119 if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
121 props = (propsp != NULL ? propsp->data : NULL);
123 EVP_CIPHER_free(ctx->sc.cipher);
124 ctx->sc.cipher_intent = p->data != NULL;
126 && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
130 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
132 OPENSSL_free(ctx->sc.cipher_pass);
133 ctx->sc.cipher_pass = NULL;
134 if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
135 &ctx->sc.cipher_pass_length))
141 /* Private key : DER */
142 static int rsa_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
143 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
145 struct rsa_priv_ctx_st *ctx = vctx;
146 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
147 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
148 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
151 if (rsa_import != NULL) {
154 if ((rsa = rsa_new(ctx->provctx)) != NULL
155 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
156 && rsa_priv_der(ctx, rsa, out, cb, cbarg))
163 static int rsa_priv_der(void *vctx, void *rsa, BIO *out,
164 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
166 struct rsa_priv_ctx_st *ctx = vctx;
170 ctx->sc.cbarg = cbarg;
172 ret = ossl_prov_write_priv_der_from_obj(out, rsa, EVP_PKEY_RSA,
174 (i2d_of_void *)i2d_RSAPrivateKey,
180 /* Private key : PEM */
181 static int rsa_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
182 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
184 struct rsa_priv_ctx_st *ctx = vctx;
185 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
186 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
187 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
190 if (rsa_import != NULL) {
193 if ((rsa = rsa_new(ctx->provctx)) != NULL
194 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
195 && rsa_pem_priv(ctx, rsa, out, cb, cbarg))
202 static int rsa_pem_priv(void *vctx, void *rsa, BIO *out,
203 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
205 struct rsa_priv_ctx_st *ctx = vctx;
209 ctx->sc.cbarg = cbarg;
211 ret = ossl_prov_write_priv_pem_from_obj(out, rsa, EVP_PKEY_RSA,
213 (i2d_of_void *)i2d_RSAPrivateKey,
220 * There's no specific print context, so we use the provider context
222 static void *rsa_print_newctx(void *provctx)
227 static void rsa_print_freectx(void *ctx)
231 static int rsa_priv_print_data(void *vctx, const OSSL_PARAM params[],
233 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
235 struct rsa_priv_ctx_st *ctx = vctx;
236 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
237 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
238 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
241 if (rsa_import != NULL) {
244 if ((rsa = rsa_new(ctx->provctx)) != NULL
245 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
246 && rsa_priv_print(ctx, rsa, out, cb, cbarg))
253 static int rsa_priv_print(void *ctx, void *rsa, BIO *out,
254 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
256 return ossl_prov_print_rsa(out, rsa, 1);
259 const OSSL_DISPATCH rsa_priv_der_serializer_functions[] = {
260 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
261 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
262 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
263 (void (*)(void))rsa_priv_set_ctx_params },
264 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
265 (void (*)(void))rsa_priv_settable_ctx_params },
266 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_priv_der_data },
267 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_der },
271 const OSSL_DISPATCH rsa_priv_pem_serializer_functions[] = {
272 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
273 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
274 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
275 (void (*)(void))rsa_priv_set_ctx_params },
276 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
277 (void (*)(void))rsa_priv_settable_ctx_params },
278 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pem_priv_data },
279 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pem_priv },
283 const OSSL_DISPATCH rsa_priv_text_serializer_functions[] = {
284 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_print_newctx },
285 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_print_freectx },
286 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_print },
287 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
288 (void (*)(void))rsa_priv_print_data },