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/pem.h>
12 #include <openssl/ec.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
16 #include "prov/implementations.h"
17 #include "prov/providercommonerr.h"
18 #include "serializer_local.h"
20 static OSSL_OP_serializer_newctx_fn ec_param_newctx;
21 static OSSL_OP_serializer_freectx_fn ec_param_freectx;
22 static OSSL_OP_serializer_serialize_data_fn ec_param_der_data;
23 static OSSL_OP_serializer_serialize_object_fn ec_param_der;
24 static OSSL_OP_serializer_serialize_data_fn ec_param_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn ec_param_pem;
27 static OSSL_OP_serializer_serialize_data_fn ec_param_print_data;
28 static OSSL_OP_serializer_serialize_object_fn ec_param_print;
31 /* There is no specific implementation context, so use the provider context */
32 static void *ec_param_newctx(void *provctx)
37 static void ec_param_freectx(void *vctx)
41 /* Public key : DER */
42 static int ec_param_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
43 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
45 OSSL_OP_keymgmt_new_fn *ec_new;
46 OSSL_OP_keymgmt_free_fn *ec_free;
47 OSSL_OP_keymgmt_import_fn *ec_import;
50 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
52 if (ec_import != NULL) {
56 if ((eckey = ec_new(vctx)) != NULL
57 && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
58 && ec_param_der(vctx, eckey, out, cb, cbarg))
65 static int ec_param_der(void *vctx, void *eckey, BIO *out,
66 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
68 return i2d_ECPKParameters_bio(out, EC_KEY_get0_group(eckey));
71 /* Public key : PEM */
72 static int ec_param_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
73 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
75 OSSL_OP_keymgmt_new_fn *ec_new;
76 OSSL_OP_keymgmt_free_fn *ec_free;
77 OSSL_OP_keymgmt_import_fn *ec_import;
80 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
82 if (ec_import != NULL) {
86 if ((eckey = ec_new(vctx)) != NULL
87 && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
88 && ec_param_pem(vctx, eckey, out, cb, cbarg))
95 static int ec_param_pem(void *vctx, void *eckey, BIO *out,
96 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
98 return PEM_write_bio_ECPKParameters(out, EC_KEY_get0_group(eckey));
101 static int ec_param_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
102 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
104 OSSL_OP_keymgmt_new_fn *ec_new;
105 OSSL_OP_keymgmt_free_fn *ec_free;
106 OSSL_OP_keymgmt_import_fn *ec_import;
109 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
111 if (ec_import != NULL) {
114 /* vctx == provctx */
115 if ((eckey = ec_new(vctx)) != NULL
116 && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
117 && ec_param_print(vctx, eckey, out, cb, cbarg))
124 static int ec_param_print(void *vctx, void *eckey, BIO *out,
125 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
127 return ossl_prov_print_eckey(out, eckey, ec_print_params);
130 const OSSL_DISPATCH ec_param_der_serializer_functions[] = {
131 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_param_newctx },
132 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_param_freectx },
133 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_param_der_data },
134 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_param_der },
138 const OSSL_DISPATCH ec_param_pem_serializer_functions[] = {
139 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_param_newctx },
140 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_param_freectx },
141 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_param_pem_data },
142 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_param_pem },
146 const OSSL_DISPATCH ec_param_text_serializer_functions[] = {
147 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_param_newctx },
148 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_param_freectx },
149 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_param_print },
150 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
151 (void (*)(void))ec_param_print_data },