7123dd8934f84f63c7a6cdc1b5598d3338ce953e
[openssl.git] / doc / crypto / pem_read.pod
1 =pod
2
3 =head1 NAME
4
5 PEM_read, PEM_read_bio, PEM_do_header - low-level PEM routines
6
7 =head1 SYNOPSIS
8
9  #include <openssl/pem.h>
10
11  int PEM_read(FILE *fp, char **name, char **header,
12               unsigned char **data, long *len);
13  int PEM_read_bio(BIO *bp, char **name, char **header,
14                   unsigned char **data, long *len);
15  int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo);
16  int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len,
17                    pem_password_cb *cb, void *u);
18
19 =head1 DESCRIPTION
20
21 These functions read and decode PEM-encoded objects, returning the
22 PEM type B<name>, any encapsulation B<header> and the decoded
23 B<data> of length B<len>.
24
25 PEM_read() reads from the stdio file handle B<fp>, while PEM_read_bio() reads
26 from the BIO B<bio>.
27 Both skip any non-PEM data that precedes the start of the next PEM object.
28 When an object is successfuly retrieved, the type name from the "----BEGIN
29 <type>-----" is returned via the B<name> argument, any encapsulation headers
30 are returned in B<header> and the base64-decoded content and its length are
31 returned via B<data> and B<len> respectively.
32 The B<name>, B<header> and B<data> pointers are allocated via OPENSSL_malloc()
33 and should be freed by the caller via OPENSSL_free() when no longer needed.
34
35 PEM_get_EVP_CIPHER_INFO() can be used to determine the B<data> returned by
36 PEM_read() or PEM_read_bio() is encrypted and to retrieve the associated cipher
37 and IV.
38 The caller passes a pointer to structure of type B<EVP_CIPHER_INFO> via the
39 B<cinfo> argument and the B<header> returned via PEM_read() or PEM_read_bio().
40 If the call is succesful 1 is retured and the cipher and IV are stored at the
41 address pointed to by B<cinfo>.
42 When the header is malformed, or not supported or when the cipher is unknown
43 or some internal error happens 0 is returned.
44 This function is deprecated, see B<NOTES> below.
45
46 PEM_do_header() can then be used to decrypt the data if the header
47 indicates encryption.
48 The B<cinfo> argument is a pointer to the structure initialized by the previous
49 call to PEM_get_EVP_CIPHER_INFO().
50 The B<data> and B<len> arguments are those returned by the previous call to
51 PEM_read() or PEM_read_bio().
52 The B<cb> and B<u> arguments make it possible to override the default password
53 prompt function as described in L<pem(3)>.
54 On successful completion the B<data> is decrypted in place, and B<len> is
55 updated to indicate the plaintext length.
56 This function is deprecated, see B<NOTES> below.
57
58 If the data is a priori known to not be encrypted, then neither PEM_do_header()
59 nor PEM_get_EVP_CIPHER_INFO() need be called.
60
61 The final B<data> buffer is typically an ASN.1 object which can be decoded with
62 the B<d2i> function appropriate to the type B<name>.
63
64 =head1 RETURN VALUES
65
66 PEM_read() and PEM_read_bio() return 1 on success and 0 on failure, the latter
67 includes the case when no more PEM objects remain in the input file.
68 To distinguish end of file from more serious errors the caller must peek at the
69 error stack and check for B<PEM_R_NO_START_LINE>, which indicates that no more
70 PEM objects were found.  See L<ERR_peek_last_error(3)>, L<ERR_GET_REASON(3)>.
71
72 PEM_get_EVP_CIPHER_INFO() and PEM_do_header() return 1 on success, and 0 on
73 failure.
74 The B<data> is likely meaningless if these functions fail.
75
76 =head1 NOTES
77
78 The PEM_get_EVP_CIPHER_INFO() and PEM_do_header() functions are deprecated.
79 This is because the underlying PEM encryption format is obsolete, and should
80 be avoided.
81 It uses an encryption format with an OpenSSL-specific key-derivation function,
82 which employs MD5 with an iteration count of 1!
83 Instead, private keys should be stored in PKCS#8 form, with a strong PKCS#5
84 v2.0 PBE.
85 See L<pkcs8(1)> and L<pem(3)> and L<d2i_PKCS8PrivateKey_bio(3)>.
86
87 =head1 SEE ALSO
88
89 L<pem(3)>, L<ERR_peek_last_error(3)>, L<ERR_GET_LIB(3)>, L<pkcs8(1)>,
90 L<d2i_PKCS8PrivateKey_bio(3)>.
91
92 =head1 COPYRIGHT
93
94 Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
95
96 Licensed under the OpenSSL license (the "License").  You may not use
97 this file except in compliance with the License.  You can obtain a copy
98 in the file LICENSE in the source distribution or at
99 L<https://www.openssl.org/source/license.html>.
100
101 =cut