2 * Copyright 2019-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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include <openssl/core_dispatch.h>
17 #include <openssl/core_names.h>
18 #include <openssl/err.h>
19 #include <openssl/pem.h>
20 #include <openssl/rsa.h>
21 #include <openssl/types.h>
22 #include <openssl/params.h>
23 #include <openssl/safestack.h>
24 #include "crypto/rsa.h"
26 #include "prov/implementations.h"
27 #include "prov/providercommonerr.h"
28 #include "prov/provider_ctx.h"
29 #include "serializer_local.h"
31 static OSSL_FUNC_serializer_newctx_fn rsa_priv_newctx
;
32 static OSSL_FUNC_serializer_freectx_fn rsa_priv_freectx
;
33 static OSSL_FUNC_serializer_set_ctx_params_fn rsa_priv_set_ctx_params
;
34 static OSSL_FUNC_serializer_settable_ctx_params_fn rsa_priv_settable_ctx_params
;
35 static OSSL_FUNC_serializer_serialize_data_fn rsa_priv_der_data
;
36 static OSSL_FUNC_serializer_serialize_object_fn rsa_priv_der
;
37 static OSSL_FUNC_serializer_serialize_data_fn rsa_pem_priv_data
;
38 static OSSL_FUNC_serializer_serialize_object_fn rsa_pem_priv
;
40 static OSSL_FUNC_serializer_newctx_fn rsa_print_newctx
;
41 static OSSL_FUNC_serializer_freectx_fn rsa_print_freectx
;
42 static OSSL_FUNC_serializer_serialize_data_fn rsa_priv_print_data
;
43 static OSSL_FUNC_serializer_serialize_object_fn rsa_priv_print
;
46 * Context used for private key serialization.
48 struct rsa_priv_ctx_st
{
51 struct pkcs8_encrypt_ctx_st sc
;
54 /* Private key : context */
55 static void *rsa_priv_newctx(void *provctx
)
57 struct rsa_priv_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
60 ctx
->provctx
= provctx
;
61 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
67 static void rsa_priv_freectx(void *vctx
)
69 struct rsa_priv_ctx_st
*ctx
= vctx
;
71 EVP_CIPHER_free(ctx
->sc
.cipher
);
72 OPENSSL_free(ctx
->sc
.cipher_pass
);
76 static const OSSL_PARAM
*rsa_priv_settable_ctx_params(void)
78 static const OSSL_PARAM settables
[] = {
79 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER
, NULL
, 0),
80 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS
, NULL
, 0),
87 static int rsa_priv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
89 struct rsa_priv_ctx_st
*ctx
= vctx
;
92 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_CIPHER
))
94 const OSSL_PARAM
*propsp
=
95 OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PROPERTIES
);
96 const char *props
= NULL
;
98 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
100 if (propsp
!= NULL
&& propsp
->data_type
!= OSSL_PARAM_UTF8_STRING
)
102 props
= (propsp
!= NULL
? propsp
->data
: NULL
);
104 EVP_CIPHER_free(ctx
->sc
.cipher
);
105 ctx
->sc
.cipher_intent
= p
->data
!= NULL
;
107 && ((ctx
->sc
.cipher
= EVP_CIPHER_fetch(NULL
, p
->data
, props
))
111 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PASS
))
113 OPENSSL_free(ctx
->sc
.cipher_pass
);
114 ctx
->sc
.cipher_pass
= NULL
;
115 if (!OSSL_PARAM_get_octet_string(p
, &ctx
->sc
.cipher_pass
, 0,
116 &ctx
->sc
.cipher_pass_length
))
122 /* Private key : DER */
123 static int rsa_priv_der_data(void *vctx
, const OSSL_PARAM params
[],
125 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
127 struct rsa_priv_ctx_st
*ctx
= vctx
;
128 OSSL_FUNC_keymgmt_new_fn
*rsa_new
= ossl_prov_get_keymgmt_rsa_new();
129 OSSL_FUNC_keymgmt_free_fn
*rsa_free
= ossl_prov_get_keymgmt_rsa_free();
130 OSSL_FUNC_keymgmt_import_fn
*rsa_import
= ossl_prov_get_keymgmt_rsa_import();
133 if (rsa_import
!= NULL
) {
136 if ((rsa
= rsa_new(ctx
->provctx
)) != NULL
137 && rsa_import(rsa
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
138 && rsa_priv_der(ctx
, rsa
, out
, cb
, cbarg
))
145 static int rsa_priv_der(void *vctx
, void *rsa
, OSSL_CORE_BIO
*cout
,
146 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
148 struct rsa_priv_ctx_st
*ctx
= vctx
;
150 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
156 ctx
->sc
.cbarg
= cbarg
;
158 ret
= ossl_prov_write_priv_der_from_obj(out
, rsa
,
159 ossl_prov_rsa_type_to_evp(rsa
),
160 ossl_prov_prepare_rsa_params
,
161 (i2d_of_void
*)i2d_RSAPrivateKey
,
168 /* Private key : PEM */
169 static int rsa_pem_priv_data(void *vctx
, const OSSL_PARAM params
[],
171 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
173 struct rsa_priv_ctx_st
*ctx
= vctx
;
174 OSSL_FUNC_keymgmt_new_fn
*rsa_new
= ossl_prov_get_keymgmt_rsa_new();
175 OSSL_FUNC_keymgmt_free_fn
*rsa_free
= ossl_prov_get_keymgmt_rsa_free();
176 OSSL_FUNC_keymgmt_import_fn
*rsa_import
= ossl_prov_get_keymgmt_rsa_import();
179 if (rsa_import
!= NULL
) {
182 if ((rsa
= rsa_new(ctx
->provctx
)) != NULL
183 && rsa_import(rsa
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
184 && rsa_pem_priv(ctx
, rsa
, out
, cb
, cbarg
))
191 static int rsa_pem_priv(void *vctx
, void *rsa
, OSSL_CORE_BIO
*cout
,
192 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
194 struct rsa_priv_ctx_st
*ctx
= vctx
;
196 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
202 ctx
->sc
.cbarg
= cbarg
;
204 ret
= ossl_prov_write_priv_pem_from_obj(out
, rsa
,
205 ossl_prov_rsa_type_to_evp(rsa
),
206 ossl_prov_prepare_rsa_params
,
207 (i2d_of_void
*)i2d_RSAPrivateKey
,
215 * There's no specific print context, so we use the provider context
217 static void *rsa_print_newctx(void *provctx
)
222 static void rsa_print_freectx(void *ctx
)
226 static int rsa_priv_print_data(void *vctx
, const OSSL_PARAM params
[],
228 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
230 struct rsa_priv_ctx_st
*ctx
= vctx
;
231 OSSL_FUNC_keymgmt_new_fn
*rsa_new
= ossl_prov_get_keymgmt_rsa_new();
232 OSSL_FUNC_keymgmt_free_fn
*rsa_free
= ossl_prov_get_keymgmt_rsa_free();
233 OSSL_FUNC_keymgmt_import_fn
*rsa_import
= ossl_prov_get_keymgmt_rsa_import();
236 if (rsa_import
!= NULL
) {
239 if ((rsa
= rsa_new(ctx
->provctx
)) != NULL
240 && rsa_import(rsa
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
241 && rsa_priv_print(ctx
, rsa
, out
, cb
, cbarg
))
248 static int rsa_priv_print(void *ctx
, void *rsa
, OSSL_CORE_BIO
*cout
,
249 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
251 BIO
*out
= bio_new_from_core_bio(ctx
, cout
);
257 ret
= ossl_prov_print_rsa(out
, rsa
, 1);
263 const OSSL_DISPATCH rsa_priv_der_serializer_functions
[] = {
264 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))rsa_priv_newctx
},
265 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))rsa_priv_freectx
},
266 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
267 (void (*)(void))rsa_priv_set_ctx_params
},
268 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
269 (void (*)(void))rsa_priv_settable_ctx_params
},
270 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))rsa_priv_der_data
},
271 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))rsa_priv_der
},
275 const OSSL_DISPATCH rsa_priv_pem_serializer_functions
[] = {
276 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))rsa_priv_newctx
},
277 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))rsa_priv_freectx
},
278 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
279 (void (*)(void))rsa_priv_set_ctx_params
},
280 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
281 (void (*)(void))rsa_priv_settable_ctx_params
},
282 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))rsa_pem_priv_data
},
283 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))rsa_pem_priv
},
287 const OSSL_DISPATCH rsa_priv_text_serializer_functions
[] = {
288 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))rsa_print_newctx
},
289 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))rsa_print_freectx
},
290 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))rsa_priv_print
},
291 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
,
292 (void (*)(void))rsa_priv_print_data
},