]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/serializers/serializer_ec_pub.c
d3f67fd7628866b1e75059da9f388ec44605f44d
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_ec_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_dispatch.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 "prov/provider_ctx.h"
18 #include "serializer_local.h"
19
20 static OSSL_FUNC_serializer_newctx_fn ec_pub_newctx;
21 static OSSL_FUNC_serializer_freectx_fn ec_pub_freectx;
22 static OSSL_FUNC_serializer_serialize_data_fn ec_pub_der_data;
23 static OSSL_FUNC_serializer_serialize_object_fn ec_pub_der;
24 static OSSL_FUNC_serializer_serialize_data_fn ec_pub_pem_data;
25 static OSSL_FUNC_serializer_serialize_object_fn ec_pub_pem;
26
27 static OSSL_FUNC_serializer_serialize_data_fn ec_pub_print_data;
28 static OSSL_FUNC_serializer_serialize_object_fn ec_pub_print;
29
30 /* Public key : context */
31
32 /*
33 * There's no specific implementation context, so we use the provider context
34 */
35 static void *ec_pub_newctx(void *provctx)
36 {
37 return provctx;
38 }
39
40 static void ec_pub_freectx(void *ctx)
41 {
42 }
43
44 /* Public key : DER */
45 static int ec_pub_der_data(void *vctx, const OSSL_PARAM params[],
46 OSSL_CORE_BIO *out,
47 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
48 {
49 OSSL_FUNC_keymgmt_new_fn *ec_new;
50 OSSL_FUNC_keymgmt_free_fn *ec_free;
51 OSSL_FUNC_keymgmt_import_fn *ec_import;
52 int ok = 0;
53
54 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
55
56 if (ec_import != NULL) {
57 EC_KEY *eckey;
58
59 /* vctx == provctx */
60 if ((eckey = ec_new(vctx)) != NULL
61 && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
62 && ec_pub_der(vctx, eckey, out, cb, cbarg))
63 ok = 1;
64 ec_free(eckey);
65 }
66 return ok;
67 }
68
69 static int ec_pub_der(void *ctx, void *eckey, OSSL_CORE_BIO *cout,
70 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
71 {
72 BIO *out = bio_new_from_core_bio(ctx, cout);
73 int ret;
74
75 if (out == NULL)
76 return 0;
77
78 ret = ossl_prov_write_pub_der_from_obj(out, eckey, EVP_PKEY_EC,
79 ossl_prov_prepare_ec_params,
80 ossl_prov_ec_pub_to_der);
81 BIO_free(out);
82
83 return ret;
84 }
85
86 /* Public key : PEM */
87 static int ec_pub_pem_data(void *vctx, const OSSL_PARAM params[],
88 OSSL_CORE_BIO *out,
89 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
90 {
91 OSSL_FUNC_keymgmt_new_fn *ec_new;
92 OSSL_FUNC_keymgmt_free_fn *ec_free;
93 OSSL_FUNC_keymgmt_import_fn *ec_import;
94 int ok = 0;
95
96 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
97
98 if (ec_import != NULL) {
99 EC_KEY *eckey;
100
101 /* ctx == provctx */
102 if ((eckey = ec_new(vctx)) != NULL
103 && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
104 && ec_pub_pem(vctx, eckey, out, cb, cbarg))
105 ok = 1;
106 ec_free(eckey);
107 }
108 return ok;
109 }
110
111 static int ec_pub_pem(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
112 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
113 {
114 BIO *out = bio_new_from_core_bio(vctx, cout);
115 int ret;
116
117 if (out == NULL)
118 return 0;
119
120 ret = ossl_prov_write_pub_pem_from_obj(out, eckey, EVP_PKEY_EC,
121 ossl_prov_prepare_ec_params,
122 ossl_prov_ec_pub_to_der);
123 BIO_free(out);
124
125 return ret;
126 }
127
128 static int ec_pub_print_data(void *vctx, const OSSL_PARAM params[],
129 OSSL_CORE_BIO *out,
130 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
131 {
132 OSSL_FUNC_keymgmt_new_fn *ec_new;
133 OSSL_FUNC_keymgmt_free_fn *ec_free;
134 OSSL_FUNC_keymgmt_import_fn *ec_import;
135 int ok = 0;
136
137 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
138
139 if (ec_import != NULL) {
140 EC_KEY *eckey;
141
142 /* ctx == provctx */
143 if ((eckey = ec_new(vctx)) != NULL
144 && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
145 && ec_pub_print(vctx, eckey, out, cb, cbarg))
146 ok = 1;
147 ec_free(eckey);
148 }
149 return ok;
150 }
151
152 static int ec_pub_print(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
153 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
154 {
155 BIO *out = bio_new_from_core_bio(vctx, cout);
156 int ret;
157
158 if (out == NULL)
159 return 0;
160
161 ret = ossl_prov_print_eckey(out, eckey, ec_print_pub);
162 BIO_free(out);
163
164 return ret;
165 }
166
167 const OSSL_DISPATCH ec_pub_der_serializer_functions[] = {
168 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
169 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
170 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_der_data },
171 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_der },
172 { 0, NULL }
173 };
174
175 const OSSL_DISPATCH ec_pub_pem_serializer_functions[] = {
176 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
177 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
178 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_pem_data },
179 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_pem },
180 { 0, NULL }
181 };
182
183 const OSSL_DISPATCH ec_pub_text_serializer_functions[] = {
184 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
185 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
186 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_print },
187 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
188 (void (*)(void))ec_pub_print_data },
189 { 0, NULL }
190 };