/src/openssl/crypto/encode_decode/encoder_lib.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2025 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 <ctype.h>  | 
11  |  |  | 
12  |  | #include <openssl/core_names.h>  | 
13  |  | #include <openssl/bio.h>  | 
14  |  | #include <openssl/encoder.h>  | 
15  |  | #include <openssl/buffer.h>  | 
16  |  | #include <openssl/params.h>  | 
17  |  | #include <openssl/provider.h>  | 
18  |  | #include <openssl/trace.h>  | 
19  |  | #include <crypto/bn.h>  | 
20  |  | #include "internal/bio.h"  | 
21  |  | #include "internal/ffc.h"  | 
22  |  | #include "internal/provider.h"  | 
23  |  | #include "internal/encoder.h"  | 
24  |  | #include "encoder_local.h"  | 
25  |  |  | 
26  |  | /* Number of octets per line */  | 
27  | 0  | #define LABELED_BUF_PRINT_WIDTH    15  | 
28  |  |  | 
29  |  | # ifdef SIXTY_FOUR_BIT_LONG  | 
30  |  | #  define BN_FMTu "%lu"  | 
31  |  | #  define BN_FMTx "%lx"  | 
32  |  | # endif  | 
33  |  |  | 
34  |  | # ifdef SIXTY_FOUR_BIT  | 
35  |  | #  define BN_FMTu "%llu"  | 
36  |  | #  define BN_FMTx "%llx"  | 
37  |  | # endif  | 
38  |  |  | 
39  |  | # ifdef THIRTY_TWO_BIT  | 
40  |  | #  define BN_FMTu "%u"  | 
41  |  | #  define BN_FMTx "%x"  | 
42  |  | # endif  | 
43  |  |  | 
44  |  | struct encoder_process_data_st { | 
45  |  |     OSSL_ENCODER_CTX *ctx;  | 
46  |  |  | 
47  |  |     /* Current BIO */  | 
48  |  |     BIO *bio;  | 
49  |  |  | 
50  |  |     /* Index of the current encoder instance to be processed */  | 
51  |  |     int current_encoder_inst_index;  | 
52  |  |  | 
53  |  |     /* Processing data passed down through recursion */  | 
54  |  |     int level;                   /* Recursion level */  | 
55  |  |     OSSL_ENCODER_INSTANCE *next_encoder_inst;  | 
56  |  |     int count_output_structure;  | 
57  |  |  | 
58  |  |     /* Processing data passed up through recursion */  | 
59  |  |     OSSL_ENCODER_INSTANCE *prev_encoder_inst;  | 
60  |  |     unsigned char *running_output;  | 
61  |  |     size_t running_output_length;  | 
62  |  |     /* Data type = the name of the first succeeding encoder implementation */  | 
63  |  |     const char *data_type;  | 
64  |  | };  | 
65  |  |  | 
66  |  | static int encoder_process(struct encoder_process_data_st *data);  | 
67  |  |  | 
68  |  | int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out)  | 
69  | 0  | { | 
70  | 0  |     struct encoder_process_data_st data;  | 
71  |  | 
  | 
72  | 0  |     memset(&data, 0, sizeof(data));  | 
73  | 0  |     data.ctx = ctx;  | 
74  | 0  |     data.bio = out;  | 
75  | 0  |     data.current_encoder_inst_index = OSSL_ENCODER_CTX_get_num_encoders(ctx);  | 
76  |  | 
  | 
77  | 0  |     if (data.current_encoder_inst_index == 0) { | 
78  | 0  |         ERR_raise_data(ERR_LIB_OSSL_ENCODER, OSSL_ENCODER_R_ENCODER_NOT_FOUND,  | 
79  | 0  |                        "No encoders were found. For standard encoders you need "  | 
80  | 0  |                        "at least one of the default or base providers "  | 
81  | 0  |                        "available. Did you forget to load them?");  | 
82  | 0  |         return 0;  | 
83  | 0  |     }  | 
84  |  |  | 
85  | 0  |     if (ctx->cleanup == NULL || ctx->construct == NULL) { | 
86  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INIT_FAIL);  | 
87  | 0  |         return 0;  | 
88  | 0  |     }  | 
89  |  |  | 
90  | 0  |     return encoder_process(&data) > 0;  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | #ifndef OPENSSL_NO_STDIO  | 
94  |  | static BIO *bio_from_file(FILE *fp)  | 
95  | 0  | { | 
96  | 0  |     BIO *b;  | 
97  |  | 
  | 
98  | 0  |     if ((b = BIO_new(BIO_s_file())) == NULL) { | 
99  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_BUF_LIB);  | 
100  | 0  |         return NULL;  | 
101  | 0  |     }  | 
102  | 0  |     BIO_set_fp(b, fp, BIO_NOCLOSE);  | 
103  | 0  |     return b;  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp)  | 
107  | 0  | { | 
108  | 0  |     BIO *b = bio_from_file(fp);  | 
109  | 0  |     int ret = 0;  | 
110  |  | 
  | 
111  | 0  |     if (b != NULL)  | 
112  | 0  |         ret = OSSL_ENCODER_to_bio(ctx, b);  | 
113  |  | 
  | 
114  | 0  |     BIO_free(b);  | 
115  | 0  |     return ret;  | 
116  | 0  | }  | 
117  |  | #endif  | 
118  |  |  | 
119  |  | int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,  | 
120  |  |                          size_t *pdata_len)  | 
121  | 0  | { | 
122  | 0  |     BIO *out;  | 
123  | 0  |     BUF_MEM *buf = NULL;  | 
124  | 0  |     int ret = 0;  | 
125  |  | 
  | 
126  | 0  |     if (pdata_len == NULL) { | 
127  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
128  | 0  |         return 0;  | 
129  | 0  |     }  | 
130  |  |  | 
131  | 0  |     out = BIO_new(BIO_s_mem());  | 
132  |  | 
  | 
133  | 0  |     if (out != NULL  | 
134  | 0  |         && OSSL_ENCODER_to_bio(ctx, out)  | 
135  | 0  |         && BIO_get_mem_ptr(out, &buf) > 0) { | 
136  | 0  |         ret = 1; /* Hope for the best. A too small buffer will clear this */  | 
137  |  | 
  | 
138  | 0  |         if (pdata != NULL && *pdata != NULL) { | 
139  | 0  |             if (*pdata_len < buf->length)  | 
140  |  |                 /*  | 
141  |  |                  * It's tempting to do |*pdata_len = (size_t)buf->length|  | 
142  |  |                  * However, it's believed to be confusing more than helpful,  | 
143  |  |                  * so we don't.  | 
144  |  |                  */  | 
145  | 0  |                 ret = 0;  | 
146  | 0  |             else  | 
147  | 0  |                 *pdata_len -= buf->length;  | 
148  | 0  |         } else { | 
149  |  |             /* The buffer with the right size is already allocated for us */  | 
150  | 0  |             *pdata_len = (size_t)buf->length;  | 
151  | 0  |         }  | 
152  |  | 
  | 
153  | 0  |         if (ret) { | 
154  | 0  |             if (pdata != NULL) { | 
155  | 0  |                 if (*pdata != NULL) { | 
156  | 0  |                     memcpy(*pdata, buf->data, buf->length);  | 
157  | 0  |                     *pdata += buf->length;  | 
158  | 0  |                 } else { | 
159  |  |                     /* In this case, we steal the data from BIO_s_mem() */  | 
160  | 0  |                     *pdata = (unsigned char *)buf->data;  | 
161  | 0  |                     buf->data = NULL;  | 
162  | 0  |                 }  | 
163  | 0  |             }  | 
164  | 0  |         }  | 
165  | 0  |     }  | 
166  | 0  |     BIO_free(out);  | 
167  | 0  |     return ret;  | 
168  | 0  | }  | 
169  |  |  | 
170  |  | int OSSL_ENCODER_CTX_set_selection(OSSL_ENCODER_CTX *ctx, int selection)  | 
171  | 0  | { | 
172  | 0  |     if (!ossl_assert(ctx != NULL)) { | 
173  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
174  | 0  |         return 0;  | 
175  | 0  |     }  | 
176  |  |  | 
177  | 0  |     if (!ossl_assert(selection != 0)) { | 
178  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);  | 
179  | 0  |         return 0;  | 
180  | 0  |     }  | 
181  |  |  | 
182  | 0  |     ctx->selection = selection;  | 
183  | 0  |     return 1;  | 
184  | 0  | }  | 
185  |  |  | 
186  |  | int OSSL_ENCODER_CTX_set_output_type(OSSL_ENCODER_CTX *ctx,  | 
187  |  |                                      const char *output_type)  | 
188  | 0  | { | 
189  | 0  |     if (!ossl_assert(ctx != NULL) || !ossl_assert(output_type != NULL)) { | 
190  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
191  | 0  |         return 0;  | 
192  | 0  |     }  | 
193  |  |  | 
194  | 0  |     ctx->output_type = output_type;  | 
195  | 0  |     return 1;  | 
196  | 0  | }  | 
197  |  |  | 
198  |  | int OSSL_ENCODER_CTX_set_output_structure(OSSL_ENCODER_CTX *ctx,  | 
199  |  |                                           const char *output_structure)  | 
200  | 0  | { | 
201  | 0  |     if (!ossl_assert(ctx != NULL) || !ossl_assert(output_structure != NULL)) { | 
202  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
203  | 0  |         return 0;  | 
204  | 0  |     }  | 
205  |  |  | 
206  | 0  |     ctx->output_structure = output_structure;  | 
207  | 0  |     return 1;  | 
208  | 0  | }  | 
209  |  |  | 
210  |  | static OSSL_ENCODER_INSTANCE *ossl_encoder_instance_new(OSSL_ENCODER *encoder,  | 
211  |  |                                                         void *encoderctx)  | 
212  | 0  | { | 
213  | 0  |     OSSL_ENCODER_INSTANCE *encoder_inst = NULL;  | 
214  | 0  |     const OSSL_PROVIDER *prov;  | 
215  | 0  |     OSSL_LIB_CTX *libctx;  | 
216  | 0  |     const OSSL_PROPERTY_LIST *props;  | 
217  | 0  |     const OSSL_PROPERTY_DEFINITION *prop;  | 
218  |  | 
  | 
219  | 0  |     if (!ossl_assert(encoder != NULL)) { | 
220  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
221  | 0  |         return 0;  | 
222  | 0  |     }  | 
223  |  |  | 
224  | 0  |     if ((encoder_inst = OPENSSL_zalloc(sizeof(*encoder_inst))) == NULL)  | 
225  | 0  |         return 0;  | 
226  |  |  | 
227  | 0  |     if (!OSSL_ENCODER_up_ref(encoder)) { | 
228  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);  | 
229  | 0  |         goto err;  | 
230  | 0  |     }  | 
231  |  |  | 
232  | 0  |     prov = OSSL_ENCODER_get0_provider(encoder);  | 
233  | 0  |     libctx = ossl_provider_libctx(prov);  | 
234  | 0  |     props = ossl_encoder_parsed_properties(encoder);  | 
235  | 0  |     if (props == NULL) { | 
236  | 0  |         ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,  | 
237  | 0  |                        "there are no property definitions with encoder %s",  | 
238  | 0  |                        OSSL_ENCODER_get0_name(encoder));  | 
239  | 0  |         goto err;  | 
240  | 0  |     }  | 
241  |  |  | 
242  |  |     /* The "output" property is mandatory */  | 
243  | 0  |     prop = ossl_property_find_property(props, libctx, "output");  | 
244  | 0  |     encoder_inst->output_type = ossl_property_get_string_value(libctx, prop);  | 
245  | 0  |     if (encoder_inst->output_type == NULL) { | 
246  | 0  |         ERR_raise_data(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROPERTY_DEFINITION,  | 
247  | 0  |                        "the mandatory 'output' property is missing "  | 
248  | 0  |                        "for encoder %s (properties: %s)",  | 
249  | 0  |                        OSSL_ENCODER_get0_name(encoder),  | 
250  | 0  |                        OSSL_ENCODER_get0_properties(encoder));  | 
251  | 0  |         goto err;  | 
252  | 0  |     }  | 
253  |  |  | 
254  |  |     /* The "structure" property is optional */  | 
255  | 0  |     prop = ossl_property_find_property(props, libctx, "structure");  | 
256  | 0  |     if (prop != NULL)  | 
257  | 0  |         encoder_inst->output_structure  | 
258  | 0  |             = ossl_property_get_string_value(libctx, prop);  | 
259  |  | 
  | 
