]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/encode_decode/encoder_lib.c
b25529e073cf00dba5a8f363a016120c50a604e4
[thirdparty/openssl.git] / crypto / encode_decode / encoder_lib.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 #include "e_os.h" /* strcasecmp on Windows */
11 #include <openssl/core_names.h>
12 #include <openssl/bio.h>
13 #include <openssl/encoder.h>
14 #include <openssl/buffer.h>
15 #include <openssl/params.h>
16 #include <openssl/provider.h>
17 #include <openssl/trace.h>
18 #include "internal/bio.h"
19 #include "encoder_local.h"
20
21 struct encoder_process_data_st {
22 OSSL_ENCODER_CTX *ctx;
23
24 /* Current BIO */
25 BIO *bio;
26
27 /* Index of the current encoder instance to be processed */
28 int current_encoder_inst_index;
29
30 /* Processing data passed down through recursion */
31 int level; /* Recursion level */
32 OSSL_ENCODER_INSTANCE *next_encoder_inst;
33 int count_output_structure;
34
35 /* Processing data passed up through recursion */
36 OSSL_ENCODER_INSTANCE *prev_encoder_inst;
37 unsigned char *running_output;
38 size_t running_output_length;
39 };
40
41 static int encoder_process(struct encoder_process_data_st *data);
42
43 int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)
44 {
45 struct encoder_process_data_st data;
46
47 memset(&data, 0, sizeof(data));
48 data.ctx = ctx;
49 data.bio = out;
50 data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);
51
52 return encoder_process(&data) > 0;
53 }
54
55 #ifndef OPENSSL_NO_STDIO
56 static BIO *bio_from_file(FILE *fp)
57 {
58 BIO *b;
59
60 if ((b = BIO_new(BIO_s_file())) == NULL) {
61 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);
62 return NULL;
63 }
64 BIO_set_fp(b, fp, BIO_NOCLOSE);
65 return b;
66 }
67
68 int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)
69 {
70 BIO *b = bio_from_file(fp);
71 int ret = 0;
72
73 if (b != NULL)
74 ret = OSSL_ENCODER_to_bio(ctx, b);
75
76 BIO_free(b);
77 return ret;
78 }
79 #endif
80
81 int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
82 size_t *pdata_len)
83 {
84 BIO *out = BIO_new(BIO_s_mem());
85 BUF_MEM *buf = NULL;
86 int ret = 0;
87
88 if (pdata_len == NULL) {
89 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
90 return 0;
91 }
92
93 if (OSSL_ENCODER_to_bio(ctx, out)
94 && BIO_get_mem_ptr(out, &buf) > 0) {
95 ret = 1; /* Hope for the best. A too small buffer will clear this */
96
97 if (pdata != NULL && *pdata != NULL) {
98 if (*pdata_len < buf->length)
99 /*
100 * It's tempting to do |*pdata_len = (size_t)buf->length|
101 * However, it's believed to be confusing more than helpful,
102 * so we don't.
103 */
104 ret = 0;
105 else
106 *pdata_len -= buf->length;
107 } else {
108 /* The buffer with the right size is already allocated for us */
109 *pdata_len = (size_t)buf->length;
110 }
111
112 if (ret) {
113 if (pdata != NULL) {
114 if (*pdata != NULL) {
115 memcpy(*pdata, buf->data, buf->length);
116 *pdata += buf->length;
117 } else {
118 /* In this case, we steal the data from BIO_s_mem() */
119 *pdata = (unsigned char *)buf->data;
120 buf->data = NULL;
121 }
122 }
123 }
124 }
125 BIO_free(out);
126 return ret;
127 }
128
129 int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)
130 {
131 if (!ossl_assert(ctx != NULL)) {
132 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
133 return 0;
134 }
135
136 if (!ossl_assert(selection != 0)) {
137 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
138 return 0;
139 }
140
141 ctx->selection = selection;
142 return 1;
143 }
144
145 int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,
146 const char *output_type)
147 {
148 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) {
149 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
150 return 0;
151 }
152
153 ctx->output_type = output_type;
154 return 1;
155 }
156
157 int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,
158 const char *output_structure)
159 {
160 if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) {
161 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
162 return 0;
163 }
164
165 ctx->output_structure = output_structure;
166 return 1;
167 }
168
169 static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,
170 void *encoderctx)
171 {
172 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
173 OSSL_PARAM params[4];
174
175 if (!ossl_assert(encoder != NULL)) {
176 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
177 return 0;
178 }
179
180 if (encoder->get_params == NULL) {
181 ERR_raise(ERR_LIB_OSSL_ENCODER,
182 OSSL_ENCODER_R_MISSING_GET_PARAMS);
183 return 0;
184 }
185
186 if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL) {
187 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
188 return 0;
189 }
190
191 /*
192 * Cache the input and output types for this encoder. The output type
193 * is mandatory.
194 */
195 params[0] =
196 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_TYPE,
197 (char **)&encoder_inst->output_type, 0);
198 params[1] =
199 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE,
200 (char **)&encoder_inst->output_structure,
201 0);
202 params[2] =
203 OSSL_PARAM_construct_utf8_ptr(OSSL_ENCODER_PARAM_INPUT_TYPE,
204 (char **)&encoder_inst->input_type, 0);
205 params[3] = OSSL_PARAM_construct_end();
206
207 if (!encoder->get_params(params)
208 || !OSSL_PARAM_modified(&params[0]))
209 goto err;
210
211 if (!OSSL_ENCODER_up_ref(encoder)) {
212 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
213 goto err;
214 }
215
216 encoder_inst->encoder = encoder;
217 encoder_inst->encoderctx = encoderctx;
218 return encoder_inst;
219 err:
220 ossl_encoder_instance_free(encoder_inst);
221 return NULL;
222 }
223
224 void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)
225 {
226 if (encoder_inst != NULL) {
227 if (encoder_inst->encoder != NULL)
228 encoder_inst->encoder->freectx(encoder_inst->encoderctx);
229 encoder_inst->encoderctx = NULL;
230 OSSL_ENCODER_free(encoder_inst->encoder);
231 encoder_inst->encoder = NULL;
232 OPENSSL_free(encoder_inst);
233 }
234 }
235
236 static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,
237 OSSL_ENCODER_INSTANCE *ei)
238 {
239 int ok;
240
241 if (ctx->encoder_insts == NULL
242 && (ctx->encoder_insts =
243 sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) {
244 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
245 return 0;
246 }
247
248 ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);
249 if (ok) {
250 OSSL_TRACE_BEGIN(ENCODER) {
251 BIO_printf(trc_out,
252 "(ctx %p) Added encoder instance %p (encoder %p) with:\n",
253 (void *)ctx, (void *)ei, (void *)ei->encoder);
254 BIO_printf(trc_out,
255 " output type: %s, output structure: %s, input type :%s\n",
256 ei->output_type, ei->output_structure, ei->input_type);
257 } OSSL_TRACE_END(ENCODER);
258 }
259 return ok;
260 }
261
262 int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)
263 {
264 OSSL_ENCODER_INSTANCE *encoder_inst = NULL;
265 const OSSL_PROVIDER *prov = NULL;
266 void *encoderctx = NULL;
267 void *provctx = NULL;
268
269 if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) {
270 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
271 return 0;
272 }
273
274 prov = OSSL_ENCODER_provider(encoder);
275 provctx = OSSL_PROVIDER_get0_provider_ctx(prov);
276
277 if ((encoderctx = encoder->newctx(provctx)) == NULL
278 || (encoder_inst =
279 ossl_encoder_instance_new(encoder, encoderctx)) == NULL)
280 goto err;
281 /* Avoid double free of encoderctx on further errors */
282 encoderctx = NULL;
283
284 if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))
285 goto err;
286
287 return 1;
288 err:
289 ossl_encoder_instance_free(encoder_inst);
290 if (encoderctx != NULL)
291 encoder->freectx(encoderctx);
292 return 0;
293 }
294
295 int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,
296 OSSL_LIB_CTX *libctx, const char *propq)
297 {
298 return 1;
299 }
300
301 int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)
302 {
303 if (ctx == NULL || ctx->encoder_insts == NULL)
304 return 0;
305 return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);
306 }
307
308 int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,
309 OSSL_ENCODER_CONSTRUCT *construct)
310 {
311 if (!ossl_assert(ctx != NULL)) {
312 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
313 return 0;
314 }
315 ctx->construct = construct;
316 return 1;
317 }
318
319 int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,
320 void *construct_data)
321 {
322 if (!ossl_assert(ctx != NULL)) {
323 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
324 return 0;
325 }
326 ctx->construct_data = construct_data;
327 return 1;
328 }
329
330 int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,
331 OSSL_ENCODER_CLEANUP *cleanup)
332 {
333 if (!ossl_assert(ctx != NULL)) {
334 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
335 return 0;
336 }
337 ctx->cleanup = cleanup;
338 return 1;
339 }
340
341 OSSL_ENCODER *
342 OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)
343 {
344 if (encoder_inst == NULL)
345 return NULL;
346 return encoder_inst->encoder;
347 }
348
349 void *
350 OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)
351 {
352 if (encoder_inst == NULL)
353 return NULL;
354 return encoder_inst->encoderctx;
355 }
356
357 const char *
358 OSSL_ENCODER_INSTANCE_get_input_type(OSSL_ENCODER_INSTANCE *encoder_inst)
359 {
360 if (encoder_inst == NULL)
361 return NULL;
362 return encoder_inst->input_type;
363 }
364
365 const char *
366 OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)
367 {
368 if (encoder_inst == NULL)
369 return NULL;
370 return encoder_inst->output_type;
371 }
372
373 const char *
374 OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)
375 {
376 if (encoder_inst == NULL)
377 return NULL;
378 return encoder_inst->output_structure;
379 }
380
381 static int encoder_process(struct encoder_process_data_st *data)
382 {
383 OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;
384 OSSL_ENCODER *current_encoder = NULL;
385 OSSL_ENCODER_CTX *current_encoder_ctx = NULL;
386 BIO *allocated_out = NULL;
387 const void *original_data = NULL;
388 OSSL_PARAM abstract[10];
389 const OSSL_PARAM *current_abstract = NULL;
390 int i;
391 int ok = -1; /* -1 signifies that the lookup loop gave nothing */
392 int top = 0;
393
394 if (data->next_encoder_inst == NULL) {
395 /* First iteration, where we prepare for what is to come */
396
397 data->count_output_structure =
398 data->ctx->output_structure == NULL ? -1 : 0;
399 top = 1;
400 }
401
402 for (i = data->current_encoder_inst_index; i-- > 0;) {
403 OSSL_ENCODER *next_encoder = NULL;
404 const char *current_output_type;
405 const char *current_output_structure;
406 struct encoder_process_data_st new_data;
407
408 if (!top)
409 next_encoder =
410 OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);
411
412 current_encoder_inst =
413 sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);
414 current_encoder =
415 OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);
416 current_encoder_ctx =
417 OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);
418 current_output_type =
419 OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);
420 current_output_structure =
421 OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);
422 memset(&new_data, 0, sizeof(new_data));
423 new_data.ctx = data->ctx;
424 new_data.current_encoder_inst_index = i;
425 new_data.next_encoder_inst = current_encoder_inst;
426 new_data.count_output_structure = data->count_output_structure;
427 new_data.level = data->level + 1;
428
429 OSSL_TRACE_BEGIN(ENCODER) {
430 BIO_printf(trc_out,
431 "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",
432 data->level, (void *)data->ctx,
433 (void *)current_encoder_inst, (void *)current_encoder);
434 } OSSL_TRACE_END(ENCODER);
435
436 /*
437 * If this is the top call, we check if the output type of the current
438 * encoder matches the desired output type.
439 * If this isn't the top call, i.e. this is deeper in the recursion,
440 * we instead check if the output type of the current encoder matches
441 * the name of the next encoder (the one found by the parent call).
442 */
443 if (top) {
444 if (data->ctx->output_type != NULL
445 && strcasecmp(current_output_type,
446 data->ctx->output_type) != 0) {
447 OSSL_TRACE_BEGIN(ENCODER) {
448 BIO_printf(trc_out,
449 "[%d] Skipping because current encoder output type (%s) != desired output type (%s)\n",
450 data->level,
451 current_output_type, data->ctx->output_type);
452 } OSSL_TRACE_END(ENCODER);
453 continue;
454 }
455 } else {
456 if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) {
457 OSSL_TRACE_BEGIN(ENCODER) {
458 BIO_printf(trc_out,
459 "[%d] Skipping because current encoder output type (%s) != name of encoder %p\n",
460 data->level,
461 current_output_type, (void *)next_encoder);
462 } OSSL_TRACE_END(ENCODER);
463 continue;
464 }
465 }
466
467 /*
468 * If the caller and the current encoder specify an output structure,
469 * Check if they match. If they do, count the match, otherwise skip
470 * the current encoder.
471 */
472 if (data->ctx->output_structure != NULL
473 && current_output_structure != NULL) {
474 if (strcasecmp(data->ctx->output_structure,
475 current_output_structure) != 0) {
476 OSSL_TRACE_BEGIN(ENCODER) {
477 BIO_printf(trc_out,
478 "[%d] Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",
479 data->level,
480 current_output_structure,
481 data->ctx->output_structure);
482 } OSSL_TRACE_END(ENCODER);
483 continue;
484 }
485
486 data->count_output_structure++;
487 }
488
489 /*
490 * Recurse to process the encoder implementations before the current
491 * one.
492 */
493 ok = encoder_process(&new_data);
494
495 data->prev_encoder_inst = new_data.prev_encoder_inst;
496 data->running_output = new_data.running_output;
497 data->running_output_length = new_data.running_output_length;
498
499 /*
500 * ok == -1 means that the recursion call above gave no further
501 * encoders, and that the one we're currently at should
502 * be tried.
503 * ok == 0 means that something failed in the recursion call
504 * above, making the result unsuitable for a chain.
505 * In this case, we simply continue to try finding a
506 * suitable encoder at this recursion level.
507 * ok == 1 means that the recursion call was successful, and we
508 * try to use the result at this recursion level.
509 */
510 if (ok != 0)
511 break;
512
513 OSSL_TRACE_BEGIN(ENCODER) {
514 BIO_printf(trc_out,
515 "[%d] Skipping because recusion level %d failed\n",
516 data->level, new_data.level);
517 } OSSL_TRACE_END(ENCODER);
518 }
519
520 /*
521 * If |i < 0|, we didn't find any useful encoder in this recursion, so
522 * we do the rest of the process only if |i >= 0|.
523 */
524 if (i < 0) {
525 ok = -1;
526
527 OSSL_TRACE_BEGIN(ENCODER) {
528 BIO_printf(trc_out,
529 "[%d] (ctx %p) No suitable encoder found\n",
530 data->level, (void *)data->ctx);
531 } OSSL_TRACE_END(ENCODER);
532 } else {
533 /* Preparations */
534
535 switch (ok) {
536 case 0:
537 break;
538 case -1:
539 /*
540 * We have reached the beginning of the encoder instance sequence,
541 * so we prepare the object to be encoded.
542 */
543
544 /*
545 * |data->count_output_structure| is one of these values:
546 *
547 * -1 There is no desired output structure
548 * 0 There is a desired output structure, and it wasn't
549 * matched by any of the encoder instances that were
550 * considered
551 * >0 There is a desired output structure, and at least one
552 * of the encoder instances matched it
553 */
554 if (data->count_output_structure == 0)
555 return 0;
556
557 original_data =
558 data->ctx->construct(current_encoder_inst,
559 data->ctx->construct_data);
560
561 /* Assume that the constructor recorded an error */
562 if (original_data != NULL)
563 ok = 1;
564 else
565 ok = 0;
566 break;
567 case 1:
568 if (!ossl_assert(data->running_output != NULL)) {
569 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
570 ok = 0;
571 break;
572 }
573
574 {
575 /*
576 * Create an object abstraction from the latest output, which
577 * was stolen from the previous round.
578 */
579
580 OSSL_PARAM *abstract_p = abstract;
581 const char *prev_input_type =
582 OSSL_ENCODER_INSTANCE_get_input_type(data->prev_encoder_inst);
583 const char *prev_output_structure =
584 OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);
585
586 if (prev_input_type != NULL)
587 *abstract_p++ =
588 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
589 (char *)prev_input_type, 0);
590 if (prev_output_structure != NULL)
591 *abstract_p++ =
592 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
593 (char *)prev_output_structure,
594 0);
595 *abstract_p++ =
596 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
597 data->running_output,
598 data->running_output_length);
599 *abstract_p = OSSL_PARAM_construct_end();
600 current_abstract = abstract;
601 }
602 break;
603 }
604
605 /* Calling the encoder implementation */
606
607 if (ok) {
608 OSSL_CORE_BIO *cbio = NULL;
609 BIO *current_out = NULL;
610
611 /*
612 * If we're at the last encoder instance to use, we're setting up
613 * final output. Otherwise, set up an intermediary memory output.
614 */
615 if (top)
616 current_out = data->bio;
617 else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))
618 == NULL)
619 ok = 0; /* Assume BIO_new() recorded an error */
620
621 if (ok)
622 ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;
623 if (ok) {
624 ok = current_encoder->encode(current_encoder_ctx, cbio,
625 original_data, current_abstract,
626 data->ctx->selection,
627 ossl_pw_passphrase_callback_enc,
628 &data->ctx->pwdata);
629 OSSL_TRACE_BEGIN(ENCODER) {
630 BIO_printf(trc_out,
631 "[%d] (ctx %p) Running encoder instance %p => %d\n",
632 data->level, (void *)data->ctx,
633 (void *)current_encoder_inst, ok);
634 } OSSL_TRACE_END(ENCODER);
635 }
636
637 ossl_core_bio_free(cbio);
638 data->prev_encoder_inst = current_encoder_inst;
639 }
640 }
641
642 /* Cleanup and collecting the result */
643
644 OPENSSL_free(data->running_output);
645 data->running_output = NULL;
646
647 /*
648 * Steal the output from the BIO_s_mem, if we did allocate one.
649 * That'll be the data for an object abstraction in the next round.
650 */
651 if (allocated_out != NULL) {
652 BUF_MEM *buf;
653
654 BIO_get_mem_ptr(allocated_out, &buf);
655 data->running_output = (unsigned char *)buf->data;
656 data->running_output_length = buf->length;
657 memset(buf, 0, sizeof(*buf));
658 }
659
660 BIO_free(allocated_out);
661 if (original_data != NULL)
662 data->ctx->cleanup(data->ctx->construct_data);
663 return ok;
664 }