Rename OSSL_SERIALIZER / OSSL_DESERIALIZER to OSSL_ENCODE / OSSL_DECODE
[openssl.git] / providers / implementations / encode_decode / encoder_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 "encoder_local.h"
19
20 #define EC_SELECT_PUBLIC_IMPORTABLE                                            \
21     OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
22
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;
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 */
47 static int ec_pub_der_data(void *vctx, const OSSL_PARAM params[],
48                            OSSL_CORE_BIO *out,
49                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
50 {
51     OSSL_FUNC_keymgmt_new_fn *ec_new;
52     OSSL_FUNC_keymgmt_free_fn *ec_free;
53     OSSL_FUNC_keymgmt_import_fn *ec_import;
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
63             && ec_import(eckey, EC_SELECT_PUBLIC_IMPORTABLE, params)
64             && ec_pub_der(vctx, eckey, out, cb, cbarg))
65             ok = 1;
66         ec_free(eckey);
67     }
68     return ok;
69 }
70
71 static int ec_pub_der(void *ctx, void *eckey, OSSL_CORE_BIO *cout,
72                       OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
73 {
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,
81                                            ossl_prov_prepare_ec_params,
82                                            ossl_prov_ec_pub_to_der);
83     BIO_free(out);
84
85     return ret;
86 }
87
88 /* Public key : PEM */
89 static int ec_pub_pem_data(void *vctx, const OSSL_PARAM params[],
90                            OSSL_CORE_BIO *out,
91                            OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
92 {
93     OSSL_FUNC_keymgmt_new_fn *ec_new;
94     OSSL_FUNC_keymgmt_free_fn *ec_free;
95     OSSL_FUNC_keymgmt_import_fn *ec_import;
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
105             && ec_import(eckey, EC_SELECT_PUBLIC_IMPORTABLE, params)
106             && ec_pub_pem(vctx, eckey, out, cb, cbarg))
107             ok = 1;
108         ec_free(eckey);
109     }
110     return ok;
111 }
112
113 static int ec_pub_pem(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
114                       OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
115 {
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;
128 }
129
130 static int ec_pub_print_data(void *vctx, const OSSL_PARAM params[],
131                              OSSL_CORE_BIO *out,
132                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
133 {
134     OSSL_FUNC_keymgmt_new_fn *ec_new;
135     OSSL_FUNC_keymgmt_free_fn *ec_free;
136     OSSL_FUNC_keymgmt_import_fn *ec_import;
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
146             && ec_import(eckey, EC_SELECT_PUBLIC_IMPORTABLE, params)
147             && ec_pub_print(vctx, eckey, out, cb, cbarg))
148             ok = 1;
149         ec_free(eckey);
150     }
151     return ok;
152 }
153
154 static int ec_pub_print(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
155                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
156 {
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;
167 }
168
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 },
174     { 0, NULL }
175 };
176
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 },
182     { 0, NULL }
183 };
184
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,
190       (void (*)(void))ec_pub_print_data },
191     { 0, NULL }
192 };