xref: /netbsd-src/crypto/external/bsd/openssl/dist/doc/man3/PEM_read.pod (revision b0d1725196a7921d003d2c66a14f186abda4176b)
113d40330Schristos=pod
213d40330Schristos
313d40330Schristos=head1 NAME
413d40330Schristos
513d40330SchristosPEM_write, PEM_write_bio,
613d40330SchristosPEM_read, PEM_read_bio, PEM_do_header, PEM_get_EVP_CIPHER_INFO
713d40330Schristos- PEM encoding routines
813d40330Schristos
913d40330Schristos=head1 SYNOPSIS
1013d40330Schristos
1113d40330Schristos #include <openssl/pem.h>
1213d40330Schristos
1313d40330Schristos int PEM_write(FILE *fp, const char *name, const char *header,
14*b0d17251Schristos               const unsigned char *data, long len);
1513d40330Schristos int PEM_write_bio(BIO *bp, const char *name, const char *header,
16*b0d17251Schristos                   const unsigned char *data, long len);
1713d40330Schristos
1813d40330Schristos int PEM_read(FILE *fp, char **name, char **header,
1913d40330Schristos              unsigned char **data, long *len);
2013d40330Schristos int PEM_read_bio(BIO *bp, char **name, char **header,
2113d40330Schristos                  unsigned char **data, long *len);
2213d40330Schristos
2313d40330Schristos int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo);
2413d40330Schristos int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len,
2513d40330Schristos                   pem_password_cb *cb, void *u);
2613d40330Schristos
2713d40330Schristos=head1 DESCRIPTION
2813d40330Schristos
2913d40330SchristosThese functions read and write PEM-encoded objects, using the PEM
3013d40330Schristostype B<name>, any additional B<header> information, and the raw
3113d40330SchristosB<data> of length B<len>.
3213d40330Schristos
3313d40330SchristosPEM is the term used for binary content encoding first defined in IETF
3413d40330SchristosRFC 1421.  The content is a series of base64-encoded lines, surrounded
3513d40330Schristosby begin/end markers each on their own line.  For example:
3613d40330Schristos
3713d40330Schristos -----BEGIN PRIVATE KEY-----
3813d40330Schristos MIICdg....
3913d40330Schristos ... bhTQ==
4013d40330Schristos -----END PRIVATE KEY-----
4113d40330Schristos
4213d40330SchristosOptional header line(s) may appear after the begin line, and their
4313d40330Schristosexistence depends on the type of object being written or read.
4413d40330Schristos
4513d40330SchristosPEM_write() writes to the file B<fp>, while PEM_write_bio() writes to
4613d40330Schristosthe BIO B<bp>.  The B<name> is the name to use in the marker, the
4713d40330SchristosB<header> is the header value or NULL, and B<data> and B<len> specify
4813d40330Schristosthe data and its length.
4913d40330Schristos
5013d40330SchristosThe final B<data> buffer is typically an ASN.1 object which can be decoded with
5113d40330Schristosthe B<d2i> function appropriate to the type B<name>; see L<d2i_X509(3)>
5213d40330Schristosfor examples.
5313d40330Schristos
5413d40330SchristosPEM_read() reads from the file B<fp>, while PEM_read_bio() reads
5513d40330Schristosfrom the BIO B<bp>.
5613d40330SchristosBoth skip any non-PEM data that precedes the start of the next PEM object.
5713d40330SchristosWhen an object is successfully retrieved, the type name from the "----BEGIN
5813d40330Schristos<type>-----" is returned via the B<name> argument, any encapsulation headers
5913d40330Schristosare returned in B<header> and the base64-decoded content and its length are
6013d40330Schristosreturned via B<data> and B<len> respectively.
6113d40330SchristosThe B<name>, B<header> and B<data> pointers are allocated via OPENSSL_malloc()
6213d40330Schristosand should be freed by the caller via OPENSSL_free() when no longer needed.
6313d40330Schristos
6413d40330SchristosPEM_get_EVP_CIPHER_INFO() can be used to determine the B<data> returned by
6513d40330SchristosPEM_read() or PEM_read_bio() is encrypted and to retrieve the associated cipher
6613d40330Schristosand IV.
6713d40330SchristosThe caller passes a pointer to structure of type B<EVP_CIPHER_INFO> via the
6813d40330SchristosB<cinfo> argument and the B<header> returned via PEM_read() or PEM_read_bio().
6913d40330SchristosIf the call is successful 1 is returned and the cipher and IV are stored at the
7013d40330Schristosaddress pointed to by B<cinfo>.
7113d40330SchristosWhen the header is malformed, or not supported or when the cipher is unknown
7213d40330Schristosor some internal error happens 0 is returned.
7313d40330SchristosThis function is deprecated, see B<NOTES> below.
7413d40330Schristos
7513d40330SchristosPEM_do_header() can then be used to decrypt the data if the header
7613d40330Schristosindicates encryption.
7713d40330SchristosThe B<cinfo> argument is a pointer to the structure initialized by the previous
7813d40330Schristoscall to PEM_get_EVP_CIPHER_INFO().
7913d40330SchristosThe B<data> and B<len> arguments are those returned by the previous call to
8013d40330SchristosPEM_read() or PEM_read_bio().
8113d40330SchristosThe B<cb> and B<u> arguments make it possible to override the default password
8213d40330Schristosprompt function as described in L<PEM_read_PrivateKey(3)>.
8313d40330SchristosOn successful completion the B<data> is decrypted in place, and B<len> is
8413d40330Schristosupdated to indicate the plaintext length.
8513d40330SchristosThis function is deprecated, see B<NOTES> below.
8613d40330Schristos
8713d40330SchristosIf the data is a priori known to not be encrypted, then neither PEM_do_header()
8813d40330Schristosnor PEM_get_EVP_CIPHER_INFO() need be called.
8913d40330Schristos
9013d40330Schristos=head1 RETURN VALUES
9113d40330Schristos
9213d40330SchristosPEM_read() and PEM_read_bio() return 1 on success and 0 on failure, the latter
9313d40330Schristosincludes the case when no more PEM objects remain in the input file.
9413d40330SchristosTo distinguish end of file from more serious errors the caller must peek at the
9513d40330Schristoserror stack and check for B<PEM_R_NO_START_LINE>, which indicates that no more
9613d40330SchristosPEM objects were found.  See L<ERR_peek_last_error(3)>, L<ERR_GET_REASON(3)>.
9713d40330Schristos
9813d40330SchristosPEM_get_EVP_CIPHER_INFO() and PEM_do_header() return 1 on success, and 0 on
9913d40330Schristosfailure.
10013d40330SchristosThe B<data> is likely meaningless if these functions fail.
10113d40330Schristos
10213d40330Schristos=head1 NOTES
10313d40330Schristos
10413d40330SchristosThe PEM_get_EVP_CIPHER_INFO() and PEM_do_header() functions are deprecated.
10513d40330SchristosThis is because the underlying PEM encryption format is obsolete, and should
10613d40330Schristosbe avoided.
10713d40330SchristosIt uses an encryption format with an OpenSSL-specific key-derivation function,
10813d40330Schristoswhich employs MD5 with an iteration count of 1!
10913d40330SchristosInstead, private keys should be stored in PKCS#8 form, with a strong PKCS#5
11013d40330Schristosv2.0 PBE.
11113d40330SchristosSee L<PEM_write_PrivateKey(3)> and L<d2i_PKCS8PrivateKey_bio(3)>.
11213d40330Schristos
11313d40330SchristosPEM_do_header() makes no assumption regarding the pass phrase received from the
11413d40330Schristospassword callback.
11513d40330SchristosIt will simply be treated as a byte sequence.
11613d40330Schristos
11713d40330Schristos=head1 SEE ALSO
11813d40330Schristos
11913d40330SchristosL<ERR_peek_last_error(3)>, L<ERR_GET_LIB(3)>,
12013d40330SchristosL<d2i_PKCS8PrivateKey_bio(3)>,
12113d40330SchristosL<passphrase-encoding(7)>
12213d40330Schristos
12313d40330Schristos=head1 COPYRIGHT
12413d40330Schristos
125*b0d17251SchristosCopyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
12613d40330Schristos
127*b0d17251SchristosLicensed under the Apache License 2.0 (the "License").  You may not use
12813d40330Schristosthis file except in compliance with the License.  You can obtain a copy
12913d40330Schristosin the file LICENSE in the source distribution or at
13013d40330SchristosL<https://www.openssl.org/source/license.html>.
13113d40330Schristos
13213d40330Schristos=cut
133