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/types.h>
15 #include <openssl/params.h>
16 #include "crypto/ecx.h"
18 #include "prov/implementations.h"
19 #include "serializer_local.h"
21 static OSSL_OP_serializer_newctx_fn x25519_priv_newctx
;
22 static OSSL_OP_serializer_newctx_fn x448_priv_newctx
;
23 static OSSL_OP_serializer_newctx_fn ed25519_priv_newctx
;
24 static OSSL_OP_serializer_newctx_fn ed448_priv_newctx
;
25 static OSSL_OP_serializer_freectx_fn ecx_priv_freectx
;
26 static OSSL_OP_serializer_set_ctx_params_fn ecx_priv_set_ctx_params
;
27 static OSSL_OP_serializer_settable_ctx_params_fn ecx_priv_settable_ctx_params
;
28 static OSSL_OP_serializer_serialize_data_fn ecx_priv_der_data
;
29 static OSSL_OP_serializer_serialize_object_fn ecx_priv_der
;
30 static OSSL_OP_serializer_serialize_data_fn ecx_priv_pem_data
;
31 static OSSL_OP_serializer_serialize_object_fn ecx_priv_pem
;
33 static OSSL_OP_serializer_serialize_data_fn ecx_priv_print_data
;
34 static OSSL_OP_serializer_serialize_object_fn ecx_priv_print
;
37 * Context used for private key serialization.
39 struct ecx_priv_ctx_st
{
42 struct pkcs8_encrypt_ctx_st sc
;
46 /* Private key : context */
47 static void *ecx_priv_newctx(void *provctx
, ECX_KEY_TYPE type
)
49 struct ecx_priv_ctx_st
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
52 ctx
->provctx
= provctx
;
54 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
61 static void *x25519_priv_newctx(void *provctx
)
63 return ecx_priv_newctx(provctx
, ECX_KEY_TYPE_X25519
);
66 static void *x448_priv_newctx(void *provctx
)
68 return ecx_priv_newctx(provctx
, ECX_KEY_TYPE_X448
);
71 static void *ed25519_priv_newctx(void *provctx
)
73 return ecx_priv_newctx(provctx
, ECX_KEY_TYPE_ED25519
);
76 static void *ed448_priv_newctx(void *provctx
)
78 return ecx_priv_newctx(provctx
, ECX_KEY_TYPE_ED448
);
81 static void ecx_priv_freectx(void *vctx
)
83 struct ecx_priv_ctx_st
*ctx
= vctx
;
85 EVP_CIPHER_free(ctx
->sc
.cipher
);
86 OPENSSL_free(ctx
->sc
.cipher_pass
);
90 static const OSSL_PARAM
*ecx_priv_settable_ctx_params(void)
92 static const OSSL_PARAM settables
[] = {
93 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER
, NULL
, 0),
94 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS
, NULL
, 0),
101 static int ecx_priv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
103 struct ecx_priv_ctx_st
*ctx
= vctx
;
106 p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_CIPHER
);
108 const OSSL_PARAM
*propsp
=
109 OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PROPERTIES
);
112 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
114 if (propsp
!= NULL
&& propsp
->data_type
!= OSSL_PARAM_UTF8_STRING
)
116 props
= (propsp
!= NULL
? propsp
->data
: NULL
);
118 EVP_CIPHER_free(ctx
->sc
.cipher
);
119 ctx
->sc
.cipher_intent
= p
->data
!= NULL
;
121 && ((ctx
->sc
.cipher
= EVP_CIPHER_fetch(NULL
, p
->data
, props
))
125 p
= OSSL_PARAM_locate_const(params
, OSSL_SERIALIZER_PARAM_PASS
);
127 OPENSSL_free(ctx
->sc
.cipher_pass
);
128 ctx
->sc
.cipher_pass
= NULL
;
129 if (!OSSL_PARAM_get_octet_string(p
, &ctx
->sc
.cipher_pass
, 0,
130 &ctx
->sc
.cipher_pass_length
))
136 /* Private key : DER */
137 static int ecx_priv_der_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
138 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
140 struct ecx_priv_ctx_st
*ctx
= vctx
;
141 OSSL_OP_keymgmt_new_fn
*ecx_new
;
142 OSSL_OP_keymgmt_free_fn
*ecx_free
;
143 OSSL_OP_keymgmt_import_fn
*ecx_import
;
146 ecx_get_new_free_import(ctx
->type
, &ecx_new
, &ecx_free
, &ecx_import
);
148 if (ecx_import
!= NULL
) {
151 if ((ecxkey
= ecx_new(ctx
->provctx
)) != NULL
152 && ecx_import(ecxkey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
153 && ecx_priv_der(ctx
, ecxkey
, out
, cb
, cbarg
))
160 static int ecx_priv_der(void *vctx
, void *vecxkey
, BIO
*out
,
161 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
163 struct ecx_priv_ctx_st
*ctx
= vctx
;
164 ECX_KEY
*ecxkey
= vecxkey
;
166 int nid
= KEYTYPE2NID(ctx
->type
);
169 ctx
->sc
.cbarg
= cbarg
;
171 ret
= ossl_prov_write_priv_der_from_obj(out
, ecxkey
,
174 ossl_prov_ecx_priv_to_der
,
180 /* Private key : PEM */
181 static int ecx_priv_pem_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
182 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
184 struct ecx_priv_ctx_st
*ctx
= vctx
;
185 OSSL_OP_keymgmt_new_fn
*ecx_new
;
186 OSSL_OP_keymgmt_free_fn
*ecx_free
;
187 OSSL_OP_keymgmt_import_fn
*ecx_import
;
190 ecx_get_new_free_import(ctx
->type
, &ecx_new
, &ecx_free
, &ecx_import
);
192 if (ecx_import
!= NULL
) {
195 if ((ecxkey
= ecx_new(ctx
->provctx
)) != NULL
196 && ecx_import(ecxkey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
197 && ecx_priv_pem(ctx
->provctx
, ecxkey
, out
, cb
, cbarg
))
204 static int ecx_priv_pem(void *vctx
, void *ecxkey
, BIO
*out
,
205 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
207 struct ecx_priv_ctx_st
*ctx
= vctx
;
209 int nid
= KEYTYPE2NID(ctx
->type
);
212 ctx
->sc
.cbarg
= cbarg
;
214 ret
= ossl_prov_write_priv_pem_from_obj(out
, ecxkey
,
217 ossl_prov_ecx_priv_to_der
,
223 static int ecx_priv_print_data(void *vctx
, const OSSL_PARAM params
[], BIO
*out
,
224 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
226 struct ecx_priv_ctx_st
*ctx
= vctx
;
227 OSSL_OP_keymgmt_new_fn
*ecx_new
;
228 OSSL_OP_keymgmt_free_fn
*ecx_free
;
229 OSSL_OP_keymgmt_import_fn
*ecx_import
;
232 ecx_get_new_free_import(ctx
->type
, &ecx_new
, &ecx_free
, &ecx_import
);
234 if (ecx_import
!= NULL
) {
237 if ((ecxkey
= ecx_new(ctx
->provctx
)) != NULL
238 && ecx_import(ecxkey
, OSSL_KEYMGMT_SELECT_KEYPAIR
, params
)
239 && ecx_priv_print(ctx
, ecxkey
, out
, cb
, cbarg
))
246 static int ecx_priv_print(void *ctx
, void *ecxkey
, BIO
*out
,
247 OSSL_PASSPHRASE_CALLBACK
*cb
, void *cbarg
)
249 return ossl_prov_print_ecx(out
, ecxkey
, ecx_print_priv
);
252 #define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
253 const OSSL_DISPATCH alg##_priv_##type##_serializer_functions[] = { \
254 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_priv_newctx }, \
255 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_priv_freectx }, \
256 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS, \
257 (void (*)(void))ecx_priv_set_ctx_params }, \
258 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS, \
259 (void (*)(void))ecx_priv_settable_ctx_params }, \
260 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
261 (void (*)(void))ecx_priv_##type##_data }, \
262 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
263 (void (*)(void))ecx_priv_##type }, \
267 #define MAKE_SERIALIZER_FUNCTIONS_GROUP(alg) \
268 MAKE_SERIALIZER_FUNCTIONS(alg, der) \
269 MAKE_SERIALIZER_FUNCTIONS(alg, pem) \
270 const OSSL_DISPATCH alg##_priv_print_serializer_functions[] = { \
271 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_priv_newctx }, \
272 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_priv_freectx }, \
273 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
274 (void (*)(void))ecx_priv_print }, \
275 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
276 (void (*)(void))ecx_priv_print_data }, \
280 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519
)
281 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448
)
282 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519
)
283 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448
)