Write X509_dup, PEM_read, etc.
[openssl.git] / doc / crypto / PEM_read.pod
1 =pod
2
3 =head1 NAME
4
5 PEM_write, PEM_write_bio,
6 PEM_read, PEM_read_bio, PEM_do_header, PEM_get_EVP_CIPHER_INFO,
7 pem_password_cb
8 - PEM encoding routines
9
10 =head1 SYNOPSIS
11
12  #include <openssl/pem.h>
13
14  int PEM_write(FILE *fp, const char *name, const char *header,
15                const unsigned char *data, long len)
16  int PEM_write_bio(BIO *bp, const char *name, const char *header,
17                    const unsigned char *data, long len)
18
19  int PEM_read(FILE *fp, char **name, char **header,
20               unsigned char **data, long *len);
21  int PEM_read_bio(BIO *bp, char **name, char **header,
22                   unsigned char **data, long *len);
23
24  int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cinfo);
25  int PEM_do_header(EVP_CIPHER_INFO *cinfo, unsigned char *data, long *len,
26                    pem_password_cb *cb, void *u);
27
28  typedef int pem_password_cb (char *buf, int size, int rwflag, void *u);
29
30 =head1 DESCRIPTION
31
32 These functions read and write PEM-encoded objects, using the PEM
33 type B<name>, any additional B<header> information, and the raw
34 B<data> of length B<len>.
35
36 PEM is the term used for binary content encoding first defined in IETF
37 RFC 1421.  The content is a series of base64-encoded lines, surrounded
38 by begin/end markers each on their own line.  For example:
39
40  -----BEGIN PRIVATE KEY-----
41  MIICdg....
42  ... bhTQ==
43  -----END PRIVATE KEY-----
44
45 Optional header line(s) may appear after the begin line, and their
46 existence depends on the type of object being written or read.
47
48 PEM_write() writes to the file B<fp>, while PEM_write_bio() writes to
49 the BIO B<bp>.  The B<name> is the name to use in the marker, the
50 B<header> is the header value or NULL, and B<data> and B<len> specify
51 the data and its length.
52
53 The final B<data> buffer is typically an ASN.1 object which can be decoded with
54 the B<d2i> function appropriate to the type B<name>; see L<d2i_X509(3)>
55 for examples.
56
57 PEM_read() reads from the file B<fp>, while PEM_read_bio() reads
58 from the BIO B<bp>.
59 Both skip any non-PEM data that precedes the start of the next PEM object.
60 When an object is successfuly retrieved, the type name from the "----BEGIN
61 <type>-----" is returned via the B<name> argument, any encapsulation headers
62 are returned in B<header> and the base64-decoded content and its length are
63 returned via B<data> and B<len> respectively.
64 The B<name>, B<header> and B<data> pointers are allocated via OPENSSL_malloc()
65 and should be freed by the caller via OPENSSL_free() when no longer needed.
66
67 PEM_get_EVP_CIPHER_INFO() can be used to determine the B<data> returned by
68 PEM_read() or PEM_read_bio() is encrypted and to retrieve the associated cipher
69 and IV.
70 The caller passes a pointer to structure of type B<EVP_CIPHER_INFO> via the
71 B<cinfo> argument and the B<header> returned via PEM_read() or PEM_read_bio().
72 If the call is succesful 1 is retured and the cipher and IV are stored at the
73 address pointed to by B<cinfo>.
74 When the header is malformed, or not supported or when the cipher is unknown
75 or some internal error happens 0 is returned.
76 This function is deprecated, see B<NOTES> below.
77
78 PEM_do_header() can then be used to decrypt the data if the header
79 indicates encryption.
80 The B<cinfo> argument is a pointer to the structure initialized by the previous
81 call to PEM_get_EVP_CIPHER_INFO().
82 The B<data> and B<len> arguments are those returned by the previous call to
83 PEM_read() or PEM_read_bio().
84 The B<cb> and B<u> arguments make it possible to override the default password
85 prompt function as described in L<PEM_read_PrivateKey(3)>.
86 On successful completion the B<data> is decrypted in place, and B<len> is
87 updated to indicate the plaintext length.
88 This function is deprecated, see B<NOTES> below.
89
90 If the data is a priori known to not be encrypted, then neither PEM_do_header()
91 nor PEM_get_EVP_CIPHER_INFO() need be called.
92
93 =head1 RETURN VALUES
94
95 PEM_read() and PEM_read_bio() return 1 on success and 0 on failure, the latter
96 includes the case when no more PEM objects remain in the input file.
97 To distinguish end of file from more serious errors the caller must peek at the
98 error stack and check for B<PEM_R_NO_START_LINE>, which indicates that no more
99 PEM objects were found.  See L<ERR_peek_last_error(3)>, L<ERR_GET_REASON(3)>.
100
101 PEM_get_EVP_CIPHER_INFO() and PEM_do_header() return 1 on success, and 0 on
102 failure.
103 The B<data> is likely meaningless if these functions fail.
104
105 =head1 NOTES
106
107 The PEM_get_EVP_CIPHER_INFO() and PEM_do_header() functions are deprecated.
108 This is because the underlying PEM encryption format is obsolete, and should
109 be avoided.
110 It uses an encryption format with an OpenSSL-specific key-derivation function,
111 which employs MD5 with an iteration count of 1!
112 Instead, private keys should be stored in PKCS#8 form, with a strong PKCS#5
113 v2.0 PBE.
114 See L<PEM_write_PrivateKey(3)> and L<d2i_PKCS8PrivateKey_bio(3)>.
115
116 =head1 SEE ALSO
117
118 L<ERR_peek_last_error(3)>, L<ERR_GET_LIB(3)>,
119 L<d2i_PKCS8PrivateKey_bio(3)>.
120
121 =head1 COPYRIGHT
122
123 Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
124
125 Licensed under the OpenSSL license (the "License").  You may not use
126 this file except in compliance with the License.  You can obtain a copy
127 in the file LICENSE in the source distribution or at
128 L<https://www.openssl.org/source/license.html>.
129
130 =cut