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_dispatch.h>
11 #include <openssl/core_names.h>
12 #include <openssl/err.h>
13 #include <openssl/pem.h>
14 #include <openssl/ec.h>
15 #include <openssl/types.h>
16 #include <openssl/params.h>
18 #include "prov/implementations.h"
19 #include "prov/provider_ctx.h"
20 #include "serializer_local.h"
22 static OSSL_FUNC_serializer_newctx_fn ec_priv_newctx
;
23 static OSSL_FUNC_serializer_freectx_fn ec_priv_freectx
;
24 static OSSL_FUNC_serializer_set_ctx_params_fn ec_priv_set_ctx_params
;
25 static OSSL_FUNC_serializer_settable_ctx_params_fn ec_priv_settable_ctx_params
;
26 static OSSL_FUNC_serializer_serialize_data_fn ec_priv_der_data
;
27 static OSSL_FUNC_serializer_serialize_object_fn ec_priv_der
;
28 static OSSL_FUNC_serializer_serialize_data_fn ec_pem_priv_data
;
29 static OSSL_FUNC_serializer_serialize_object_fn ec_pem_priv
;
31 static OSSL_FUNC_serializer_newctx_fn ec_print_newctx
;
32 static OSSL_FUNC_serializer_freectx_fn ec_print_freectx
;
33 static OSSL_FUNC_serializer_serialize_data_fn ec_priv_print_data
;
34 static OSSL_FUNC_serializer_serialize_object_fn ec_priv_print
;
37 * Context used for private key serialization.
39 struct ec_priv_ctx_st
{
42 struct pkcs8_encrypt_ctx_st sc
;
45 /* Private key : context */
46 static void *ec_priv_newctx(void *provctx
)
48 struct ec_priv_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
51 ctx
->provctx
= provctx
;
53 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
59 static void ec_priv_freectx(void *vctx
)
61 struct ec_priv_ctx_st
*ctx
= vctx
;
63 EVP_CIPHER_free(ctx
->sc
.cipher
);
64 OPENSSL_free(ctx
->sc
.cipher_pass
);
68 static const OSSL_PARAM
*ec_priv_settable_ctx_params(void)
70 static const OSSL_PARAM settables
[] = {
71 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER
, NULL
, 0),
72 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS
, NULL
, 0),
79 static int ec_priv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
81 struct ec_priv_ctx_st
*ctx
= vctx
;
84 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_CIPHER
))
86 const OSSL_PARAM
*propsp
=
87 OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PROPERTIES
);
88 const char *props
= NULL
;
90 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
92 if (propsp
!= NULL
&& propsp
->data_type
!= OSSL_PARAM_UTF8_STRING
)
94 props
= (propsp
!= NULL
? propsp
->data
: NULL
);
96 EVP_CIPHER_free(ctx
->sc
.cipher
);
97 ctx
->sc
.cipher_intent
= p
->data
!= NULL
;
99 && ((ctx
->sc
.cipher
= EVP_CIPHER_fetch(NULL
, p
->data
, props
))
103 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PASS
))
105 OPENSSL_free(ctx
->sc
.cipher_pass
);
106 ctx
->sc
.cipher_pass
= NULL
;
107 if (!OSSL_PARAM_get_octet_string(p
, &ctx
->sc
.cipher_pass
, 0,
108 &ctx
->sc
.cipher_pass_length
))
114 /* Private key : DER */
115 static int ec_priv_der_data(void *vctx
, const OSSL_PARAM params
[],
117 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
119 struct ec_priv_ctx_st
*ctx
= vctx
;
120 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
121 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
122 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
125 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
127 if (ec_import
!= NULL
) {
130 if ((eckey
= ec_new(ctx
->provctx
)) != NULL
131 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
132 && ec_priv_der(ctx
, eckey
, out
, cb
, cbarg
))
139 static int ec_priv_der(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
140 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
142 struct ec_priv_ctx_st
*ctx
= vctx
;
143 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
150 ctx
->sc
.cbarg
= cbarg
;
152 ret
= ossl_prov_write_priv_der_from_obj(out
, eckey
, EVP_PKEY_EC
,
153 ossl_prov_prepare_ec_params
,
154 ossl_prov_ec_priv_to_der
,
161 /* Private key : PEM */
162 static int ec_pem_priv_data(void *vctx
, const OSSL_PARAM params
[],
164 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
166 struct ec_priv_ctx_st
*ctx
= vctx
;
167 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
168 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
169 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
172 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
174 if (ec_import
!= NULL
) {
177 if ((eckey
= ec_new(ctx
->provctx
)) != NULL
178 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
179 && ec_pem_priv(ctx
, eckey
, out
, cb
, cbarg
))
186 static int ec_pem_priv(void *vctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
187 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
189 struct ec_priv_ctx_st
*ctx
= vctx
;
190 BIO
*out
= bio_new_from_core_bio(ctx
->provctx
, cout
);
197 ctx
->sc
.cbarg
= cbarg
;
199 ret
= ossl_prov_write_priv_pem_from_obj(out
, eckey
, EVP_PKEY_EC
,
200 ossl_prov_prepare_ec_params
,
201 ossl_prov_ec_priv_to_der
,
209 * There's no specific print context, so we use the provider context
211 static void *ec_print_newctx(void *provctx
)
216 static void ec_print_freectx(void *ctx
)
220 static int ec_priv_print_data(void *vctx
, const OSSL_PARAM params
[],
222 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
224 struct ec_priv_ctx_st
*ctx
= vctx
;
225 OSSL_FUNC_keymgmt_new_fn
*ec_new
;
226 OSSL_FUNC_keymgmt_free_fn
*ec_free
;
227 OSSL_FUNC_keymgmt_import_fn
*ec_import
;
230 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
232 if (ec_import
!= NULL
) {
235 if ((eckey
= ec_new(ctx
->provctx
)) != NULL
236 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
237 && ec_priv_print(ctx
, eckey
, out
, cb
, cbarg
))
244 static int ec_priv_print(void *ctx
, void *eckey
, OSSL_CORE_BIO
*cout
,
245 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
247 BIO
*out
= bio_new_from_core_bio(ctx
, cout
);
253 ret
= ossl_prov_print_eckey(out
, eckey
, ec_print_priv
);
259 const OSSL_DISPATCH ec_priv_der_serializer_functions
[] = {
260 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))ec_priv_newctx
},
261 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))ec_priv_freectx
},
262 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
263 (void (*)(void))ec_priv_set_ctx_params
},
264 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
265 (void (*)(void))ec_priv_settable_ctx_params
},
266 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))ec_priv_der_data
},
267 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))ec_priv_der
},
271 const OSSL_DISPATCH ec_priv_pem_serializer_functions
[] = {
272 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))ec_priv_newctx
},
273 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))ec_priv_freectx
},
274 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
275 (void (*)(void))ec_priv_set_ctx_params
},
276 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
277 (void (*)(void))ec_priv_settable_ctx_params
},
278 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))ec_pem_priv_data
},
279 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))ec_pem_priv
},
283 const OSSL_DISPATCH ec_priv_text_serializer_functions
[] = {
284 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))ec_print_newctx
},
285 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))ec_print_freectx
},
286 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))ec_priv_print
},
287 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
,
288 (void (*)(void))ec_priv_print_data
},