]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_ecx_pub.c
Make the naming scheme for dispatched functions more consistent
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_ecx_pub.c
CommitLineData
8efc4a9c
MC
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>
8efc4a9c
MC
11#include <openssl/err.h>
12#include <openssl/pem.h>
13#include <openssl/types.h>
14#include <openssl/params.h>
244bc297 15#include "crypto/ecx.h"
8efc4a9c
MC
16#include "prov/bio.h"
17#include "prov/implementations.h"
d40b42ab 18#include "prov/provider_ctx.h"
8efc4a9c
MC
19#include "serializer_local.h"
20
363b1e5d
DMSP
21static OSSL_FUNC_serializer_newctx_fn x25519_pub_newctx;
22static OSSL_FUNC_serializer_newctx_fn x448_pub_newctx;
23static OSSL_FUNC_serializer_newctx_fn ed25519_pub_newctx;
24static OSSL_FUNC_serializer_newctx_fn ed448_pub_newctx;
25static OSSL_FUNC_serializer_freectx_fn ecx_pub_freectx;
26static OSSL_FUNC_serializer_serialize_data_fn ecx_pub_der_data;
27static OSSL_FUNC_serializer_serialize_object_fn ecx_pub_der;
28static OSSL_FUNC_serializer_serialize_data_fn ecx_pub_pem_data;
29static OSSL_FUNC_serializer_serialize_object_fn ecx_pub_pem;
30
31static OSSL_FUNC_serializer_serialize_data_fn ecx_pub_print_data;
32static OSSL_FUNC_serializer_serialize_object_fn ecx_pub_print;
8efc4a9c
MC
33
34/*
35 * Context used for public key serialization.
36 */
37struct ecx_pub_ctx_st {
38 void *provctx;
39 ECX_KEY_TYPE type;
40};
41
42/* Public key : context */
43static void *ecx_pub_newctx(void *provctx, ECX_KEY_TYPE type)
44{
45 struct ecx_pub_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
46
47 if (ctx != NULL) {
48 ctx->provctx = provctx;
49 ctx->type = type;
50 }
51 return ctx;
52}
53
54static void *x25519_pub_newctx(void *provctx)
55{
56 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X25519);
57}
58
59static void *x448_pub_newctx(void *provctx)
60{
61 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448);
62}
63
244bc297
MC
64static void *ed25519_pub_newctx(void *provctx)
65{
66 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED25519);
67}
68
69static void *ed448_pub_newctx(void *provctx)
70{
71 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED448);
72}
73
8efc4a9c
MC
74static void ecx_pub_freectx(void *ctx)
75{
76 OPENSSL_free(ctx);
77}
78
79/* Public key : DER */
d40b42ab
MC
80static int ecx_pub_der_data(void *vctx, const OSSL_PARAM params[],
81 OSSL_CORE_BIO *out,
8efc4a9c
MC
82 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
83{
84 struct ecx_pub_ctx_st *ctx = vctx;
363b1e5d
DMSP
85 OSSL_FUNC_keymgmt_new_fn *ecx_new;
86 OSSL_FUNC_keymgmt_free_fn *ecx_free;
87 OSSL_FUNC_keymgmt_import_fn *ecx_import;
8efc4a9c
MC
88 int ok = 0;
89
90 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
91
92 if (ecx_import != NULL) {
93 ECX_KEY *ecxkey;
94
95 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
96 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
97 && ecx_pub_der(ctx, ecxkey, out, cb, cbarg))
98 ok = 1;
99 ecx_free(ecxkey);
100 }
101 return ok;
102}
103
d40b42ab 104static int ecx_pub_der(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
8efc4a9c
MC
105 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
106{
107 struct ecx_pub_ctx_st *ctx = vctx;
d40b42ab
MC
108 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
109 int ret;
8efc4a9c 110
d40b42ab
MC
111 if (out == NULL)
112 return 0;
113
114 ret = ossl_prov_write_pub_der_from_obj(out, ecxkey,
115 KEYTYPE2NID(ctx->type),
116 NULL,
117 ossl_prov_ecx_pub_to_der);
118 BIO_free(out);
119
120 return ret;
8efc4a9c
MC
121}
122
123/* Public key : PEM */
d40b42ab
MC
124static int ecx_pub_pem_data(void *vctx, const OSSL_PARAM params[],
125 OSSL_CORE_BIO *out,
8efc4a9c
MC
126 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
127{
128 struct ecx_pub_ctx_st *ctx = vctx;
363b1e5d
DMSP
129 OSSL_FUNC_keymgmt_new_fn *ecx_new;
130 OSSL_FUNC_keymgmt_free_fn *ecx_free;
131 OSSL_FUNC_keymgmt_import_fn *ecx_import;
8efc4a9c
MC
132 int ok = 0;
133
134 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
135
136 if (ecx_import != NULL) {
137 ECX_KEY *ecxkey;
138
139 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
140 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
141 && ecx_pub_pem(ctx, ecxkey, out, cb, cbarg))
142 ok = 1;
143 ecx_free(ecxkey);
144 }
145 return ok;
146}
147
d40b42ab 148static int ecx_pub_pem(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
8efc4a9c
MC
149 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
150{
151 struct ecx_pub_ctx_st *ctx = vctx;
d40b42ab
MC
152 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
153 int ret;
8efc4a9c 154
d40b42ab
MC
155 if (out == NULL)
156 return 0;
8efc4a9c 157
d40b42ab
MC
158 ret = ossl_prov_write_pub_pem_from_obj(out, ecxkey,
159 KEYTYPE2NID(ctx->type),
160 NULL,
161 ossl_prov_ecx_pub_to_der);
162 BIO_free(out);
163
164 return ret;
8efc4a9c
MC
165}
166
d40b42ab
MC
167static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[],
168 OSSL_CORE_BIO *out,
8efc4a9c
MC
169 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
170{
171 struct ecx_pub_ctx_st *ctx = vctx;
363b1e5d
DMSP
172 OSSL_FUNC_keymgmt_new_fn *ecx_new;
173 OSSL_FUNC_keymgmt_free_fn *ecx_free;
174 OSSL_FUNC_keymgmt_import_fn *ecx_import;
8efc4a9c
MC
175 int ok = 0;
176
177 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
178
179 if (ecx_import != NULL) {
180 ECX_KEY *ecxkey;
181
182 if ((ecxkey = ecx_new(ctx)) != NULL
183 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
184 && ecx_pub_print(ctx, ecxkey, out, cb, cbarg))
185 ok = 1;
186 ecx_free(ecxkey);
187 }
188 return ok;
189}
190
d40b42ab 191static int ecx_pub_print(void *vctx, void *ecxkey, OSSL_CORE_BIO *cout,
8efc4a9c
MC
192 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
193{
d40b42ab
MC
194 struct ecx_pub_ctx_st *ctx = vctx;
195 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
196 int ret;
197
198 if (out == NULL)
199 return 0;
200
201 ret = ossl_prov_print_ecx(out, ecxkey, ecx_print_pub);
202 BIO_free(out);
203
204 return ret;
8efc4a9c
MC
205}
206
207#define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
208 const OSSL_DISPATCH alg##_pub_##type##_serializer_functions[] = { \
209 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_pub_newctx }, \
210 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_pub_freectx }, \
211 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
212 (void (*)(void))ecx_pub_##type##_data }, \
213 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
214 (void (*)(void))ecx_pub_##type }, \
215 { 0, NULL } \
216 };
217
218#define MAKE_SERIALIZER_FUNCTIONS_GROUP(alg) \
219 MAKE_SERIALIZER_FUNCTIONS(alg, der) \
220 MAKE_SERIALIZER_FUNCTIONS(alg, pem) \
221 MAKE_SERIALIZER_FUNCTIONS(alg, print)
222
223MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
224MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
244bc297
MC
225MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
226MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)