-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathgost_prov_decoder.c
More file actions
317 lines (262 loc) · 9.7 KB
/
gost_prov_decoder.c
File metadata and controls
317 lines (262 loc) · 9.7 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <openssl/core_names.h>
#include <openssl/core_object.h>
#include "gost_prov.h"
#include "gost_lcl.h"
#include <openssl/asn1t.h>
#include <ctype.h>
/*
* Forward declarations of all generic OSSL_DISPATCH functions, to make sure
* they are correctly defined further down. For the structure specific ones
* MAKE_DECODER_FUNCTIONS() does it for us.
*/
static OSSL_FUNC_decoder_newctx_fn decoder_newctx;
static OSSL_FUNC_decoder_freectx_fn decoder_freectx;
typedef struct {
PROV_CTX *provctx;
} GOST_DECODER_CTX;
typedef int (st2ec_fn)(EC_KEY *ec, int *key_type, const void *st);
typedef void *(bio2st_fn)(BIO *bio, void **st);
typedef void (st_free_fn)(void *st);
typedef struct {
X509_ALGOR *algor;
ASN1_BIT_STRING *public_key;
} x509_pubkey_st;
typedef struct {
int key_type;
unsigned char *data;
long int data_size;
} params_st;
ASN1_NDEF_SEQUENCE(x509_pubkey_st) = {
ASN1_SIMPLE(x509_pubkey_st, algor, X509_ALGOR),
ASN1_SIMPLE(x509_pubkey_st, public_key, ASN1_BIT_STRING)
} ASN1_NDEF_SEQUENCE_END(x509_pubkey_st)
IMPLEMENT_ASN1_FUNCTIONS(x509_pubkey_st)
static st2ec_fn pkcs8_decode_wrapper;
static bio2st_fn pkcs8_read_bio_der_wrapper;
static st_free_fn pkcs8_free_wrapper;
static st2ec_fn x509_pub_decode_wrapper;
static bio2st_fn x509_pub_read_bio_der_wrapper;
static st_free_fn x509_pub_free_wrapper;
static st2ec_fn param_decode_wrapper;
static bio2st_fn param_read_bio_pem_wrapper;
static st_free_fn param_free_wrapper;
static int pkcs8_decode_wrapper(EC_KEY *ec, int *key_type, const void *st)
{
return internal_priv_decode(ec, key_type, (PKCS8_PRIV_KEY_INFO *)st);
}
static void *pkcs8_read_bio_der_wrapper(BIO *bio, void **st)
{
return d2i_PKCS8_PRIV_KEY_INFO_bio(bio, (PKCS8_PRIV_KEY_INFO **)st);
}
static void pkcs8_free_wrapper(void *st)
{
PKCS8_PRIV_KEY_INFO_free((PKCS8_PRIV_KEY_INFO *)st);
}
static int x509_pub_decode_wrapper(EC_KEY *ec, int *key_type, const void *st)
{
return internal_pub_decode_ec(ec, key_type,
((x509_pubkey_st *)st)->algor,
((x509_pubkey_st *)st)->public_key->data,
((x509_pubkey_st *)st)->public_key->length);
}
static void *x509_pub_read_bio_der_wrapper(BIO *bio, void **st)
{
unsigned char *data = NULL;
size_t dlen;
dlen = BIO_get_mem_data(bio, &data);
if (!dlen)
return 0;
return d2i_x509_pubkey_st((x509_pubkey_st **)st, (const unsigned char **) &data, dlen);
}
static void x509_pub_free_wrapper(void *st)
{
x509_pubkey_st_free((x509_pubkey_st *)st);
}
static int param_decode_wrapper(EC_KEY *ec, int *key_type, const void *st)
{
const params_st *params = (const params_st *)st;
const unsigned char *pdata = params->data;
if (!internal_gost2001_param_decode(ec, &pdata, params->data_size))
return 0;
*key_type = params->key_type;
return 1;
}
static int get_key_type_from_pem_name(char *pem_name)
{
if (!pem_name)
return NID_undef;
char *space_pos = strchr(pem_name, ' ');
if (space_pos)
*space_pos = '\0';
size_t i;
for (i = 0; pem_name[i]; ++i)
pem_name[i] = tolower((unsigned char)pem_name[i]);
return OBJ_sn2nid(pem_name);
}
static void *param_read_bio_pem_wrapper(BIO *bio, void **st)
{
params_st *params = NULL;
char *name = NULL;
char *header = NULL;
params = OPENSSL_zalloc(sizeof(params_st));
if (!params)
goto exit;
int len = PEM_read_bio(bio, &name, &header,
¶ms->data,
¶ms->data_size);
if (len <= 0 || name == NULL)
goto exit;
params->key_type = get_key_type_from_pem_name(name);
if (params->key_type == NID_undef)
goto exit;
exit:
OPENSSL_free(name);
OPENSSL_free(header);
*st = params;
return *st;
}
static void param_free_wrapper(void *st)
{
params_st *params = (params_st *)st;
if (!params)
return;
OPENSSL_free(params->data);
OPENSSL_free(params);
}
static void *decoder_newctx(void *provctx)
{
if (!provctx)
return NULL;
GOST_DECODER_CTX *ctx = OPENSSL_zalloc(sizeof(GOST_DECODER_CTX));
if (!ctx)
return NULL;
ctx->provctx = provctx;
return ctx;
}
static void decoder_freectx(void *ctx)
{
GOST_DECODER_CTX *ectx = ctx;
OPENSSL_free(ectx);
}
static int decoder_does_selection(int selection, int selection_mask)
{
return FLAGS_INTERSECT(selection, selection_mask);
}
static int decoder_decode(void *ctx, OSSL_CORE_BIO *cbio, int selection,
int selection_mask, OSSL_CALLBACK *data_cb,
void *data_cdarg, bio2st_fn *bio2st,
st2ec_fn *st2ec, st_free_fn *st_free)
{
GOST_DECODER_CTX *dctx = NULL;
GOST_KEY_DATA *key_data = NULL;
BIO *bio = NULL;
void *st = NULL;
int ret = 1;
if (!FLAGS_INTERSECT(selection, selection_mask))
goto exit;
if (!cbio || !data_cb)
goto exit;
dctx = ctx;
if (!dctx || !dctx->provctx || !dctx->provctx->libctx)
goto exit;
bio = BIO_new_from_core_bio(dctx->provctx->libctx, cbio);
if (!bio)
goto exit;
if (!bio2st(bio, &st))
goto exit;
key_data = OPENSSL_zalloc(sizeof(GOST_KEY_DATA));
if (!key_data) {
ret = 0;
goto exit;
}
key_data->ec = EC_KEY_new();
if (!key_data->ec) {
ret = 0;
goto exit;
}
if (!st2ec(key_data->ec, &key_data->type, st))
goto exit;
key_data->param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_data->ec));
if (key_data->param_nid == NID_undef) {
ret = 0;
goto exit;
}
OSSL_PARAM params[4];
int object_type = OSSL_OBJECT_PKEY;
params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
(char *)OBJ_nid2sn(key_data->type), 0);
params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
&key_data, sizeof(key_data));
params[3] = OSSL_PARAM_construct_end();
ret = data_cb(params, data_cdarg);
exit:
keymgmt_free(key_data);
BIO_free(bio);
st_free(st);
return ret;
}
typedef void (*fptr_t)(void);
#define MAKE_DECODER_FUNCTIONS(input, structure, bio2st, st2ec, st_free, selection_mask) \
static OSSL_FUNC_decoder_decode_fn input##_##structure##_decoder_decode; \
static int input##_##structure##_decoder_decode(void *ctx, OSSL_CORE_BIO *cbio, \
int selection, \
OSSL_CALLBACK *data_cb, \
void *data_cdarg, \
OSSL_PASSPHRASE_CALLBACK *cb, \
void *cbarg) \
{ \
return decoder_decode(ctx, cbio, selection, selection_mask, data_cb, data_cdarg, \
bio2st, st2ec, st_free); \
} \
static OSSL_FUNC_decoder_does_selection_fn \
input##_##structure##_decoder_does_selection; \
static int input##_##structure##_decoder_does_selection(void *ctx, \
int selection) \
{ \
return decoder_does_selection(selection, selection_mask); \
} \
static const OSSL_DISPATCH id_##input##_##structure##_decoder_functions[] = { \
{ OSSL_FUNC_DECODER_NEWCTX, (fptr_t)decoder_newctx }, \
{ OSSL_FUNC_DECODER_FREECTX, (fptr_t)decoder_freectx }, \
{ OSSL_FUNC_DECODER_DOES_SELECTION, \
(fptr_t)input##_##structure##_decoder_does_selection }, \
{ OSSL_FUNC_DECODER_DECODE, (fptr_t)input##_##structure##_decoder_decode }, \
{ 0, NULL } \
};
#define DECODER(decoder_name, input, structure) \
{ \
decoder_name, \
"provider=gostprov,input=" #input ",structure=" #structure, \
(id_##input##_##structure##_decoder_functions) \
}
MAKE_DECODER_FUNCTIONS(der, PrivateKeyInfo, pkcs8_read_bio_der_wrapper,
pkcs8_decode_wrapper, pkcs8_free_wrapper,
OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
MAKE_DECODER_FUNCTIONS(der, SubjectPublicKeyInfo, x509_pub_read_bio_der_wrapper,
x509_pub_decode_wrapper, x509_pub_free_wrapper,
OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
MAKE_DECODER_FUNCTIONS(pem, type_specific, param_read_bio_pem_wrapper,
param_decode_wrapper, param_free_wrapper,
OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
/*
* Each algorithm of PRIVATE KEYS (PrivateKeyInfo) and PUBLIC KEYS (SubjectPublicKeyInfo)
* is registered separately because OpenSSL extracts the algorithm OID from ASN.1
* structure and directly maps it to the specific decoder (no iteration needed).
*
* Decoding of the keys from PEM to DER happens in default provider, while the key
* parameters (type_specific) are decoded directly from PEM since the PEM header
* is crucial for the algorithm identification.
*/
const OSSL_ALGORITHM GOST_prov_decoder[] = {
DECODER(ALG_NAME_GOST2001, der, SubjectPublicKeyInfo),
DECODER(ALG_NAME_GOST2001, pem, type_specific), /* Decode domain parameters */
DECODER(ALG_NAME_GOST2001DH, der, SubjectPublicKeyInfo),
DECODER(ALG_NAME_GOST2012_256, der, SubjectPublicKeyInfo),
DECODER(ALG_NAME_GOST2012_512, der, SubjectPublicKeyInfo),
DECODER(ALG_NAME_GOST2001, der, PrivateKeyInfo),
DECODER(ALG_NAME_GOST2001DH, der, PrivateKeyInfo),
DECODER(ALG_NAME_GOST2012_256, der, PrivateKeyInfo),
DECODER(ALG_NAME_GOST2012_512, der, PrivateKeyInfo),
{ NULL, NULL, NULL }
};