]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_ec_pub.c
Make the naming scheme for dispatched functions more consistent
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_ec_pub.c
CommitLineData
f552d900
SL
1/*
2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
23c48d94 10#include <openssl/core_dispatch.h>
f552d900
SL
11#include <openssl/err.h>
12#include <openssl/pem.h>
13#include <openssl/types.h>
14#include <openssl/params.h>
15#include "prov/bio.h"
16#include "prov/implementations.h"
d40b42ab 17#include "prov/provider_ctx.h"
f552d900
SL
18#include "serializer_local.h"
19
363b1e5d
DMSP
20static OSSL_FUNC_serializer_newctx_fn ec_pub_newctx;
21static OSSL_FUNC_serializer_freectx_fn ec_pub_freectx;
22static OSSL_FUNC_serializer_serialize_data_fn ec_pub_der_data;
23static OSSL_FUNC_serializer_serialize_object_fn ec_pub_der;
24static OSSL_FUNC_serializer_serialize_data_fn ec_pub_pem_data;
25static OSSL_FUNC_serializer_serialize_object_fn ec_pub_pem;
f552d900 26
363b1e5d
DMSP
27static OSSL_FUNC_serializer_serialize_data_fn ec_pub_print_data;
28static OSSL_FUNC_serializer_serialize_object_fn ec_pub_print;
f552d900
SL
29
30/* Public key : context */
31
32/*
33 * There's no specific implementation context, so we use the provider context
34 */
35static void *ec_pub_newctx(void *provctx)
36{
37 return provctx;
38}
39
40static void ec_pub_freectx(void *ctx)
41{
42}
43
44/* Public key : DER */
d40b42ab
MC
45static int ec_pub_der_data(void *vctx, const OSSL_PARAM params[],
46 OSSL_CORE_BIO *out,
f552d900
SL
47 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
48{
363b1e5d
DMSP
49 OSSL_FUNC_keymgmt_new_fn *ec_new;
50 OSSL_FUNC_keymgmt_free_fn *ec_free;
51 OSSL_FUNC_keymgmt_import_fn *ec_import;
f552d900
SL
52 int ok = 0;
53
54 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
55
56 if (ec_import != NULL) {
57 EC_KEY *eckey;
58
59 /* vctx == provctx */
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))
63 ok = 1;
64 ec_free(eckey);
65 }
66 return ok;
67}
68
d40b42ab 69static int ec_pub_der(void *ctx, void *eckey, OSSL_CORE_BIO *cout,
f552d900
SL
70 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
71{
d40b42ab
MC
72 BIO *out = bio_new_from_core_bio(ctx, cout);
73 int ret;
74
75 if (out == NULL)
76 return 0;
77
78 ret = ossl_prov_write_pub_der_from_obj(out, eckey, EVP_PKEY_EC,
f552d900
SL
79 ossl_prov_prepare_ec_params,
80 ossl_prov_ec_pub_to_der);
d40b42ab
MC
81 BIO_free(out);
82
83 return ret;
f552d900
SL
84}
85
86/* Public key : PEM */
d40b42ab
MC
87static int ec_pub_pem_data(void *vctx, const OSSL_PARAM params[],
88 OSSL_CORE_BIO *out,
f552d900
SL
89 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
90{
363b1e5d
DMSP
91 OSSL_FUNC_keymgmt_new_fn *ec_new;
92 OSSL_FUNC_keymgmt_free_fn *ec_free;
93 OSSL_FUNC_keymgmt_import_fn *ec_import;
f552d900
SL
94 int ok = 0;
95
96 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
97
98 if (ec_import != NULL) {
99 EC_KEY *eckey;
100
101 /* ctx == provctx */
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))
105 ok = 1;
106 ec_free(eckey);
107 }
108 return ok;
109}
110
d40b42ab 111static int ec_pub_pem(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
f552d900
SL
112 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
113{
d40b42ab
MC
114 BIO *out = bio_new_from_core_bio(vctx, cout);
115 int ret;
116
117 if (out == NULL)
118 return 0;
119
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);
123 BIO_free(out);
124
125 return ret;
f552d900
SL
126}
127
d40b42ab
MC
128static int ec_pub_print_data(void *vctx, const OSSL_PARAM params[],
129 OSSL_CORE_BIO *out,
f552d900
SL
130 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
131{
363b1e5d
DMSP
132 OSSL_FUNC_keymgmt_new_fn *ec_new;
133 OSSL_FUNC_keymgmt_free_fn *ec_free;
134 OSSL_FUNC_keymgmt_import_fn *ec_import;
f552d900
SL
135 int ok = 0;
136
137 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
138
139 if (ec_import != NULL) {
140 EC_KEY *eckey;
141
142 /* ctx == provctx */
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))
146 ok = 1;
147 ec_free(eckey);
148 }
149 return ok;
150}
151
d40b42ab 152static int ec_pub_print(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
f552d900
SL
153 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
154{
d40b42ab
MC
155 BIO *out = bio_new_from_core_bio(vctx, cout);
156 int ret;
157
158 if (out == NULL)
159 return 0;
160
161 ret = ossl_prov_print_eckey(out, eckey, ec_print_pub);
162 BIO_free(out);
163
164 return ret;
f552d900
SL
165}
166
167const 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 },
172 { 0, NULL }
173};
174
175const 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 },
180 { 0, NULL }
181};
182
183const 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 },
189 { 0, NULL }
190};