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/types.h>
15 #include <openssl/params.h>
16 #include "crypto/ecx.h"
18 #include "prov/implementations.h"
19 #include "prov/provider_ctx.h"
20 #include "serializer_local.h"
22 static OSSL_FUNC_serializer_newctx_fn x25519_priv_newctx;
23 static OSSL_FUNC_serializer_newctx_fn x448_priv_newctx;
24 static OSSL_FUNC_serializer_newctx_fn ed25519_priv_newctx;
25 static OSSL_FUNC_serializer_newctx_fn ed448_priv_newctx;
26 static OSSL_FUNC_serializer_freectx_fn ecx_priv_freectx;
27 static OSSL_FUNC_serializer_set_ctx_params_fn ecx_priv_set_ctx_params;
28 static OSSL_FUNC_serializer_settable_ctx_params_fn ecx_priv_settable_ctx_params;
29 static OSSL_FUNC_serializer_serialize_data_fn ecx_priv_der_data;
30 static OSSL_FUNC_serializer_serialize_object_fn ecx_priv_der;
31 static OSSL_FUNC_serializer_serialize_data_fn ecx_priv_pem_data;
32 static OSSL_FUNC_serializer_serialize_object_fn ecx_priv_pem;
34 static OSSL_FUNC_serializer_serialize_data_fn ecx_priv_print_data;
35 static OSSL_FUNC_serializer_serialize_object_fn ecx_priv_print;
38 * Context used for private key serialization.
40 struct ecx_priv_ctx_st {
43 struct pkcs8_encrypt_ctx_st sc;
47 /* Private key : context */
48 static void *ecx_priv_newctx(void *provctx, ECX_KEY_TYPE type)
50 struct ecx_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
53 ctx->provctx = provctx;
55 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
62 static void *x25519_priv_newctx(void *provctx)
64 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_X25519);
67 static void *x448_priv_newctx(void *provctx)
69 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_X448);
72 static void *ed25519_priv_newctx(void *provctx)
74 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_ED25519);
77 static void *ed448_priv_newctx(void *provctx)
79 return ecx_priv_newctx(provctx, ECX_KEY_TYPE_ED448);
82 static void ecx_priv_freectx(void *vctx)
84 struct ecx_priv_ctx_st *ctx = vctx;
86 EVP_CIPHER_free(ctx->sc.cipher);
87 OPENSSL_free(ctx->sc.cipher_pass);
91 static const OSSL_PARAM *ecx_priv_settable_ctx_params(void)
93 static const OSSL_PARAM settables[] = {
94 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
95 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
102 static int ecx_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
104 struct ecx_priv_ctx_st *ctx = vctx;
107 p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER);
109 const OSSL_PARAM *propsp =
110 OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
113 if (p->data_type != OSSL_PARAM_UTF8_STRING)
115 if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
117 props = (propsp != NULL ? propsp->data : NULL);
119 EVP_CIPHER_free(ctx->sc.cipher);
120 ctx->sc.cipher_intent = p->data != NULL;
122 && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
126 p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS);
128 OPENSSL_free(ctx->sc.cipher_pass);
129 ctx->sc.cipher_pass = NULL;
130 if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
131 &ctx->sc.cipher_pass_length))
137 /* Private key : DER */
138 static int ecx_priv_der_data(void *vctx, const OSSL_PARAM params[],
140 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
142 struct ecx_priv_ctx_st *ctx = vctx;
143 OSSL_FUNC_keymgmt_new_fn *ecx_new;
144 OSSL_FUNC_keymgmt_free_fn *ecx_free;
145 OSSL_FUNC_keymgmt_import_fn *ecx_import;
148 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
150 if (ecx_import != NULL) {
153 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
154 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
155 && ecx_priv_der(ctx, ecxkey, out, cb, cbarg))
162 static int ecx_priv_der(void *vctx, void *vecxkey, OSSL_CORE_BIO *cout,
163 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
165 struct ecx_priv_ctx_st *ctx = vctx;
166 ECX_KEY *ecxkey = vecxkey;
168 int nid = KEYTYPE2NID(ctx->type);
169 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
175 ctx->sc.cbarg = cbarg;
177 ret = ossl_prov_write_priv_der_from_obj(out, ecxkey,
180 ossl_prov_ecx_priv_to_der,
187 /* Private key : PEM */
188 static int ecx_priv_pem_data(void *vctx, const OSSL_PARAM params[],
190 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
192 struct ecx_priv_ctx_st *ctx = vctx;
193 OSSL_FUNC_keymgmt_new_fn *ecx_new;
194 OSSL_FUNC_keymgmt_free_fn *ecx_free;
195 OSSL_FUNC_keymgmt_import_fn *ecx_import;
198 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
200 if (ecx_import != NULL) {
203 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
204 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
205 && ecx_priv_pem(ctx->provctx, ecxkey, out, cb, cbarg))
212 static int ecx_priv_pem(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
213 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
215 struct ecx_priv_ctx_st *ctx = vctx;
217 int nid = KEYTYPE2NID(ctx->type);
218 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
224 ctx->sc.cbarg = cbarg;
226 ret = ossl_prov_write_priv_pem_from_obj(out, ecxkey,
229 ossl_prov_ecx_priv_to_der,
236 static int ecx_priv_print_data(void *vctx, const OSSL_PARAM params[],
238 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
240 struct ecx_priv_ctx_st *ctx = vctx;
241 OSSL_FUNC_keymgmt_new_fn *ecx_new;
242 OSSL_FUNC_keymgmt_free_fn *ecx_free;
243 OSSL_FUNC_keymgmt_import_fn *ecx_import;
246 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
248 if (ecx_import != NULL) {
251 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
252 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
253 && ecx_priv_print(ctx, ecxkey, out, cb, cbarg))
260 static int ecx_priv_print(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
261 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
263 struct ecx_priv_ctx_st *ctx = vctx;
264 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
270 ret = ossl_prov_print_ecx(out, ecxkey, ecx_print_priv);
276 #define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
277 const OSSL_DISPATCH alg##_priv_##type##_serializer_functions[] = { \
278 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_priv_newctx }, \
279 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_priv_freectx }, \
280 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS, \
281 (void (*)(void))ecx_priv_set_ctx_params }, \
282 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS, \
283 (void (*)(void))ecx_priv_settable_ctx_params }, \
284 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
285 (void (*)(void))ecx_priv_##type##_data }, \
286 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
287 (void (*)(void))ecx_priv_##type }, \
291 #define MAKE_SERIALIZER_FUNCTIONS_GROUP(alg) \
292 MAKE_SERIALIZER_FUNCTIONS(alg, der) \
293 MAKE_SERIALIZER_FUNCTIONS(alg, pem) \
294 const OSSL_DISPATCH alg##_priv_print_serializer_functions[] = { \
295 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_priv_newctx }, \
296 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_priv_freectx }, \
297 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
298 (void (*)(void))ecx_priv_print }, \
299 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
300 (void (*)(void))ecx_priv_print_data }, \
304 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
305 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
306 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
307 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)