1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/*
* DH low level APIs are deprecated for public use, but still ok for
* internal use.
*/
#include "internal/deprecated.h"
#include <openssl/core_numbers.h>
#include <openssl/pem.h>
#include <openssl/dh.h>
#include <openssl/types.h>
#include <openssl/params.h>
#include "prov/bio.h"
#include "prov/implementations.h"
#include "prov/providercommonerr.h"
#include "prov/provider_ctx.h"
#include "serializer_local.h"
static OSSL_OP_serializer_newctx_fn dh_param_newctx;
static OSSL_OP_serializer_freectx_fn dh_param_freectx;
static OSSL_OP_serializer_serialize_data_fn dh_param_der_data;
static OSSL_OP_serializer_serialize_object_fn dh_param_der;
static OSSL_OP_serializer_serialize_data_fn dh_param_pem_data;
static OSSL_OP_serializer_serialize_object_fn dh_param_pem;
static OSSL_OP_serializer_serialize_data_fn dh_param_print_data;
static OSSL_OP_serializer_serialize_object_fn dh_param_print;
/* Parameters : context */
/*
* There's no specific implementation context, so we use the provider context
*/
static void *dh_param_newctx(void *provctx)
{
return provctx;
}
static void dh_param_freectx(void *ctx)
{
}
/* Public key : DER */
static int dh_param_der_data(void *ctx, const OSSL_PARAM params[],
OSSL_CORE_BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;
if (dh_import != NULL) {
DH *dh;
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
&& dh_param_der(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
static int dh_param_der(void *ctx, void *dh, OSSL_CORE_BIO *cout,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
BIO *out = bio_new_from_core_bio(ctx, cout);
int ret;
if (out == NULL)
return 0;
ret = i2d_DHparams_bio(out, dh);
BIO_free(out);
return ret;
}
/* Public key : PEM */
static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[],
OSSL_CORE_BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;
if (dh_import != NULL) {
DH *dh;
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
&& dh_param_pem(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
static int dh_param_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
BIO *out = bio_new_from_core_bio(ctx, cout);
int ret;
if (out == NULL)
return 0;
ret = PEM_write_bio_DHparams(out, dh);
BIO_free(out);
return ret;
}
static int dh_param_print_data(void *ctx, const OSSL_PARAM params[],
OSSL_CORE_BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
OSSL_OP_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
OSSL_OP_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
OSSL_OP_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
int ok = 0;
if (dh_import != NULL) {
DH *dh;
/* ctx == provctx */
if ((dh = dh_new(ctx)) != NULL
&& dh_import(dh, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
&& dh_param_print(ctx, dh, out, cb, cbarg))
ok = 1;
dh_free(dh);
}
return ok;
}
static int dh_param_print(void *ctx, void *dh, OSSL_CORE_BIO *cout,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
BIO *out = bio_new_from_core_bio(ctx, cout);
int ret;
if (out == NULL)
return 0;
ret = ossl_prov_print_dh(out, dh, dh_print_params);
BIO_free(out);
return ret;
}
const OSSL_DISPATCH dh_param_der_serializer_functions[] = {
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_der_data },
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_der },
{ 0, NULL }
};
const OSSL_DISPATCH dh_param_pem_serializer_functions[] = {
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_pem_data },
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_pem },
{ 0, NULL }
};
const OSSL_DISPATCH dh_param_text_serializer_functions[] = {
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_print },
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
(void (*)(void))dh_param_print_data },
{ 0, NULL }
};
|