xref: /freebsd/crypto/openssl/doc/man3/OSSL_DECODER_from_bio.pod (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1=pod
2
3=head1 NAME
4
5OSSL_DECODER_from_data,
6OSSL_DECODER_from_bio,
7OSSL_DECODER_from_fp
8- Routines to perform a decoding
9
10=head1 SYNOPSIS
11
12 #include <openssl/decoder.h>
13
14 int OSSL_DECODER_from_bio(OSSL_DECODER_CTX *ctx, BIO *in);
15 int OSSL_DECODER_from_fp(OSSL_DECODER_CTX *ctx, FILE *fp);
16 int OSSL_DECODER_from_data(OSSL_DECODER_CTX *ctx, const unsigned char **pdata,
17                            size_t *pdata_len);
18
19Feature availability macros:
20
21=over 4
22
23=item OSSL_DECODER_from_fp() is only available when B<OPENSSL_NO_STDIO>
24is undefined.
25
26=back
27
28=head1 DESCRIPTION
29
30OSSL_DECODER_from_data() runs the decoding process for the context I<ctx>,
31with input coming from I<*pdata>, I<*pdata_len> bytes long.  Both I<*pdata>
32and I<*pdata_len> must be non-NULL.  When OSSL_DECODER_from_data() returns,
33I<*pdata> is updated to point at the location after what has been decoded,
34and I<*pdata_len> to have the number of remaining bytes.
35
36OSSL_DECODER_from_bio() runs the decoding process for the context I<ctx>,
37with the input coming from the B<BIO> I<in>.  Should it make a difference,
38it's recommended to have the BIO set in binary mode rather than text mode.
39
40OSSL_DECODER_from_fp() does the same thing as OSSL_DECODER_from_bio(),
41except that the input is coming from the B<FILE> I<fp>.
42
43=head1 RETURN VALUES
44
45OSSL_DECODER_from_bio(), OSSL_DECODER_from_data() and OSSL_DECODER_from_fp()
46return 1 on success, or 0 on failure.
47
48=head1 EXAMPLES
49
50To decode an RSA key encoded with PEM from a bio:
51
52 OSSL_DECODER_CTX *dctx;
53 EVP_PKEY *pkey = NULL;
54 const char *format = "PEM";   /* NULL for any format */
55 const char *structure = NULL; /* any structure */
56 const char *keytype = "RSA";  /* NULL for any key */
57 const unsigned char *pass = "my password";
58
59 dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, format, structure,
60                                      keytype,
61                                      OSSL_KEYMGMT_SELECT_KEYPAIR,
62                                      NULL, NULL);
63 if (dctx == NULL) {
64     /* error: no suitable potential decoders found */
65 }
66 if (pass != NULL)
67     OSSL_DECODER_CTX_set_passphrase(dctx, pass, strlen(pass));
68 if (OSSL_DECODER_from_bio(dctx, bio)) {
69     /* pkey is created with the decoded data from the bio */
70 } else {
71     /* decoding failure */
72 }
73 OSSL_DECODER_CTX_free(dctx);
74
75To decode an EC key encoded with DER from a buffer:
76
77 OSSL_DECODER_CTX *dctx;
78 EVP_PKEY *pkey = NULL;
79 const char *format = "DER";   /* NULL for any format */
80 const char *structure = NULL; /* any structure */
81 const char *keytype = "EC";   /* NULL for any key */
82 const unsigned char *pass = NULL
83 const unsigned char *data = buffer;
84 size_t datalen = sizeof(buffer);
85
86 dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, format, structure,
87                                      keytype,
88                                      OSSL_KEYMGMT_SELECT_KEYPAIR
89                                      | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
90                                      NULL, NULL);
91 if (dctx == NULL) {
92     /* error: no suitable potential decoders found */
93 }
94 if (pass != NULL)
95     OSSL_DECODER_CTX_set_passphrase(dctx, pass, strlen(pass));
96 if (OSSL_DECODER_from_data(dctx, &data, &datalen)) {
97     /* pkey is created with the decoded data from the buffer */
98 } else {
99     /* decoding failure */
100 }
101 OSSL_DECODER_CTX_free(dctx);
102
103=head1 SEE ALSO
104
105L<provider(7)>, L<OSSL_DECODER_CTX(3)>
106
107=head1 HISTORY
108
109The functions described here were added in OpenSSL 3.0.
110
111=head1 COPYRIGHT
112
113Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
114
115Licensed under the Apache License 2.0 (the "License").  You may not use
116this file except in compliance with the License.  You can obtain a copy
117in the file LICENSE in the source distribution or at
118L<https://www.openssl.org/source/license.html>.
119
120=cut
121