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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/*
* Copyright 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
*/
#include <openssl/core_names.h>
#include <openssl/bio.h>
#include <openssl/params.h>
#include <openssl/provider.h>
#include "serializer_local.h"
#include "e_os.h"
struct deser_process_data_st {
OSSL_DESERIALIZER_CTX *ctx;
/* Current BIO */
BIO *bio;
/* Index of the current deserializer instance to be processed */
size_t current_deser_inst_index;
};
static int deser_process(const OSSL_PARAM params[], void *arg);
int OSSL_DESERIALIZER_from_bio(OSSL_DESERIALIZER_CTX *ctx, BIO *in)
{
struct deser_process_data_st data;
int ok = 0;
memset(&data, 0, sizeof(data));
data.ctx = ctx;
data.bio = in;
ok = deser_process(NULL, &data);
/* Clear any internally cached passphrase */
if (!ctx->flag_user_passphrase) {
OSSL_DESERIALIZER_CTX_set_passphrase(ctx, NULL, 0);
ctx->flag_user_passphrase = 0;
}
return ok;
}
#ifndef OPENSSL_NO_STDIO
static BIO *bio_from_file(FILE *fp)
{
BIO *b;
if ((b = BIO_new(BIO_s_file())) == NULL) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_BIO_LIB);
return NULL;
}
BIO_set_fp(b, fp, BIO_NOCLOSE);
return b;
}
int OSSL_DESERIALIZER_from_fp(OSSL_DESERIALIZER_CTX *ctx, FILE *fp)
{
BIO *b = bio_from_file(fp);
int ret = 0;
if (b != NULL)
ret = OSSL_DESERIALIZER_from_bio(ctx, b);
BIO_free(b);
return ret;
}
#endif
int OSSL_DESERIALIZER_CTX_set_input_type(OSSL_DESERIALIZER_CTX *ctx,
const char *input_type)
{
if (!ossl_assert(ctx != NULL)) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
/*
* NULL is a valid starting input type, and means that the caller leaves
* it to code to discover what the starting input type is.
*/
ctx->start_input_type = input_type;
return 1;
}
int OSSL_DESERIALIZER_CTX_add_deserializer(OSSL_DESERIALIZER_CTX *ctx,
OSSL_DESERIALIZER *deser)
{
OSSL_DESERIALIZER_INSTANCE *deser_inst = NULL;
const OSSL_PROVIDER *prov = NULL;
OSSL_PARAM params[2];
void *provctx = NULL;
if (!ossl_assert(ctx != NULL) || !ossl_assert(deser != NULL)) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (deser->get_params == NULL) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER,
OSSL_DESERIALIZER_R_MISSING_GET_PARAMS);
return 0;
}
if (ctx->deser_insts == NULL
&& (ctx->deser_insts =
sk_OSSL_DESERIALIZER_INSTANCE_new_null()) == NULL) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_MALLOC_FAILURE);
return 0;
}
if ((deser_inst = OPENSSL_zalloc(sizeof(*deser_inst))) == NULL) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!OSSL_DESERIALIZER_up_ref(deser)) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_INTERNAL_ERROR);
goto err;
}
deser_inst->deser = deser;
prov = OSSL_DESERIALIZER_provider(deser_inst->deser);
provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
/* Cache the input type for this serializer */
params[0] =
OSSL_PARAM_construct_utf8_ptr(OSSL_DESERIALIZER_PARAM_INPUT_TYPE,
(char **)&deser_inst->input_type, 0);
params[1] = OSSL_PARAM_construct_end();
if (!deser_inst->deser->get_params(params)
|| !OSSL_PARAM_modified(¶ms[0]))
goto err;
if ((deser_inst->deserctx = deser_inst->deser->newctx(provctx))
== NULL)
goto err;
if (sk_OSSL_DESERIALIZER_INSTANCE_push(ctx->deser_insts, deser_inst) <= 0)
goto err;
return 1;
err:
if (deser_inst != NULL) {
if (deser_inst->deser != NULL)
deser_inst->deser->freectx(deser_inst->deserctx);
OSSL_DESERIALIZER_free(deser_inst->deser);
OPENSSL_free(deser_inst);
}
return 0;
}
int OSSL_DESERIALIZER_CTX_add_extra(OSSL_DESERIALIZER_CTX *ctx,
OPENSSL_CTX *libctx, const char *propq)
{
/*
* This function goes through existing deserializer methods in
* |ctx->deser_insts|, and tries to fetch new deserializers that produce
* what the existing ones want as input, and push those newly fetched
* deserializers on top of the same stack.
* Then it does the same again, but looping over the newly fetched
* deserializers, until there are no more serializers to be fetched, or
* when we have done this 10 times.
*
* we do this with sliding windows on the stack by keeping track of indexes
* and of the end.
*
* +----------------+
* | DER to RSA | <--- w_prev_start
* +----------------+
* | DER to DSA |
* +----------------+
* | DER to DH |
* +----------------+
* | PEM to DER | <--- w_prev_end, w_new_start
* +----------------+
* <--- w_new_end
*/
size_t w_prev_start, w_prev_end; /* "previous" deserializers */
size_t w_new_start, w_new_end; /* "new" deserializers */
size_t count = 0; /* Calculates how many were added in each iteration */
size_t depth = 0; /* Counts the number of iterations */
if (!ossl_assert(ctx != NULL)) {
ERR_raise(ERR_LIB_OSSL_DESERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
/*
* If there is no stack of OSSL_DESERIALIZER_INSTANCE, we have nothing
* more to add. That's fine.
*/
if (ctx->deser_insts == NULL)
return 1;
w_prev_start = 0;
w_prev_end = sk_OSSL_DESERIALIZER_INSTANCE_num(ctx->deser_insts);
do {
size_t i;
w_new_start = w_new_end = w_prev_end;
for (i = w_prev_start; i < w_prev_end; i++) {
OSSL_DESERIALIZER_INSTANCE *deser_inst =
sk_OSSL_DESERIALIZER_INSTANCE_value(ctx->deser_insts, i);
const char *name = deser_inst->input_type;
OSSL_DESERIALIZER *deser = NULL;
/*
* If the caller has specified what the initial input should be,
* and the deserializer implementation we're looking at has that
* input type, there's no point adding on more implementations
* on top of this one, so we don't.
*/
if (ctx->start_input_type != NULL
&& strcasecmp(ctx->start_input_type,
deser_inst->input_type) != 0)
continue;
ERR_set_mark();
deser = OSSL_DESERIALIZER_fetch(libctx, name, propq);
ERR_pop_to_mark();
if (deser != NULL) {
size_t j;
/*
* Check that we don't already have this deserializer in our
* stack We only need to check among the newly added ones.
*/
for (j = w_new_start; j < w_new_end; j++) {
OSSL_DESERIALIZER_INSTANCE *check_inst =
sk_OSSL_DESERIALIZER_INSTANCE_value(ctx->deser_insts, j);
if (deser == check_inst->deser) {
/* We found it, so drop the new fetch */
OSSL_DESERIALIZER_free(deser);
deser = NULL;
break;
}
}
}
if (deser == NULL)
continue;
/*
* Apart from keeping w_new_end up to date, We don't care about
* errors here. If it doesn't collect, then it doesn't...
*/
if (OSSL_DESERIALIZER_CTX_add_deserializer(ctx, deser)) /* ref++ */
w_new_end++;
OSSL_DESERIALIZER_free(deser); /* ref-- */
}
/*
|