1OSSL_ENCODER_TO_BIO(3ossl)          OpenSSL         OSSL_ENCODER_TO_BIO(3ossl)
2
3
4

NAME

6       OSSL_ENCODER_to_data, OSSL_ENCODER_to_bio, OSSL_ENCODER_to_fp -
7       Routines to perform an encoding
8

SYNOPSIS

10        #include <openssl/encoder.h>
11
12        int OSSL_ENCODER_to_data(OSSL_ENCODER_CTX *ctx, unsigned char **pdata,
13                                 size_t *pdata_len);
14        int OSSL_ENCODER_to_bio(OSSL_ENCODER_CTX *ctx, BIO *out);
15        int OSSL_ENCODER_to_fp(OSSL_ENCODER_CTX *ctx, FILE *fp);
16
17       Feature availability macros:
18
19       OSSL_ENCODER_to_fp() is only available when OPENSSL_NO_STDIO is
20       undefined.
21

DESCRIPTION

23       OSSL_ENCODER_to_data() runs the encoding process for the context ctx,
24       with the output going to the *pdata and *pdata_len.  If *pdata is NULL
25       when OSSL_ENCODER_to_data() is called, a buffer will be allocated using
26       OPENSSL_zalloc(3), and *pdata will be set to point at the start of that
27       buffer, and *pdata_len will be assigned its length when
28       OSSL_ENCODER_to_data() returns.  If *pdata is non-NULL when
29       OSSL_ENCODER_to_data() is called, *pdata_len is assumed to have its
30       size.  In this case, *pdata will be set to point after the encoded
31       bytes, and *pdata_len will be assigned the number of remaining bytes.
32
33       OSSL_ENCODER_to_bio() runs the encoding process for the context ctx,
34       with the output going to the BIO out.
35
36       OSSL_ENCODER_to_fp() does the same thing as OSSL_ENCODER_to_bio(),
37       except that the output is going to the FILE fp.
38
39       For OSSL_ENCODER_to_bio() and OSSL_ENCODER_to_fp(), the application is
40       required to set up the BIO or FILE properly, for example to have it in
41       text or binary mode as is appropriate for the encoder output type.
42

RETURN VALUES

44       OSSL_ENCODER_to_bio(), OSSL_ENCODER_to_fp() and OSSL_ENCODER_to_data()
45       return 1 on success, or 0 on failure.
46

EXAMPLES

48       To encode a pkey as PKCS#8 with PEM format into a bio:
49
50        OSSL_ENCODER_CTX *ectx;
51        const char *format = "PEM";
52        const char *structure = "PrivateKeyInfo"; /* PKCS#8 structure */
53        const unsigned char *pass = "my password";
54
55        ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey,
56                                             OSSL_KEYMGMT_SELECT_KEYPAIR
57                                             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
58                                             format, structure,
59                                             NULL);
60        if (ectx == NULL) {
61            /* error: no suitable potential encoders found */
62        }
63        if (pass != NULL)
64            OSSL_ENCODER_CTX_set_passphrase(ectx, pass, strlen(pass));
65        if (OSSL_ENCODER_to_bio(ectx, bio)) {
66            /* pkey was successfully encoded into the bio */
67        } else {
68            /* encoding failure */
69        }
70        OSSL_ENCODER_CTX_free(ectx);
71
72       To encode a pkey as PKCS#8 with DER format encrypted with AES-256-CBC
73       into a buffer:
74
75        OSSL_ENCODER_CTX *ectx;
76        const char *format = "DER";
77        const char *structure = "PrivateKeyInfo"; /* PKCS#8 structure */
78        const unsigned char *pass = "my password";
79        unsigned char *data = NULL;
80        size_t datalen;
81
82        ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey,
83                                             OSSL_KEYMGMT_SELECT_KEYPAIR
84                                             | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
85                                             format, structure,
86                                             NULL);
87        if (ectx == NULL) {
88            /* error: no suitable potential encoders found */
89        }
90        if (pass != NULL) {
91            OSSL_ENCODER_CTX_set_passphrase(ectx, pass, strlen(pass));
92            OSSL_ENCODER_CTX_set_cipher(ctx, "AES-256-CBC", NULL);
93        }
94        if (OSSL_ENCODER_to_data(ectx, &data, &datalen)) {
95            /*
96             * pkey was successfully encoded into a newly allocated
97             * data buffer
98             */
99        } else {
100            /* encoding failure */
101        }
102        OSSL_ENCODER_CTX_free(ectx);
103

SEE ALSO

105       provider(7), OSSL_ENCODER_CTX(3)
106

HISTORY

108       The functions described here were added in OpenSSL 3.0.
109
111       Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
112
113       Licensed under the Apache License 2.0 (the "License").  You may not use
114       this file except in compliance with the License.  You can obtain a copy
115       in the file LICENSE in the source distribution or at
116       <https://www.openssl.org/source/license.html>.
117
118
119
1203.0.5                             2022-07-05        OSSL_ENCODER_TO_BIO(3ossl)
Impressum