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/err.h>
12 #include <openssl/pem.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
16 #include "prov/implementations.h"
17 #include "prov/provider_ctx.h"
18 #include "serializer_local.h"
20 static OSSL_OP_serializer_newctx_fn ec_pub_newctx;
21 static OSSL_OP_serializer_freectx_fn ec_pub_freectx;
22 static OSSL_OP_serializer_serialize_data_fn ec_pub_der_data;
23 static OSSL_OP_serializer_serialize_object_fn ec_pub_der;
24 static OSSL_OP_serializer_serialize_data_fn ec_pub_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn ec_pub_pem;
27 static OSSL_OP_serializer_serialize_data_fn ec_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn ec_pub_print;
30 /* Public key : context */
33 * There's no specific implementation context, so we use the provider context
35 static void *ec_pub_newctx(void *provctx)
40 static void ec_pub_freectx(void *ctx)
44 /* Public key : DER */
45 static int ec_pub_der_data(void *vctx, const OSSL_PARAM params[],
47 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
49 OSSL_OP_keymgmt_new_fn *ec_new;
50 OSSL_OP_keymgmt_free_fn *ec_free;
51 OSSL_OP_keymgmt_import_fn *ec_import;
54 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
56 if (ec_import != NULL) {
60 if ((eckey = ec_new(vctx)) != NULL
61 && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
62 && ec_pub_der(vctx, eckey, out, cb, cbarg))
69 static int ec_pub_der(void *ctx, void *eckey, OSSL_CORE_BIO *cout,
70 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
72 BIO *out = bio_new_from_core_bio(ctx, cout);
78 ret = ossl_prov_write_pub_der_from_obj(out, eckey, EVP_PKEY_EC,
79 ossl_prov_prepare_ec_params,
80 ossl_prov_ec_pub_to_der);
86 /* Public key : PEM */
87 static int ec_pub_pem_data(void *vctx, const OSSL_PARAM params[],
89 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
91 OSSL_OP_keymgmt_new_fn *ec_new;
92 OSSL_OP_keymgmt_free_fn *ec_free;
93 OSSL_OP_keymgmt_import_fn *ec_import;
96 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
98 if (ec_import != NULL) {
102 if ((eckey = ec_new(vctx)) != NULL
103 && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
104 && ec_pub_pem(vctx, eckey, out, cb, cbarg))
111 static int ec_pub_pem(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
112 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
114 BIO *out = bio_new_from_core_bio(vctx, cout);
120 ret = ossl_prov_write_pub_pem_from_obj(out, eckey, EVP_PKEY_EC,
121 ossl_prov_prepare_ec_params,
122 ossl_prov_ec_pub_to_der);
128 static int ec_pub_print_data(void *vctx, const OSSL_PARAM params[],
130 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
132 OSSL_OP_keymgmt_new_fn *ec_new;
133 OSSL_OP_keymgmt_free_fn *ec_free;
134 OSSL_OP_keymgmt_import_fn *ec_import;
137 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
139 if (ec_import != NULL) {
143 if ((eckey = ec_new(vctx)) != NULL
144 && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
145 && ec_pub_print(vctx, eckey, out, cb, cbarg))
152 static int ec_pub_print(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
153 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
155 BIO *out = bio_new_from_core_bio(vctx, cout);
161 ret = ossl_prov_print_eckey(out, eckey, ec_print_pub);
167 const OSSL_DISPATCH ec_pub_der_serializer_functions[] = {
168 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
169 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
170 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_der_data },
171 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_der },
175 const OSSL_DISPATCH ec_pub_pem_serializer_functions[] = {
176 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
177 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
178 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_pem_data },
179 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_pem },
183 const OSSL_DISPATCH ec_pub_text_serializer_functions[] = {
184 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
185 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
186 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_print },
187 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
188 (void (*)(void))ec_pub_print_data },