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