]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_dh_param.c
Maintain strict type discipline between the core and providers
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_dh_param.c
CommitLineData
045e51cb 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
045e51cb
RL
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
ada66e78
P
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
045e51cb
RL
16#include <openssl/core_numbers.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"
d40b42ab 24#include "prov/provider_ctx.h"
045e51cb
RL
25#include "serializer_local.h"
26
27static OSSL_OP_serializer_newctx_fn dh_param_newctx;
28static OSSL_OP_serializer_freectx_fn dh_param_freectx;
29static OSSL_OP_serializer_serialize_data_fn dh_param_der_data;
30static OSSL_OP_serializer_serialize_object_fn dh_param_der;
31static OSSL_OP_serializer_serialize_data_fn dh_param_pem_data;
32static OSSL_OP_serializer_serialize_object_fn dh_param_pem;
33
34static OSSL_OP_serializer_serialize_data_fn dh_param_print_data;
35static OSSL_OP_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 */
42static void *dh_param_newctx(void *provctx)
43{
44 return provctx;
45}
46
47static void dh_param_freectx(void *ctx)
48{
49}
50
51/* Public key : DER */
d40b42ab
MC
52static int dh_param_der_data(void *ctx, const OSSL_PARAM params[],
53 OSSL_CORE_BIO *out,
045e51cb
RL
54 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
55{
32b0645c
RL
56 OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
57 OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
58 OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
045e51cb
RL
59 int ok = 0;
60
32b0645c
RL
61 if (dh_import != NULL) {
62 DH *dh;
045e51cb 63
32b0645c
RL
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);
045e51cb
RL
70 }
71 return ok;
72}
73
d40b42ab 74static int dh_param_der(void *ctx, void *dh, OSSL_CORE_BIO *cout,
045e51cb
RL
75 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
76{
d40b42ab
MC
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;
045e51cb
RL
86}
87
88/* Public key : PEM */
d40b42ab
MC
89static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[],
90 OSSL_CORE_BIO *out,
32b0645c 91 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 92{
32b0645c
RL
93 OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
94 OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
95 OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
045e51cb
RL
96 int ok = 0;
97
32b0645c
RL
98 if (dh_import != NULL) {
99 DH *dh;
045e51cb 100
32b0645c
RL
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);
045e51cb
RL
107 }
108 return ok;
109}
110
d40b42ab 111static int dh_param_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout,
045e51cb
RL
112 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
113{
d40b42ab
MC
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;
045e51cb
RL
124}
125
d40b42ab
MC
126static int dh_param_print_data(void *ctx, const OSSL_PARAM params[],
127 OSSL_CORE_BIO *out,
32b0645c 128 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
045e51cb 129{
32b0645c
RL
130 OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
131 OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
132 OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
045e51cb
RL
133 int ok = 0;
134
32b0645c
RL
135 if (dh_import != NULL) {
136 DH *dh;
045e51cb 137
32b0645c
RL
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);
045e51cb
RL
144 }
145 return ok;
146}
147
d40b42ab 148static int dh_param_print(void *ctx, void *dh, OSSL_CORE_BIO *cout,
045e51cb
RL
149 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
150{
d40b42ab
MC
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;
045e51cb
RL
161}
162
163const 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
171const 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
179const 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};