260  | 0  |     encoder_inst->encoder = encoder;  | 
261  | 0  |     encoder_inst->encoderctx = encoderctx;  | 
262  | 0  |     return encoder_inst;  | 
263  | 0  |  err:  | 
264  | 0  |     ossl_encoder_instance_free(encoder_inst);  | 
265  | 0  |     return NULL;  | 
266  | 0  | }  | 
267  |  |  | 
268  |  | void ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst)  | 
269  | 0  | { | 
270  | 0  |     if (encoder_inst != NULL) { | 
271  | 0  |         if (encoder_inst->encoder != NULL)  | 
272  | 0  |             encoder_inst->encoder->freectx(encoder_inst->encoderctx);  | 
273  | 0  |         encoder_inst->encoderctx = NULL;  | 
274  | 0  |         OSSL_ENCODER_free(encoder_inst->encoder);  | 
275  | 0  |         encoder_inst->encoder = NULL;  | 
276  | 0  |         OPENSSL_free(encoder_inst);  | 
277  | 0  |     }  | 
278  | 0  | }  | 
279  |  |  | 
280  |  | static int ossl_encoder_ctx_add_encoder_inst(OSSL_ENCODER_CTX *ctx,  | 
281  |  |                                              OSSL_ENCODER_INSTANCE *ei)  | 
282  | 0  | { | 
283  | 0  |     int ok;  | 
284  |  | 
  | 
285  | 0  |     if (ctx->encoder_insts == NULL  | 
286  | 0  |         && (ctx->encoder_insts =  | 
287  | 0  |             sk_OSSL_ENCODER_INSTANCE_new_null()) == NULL) { | 
288  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB);  | 
289  | 0  |         return 0;  | 
290  | 0  |     }  | 
291  |  |  | 
292  | 0  |     ok = (sk_OSSL_ENCODER_INSTANCE_push(ctx->encoder_insts, ei) > 0);  | 
293  | 0  |     if (ok) { | 
294  | 0  |         OSSL_TRACE_BEGIN(ENCODER) { | 
295  | 0  |             BIO_printf(trc_out,  | 
296  | 0  |                        "(ctx %p) Added encoder instance %p (encoder %p):\n"  | 
297  | 0  |                        "    %s with %s\n",  | 
298  | 0  |                        (void *)ctx, (void *)ei, (void *)ei->encoder,  | 
299  | 0  |                        OSSL_ENCODER_get0_name(ei->encoder),  | 
300  | 0  |                        OSSL_ENCODER_get0_properties(ei->encoder));  | 
301  | 0  |         } OSSL_TRACE_END(ENCODER);  | 
302  | 0  |     }  | 
303  | 0  |     return ok;  | 
304  | 0  | }  | 
305  |  |  | 
306  |  | int OSSL_ENCODER_CTX_add_encoder(OSSL_ENCODER_CTX *ctx, OSSL_ENCODER *encoder)  | 
307  | 0  | { | 
308  | 0  |     OSSL_ENCODER_INSTANCE *encoder_inst = NULL;  | 
309  | 0  |     const OSSL_PROVIDER *prov = NULL;  | 
310  | 0  |     void *encoderctx = NULL;  | 
311  | 0  |     void *provctx = NULL;  | 
312  |  | 
  | 
313  | 0  |     if (!ossl_assert(ctx != NULL) || !ossl_assert(encoder != NULL)) { | 
314  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
315  | 0  |         return 0;  | 
316  | 0  |     }  | 
317  |  |  | 
318  | 0  |     prov = OSSL_ENCODER_get0_provider(encoder);  | 
319  | 0  |     provctx = OSSL_PROVIDER_get0_provider_ctx(prov);  | 
320  |  | 
  | 
321  | 0  |     if ((encoderctx = encoder->newctx(provctx)) == NULL  | 
322  | 0  |         || (encoder_inst =  | 
323  | 0  |             ossl_encoder_instance_new(encoder, encoderctx)) == NULL)  | 
324  | 0  |         goto err;  | 
325  |  |     /* Avoid double free of encoderctx on further errors */  | 
326  | 0  |     encoderctx = NULL;  | 
327  |  | 
  | 
328  | 0  |     if (!ossl_encoder_ctx_add_encoder_inst(ctx, encoder_inst))  | 
329  | 0  |         goto err;  | 
330  |  |  | 
331  | 0  |     return 1;  | 
332  | 0  |  err:  | 
333  | 0  |     ossl_encoder_instance_free(encoder_inst);  | 
334  | 0  |     if (encoderctx != NULL)  | 
335  | 0  |         encoder->freectx(encoderctx);  | 
336  | 0  |     return 0;  | 
337  | 0  | }  | 
338  |  |  | 
339  |  | int OSSL_ENCODER_CTX_add_extra(OSSL_ENCODER_CTX *ctx,  | 
340  |  |                                OSSL_LIB_CTX *libctx, const char *propq)  | 
341  | 0  | { | 
342  | 0  |     return 1;  | 
343  | 0  | }  | 
344  |  |  | 
345  |  | int OSSL_ENCODER_CTX_get_num_encoders(OSSL_ENCODER_CTX *ctx)  | 
346  | 0  | { | 
347  | 0  |     if (ctx == NULL || ctx->encoder_insts == NULL)  | 
348  | 0  |         return 0;  | 
349  | 0  |     return sk_OSSL_ENCODER_INSTANCE_num(ctx->encoder_insts);  | 
350  | 0  | }  | 
351  |  |  | 
352  |  | int OSSL_ENCODER_CTX_set_construct(OSSL_ENCODER_CTX *ctx,  | 
353  |  |                                    OSSL_ENCODER_CONSTRUCT *construct)  | 
354  | 0  | { | 
355  | 0  |     if (!ossl_assert(ctx != NULL)) { | 
356  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
357  | 0  |         return 0;  | 
358  | 0  |     }  | 
359  | 0  |     ctx->construct = construct;  | 
360  | 0  |     return 1;  | 
361  | 0  | }  | 
362  |  |  | 
363  |  | int OSSL_ENCODER_CTX_set_construct_data(OSSL_ENCODER_CTX *ctx,  | 
364  |  |                                         void *construct_data)  | 
365  | 0  | { | 
366  | 0  |     if (!ossl_assert(ctx != NULL)) { | 
367  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
368  | 0  |         return 0;  | 
369  | 0  |     }  | 
370  | 0  |     ctx->construct_data = construct_data;  | 
371  | 0  |     return 1;  | 
372  | 0  | }  | 
373  |  |  | 
374  |  | int OSSL_ENCODER_CTX_set_cleanup(OSSL_ENCODER_CTX *ctx,  | 
375  |  |                                  OSSL_ENCODER_CLEANUP *cleanup)  | 
376  | 0  | { | 
377  | 0  |     if (!ossl_assert(ctx != NULL)) { | 
378  | 0  |         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);  | 
379  | 0  |         return 0;  | 
380  | 0  |     }  | 
381  | 0  |     ctx->cleanup = cleanup;  | 
382  | 0  |     return 1;  | 
383  | 0  | }  | 
384  |  |  | 
385  |  | OSSL_ENCODER *  | 
386  |  | OSSL_ENCODER_INSTANCE_get_encoder(OSSL_ENCODER_INSTANCE *encoder_inst)  | 
387  | 0  | { | 
388  | 0  |     if (encoder_inst == NULL)  | 
389  | 0  |         return NULL;  | 
390  | 0  |     return encoder_inst->encoder;  | 
391  | 0  | }  | 
392  |  |  | 
393  |  | void *  | 
394  |  | OSSL_ENCODER_INSTANCE_get_encoder_ctx(OSSL_ENCODER_INSTANCE *encoder_inst)  | 
395  | 0  | { | 
396  | 0  |     if (encoder_inst == NULL)  | 
397  | 0  |         return NULL;  | 
398  | 0  |     return encoder_inst->encoderctx;  | 
399  | 0  | }  | 
400  |  |  | 
401  |  | const char *  | 
402  |  | OSSL_ENCODER_INSTANCE_get_output_type(OSSL_ENCODER_INSTANCE *encoder_inst)  | 
403  | 0  | { | 
404  | 0  |     if (encoder_inst == NULL)  | 
405  | 0  |         return NULL;  | 
406  | 0  |     return encoder_inst->output_type;  | 
407  | 0  | }  | 
408  |  |  | 
409  |  | const char *  | 
410  |  | OSSL_ENCODER_INSTANCE_get_output_structure(OSSL_ENCODER_INSTANCE *encoder_inst)  | 
411  | 0  | { | 
412  | 0  |     if (encoder_inst == NULL)  | 
413  | 0  |         return NULL;  | 
414  | 0  |     return encoder_inst->output_structure;  | 
415  | 0  | }  | 
416  |  |  | 
417  |  | static int encoder_process(struct encoder_process_data_st *data)  | 
418  | 0  | { | 
419  | 0  |     OSSL_ENCODER_INSTANCE *current_encoder_inst = NULL;  | 
420  | 0  |     OSSL_ENCODER *current_encoder = NULL;  | 
421  | 0  |     OSSL_ENCODER_CTX *current_encoder_ctx = NULL;  | 
422  | 0  |     BIO *allocated_out = NULL;  | 
423  | 0  |     const void *original_data = NULL;  | 
424  | 0  |     OSSL_PARAM abstract[10];  | 
425  | 0  |     const OSSL_PARAM *current_abstract = NULL;  | 
426  | 0  |     int i;  | 
427  | 0  |     int ok = -1;  /* -1 signifies that the lookup loop gave nothing */  | 
428  | 0  |     int top = 0;  | 
429  |  | 
  | 
430  | 0  |     if (data->next_encoder_inst == NULL) { | 
431  |  |         /* First iteration, where we prepare for what is to come */  | 
432  |  | 
  | 
433  | 0  |         data->count_output_structure =  | 
434  | 0  |             data->ctx->output_structure == NULL ? -1 : 0;  | 
435  | 0  |         top = 1;  | 
436  | 0  |     }  | 
437  |  | 
  | 
438  | 0  |     for (i = data->current_encoder_inst_index; i-- > 0;) { | 
439  | 0  |         OSSL_ENCODER *next_encoder = NULL;  | 
440  | 0  |         const char *current_output_type;  | 
441  | 0  |         const char *current_output_structure;  | 
442  | 0  |         struct encoder_process_data_st new_data;  | 
443  |  | 
  | 
444  | 0  |         if (!top)  | 
445  | 0  |             next_encoder =  | 
446  | 0  |                 OSSL_ENCODER_INSTANCE_get_encoder(data->next_encoder_inst);  | 
447  |  | 
  | 
448  | 0  |         current_encoder_inst =  | 
449  | 0  |             sk_OSSL_ENCODER_INSTANCE_value(data->ctx->encoder_insts, i);  | 
450  | 0  |         current_encoder =  | 
451  | 0  |             OSSL_ENCODER_INSTANCE_get_encoder(current_encoder_inst);  | 
452  | 0  |         current_encoder_ctx =  | 
453  | 0  |             OSSL_ENCODER_INSTANCE_get_encoder_ctx(current_encoder_inst);  | 
454  | 0  |         current_output_type =  | 
455  | 0  |             OSSL_ENCODER_INSTANCE_get_output_type(current_encoder_inst);  | 
456  | 0  |         current_output_structure =  | 
457  | 0  |             OSSL_ENCODER_INSTANCE_get_output_structure(current_encoder_inst);  | 
458  | 0  |         memset(&new_data, 0, sizeof(new_data));  | 
459  | 0  |         new_data.ctx = data->ctx;  | 
460  | 0  |         new_data.current_encoder_inst_index = i;  | 
461  | 0  |         new_data.next_encoder_inst = current_encoder_inst;  | 
462  | 0  |         new_data.count_output_structure = data->count_output_structure;  | 
463  | 0  |         new_data.level = data->level + 1;  | 
464  |  | 
  | 
465  | 0  |         OSSL_TRACE_BEGIN(ENCODER) { | 
466  | 0  |             BIO_printf(trc_out,  | 
467  | 0  |                        "[%d] (ctx %p) Considering encoder instance %p (encoder %p)\n",  | 
468  | 0  |                        data->level, (void *)data->ctx,  | 
469  | 0  |                        (void *)current_encoder_inst, (void *)current_encoder);  | 
470  | 0  |         } OSSL_TRACE_END(ENCODER);  | 
471  |  |  | 
472  |  |         /*  | 
473  |  |          * If this is the top call, we check if the output type of the current  | 
474  |  |          * encoder matches the desired output type.  | 
475  |  |          * If this isn't the top call, i.e. this is deeper in the recursion,  | 
476  |  |          * we instead check if the output type of the current encoder matches  | 
477  |  |          * the name of the next encoder (the one found by the parent call).  | 
478  |  |          */  | 
479  | 0  |         if (top) { | 
480  | 0  |             if (data->ctx->output_type != NULL  | 
481  | 0  |                 && OPENSSL_strcasecmp(current_output_type,  | 
482  | 0  |                                       data->ctx->output_type) != 0) { | 
483  | 0  |                 OSSL_TRACE_BEGIN(ENCODER) { | 
484  | 0  |                     BIO_printf(trc_out,  | 
485  | 0  |                                "[%d]    Skipping because current encoder output type (%s) != desired output type (%s)\n",  | 
486  | 0  |                                data->level,  | 
487  | 0  |                                current_output_type, data->ctx->output_type);  | 
488  | 0  |                 } OSSL_TRACE_END(ENCODER);  | 
489  | 0  |                 continue;  | 
490  | 0  |             }  | 
491  | 0  |         } else { | 
492  | 0  |             if (!OSSL_ENCODER_is_a(next_encoder, current_output_type)) { | 
493  | 0  |                 OSSL_TRACE_BEGIN(ENCODER) { | 
494  | 0  |                     BIO_printf(trc_out,  | 
495  | 0  |                                "[%d]    Skipping because current encoder output type (%s) != name of encoder %p\n",  | 
496  | 0  |                                data->level,  | 
497  | 0  |                                current_output_type, (void *)next_encoder);  | 
498  | 0  |                 } OSSL_TRACE_END(ENCODER);  | 
499  | 0  |                 continue;  | 
500  | 0  |             }  | 
501  | 0  |         }  | 
502  |  |  | 
503  |  |         /*  | 
504  |  |          * If the caller and the current encoder specify an output structure,  | 
505  |  |          * Check if they match.  If they do, count the match, otherwise skip  | 
506  |  |          * the current encoder.  | 
507  |  |          */  | 
508  | 0  |         if (data->ctx->output_structure != NULL  | 
509  | 0  |             && current_output_structure != NULL) { | 
510  | 0  |             if (OPENSSL_strcasecmp(data->ctx->output_structure,  | 
511  | 0  |                                    current_output_structure) != 0) { | 
512  | 0  |                 OSSL_TRACE_BEGIN(ENCODER) { | 
513  | 0  |                     BIO_printf(trc_out,  | 
514  | 0  |                                "[%d]    Skipping because current encoder output structure (%s) != ctx output structure (%s)\n",  | 
515  | 0  |                                data->level,  | 
516  | 0  |                                current_output_structure,  | 
517  | 0  |                                data->ctx->output_structure);  | 
518  | 0  |                 } OSSL_TRACE_END(ENCODER);  | 
519  | 0  |                 continue;  | 
520  | 0  |             }  | 
521  |  |  | 
522  | 0  |             data->count_output_structure++;  | 
523  | 0  |         }  | 
524  |  |  | 
525  |  |         /*  | 
526  |  |          * Recurse to process the encoder implementations before the current  | 
527  |  |          * one.  | 
528  |  |          */  | 
529  | 0  |         ok = encoder_process(&new_data);  | 
530  |  | 
  | 
531  | 0  |         data->prev_encoder_inst = new_data.prev_encoder_inst;  | 
532  | 0  |         data->running_output = new_data.running_output;  | 
533  | 0  |         data->running_output_length = new_data.running_output_length;  | 
534  |  |  | 
535  |  |         /*  | 
536  |  |          * ok == -1     means that the recursion call above gave no further  | 
537  |  |          *              encoders, and that the one we're currently at should  | 
538  |  |          *              be tried.  | 
539  |  |          * ok == 0      means that something failed in the recursion call  | 
540  |  |          *              above, making the result unsuitable for a chain.  | 
541  |  |          *              In this case, we simply continue to try finding a  | 
542  |  |          *              suitable encoder at this recursion level.  | 
543  |  |          * ok == 1      means that the recursion call was successful, and we  | 
544  |  |          *              try to use the result at this recursion level.  | 
545  |  |          */  | 
546  | 0  |         if (ok != 0)  | 
547  | 0  |             break;  | 
548  |  |  | 
549  | 0  |         OSSL_TRACE_BEGIN(ENCODER) { | 
550  | 0  |             BIO_printf(trc_out,  | 
551  | 0  |                        "[%d]    Skipping because recursion level %d failed\n",  | 
552  | 0  |                        data->level, new_data.level);  | 
553  | 0  |         } OSSL_TRACE_END(ENCODER);  | 
554  | 0  |     }  | 
555  |  |  | 
556  |  |     /*  | 
557  |  |      * If |i < 0|, we didn't find any useful encoder in this recursion, so  | 
558  |  |      * we do the rest of the process only if |i >= 0|.  | 
559  |  |      */  | 
560  | 0  |     if (i < 0) { | 
561  | 0  |         ok = -1;  | 
562  |  | 
  | 
563  | 0  |         OSSL_TRACE_BEGIN(ENCODER) { | 
564  | 0  |             BIO_printf(trc_out,  | 
565  | 0  |                        "[%d] (ctx %p) No suitable encoder found\n",  | 
566  | 0  |                        data->level, (void *)data->ctx);  | 
567  | 0  |         } OSSL_TRACE_END(ENCODER);  | 
568  | 0  |     } else { | 
569  |  |         /* Preparations */  | 
570  |  | 
  | 
571  | 0  |         switch (ok) { | 
572  | 0  |         case 0:  | 
573  | 0  |             break;  | 
574  | 0  |         case -1:  | 
575  |  |             /*  | 
576  |  |              * We have reached the beginning of the encoder instance sequence,  | 
577  |  |              * so we prepare the object to be encoded.  | 
578  |  |              */  | 
579  |  |  | 
580  |  |             /*  | 
581  |  |              * |data->count_output_structure| is one of these values:  | 
582  |  |              *  | 
583  |  |              * -1       There is no desired output structure  | 
584  |  |              *  0       There is a desired output structure, and it wasn't  | 
585  |  |              *          matched by any of the encoder instances that were  | 
586  |  |              *          considered  | 
587  |  |              * >0       There is a desired output structure, and at least one  | 
588  |  |              *          of the encoder instances matched it  | 
589  |  |              */  | 
590  | 0  |             if (data->count_output_structure == 0)  | 
591  | 0  |                 return 0;  | 
592  |  |  | 
593  | 0  |             original_data =  | 
594  | 0  |                 data->ctx->construct(current_encoder_inst,  | 
595  | 0  |                                      data->ctx->construct_data);  | 
596  |  |  | 
597  |  |             /* Also set the data type, using the encoder implementation name */  | 
598  | 0  |             data->data_type = OSSL_ENCODER_get0_name(current_encoder);  | 
599  |  |  | 
600  |  |             /* Assume that the constructor recorded an error */  | 
601  | 0  |             if (original_data != NULL)  | 
602  | 0  |                 ok = 1;  | 
603  | 0  |             else  | 
604  | 0  |                 ok = 0;  | 
605  | 0  |             break;  | 
606  | 0  |         case 1:  | 
607  | 0  |             if (!ossl_assert(data->running_output != NULL)) { | 
608  | 0  |                 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);  | 
609  | 0  |                 ok = 0;  | 
610  | 0  |                 break;  | 
611  | 0  |             }  | 
612  |  |  | 
613  | 0  |             { | 
614  |  |                 /*  | 
615  |  |                  * Create an object abstraction from the latest output, which  | 
616  |  |                  * was stolen from the previous round.  | 
617  |  |                  */  | 
618  |  | 
  | 
619  | 0  |                 OSSL_PARAM *abstract_p = abstract;  | 
620  | 0  |                 const char *prev_output_structure =  | 
621  | 0  |                     OSSL_ENCODER_INSTANCE_get_output_structure(data->prev_encoder_inst);  | 
622  |  | 
  | 
623  | 0  |                 *abstract_p++ =  | 
624  | 0  |                     OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,  | 
625  | 0  |                                                      (char *)data->data_type, 0);  | 
626  | 0  |                 if (prev_output_structure != NULL)  | 
627  | 0  |                     *abstract_p++ =  | 
628  | 0  |                         OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,  | 
629  | 0  |                                                          (char *)prev_output_structure,  | 
630  | 0  |                                                          0);  | 
631  | 0  |                 *abstract_p++ =  | 
632  | 0  |                     OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,  | 
633  | 0  |                                                       data->running_output,  | 
634  | 0  |                                                       data->running_output_length);  | 
635  | 0  |                 *abstract_p = OSSL_PARAM_construct_end();  | 
636  | 0  |                 current_abstract = abstract;  | 
637  | 0  |             }  | 
638  | 0  |             break;  | 
639  | 0  |         }  | 
640  |  |  | 
641  |  |         /* Calling the encoder implementation */  | 
642  |  |  | 
643  | 0  |         if (ok) { | 
644  | 0  |             OSSL_CORE_BIO *cbio = NULL;  | 
645  | 0  |             BIO *current_out = NULL;  | 
646  |  |  | 
647  |  |             /*  | 
648  |  |              * If we're at the last encoder instance to use, we're setting up  | 
649  |  |              * final output.  Otherwise, set up an intermediary memory output.  | 
650  |  |              */  | 
651  | 0  |             if (top)  | 
652  | 0  |                 current_out = data->bio;  | 
653  | 0  |             else if ((current_out = allocated_out = BIO_new(BIO_s_mem()))  | 
654  | 0  |                      == NULL)  | 
655  | 0  |                 ok = 0;     /* Assume BIO_new() recorded an error */  | 
656  |  | 
  | 
657  | 0  |             if (ok)  | 
658  | 0  |                 ok = (cbio = ossl_core_bio_new_from_bio(current_out)) != NULL;  | 
659  | 0  |             if (ok) { | 
660  | 0  |                 ok = current_encoder->encode(current_encoder_ctx, cbio,  | 
661  | 0  |                                              original_data, current_abstract,  | 
662  | 0  |                                              data->ctx->selection,  | 
663  | 0  |                                              ossl_pw_passphrase_callback_enc,  | 
664  | 0  |                                              &data->ctx->pwdata);  | 
665  | 0  |                 OSSL_TRACE_BEGIN(ENCODER) { | 
666  | 0  |                     BIO_printf(trc_out,  | 
667  | 0  |                                "[%d] (ctx %p) Running encoder instance %p => %d\n",  | 
668  | 0  |                                data->level, (void *)data->ctx,  | 
669  | 0  |                                (void *)current_encoder_inst, ok);  | 
670  | 0  |                 } OSSL_TRACE_END(ENCODER);  | 
671  | 0  |             }  | 
672  |  | 
  | 
673  | 0  |             ossl_core_bio_free(cbio);  | 
674  | 0  |             data->prev_encoder_inst = current_encoder_inst;  | 
675  | 0  |         }  | 
676  | 0  |     }  | 
677  |  |  | 
678  |  |     /* Cleanup and collecting the result */  | 
679  |  |  | 
680  | 0  |     OPENSSL_free(data->running_output);  | 
681  | 0  |     data->running_output = NULL;  | 
682  |  |  | 
683  |  |     /*  | 
684  |  |      * Steal the output from the BIO_s_mem, if we did allocate one.  | 
685  |  |      * That'll be the data for an object abstraction in the next round.  | 
686  |  |      */  | 
687  | 0  |     if (allocated_out != NULL) { | 
688  | 0  |         BUF_MEM *buf;  | 
689  |  | 
  | 
690  | 0  |         BIO_get_mem_ptr(allocated_out, &buf);  | 
691  | 0  |         data->running_output = (unsigned char *)buf->data;  | 
692  | 0  |         data->running_output_length = buf->length;  | 
693  | 0  |         memset(buf, 0, sizeof(*buf));  | 
694  | 0  |     }  | 
695  |  | 
  | 
