]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/encoder_ec_param.c
Rename OSSL_SERIALIZER / OSSL_DESERIALIZER to OSSL_ENCODE / OSSL_DECODE
[thirdparty/openssl.git] / providers / implementations / encode_decode / encoder_ec_param.c
CommitLineData
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/pem.h>
12#include <openssl/ec.h>
13#include <openssl/types.h>
14#include <openssl/params.h>
15#include "prov/bio.h"
16#include "prov/implementations.h"
17#include "prov/providercommonerr.h"
d40b42ab 18#include "prov/provider_ctx.h"
ece9304c 19#include "encoder_local.h"
f552d900 20
ece9304c
RL
21static OSSL_FUNC_encoder_newctx_fn ec_param_newctx;
22static OSSL_FUNC_encoder_freectx_fn ec_param_freectx;
23static OSSL_FUNC_encoder_encode_data_fn ec_param_der_data;
24static OSSL_FUNC_encoder_encode_object_fn ec_param_der;
25static OSSL_FUNC_encoder_encode_data_fn ec_param_pem_data;
26static OSSL_FUNC_encoder_encode_object_fn ec_param_pem;
f552d900 27
ece9304c
RL
28static OSSL_FUNC_encoder_encode_data_fn ec_param_print_data;
29static OSSL_FUNC_encoder_encode_object_fn ec_param_print;
f552d900
SL
30
31
32/* There is no specific implementation context, so use the provider context */
33static void *ec_param_newctx(void *provctx)
34{
35 return provctx;
36}
37
38static void ec_param_freectx(void *vctx)
39{
40}
41
42/* Public key : DER */
d40b42ab
MC
43static int ec_param_der_data(void *vctx, const OSSL_PARAM params[],
44 OSSL_CORE_BIO *out,
45 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
f552d900 46{
363b1e5d
DMSP
47 OSSL_FUNC_keymgmt_new_fn *ec_new;
48 OSSL_FUNC_keymgmt_free_fn *ec_free;
49 OSSL_FUNC_keymgmt_import_fn *ec_import;
f552d900
SL
50 int ok = 0;
51
52 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
53
54 if (ec_import != NULL) {
55 EC_KEY *eckey;
56
57 /* vctx == provctx */
58 if ((eckey = ec_new(vctx)) != NULL
59 && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
60 && ec_param_der(vctx, eckey, out, cb, cbarg))
61 ok = 1;
62 ec_free(eckey);
63 }
64 return ok;
65}
66
d40b42ab 67static int ec_param_der(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
ece9304c 68 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
f552d900 69{
d40b42ab
MC
70 BIO *out = bio_new_from_core_bio(vctx, cout);
71 int ret;
72
73 if (out == NULL)
74 return 0;
75
76 ret = i2d_ECPKParameters_bio(out, EC_KEY_get0_group(eckey));
77 BIO_free(out);
78
79 return ret;
f552d900
SL
80}
81
82/* Public key : PEM */
d40b42ab
MC
83static int ec_param_pem_data(void *vctx, const OSSL_PARAM params[],
84 OSSL_CORE_BIO *out,
85 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
f552d900 86{
363b1e5d
DMSP
87 OSSL_FUNC_keymgmt_new_fn *ec_new;
88 OSSL_FUNC_keymgmt_free_fn *ec_free;
89 OSSL_FUNC_keymgmt_import_fn *ec_import;
f552d900
SL
90 int ok = 0;
91
92 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
93
94 if (ec_import != NULL) {
95 EC_KEY *eckey;
96
97 /* vctx == provctx */
98 if ((eckey = ec_new(vctx)) != NULL
99 && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
100 && ec_param_pem(vctx, eckey, out, cb, cbarg))
101 ok = 1;
102 ec_free(eckey);
103 }
104 return ok;
105}
106
d40b42ab 107static int ec_param_pem(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
ece9304c 108 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
f552d900 109{
d40b42ab
MC
110 BIO *out = bio_new_from_core_bio(vctx, cout);
111 int ret;
112
113 if (out == NULL)
114 return 0;
115
116 ret = PEM_write_bio_ECPKParameters(out, EC_KEY_get0_group(eckey));
117 BIO_free(out);
118
119 return ret;
f552d900
SL
120}
121
d40b42ab
MC
122static int ec_param_print_data(void *vctx, const OSSL_PARAM params[],
123 OSSL_CORE_BIO *out,
124 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
f552d900 125{
363b1e5d
DMSP
126 OSSL_FUNC_keymgmt_new_fn *ec_new;
127 OSSL_FUNC_keymgmt_free_fn *ec_free;
128 OSSL_FUNC_keymgmt_import_fn *ec_import;
f552d900
SL
129 int ok = 0;
130
131 ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
132
133 if (ec_import != NULL) {
134 EC_KEY *eckey;
135
136 /* vctx == provctx */
137 if ((eckey = ec_new(vctx)) != NULL
138 && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
139 && ec_param_print(vctx, eckey, out, cb, cbarg))
140 ok = 1;
141 ec_free(eckey);
142 }
143 return ok;
144}
145
d40b42ab 146static int ec_param_print(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
ece9304c 147 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
f552d900 148{
d40b42ab
MC
149 BIO *out = bio_new_from_core_bio(vctx, cout);
150 int ret;
151
152 if (out == NULL)
153 return 0;
154
155 ret = ossl_prov_print_eckey(out, eckey, ec_print_params);
156 BIO_free(out);
157
158 return ret;
f552d900
SL
159}
160
ece9304c
RL
161const OSSL_DISPATCH ec_param_der_encoder_functions[] = {
162 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_param_newctx },
163 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_param_freectx },
164 { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))ec_param_der_data },
165 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_param_der },
f552d900
SL
166 { 0, NULL }
167};
168
ece9304c
RL
169const OSSL_DISPATCH ec_param_pem_encoder_functions[] = {
170 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_param_newctx },
171 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_param_freectx },
172 { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))ec_param_pem_data },
173 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_param_pem },
f552d900
SL
174 { 0, NULL }
175};
176
ece9304c
RL
177const OSSL_DISPATCH ec_param_text_encoder_functions[] = {
178 { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_param_newctx },
179 { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_param_freectx },
180 { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_param_print },
181 { OSSL_FUNC_ENCODER_ENCODE_DATA,
f552d900
SL
182 (void (*)(void))ec_param_print_data },
183 { 0, NULL }
184};