Implement the ECX Serializers
[oweals/openssl.git] / providers / implementations / serializers / serializer_ecx_pub.c
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
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 "prov/bio.h"
16 #include "prov/implementations.h"
17 #include "serializer_local.h"
18
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;
26
27 static OSSL_OP_serializer_serialize_data_fn ecx_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn ecx_pub_print;
29
30 /*
31  * Context used for public key serialization.
32  */
33 struct ecx_pub_ctx_st {
34     void *provctx;
35     ECX_KEY_TYPE type;
36 };
37
38 /* Public key : context */
39 static void *ecx_pub_newctx(void *provctx, ECX_KEY_TYPE type)
40 {
41     struct ecx_pub_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
42
43     if (ctx != NULL) {
44         ctx->provctx = provctx;
45         ctx->type = type;
46     }
47     return ctx;
48 }
49
50 static void *x25519_pub_newctx(void *provctx)
51 {
52     return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X25519);
53 }
54
55 static void *x448_pub_newctx(void *provctx)
56 {
57     return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448);
58 }
59
60 static void ecx_pub_freectx(void *ctx)
61 {
62     OPENSSL_free(ctx);
63 }
64
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)
68 {
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;
73     int ok = 0;
74
75     ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
76
77     if (ecx_import != NULL) {
78         ECX_KEY *ecxkey;
79
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))
83             ok = 1;
84         ecx_free(ecxkey);
85     }
86     return ok;
87 }
88
89 static int ecx_pub_der(void *vctx, void *ecxkey, BIO *out,
90                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
91 {
92     struct ecx_pub_ctx_st *ctx = vctx;
93
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,
97                                             NULL,
98                                             ossl_prov_ecx_pub_to_der);
99 }
100
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)
104 {
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;
109     int ok = 0;
110
111     ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
112
113     if (ecx_import != NULL) {
114         ECX_KEY *ecxkey;
115
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))
119             ok = 1;
120         ecx_free(ecxkey);
121     }
122     return ok;
123 }
124
125 static int ecx_pub_pem(void *vctx, void *ecxkey, BIO *out,
126                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
127 {
128     struct ecx_pub_ctx_st *ctx = vctx;
129
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,
133                                             NULL,
134                                             ossl_prov_ecx_pub_to_der);
135
136 }
137
138 static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
139                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
140 {
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;
145     int ok = 0;
146
147     ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import);
148
149     if (ecx_import != NULL) {
150         ECX_KEY *ecxkey;
151
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))
155             ok = 1;
156         ecx_free(ecxkey);
157     }
158     return ok;
159 }
160
161 static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out,
162                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
163 {
164     return ossl_prov_print_ecx(out, ecxkey, ecx_print_pub);
165 }
166
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 }, \
175         { 0, NULL } \
176     };
177
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)
182
183 MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
184 MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)