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>
15 #include "crypto/ecx.h"
17 #include "prov/implementations.h"
18 #include "serializer_local.h"
20 static OSSL_OP_serializer_newctx_fn x25519_pub_newctx;
21 static OSSL_OP_serializer_newctx_fn x448_pub_newctx;
22 static OSSL_OP_serializer_newctx_fn ed25519_pub_newctx;
23 static OSSL_OP_serializer_newctx_fn ed448_pub_newctx;
24 static OSSL_OP_serializer_freectx_fn ecx_pub_freectx;
25 static OSSL_OP_serializer_serialize_data_fn ecx_pub_der_data;
26 static OSSL_OP_serializer_serialize_object_fn ecx_pub_der;
27 static OSSL_OP_serializer_serialize_data_fn ecx_pub_pem_data;
28 static OSSL_OP_serializer_serialize_object_fn ecx_pub_pem;
30 static OSSL_OP_serializer_serialize_data_fn ecx_pub_print_data;
31 static OSSL_OP_serializer_serialize_object_fn ecx_pub_print;
34 * Context used for public key serialization.
36 struct ecx_pub_ctx_st {
41 /* Public key : context */
42 static void *ecx_pub_newctx(void *provctx, ECX_KEY_TYPE type)
44 struct ecx_pub_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
47 ctx->provctx = provctx;
53 static void *x25519_pub_newctx(void *provctx)
55 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X25519);
58 static void *x448_pub_newctx(void *provctx)
60 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448);
63 static void *ed25519_pub_newctx(void *provctx)
65 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED25519);
68 static void *ed448_pub_newctx(void *provctx)
70 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED448);
73 static void ecx_pub_freectx(void *ctx)
78 /* Public key : DER */
79 static int ecx_pub_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
80 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
82 struct ecx_pub_ctx_st *ctx = vctx;
83 OSSL_OP_keymgmt_new_fn *ecx_new;
84 OSSL_OP_keymgmt_free_fn *ecx_free;
85 OSSL_OP_keymgmt_import_fn *ecx_import;
88 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
90 if (ecx_import != NULL) {
93 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
94 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
95 && ecx_pub_der(ctx, ecxkey, out, cb, cbarg))
102 static int ecx_pub_der(void *vctx, void *ecxkey, BIO *out,
103 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
105 struct ecx_pub_ctx_st *ctx = vctx;
107 return ossl_prov_write_pub_der_from_obj(out, ecxkey,
108 KEYTYPE2NID(ctx->type),
110 ossl_prov_ecx_pub_to_der);
113 /* Public key : PEM */
114 static int ecx_pub_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
115 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
117 struct ecx_pub_ctx_st *ctx = vctx;
118 OSSL_OP_keymgmt_new_fn *ecx_new;
119 OSSL_OP_keymgmt_free_fn *ecx_free;
120 OSSL_OP_keymgmt_import_fn *ecx_import;
123 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
125 if (ecx_import != NULL) {
128 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
129 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
130 && ecx_pub_pem(ctx, ecxkey, out, cb, cbarg))
137 static int ecx_pub_pem(void *vctx, void *ecxkey, BIO *out,
138 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
140 struct ecx_pub_ctx_st *ctx = vctx;
142 return ossl_prov_write_pub_pem_from_obj(out, ecxkey,
143 KEYTYPE2NID(ctx->type),
145 ossl_prov_ecx_pub_to_der);
149 static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
150 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
152 struct ecx_pub_ctx_st *ctx = vctx;
153 OSSL_OP_keymgmt_new_fn *ecx_new;
154 OSSL_OP_keymgmt_free_fn *ecx_free;
155 OSSL_OP_keymgmt_import_fn *ecx_import;
158 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
160 if (ecx_import != NULL) {
163 if ((ecxkey = ecx_new(ctx)) != NULL
164 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
165 && ecx_pub_print(ctx, ecxkey, out, cb, cbarg))
172 static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out,
173 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
175 return ossl_prov_print_ecx(out, ecxkey, ecx_print_pub);
178 #define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
179 const OSSL_DISPATCH alg##_pub_##type##_serializer_functions[] = { \
180 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_pub_newctx }, \
181 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_pub_freectx }, \
182 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
183 (void (*)(void))ecx_pub_##type##_data }, \
184 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
185 (void (*)(void))ecx_pub_##type }, \
189 #define MAKE_SERIALIZER_FUNCTIONS_GROUP(alg) \
190 MAKE_SERIALIZER_FUNCTIONS(alg, der) \
191 MAKE_SERIALIZER_FUNCTIONS(alg, pem) \
192 MAKE_SERIALIZER_FUNCTIONS(alg, print)
194 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
195 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
196 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
197 MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)