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_numbers.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 "serializer_local.h"
21 static OSSL_OP_serializer_newctx_fn ec_priv_newctx
;
22 static OSSL_OP_serializer_freectx_fn ec_priv_freectx
;
23 static OSSL_OP_serializer_set_ctx_params_fn ec_priv_set_ctx_params
;
24 static OSSL_OP_serializer_settable_ctx_params_fn ec_priv_settable_ctx_params
;
25 static OSSL_OP_serializer_serialize_data_fn ec_priv_der_data
;
26 static OSSL_OP_serializer_serialize_object_fn ec_priv_der
;
27 static OSSL_OP_serializer_serialize_data_fn ec_pem_priv_data
;
28 static OSSL_OP_serializer_serialize_object_fn ec_pem_priv
;
30 static OSSL_OP_serializer_newctx_fn ec_print_newctx
;
31 static OSSL_OP_serializer_freectx_fn ec_print_freectx
;
32 static OSSL_OP_serializer_serialize_data_fn ec_priv_print_data
;
33 static OSSL_OP_serializer_serialize_object_fn ec_priv_print
;
36 * Context used for private key serialization.
38 struct ec_priv_ctx_st
{
41 struct pkcs8_encrypt_ctx_st sc
;
44 /* Private key : context */
45 static void *ec_priv_newctx(void *provctx
)
47 struct ec_priv_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
50 ctx
->provctx
= provctx
;
52 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
58 static void ec_priv_freectx(void *vctx
)
60 struct ec_priv_ctx_st
*ctx
= vctx
;
62 EVP_CIPHER_free(ctx
->sc
.cipher
);
63 OPENSSL_free(ctx
->sc
.cipher_pass
);
67 static const OSSL_PARAM
*ec_priv_settable_ctx_params(void)
69 static const OSSL_PARAM settables
[] = {
70 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER
, NULL
, 0),
71 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS
, NULL
, 0),
78 static int ec_priv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
80 struct ec_priv_ctx_st
*ctx
= vctx
;
83 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_CIPHER
))
85 const OSSL_PARAM
*propsp
=
86 OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PROPERTIES
);
87 const char *props
= NULL
;
89 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
91 if (propsp
!= NULL
&& propsp
->data_type
!= OSSL_PARAM_UTF8_STRING
)
93 props
= (propsp
!= NULL
? propsp
->data
: NULL
);
95 EVP_CIPHER_free(ctx
->sc
.cipher
);
96 ctx
->sc
.cipher_intent
= p
->data
!= NULL
;
98 && ((ctx
->sc
.cipher
= EVP_CIPHER_fetch(NULL
, p
->data
, props
))
102 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PASS
))
104 OPENSSL_free(ctx
->sc
.cipher_pass
);
105 ctx
->sc
.cipher_pass
= NULL
;
106 if (!OSSL_PARAM_get_octet_string(p
, &ctx
->sc
.cipher_pass
, 0,
107 &ctx
->sc
.cipher_pass_length
))
113 /* Private key : DER */
114 static int ec_priv_der_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
115 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
117 struct ec_priv_ctx_st
*ctx
= vctx
;
118 OSSL_OP_keymgmt_new_fn
*ec_new
;
119 OSSL_OP_keymgmt_free_fn
*ec_free
;
120 OSSL_OP_keymgmt_import_fn
*ec_import
;
123 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
125 if (ec_import
!= NULL
) {
128 if ((eckey
= ec_new(ctx
->provctx
)) != NULL
129 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
130 && ec_priv_der(ctx
, eckey
, out
, cb
, cbarg
))
137 static int ec_priv_der(void *vctx
, void *eckey
, BIO
*out
,
138 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
140 struct ec_priv_ctx_st
*ctx
= vctx
;
143 ctx
->sc
.cbarg
= cbarg
;
145 return ossl_prov_write_priv_der_from_obj(out
, eckey
, EVP_PKEY_EC
,
146 ossl_prov_prepare_ec_params
,
147 ossl_prov_ec_priv_to_der
,
151 /* Private key : PEM */
152 static int ec_pem_priv_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
153 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
155 struct ec_priv_ctx_st
*ctx
= vctx
;
156 OSSL_OP_keymgmt_new_fn
*ec_new
;
157 OSSL_OP_keymgmt_free_fn
*ec_free
;
158 OSSL_OP_keymgmt_import_fn
*ec_import
;
161 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
163 if (ec_import
!= NULL
) {
166 if ((eckey
= ec_new(ctx
->provctx
)) != NULL
167 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
168 && ec_pem_priv(ctx
, eckey
, out
, cb
, cbarg
))
175 static int ec_pem_priv(void *vctx
, void *eckey
, BIO
*out
,
176 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
178 struct ec_priv_ctx_st
*ctx
= vctx
;
181 ctx
->sc
.cbarg
= cbarg
;
183 return ossl_prov_write_priv_pem_from_obj(out
, eckey
, EVP_PKEY_EC
,
184 ossl_prov_prepare_ec_params
,
185 ossl_prov_ec_priv_to_der
,
190 * There's no specific print context, so we use the provider context
192 static void *ec_print_newctx(void *provctx
)
197 static void ec_print_freectx(void *ctx
)
201 static int ec_priv_print_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
202 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
204 struct ec_priv_ctx_st
*ctx
= vctx
;
205 OSSL_OP_keymgmt_new_fn
*ec_new
;
206 OSSL_OP_keymgmt_free_fn
*ec_free
;
207 OSSL_OP_keymgmt_import_fn
*ec_import
;
210 ec_get_new_free_import(&ec_new
, &ec_free
, &ec_import
);
212 if (ec_import
!= NULL
) {
215 if ((eckey
= ec_new(ctx
->provctx
)) != NULL
216 && ec_import(eckey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
217 && ec_priv_print(ctx
, eckey
, out
, cb
, cbarg
))
224 static int ec_priv_print(void *vctx
, void *eckey
, BIO
*out
,
225 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
227 return ossl_prov_print_eckey(out
, eckey
, ec_print_priv
);
230 const OSSL_DISPATCH ec_priv_der_serializer_functions
[] = {
231 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))ec_priv_newctx
},
232 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))ec_priv_freectx
},
233 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
234 (void (*)(void))ec_priv_set_ctx_params
},
235 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
236 (void (*)(void))ec_priv_settable_ctx_params
},
237 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))ec_priv_der_data
},
238 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))ec_priv_der
},
242 const OSSL_DISPATCH ec_priv_pem_serializer_functions
[] = {
243 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))ec_priv_newctx
},
244 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))ec_priv_freectx
},
245 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
,
246 (void (*)(void))ec_priv_set_ctx_params
},
247 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
,
248 (void (*)(void))ec_priv_settable_ctx_params
},
249 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
, (void (*)(void))ec_pem_priv_data
},
250 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))ec_pem_priv
},
254 const OSSL_DISPATCH ec_priv_text_serializer_functions
[] = {
255 { OSSL_FUNC_SERIALIZER_NEWCTX
, (void (*)(void))ec_print_newctx
},
256 { OSSL_FUNC_SERIALIZER_FREECTX
, (void (*)(void))ec_print_freectx
},
257 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
, (void (*)(void))ec_priv_print
},
258 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
,
259 (void (*)(void))ec_priv_print_data
},