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;
82 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
88 static void rsa_priv_freectx(void *vctx)
90 struct rsa_priv_ctx_st *ctx = vctx;
92 EVP_CIPHER_free(ctx->sc.cipher);
93 OPENSSL_free(ctx->sc.cipher_pass);
97 static const OSSL_PARAM *rsa_priv_settable_ctx_params(void)
99 static const OSSL_PARAM settables[] = {
100 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
101 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
108 static int rsa_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
110 struct rsa_priv_ctx_st *ctx = vctx;
113 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
115 const OSSL_PARAM *propsp =
116 OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
117 const char *props = NULL;
119 if (p->data_type != OSSL_PARAM_UTF8_STRING)
121 if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
123 props = (propsp != NULL ? propsp->data : NULL);
125 EVP_CIPHER_free(ctx->sc.cipher);
126 ctx->sc.cipher_intent = p->data != NULL;
128 && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
132 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
134 OPENSSL_free(ctx->sc.cipher_pass);
135 ctx->sc.cipher_pass = NULL;
136 if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
137 &ctx->sc.cipher_pass_length))
143 /* Private key : DER */
144 static int rsa_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
145 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
147 struct rsa_priv_ctx_st *ctx = vctx;
148 OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
149 ossl_prov_get_rsa_importkey();
152 if (rsa_importkey != NULL) {
153 RSA *rsa = rsa_importkey(ctx->provctx, params);
155 ok = rsa_priv_der(vctx, rsa, out, cb, cbarg);
161 static int rsa_priv_der(void *vctx, void *rsa, BIO *out,
162 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
164 struct rsa_priv_ctx_st *ctx = vctx;
168 ctx->sc.cbarg = cbarg;
170 ret = ossl_prov_write_priv_der_from_obj(out, rsa, EVP_PKEY_RSA,
172 (i2d_of_void *)i2d_RSAPrivateKey,
178 /* Private key : PEM */
179 static int rsa_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
180 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
182 struct rsa_priv_ctx_st *ctx = vctx;
183 OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
184 ossl_prov_get_rsa_importkey();
187 if (rsa_importkey != NULL) {
188 RSA *rsa = rsa_importkey(ctx, params);
190 ok = rsa_pem_priv(vctx, rsa, out, cb, cbarg);
196 static int rsa_pem_priv(void *vctx, void *rsa, BIO *out,
197 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
199 struct rsa_priv_ctx_st *ctx = vctx;
203 ctx->sc.cbarg = cbarg;
205 ret = ossl_prov_write_priv_pem_from_obj(out, rsa, EVP_PKEY_RSA,
207 (i2d_of_void *)i2d_RSAPrivateKey,
214 * There's no specific print context, so we use the provider context
216 static void *rsa_print_newctx(void *provctx)
221 static void rsa_print_freectx(void *ctx)
225 static int rsa_priv_print_data(void *provctx, const OSSL_PARAM params[],
227 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
229 OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
230 ossl_prov_get_rsa_importkey();
233 if (rsa_importkey != NULL) {
234 RSA *rsa = rsa_importkey(provctx, params); /* ctx == provctx */
236 ok = rsa_priv_print(provctx, rsa, out, cb, cbarg);
242 static int rsa_priv_print(void *ctx, void *rsa, BIO *out,
243 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
245 return ossl_prov_print_rsa(out, rsa, 1);
248 const OSSL_DISPATCH rsa_priv_der_serializer_functions[] = {
249 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
250 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
251 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
252 (void (*)(void))rsa_priv_set_ctx_params },
253 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
254 (void (*)(void))rsa_priv_settable_ctx_params },
255 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_priv_der_data },
256 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_der },
260 const OSSL_DISPATCH rsa_priv_pem_serializer_functions[] = {
261 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
262 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
263 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
264 (void (*)(void))rsa_priv_set_ctx_params },
265 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
266 (void (*)(void))rsa_priv_settable_ctx_params },
267 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pem_priv_data },
268 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pem_priv },
272 const OSSL_DISPATCH rsa_priv_text_serializer_functions[] = {
273 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_print_newctx },
274 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_print_freectx },
275 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_print },
276 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
277 (void (*)(void))rsa_priv_print_data },