696  | 0  |     BIO_free(allocated_out);  | 
697  | 0  |     if (original_data != NULL)  | 
698  | 0  |         data->ctx->cleanup(data->ctx->construct_data);  | 
699  | 0  |     return ok;  | 
700  | 0  | }  | 
701  |  |  | 
702  |  | int ossl_bio_print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn)  | 
703  | 0  | { | 
704  | 0  |     int ret = 0, use_sep = 0;  | 
705  | 0  |     char *hex_str = NULL, *p;  | 
706  | 0  |     const char spaces[] = "    ";  | 
707  | 0  |     const char *post_label_spc = " ";  | 
708  |  | 
  | 
709  | 0  |     const char *neg = "";  | 
710  | 0  |     int bytes;  | 
711  |  | 
  | 
712  | 0  |     if (bn == NULL)  | 
713  | 0  |         return 0;  | 
714  | 0  |     if (label == NULL) { | 
715  | 0  |         label = "";  | 
716  | 0  |         post_label_spc = "";  | 
717  | 0  |     }  | 
718  |  | 
  | 
719  | 0  |     if (BN_is_zero(bn))  | 
720  | 0  |         return BIO_printf(out, "%s%s0\n", label, post_label_spc);  | 
721  |  |  | 
722  | 0  |     if (BN_num_bytes(bn) <= BN_BYTES) { | 
723  | 0  |         BN_ULONG *words = bn_get_words(bn);  | 
724  |  | 
  | 
725  | 0  |         if (BN_is_negative(bn))  | 
726  | 0  |             neg = "-";  | 
727  |  | 
  | 
728  | 0  |         return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",  | 
729  | 0  |                           label, post_label_spc, neg, words[0], neg, words[0]);  | 
730  | 0  |     }  | 
731  |  |  | 
732  | 0  |     hex_str = BN_bn2hex(bn);  | 
733  | 0  |     if (hex_str == NULL)  | 
734  | 0  |         return 0;  | 
735  |  |  | 
736  | 0  |     p = hex_str;  | 
737  | 0  |     if (*p == '-') { | 
738  | 0  |         ++p;  | 
739  | 0  |         neg = " (Negative)";  | 
740  | 0  |     }  | 
741  | 0  |     if (BIO_printf(out, "%s%s\n", label, neg) <= 0)  | 
742  | 0  |         goto err;  | 
743  |  |  | 
744  |  |     /* Keep track of how many bytes we have printed out so far */  | 
745  | 0  |     bytes = 0;  | 
746  |  | 
  | 
