xref: /freebsd/crypto/openssl/doc/man3/PEM_read.pod (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimPEM_write, PEM_write_bio,
6e71b7053SJung-uk KimPEM_read, PEM_read_bio, PEM_do_header, PEM_get_EVP_CIPHER_INFO
7e71b7053SJung-uk Kim- PEM encoding routines
8e71b7053SJung-uk Kim
9e71b7053SJung-uk Kim=head1 SYNOPSIS
10e71b7053SJung-uk Kim
11e71b7053SJung-uk Kim #include <openssl/pem.h>
12e71b7053SJung-uk Kim
13e71b7053SJung-uk Kim int PEM_write(FILE *fp, const char *name, const char *header,
14*b077aed3SPierre Pronchery               const unsigned char *data, long len);
15e71b7053SJung-uk Kim int PEM_write_bio(BIO *bp, const char *name, const char *header,
16*b077aed3SPierre Pronchery                   const unsigned char *data, long len);
17e71b7053SJung-uk Kim
18e71b7053SJung-uk Kim int PEM_read(FILE *fp, char **name, char **header,
19e71b7053SJung-uk Kim              unsigned char **data, long *len);
20e71b7053SJung-uk Kim int PEM_read_bio(BIO *bp, char **name, char **header,
21e71b7053SJung-uk Kim                  unsigned char **data, long *len);
22e71b7053SJung-uk Kim
23e71b7053SJung-uk Kim int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo);
24e71b7053SJung-uk Kim int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len,
25e71b7053SJung-uk Kim                   pem_password_cb *cb, void *u);
26e71b7053SJung-uk Kim
27e71b7053SJung-uk Kim=head1 DESCRIPTION
28e71b7053SJung-uk Kim
29e71b7053SJung-uk KimThese functions read and write PEM-encoded objects, using the PEM
30e71b7053SJung-uk Kimtype B<name>, any additional B<header> information, and the raw
31e71b7053SJung-uk KimB<data> of length B<len>.
32e71b7053SJung-uk Kim
33e71b7053SJung-uk KimPEM is the term used for binary content encoding first defined in IETF
34e71b7053SJung-uk KimRFC 1421.  The content is a series of base64-encoded lines, surrounded
35e71b7053SJung-uk Kimby begin/end markers each on their own line.  For example:
36e71b7053SJung-uk Kim
37e71b7053SJung-uk Kim -----BEGIN PRIVATE KEY-----
38e71b7053SJung-uk Kim MIICdg....
39e71b7053SJung-uk Kim ... bhTQ==
40e71b7053SJung-uk Kim -----END PRIVATE KEY-----
41e71b7053SJung-uk Kim
42e71b7053SJung-uk KimOptional header line(s) may appear after the begin line, and their
43e71b7053SJung-uk Kimexistence depends on the type of object being written or read.
44e71b7053SJung-uk Kim
45e71b7053SJung-uk KimPEM_write() writes to the file B<fp>, while PEM_write_bio() writes to
46e71b7053SJung-uk Kimthe BIO B<bp>.  The B<name> is the name to use in the marker, the
47e71b7053SJung-uk KimB<header> is the header value or NULL, and B<data> and B<len> specify
48e71b7053SJung-uk Kimthe data and its length.
49e71b7053SJung-uk Kim
50e71b7053SJung-uk KimThe final B<data> buffer is typically an ASN.1 object which can be decoded with
51e71b7053SJung-uk Kimthe B<d2i> function appropriate to the type B<name>; see L<d2i_X509(3)>
52e71b7053SJung-uk Kimfor examples.
53e71b7053SJung-uk Kim
54e71b7053SJung-uk KimPEM_read() reads from the file B<fp>, while PEM_read_bio() reads
55e71b7053SJung-uk Kimfrom the BIO B<bp>.
56e71b7053SJung-uk KimBoth skip any non-PEM data that precedes the start of the next PEM object.
57e71b7053SJung-uk KimWhen an object is successfully retrieved, the type name from the "----BEGIN
58e71b7053SJung-uk Kim<type>-----" is returned via the B<name> argument, any encapsulation headers
59e71b7053SJung-uk Kimare returned in B<header> and the base64-decoded content and its length are
60e71b7053SJung-uk Kimreturned via B<data> and B<len> respectively.
61e71b7053SJung-uk KimThe B<name>, B<header> and B<data> pointers are allocated via OPENSSL_malloc()
62e71b7053SJung-uk Kimand should be freed by the caller via OPENSSL_free() when no longer needed.
63e71b7053SJung-uk Kim
64e71b7053SJung-uk KimPEM_get_EVP_CIPHER_INFO() can be used to determine the B<data> returned by
65e71b7053SJung-uk KimPEM_read() or PEM_read_bio() is encrypted and to retrieve the associated cipher
66e71b7053SJung-uk Kimand IV.
67e71b7053SJung-uk KimThe caller passes a pointer to structure of type B<EVP_CIPHER_INFO> via the
68e71b7053SJung-uk KimB<cinfo> argument and the B<header> returned via PEM_read() or PEM_read_bio().
69e71b7053SJung-uk KimIf the call is successful 1 is returned and the cipher and IV are stored at the
70e71b7053SJung-uk Kimaddress pointed to by B<cinfo>.
71e71b7053SJung-uk KimWhen the header is malformed, or not supported or when the cipher is unknown
72e71b7053SJung-uk Kimor some internal error happens 0 is returned.
73e71b7053SJung-uk KimThis function is deprecated, see B<NOTES> below.
74e71b7053SJung-uk Kim
75e71b7053SJung-uk KimPEM_do_header() can then be used to decrypt the data if the header
76e71b7053SJung-uk Kimindicates encryption.
77e71b7053SJung-uk KimThe B<cinfo> argument is a pointer to the structure initialized by the previous
78e71b7053SJung-uk Kimcall to PEM_get_EVP_CIPHER_INFO().
79e71b7053SJung-uk KimThe B<data> and B<len> arguments are those returned by the previous call to
80e71b7053SJung-uk KimPEM_read() or PEM_read_bio().
81e71b7053SJung-uk KimThe B<cb> and B<u> arguments make it possible to override the default password
82e71b7053SJung-uk Kimprompt function as described in L<PEM_read_PrivateKey(3)>.
83e71b7053SJung-uk KimOn successful completion the B<data> is decrypted in place, and B<len> is
84e71b7053SJung-uk Kimupdated to indicate the plaintext length.
85e71b7053SJung-uk KimThis function is deprecated, see B<NOTES> below.
86e71b7053SJung-uk Kim
87e71b7053SJung-uk KimIf the data is a priori known to not be encrypted, then neither PEM_do_header()
88e71b7053SJung-uk Kimnor PEM_get_EVP_CIPHER_INFO() need be called.
89e71b7053SJung-uk Kim
90e71b7053SJung-uk Kim=head1 RETURN VALUES
91e71b7053SJung-uk Kim
92e71b7053SJung-uk KimPEM_read() and PEM_read_bio() return 1 on success and 0 on failure, the latter
93e71b7053SJung-uk Kimincludes the case when no more PEM objects remain in the input file.
94e71b7053SJung-uk KimTo distinguish end of file from more serious errors the caller must peek at the
95e71b7053SJung-uk Kimerror stack and check for B<PEM_R_NO_START_LINE>, which indicates that no more
96e71b7053SJung-uk KimPEM objects were found.  See L<ERR_peek_last_error(3)>, L<ERR_GET_REASON(3)>.
97e71b7053SJung-uk Kim
98e71b7053SJung-uk KimPEM_get_EVP_CIPHER_INFO() and PEM_do_header() return 1 on success, and 0 on
99e71b7053SJung-uk Kimfailure.
100e71b7053SJung-uk KimThe B<data> is likely meaningless if these functions fail.
101e71b7053SJung-uk Kim
102e71b7053SJung-uk Kim=head1 NOTES
103e71b7053SJung-uk Kim
104e71b7053SJung-uk KimThe PEM_get_EVP_CIPHER_INFO() and PEM_do_header() functions are deprecated.
105e71b7053SJung-uk KimThis is because the underlying PEM encryption format is obsolete, and should
106e71b7053SJung-uk Kimbe avoided.
107e71b7053SJung-uk KimIt uses an encryption format with an OpenSSL-specific key-derivation function,
108e71b7053SJung-uk Kimwhich employs MD5 with an iteration count of 1!
109e71b7053SJung-uk KimInstead, private keys should be stored in PKCS#8 form, with a strong PKCS#5
110e71b7053SJung-uk Kimv2.0 PBE.
111e71b7053SJung-uk KimSee L<PEM_write_PrivateKey(3)> and L<d2i_PKCS8PrivateKey_bio(3)>.
112e71b7053SJung-uk Kim
113e71b7053SJung-uk KimPEM_do_header() makes no assumption regarding the pass phrase received from the
114e71b7053SJung-uk Kimpassword callback.
115e71b7053SJung-uk KimIt will simply be treated as a byte sequence.
116e71b7053SJung-uk Kim
117e71b7053SJung-uk Kim=head1 SEE ALSO
118e71b7053SJung-uk Kim
119e71b7053SJung-uk KimL<ERR_peek_last_error(3)>, L<ERR_GET_LIB(3)>,
120e71b7053SJung-uk KimL<d2i_PKCS8PrivateKey_bio(3)>,
121e71b7053SJung-uk KimL<passphrase-encoding(7)>
122e71b7053SJung-uk Kim
123e71b7053SJung-uk Kim=head1 COPYRIGHT
124e71b7053SJung-uk Kim
125*b077aed3SPierre ProncheryCopyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
126e71b7053SJung-uk Kim
127*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
128e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
129e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
130e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
131e71b7053SJung-uk Kim
132e71b7053SJung-uk Kim=cut
133