]>
Commit | Line | Data |
---|---|---|
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 | ||
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> | |
244bc297 | 15 | #include "crypto/ecx.h" |
8efc4a9c MC |
16 | #include "prov/bio.h" |
17 | #include "prov/implementations.h" | |
18 | #include "serializer_local.h" | |
19 | ||
20 | static OSSL_OP_serializer_newctx_fn x25519_pub_newctx; | |
21 | static OSSL_OP_serializer_newctx_fn x448_pub_newctx; | |
244bc297 MC |
22 | static OSSL_OP_serializer_newctx_fn ed25519_pub_newctx; |
23 | static OSSL_OP_serializer_newctx_fn ed448_pub_newctx; | |
8efc4a9c MC |
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; | |
29 | ||
30 | static OSSL_OP_serializer_serialize_data_fn ecx_pub_print_data; | |
31 | static OSSL_OP_serializer_serialize_object_fn ecx_pub_print; | |
32 | ||
33 | /* | |
34 | * Context used for public key serialization. | |
35 | */ | |
36 | struct ecx_pub_ctx_st { | |
37 | void *provctx; | |
38 | ECX_KEY_TYPE type; | |
39 | }; | |
40 | ||
41 | /* Public key : context */ | |
42 | static void *ecx_pub_newctx(void *provctx, ECX_KEY_TYPE type) | |
43 | { | |
44 | struct ecx_pub_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
45 | ||
46 | if (ctx != NULL) { | |
47 | ctx->provctx = provctx; | |
48 | ctx->type = type; | |
49 | } | |
50 | return ctx; | |
51 | } | |
52 | ||
53 | static void *x25519_pub_newctx(void *provctx) | |
54 | { | |
55 | return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X25519); | |
56 | } | |
57 | ||
58 | static void *x448_pub_newctx(void *provctx) | |
59 | { | |
60 | return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448); | |
61 | } | |
62 | ||
244bc297 MC |
63 | static void *ed25519_pub_newctx(void *provctx) |
64 | { | |
65 | return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED25519); | |
66 | } | |
67 | ||
68 | static void *ed448_pub_newctx(void *provctx) | |
69 | { | |
70 | return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED448); | |
71 | } | |
72 | ||
8efc4a9c MC |
73 | static void ecx_pub_freectx(void *ctx) |
74 | { | |
75 | OPENSSL_free(ctx); | |
76 | } | |
77 | ||
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) | |
81 | { | |
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; | |
86 | int ok = 0; | |
87 | ||
88 | ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import); | |
89 | ||
90 | if (ecx_import != NULL) { | |
91 | ECX_KEY *ecxkey; | |
92 | ||
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)) | |
96 | ok = 1; | |
97 | ecx_free(ecxkey); | |
98 | } | |
99 | return ok; | |
100 | } | |
101 | ||
102 | static int ecx_pub_der(void *vctx, void *ecxkey, BIO *out, | |
103 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
104 | { | |
105 | struct ecx_pub_ctx_st *ctx = vctx; | |
106 | ||
107 | return ossl_prov_write_pub_der_from_obj(out, ecxkey, | |
244bc297 | 108 | KEYTYPE2NID(ctx->type), |
8efc4a9c MC |
109 | NULL, |
110 | ossl_prov_ecx_pub_to_der); | |
111 | } | |
112 | ||
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) | |
116 | { | |
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; | |
121 | int ok = 0; | |
122 | ||
123 | ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import); | |
124 | ||
125 | if (ecx_import != NULL) { | |
126 | ECX_KEY *ecxkey; | |
127 | ||
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)) | |
131 | ok = 1; | |
132 | ecx_free(ecxkey); | |
133 | } | |
134 | return ok; | |
135 | } | |
136 | ||
137 | static int ecx_pub_pem(void *vctx, void *ecxkey, BIO *out, | |
138 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
139 | { | |
140 | struct ecx_pub_ctx_st *ctx = vctx; | |
141 | ||
142 | return ossl_prov_write_pub_pem_from_obj(out, ecxkey, | |
244bc297 | 143 | KEYTYPE2NID(ctx->type), |
8efc4a9c MC |
144 | NULL, |
145 | ossl_prov_ecx_pub_to_der); | |
146 | ||
147 | } | |
148 | ||
149 | static int ecx_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out, | |
150 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
151 | { | |
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; | |
156 | int ok = 0; | |
157 | ||
158 | ecx_get_new_free_import(ctx->type, &ecx_new, &ecx_free, &ecx_import); | |
159 | ||
160 | if (ecx_import != NULL) { | |
161 | ECX_KEY *ecxkey; | |
162 | ||
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)) | |
166 | ok = 1; | |
167 | ecx_free(ecxkey); | |
168 | } | |
169 | return ok; | |
170 | } | |
171 | ||
172 | static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out, | |
173 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
174 | { | |
175 | return ossl_prov_print_ecx(out, ecxkey, ecx_print_pub); | |
176 | } | |
177 | ||
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 }, \ | |
186 | { 0, NULL } \ | |
187 | }; | |
188 | ||
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) | |
193 | ||
194 | MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519) | |
195 | MAKE_SERIALIZER_FUNCTIONS_GROUP(x448) | |
244bc297 MC |
196 | MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519) |
197 | MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448) |