747  | 0  |     if (BIO_printf(out, "%s", spaces) <= 0)  | 
748  | 0  |         goto err;  | 
749  |  |  | 
750  |  |     /* Add a leading 00 if the top bit is set */  | 
751  | 0  |     if (*p >= '8') { | 
752  | 0  |         if (BIO_printf(out, "%02x", 0) <= 0)  | 
753  | 0  |             goto err;  | 
754  | 0  |         ++bytes;  | 
755  | 0  |         use_sep = 1;  | 
756  | 0  |     }  | 
757  | 0  |     while (*p != '\0') { | 
758  |  |         /* Do a newline after every 15 hex bytes + add the space indent */  | 
759  | 0  |         if ((bytes % 15) == 0 && bytes > 0) { | 
760  | 0  |             if (BIO_printf(out, ":\n%s", spaces) <= 0)  | 
761  | 0  |                 goto err;  | 
762  | 0  |             use_sep = 0; /* The first byte on the next line doesn't have a : */  | 
763  | 0  |         }  | 
764  | 0  |         if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "",  | 
765  | 0  |                        tolower((unsigned char)p[0]),  | 
766  | 0  |                        tolower((unsigned char)p[1])) <= 0)  | 
767  | 0  |             goto err;  | 
768  | 0  |         ++bytes;  | 
769  | 0  |         p += 2;  | 
770  | 0  |         use_sep = 1;  | 
771  | 0  |     }  | 
772  | 0  |     if (BIO_printf(out, "\n") <= 0)  | 
773  | 0  |         goto err;  | 
774  | 0  |     ret = 1;  | 
775  | 0  | err:  | 
776  | 0  |     OPENSSL_free(hex_str);  | 
777  | 0  |     return ret;  | 
778  | 0  | }  | 
779  |  |  | 
780  |  | int ossl_bio_print_labeled_buf(BIO *out, const char *label,  | 
781  |  |                            const unsigned char *buf, size_t buflen)  | 
782  | 0  | { | 
783  | 0  |     size_t i;  | 
784  |  | 
  | 
785  | 0  |     if (BIO_printf(out, "%s\n", label) <= 0)  | 
786  | 0  |         return 0;  | 
787  |  |  | 
788  | 0  |     for (i = 0; i < buflen; i++) { | 
789  | 0  |         if ((i % LABELED_BUF_PRINT_WIDTH) == 0) { | 
790  | 0  |             if (i > 0 && BIO_printf(out, "\n") <= 0)  | 
791  | 0  |                 return 0;  | 
792  | 0  |             if (BIO_printf(out, "    ") <= 0)  | 
793  | 0  |                 return 0;  | 
794  | 0  |         }  | 
795  |  |  | 
796  | 0  |         if (BIO_printf(out, "%02x%s", buf[i],  | 
797  | 0  |                                  (i == buflen - 1) ? "" : ":") <= 0)  | 
798  | 0  |             return 0;  | 
799  | 0  |     }  | 
800  | 0  |     if (BIO_printf(out, "\n") <= 0)  | 
801  | 0  |         return 0;  | 
802  |  |  | 
803  | 0  |     return 1;  | 
804  | 0  | }  | 
805  |  |  | 
806  |  | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)  | 
807  |  | int ossl_bio_print_ffc_params(BIO *out, const FFC_PARAMS *ffc)  | 
808  | 0  | { | 
809  | 0  |     if (ffc->nid != NID_undef) { | 
810  | 0  | #ifndef OPENSSL_NO_DH  | 
811  | 0  |         const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid);  | 
812  | 0  |         const char *name = ossl_ffc_named_group_get_name(group);  | 
813  |  | 
  | 
814  | 0  |         if (name == NULL)  | 
815  | 0  |             goto err;  | 
816  | 0  |         if (BIO_printf(out, "GROUP: %s\n", name) <= 0)  | 
817  | 0  |             goto err;  | 
818  | 0  |         return 1;  | 
819  |  | #else  | 
820  |  |         /* How could this be? We should not have a nid in a no-dh build. */  | 
821  |  |         goto err;  | 
822  |  | #endif  | 
823  | 0  |     }  | 
824  |  |  | 
825  | 0  |     if (!ossl_bio_print_labeled_bignum(out, "P:   ", ffc->p))  | 
826  | 0  |         goto err;  | 
827  | 0  |     if (ffc->q != NULL) { | 
828  | 0  |         if (!ossl_bio_print_labeled_bignum(out, "Q:   ", ffc->q))  | 
829  | 0  |             goto err;  | 
830  | 0  |     }  | 
831  | 0  |     if (!ossl_bio_print_labeled_bignum(out, "G:   ", ffc->g))  | 
832  | 0  |         goto err;  | 
833  | 0  |     if (ffc->j != NULL) { | 
834  | 0  |         if (!ossl_bio_print_labeled_bignum(out, "J:   ", ffc->j))  | 
835  | 0  |             goto err;  | 
836  | 0  |     }  | 
837  | 0  |     if (ffc->seed != NULL) { | 
838  | 0  |         if (!ossl_bio_print_labeled_buf(out, "SEED:", ffc->seed, ffc->seedlen))  | 
839  | 0  |             goto err;  | 
840  | 0  |     }  | 
841  | 0  |     if (ffc->gindex != -1) { | 
842  | 0  |         if (BIO_printf(out, "gindex: %d\n", ffc->gindex) <= 0)  | 
843  | 0  |             goto err;  | 
844  | 0  |     }  | 
845  | 0  |     if (ffc->pcounter != -1) { | 
846  | 0  |         if (BIO_printf(out, "pcounter: %d\n", ffc->pcounter) <= 0)  | 
847  | 0  |             goto err;  | 
848  | 0  |     }  | 
849  | 0  |     if (ffc->h != 0) { | 
850  | 0  |         if (BIO_printf(out, "h: %d\n", ffc->h) <= 0)  | 
851  | 0  |             goto err;  | 
852  | 0  |     }  | 
853  | 0  |     return 1;  | 
854  | 0  | err:  | 
855  | 0  |     return 0;  | 
856  | 0  | }  | 
857  |  |  | 
858  |  | #endif  |