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