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 "serializer_local.h"
19 static OSSL_OP_serializer_newctx_fn x25519_pub_newctx;
20 static OSSL_OP_serializer_newctx_fn x448_pub_newctx;
21 static OSSL_OP_serializer_freectx_fn ecx_pub_freectx;
22 static OSSL_OP_serializer_serialize_data_fn ecx_pub_der_data;
23 static OSSL_OP_serializer_serialize_object_fn ecx_pub_der;
24 static OSSL_OP_serializer_serialize_data_fn ecx_pub_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn ecx_pub_pem;
27 static OSSL_OP_serializer_serialize_data_fn ecx_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn ecx_pub_print;
31 * Context used for public key serialization.
33 struct ecx_pub_ctx_st {
38 /* Public key : context */
39 static void *ecx_pub_newctx(void *provctx, ECX_KEY_TYPE type)
41 struct ecx_pub_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
44 ctx->provctx = provctx;
50 static void *x25519_pub_newctx(void *provctx)
52 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X25519);
55 static void *x448_pub_newctx(void *provctx)
57 return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448);
60 static void ecx_pub_freectx(void *ctx)
65 /* Public key : DER */
66 static int ecx_pub_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
67 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
69 struct ecx_pub_ctx_st *ctx = vctx;
70 OSSL_OP_keymgmt_new_fn *ecx_new;
71 OSSL_OP_keymgmt_free_fn *ecx_free;
72 OSSL_OP_keymgmt_import_fn *ecx_import;
75 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
77 if (ecx_import != NULL) {
80 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
81 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
82 && ecx_pub_der(ctx, ecxkey, out, cb, cbarg))
89 static int ecx_pub_der(void *vctx, void *ecxkey, BIO *out,
90 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
92 struct ecx_pub_ctx_st *ctx = vctx;
94 return ossl_prov_write_pub_der_from_obj(out, ecxkey,
95 ctx->type == ECX_KEY_TYPE_X25519
96 ? EVP_PKEY_X25519 : EVP_PKEY_X448,
98 ossl_prov_ecx_pub_to_der);
101 /* Public key : PEM */
102 static int ecx_pub_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
103 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
105 struct ecx_pub_ctx_st *ctx = vctx;
106 OSSL_OP_keymgmt_new_fn *ecx_new;
107 OSSL_OP_keymgmt_free_fn *ecx_free;
108 OSSL_OP_keymgmt_import_fn *ecx_import;
111 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
113 if (ecx_import != NULL) {
116 if ((ecxkey = ecx_new(ctx->provctx)) != NULL
117 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
118 && ecx_pub_pem(ctx, ecxkey, out, cb, cbarg))
125 static int ecx_pub_pem(void *vctx, void *ecxkey, BIO *out,
126 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
128 struct ecx_pub_ctx_st *ctx = vctx;
130 return ossl_prov_write_pub_pem_from_obj(out, ecxkey,
131 ctx->type == ECX_KEY_TYPE_X25519
132 ? EVP_PKEY_X25519 : EVP_PKEY_X448,
134 ossl_prov_ecx_pub_to_der);
138 static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
139 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
141 struct ecx_pub_ctx_st *ctx = vctx;
142 OSSL_OP_keymgmt_new_fn *ecx_new;
143 OSSL_OP_keymgmt_free_fn *ecx_free;
144 OSSL_OP_keymgmt_import_fn *ecx_import;
147 ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
149 if (ecx_import != NULL) {
152 if ((ecxkey = ecx_new(ctx)) != NULL
153 && ecx_import(ecxkey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
154 && ecx_pub_print(ctx, ecxkey, out, cb, cbarg))
161 static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out,
162 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
164 return ossl_prov_print_ecx(out, ecxkey, ecx_print_pub);
167 #define MAKE_SERIALIZER_FUNCTIONS(alg, type) \
168 const OSSL_DISPATCH alg##_pub_##type##_serializer_functions[] = { \
169 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))alg##_pub_newctx }, \
170 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ecx_pub_freectx }, \
171 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, \
172 (void (*)(void))ecx_pub_##type##_data }, \
173 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, \
174 (void (*)(void))ecx_pub_##type }, \
178 #define MAKE_SERIALIZER_FUNCTIONS_GROUP(alg) \
179 MAKE_SERIALIZER_FUNCTIONS(alg, der) \
180 MAKE_SERIALIZER_FUNCTIONS(alg, pem) \
181 MAKE_SERIALIZER_FUNCTIONS(alg, print)
183 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
184 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)