]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/serializers/serializer_dh_param.c
Rename OSSL_SERIALIZER / OSSL_DESERIALIZER to OSSL_ENCODE / OSSL_DECODE
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_dh_param.c
1 /*
2 * Copyright 2019-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 /*
11 * DH low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_dispatch.h>
17 #include <openssl/pem.h>
18 #include <openssl/dh.h>
19 #include <openssl/types.h>
20 #include <openssl/params.h>
21 #include "prov/bio.h"
22 #include "prov/implementations.h"
23 #include "prov/providercommonerr.h"
24 #include "prov/provider_ctx.h"
25 #include "serializer_local.h"
26
27 static OSSL_FUNC_serializer_newctx_fn dh_param_newctx;
28 static OSSL_FUNC_serializer_freectx_fn dh_param_freectx;
29 static OSSL_FUNC_serializer_serialize_data_fn dh_param_der_data;
30 static OSSL_FUNC_serializer_serialize_object_fn dh_param_der;
31 static OSSL_FUNC_serializer_serialize_data_fn dh_param_pem_data;
32 static OSSL_FUNC_serializer_serialize_object_fn dh_param_pem;
33
34 static OSSL_FUNC_serializer_serialize_data_fn dh_param_print_data;
35 static OSSL_FUNC_serializer_serialize_object_fn dh_param_print;
36
37 /* Parameters : context */
38
39 /*
40 * There's no specific implementation context, so we use the provider context
41 */
42 static void *dh_param_newctx(void *provctx)
43 {
44 return provctx;
45 }
46
47 static void dh_param_freectx(void *ctx)
48 {
49 }
50
51 /* Public key : DER */
52 static int dh_param_der_data(void *ctx, const OSSL_PARAM params[],
53 OSSL_CORE_BIO *out,
54 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
55 {
56 OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
57 OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
58 OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
59 int ok = 0;
60
61 if (dh_import != NULL) {
62 DH *dh;
63
64 /* ctx == provctx */
65 if ((dh = dh_new(ctx)) != NULL
66 && dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
67 && dh_param_der(ctx, dh, out, cb, cbarg))
68 ok = 1;
69 dh_free(dh);
70 }
71 return ok;
72 }
73
74 static int dh_param_der(void *ctx, void *dh, OSSL_CORE_BIO *cout,
75 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
76 {
77 BIO *out = bio_new_from_core_bio(ctx, cout);
78 int ret;
79
80 if (out == NULL)
81 return 0;
82 ret = i2d_DHparams_bio(out, dh);
83 BIO_free(out);
84
85 return ret;
86 }
87
88 /* Public key : PEM */
89 static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[],
90 OSSL_CORE_BIO *out,
91 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
92 {
93 OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
94 OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
95 OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
96 int ok = 0;
97
98 if (dh_import != NULL) {
99 DH *dh;
100
101 /* ctx == provctx */
102 if ((dh = dh_new(ctx)) != NULL
103 && dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
104 && dh_param_pem(ctx, dh, out, cb, cbarg))
105 ok = 1;
106 dh_free(dh);
107 }
108 return ok;
109 }
110
111 static int dh_param_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout,
112 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
113 {
114 BIO *out = bio_new_from_core_bio(ctx, cout);
115 int ret;
116
117 if (out == NULL)
118 return 0;
119
120 ret = PEM_write_bio_DHparams(out, dh);
121 BIO_free(out);
122
123 return ret;
124 }
125
126 static int dh_param_print_data(void *ctx, const OSSL_PARAM params[],
127 OSSL_CORE_BIO *out,
128 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
129 {
130 OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
131 OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
132 OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
133 int ok = 0;
134
135 if (dh_import != NULL) {
136 DH *dh;
137
138 /* ctx == provctx */
139 if ((dh = dh_new(ctx)) != NULL
140 && dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
141 && dh_param_print(ctx, dh, out, cb, cbarg))
142 ok = 1;
143 dh_free(dh);
144 }
145 return ok;
146 }
147
148 static int dh_param_print(void *ctx, void *dh, OSSL_CORE_BIO *cout,
149 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
150 {
151 BIO *out = bio_new_from_core_bio(ctx, cout);
152 int ret;
153
154 if (out == NULL)
155 return 0;
156
157 ret = ossl_prov_print_dh(out, dh, dh_print_params);
158 BIO_free(out);
159
160 return ret;
161 }
162
163 const OSSL_DISPATCH dh_param_der_serializer_functions[] = {
164 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
165 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
166 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_der_data },
167 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_der },
168 { 0, NULL }
169 };
170
171 const OSSL_DISPATCH dh_param_pem_serializer_functions[] = {
172 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
173 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
174 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_pem_data },
175 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_pem },
176 { 0, NULL }
177 };
178
179 const OSSL_DISPATCH dh_param_text_serializer_functions[] = {
180 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
181 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
182 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_print },
183 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
184 (void (*)(void))dh_param_print_data },
185 { 0, NULL }
186 };