1.\" $OpenBSD: PEM_read.3,v 1.9 2019/06/10 14:58:48 schwarze Exp $ 2.\" OpenSSL 99d63d46 Oct 26 13:56:48 2016 -0400 3.\" 4.\" This file was written by Viktor Dukhovni 5.\" and by Rich Salz <rsalz@openssl.org>. 6.\" Copyright (c) 2016 The OpenSSL Project. All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in 17.\" the documentation and/or other materials provided with the 18.\" distribution. 19.\" 20.\" 3. All advertising materials mentioning features or use of this 21.\" software must display the following acknowledgment: 22.\" "This product includes software developed by the OpenSSL Project 23.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24.\" 25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26.\" endorse or promote products derived from this software without 27.\" prior written permission. For written permission, please contact 28.\" openssl-core@openssl.org. 29.\" 30.\" 5. Products derived from this software may not be called "OpenSSL" 31.\" nor may "OpenSSL" appear in their names without prior written 32.\" permission of the OpenSSL Project. 33.\" 34.\" 6. Redistributions of any form whatsoever must retain the following 35.\" acknowledgment: 36.\" "This product includes software developed by the OpenSSL Project 37.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38.\" 39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50.\" OF THE POSSIBILITY OF SUCH DAMAGE. 51.\" 52.Dd $Mdocdate: June 10 2019 $ 53.Dt PEM_READ 3 54.Os 55.Sh NAME 56.Nm PEM_write , 57.Nm PEM_write_bio , 58.Nm PEM_read , 59.Nm PEM_read_bio , 60.Nm PEM_do_header , 61.Nm PEM_get_EVP_CIPHER_INFO 62.Nd PEM encoding routines 63.Sh SYNOPSIS 64.In openssl/pem.h 65.Ft int 66.Fo PEM_write 67.Fa "FILE *fp" 68.Fa "const char *name" 69.Fa "const char *header" 70.Fa "const unsigned char *data" 71.Fa "long len" 72.Fc 73.Ft int 74.Fo PEM_write_bio 75.Fa "BIO *bp" 76.Fa "const char *name" 77.Fa "const char *header" 78.Fa "ocnst unsigned char *data" 79.Fa "long len" 80.Fc 81.Ft int 82.Fo PEM_read 83.Fa "FILE *fp" 84.Fa "char **name" 85.Fa "char **header" 86.Fa "unsigned char **data" 87.Fa "long *len" 88.Fc 89.Ft int 90.Fo PEM_read_bio 91.Fa "BIO *bp" 92.Fa "char **name" 93.Fa "char **header" 94.Fa "unsigned char **data" 95.Fa "long *len" 96.Fc 97.Ft int 98.Fo PEM_get_EVP_CIPHER_INFO 99.Fa "char *header" 100.Fa "EVP_CIPHER_INFO *cinfo" 101.Fc 102.Ft int 103.Fo PEM_do_header 104.Fa "EVP_CIPHER_INFO *cinfo" 105.Fa "unsigned char *data" 106.Fa "long *len" 107.Fa "pem_password_cb *cb" 108.Fa "void *u" 109.Fc 110.Sh DESCRIPTION 111These functions read and write PEM-encoded objects, using the PEM type 112.Fa name , 113any additional 114.Fa header 115information, and the raw 116.Fa data 117of length 118.Fa len . 119.Pp 120PEM is the binary content encoding first defined in IETF RFC 1421. 121The content is a series of base64-encoded lines, surrounded by 122begin/end markers each on their own line. 123For example: 124.Bd -literal -offset indent 125-----BEGIN PRIVATE KEY----- 126MIICdg.... 127\&... bhTQ== 128-----END PRIVATE KEY----- 129.Ed 130.Pp 131Optional header line(s) may appear after the begin line, and their 132existence depends on the type of object being written or read. 133.Pp 134.Fn PEM_write 135writes to the file 136.Fa fp , 137while 138.Fn PEM_write_bio 139writes to the BIO 140.Fa bp . 141The 142.Fa name 143is the name to use in the marker, the 144.Fa header 145is the header value or 146.Dv NULL , 147and 148.Fa data 149and 150.Fa len 151specify the data and its length. 152.Pp 153The final 154.Fa data 155buffer is typically an ASN.1 object which can be decoded with the 156.Fn d2i_* 157function appropriate to the type 158.Fa name ; 159see 160.Xr d2i_X509 3 161for examples. 162.Pp 163.Fn PEM_read 164reads from the file 165.Fa fp , 166while 167.Fn PEM_read_bio 168reads from the BIO 169.Fa bp . 170Both skip any non-PEM data that precedes the start of the next PEM 171object. 172When an object is successfully retrieved, the type name from the 173"----BEGIN <type>-----" is returned via the 174.Fa name 175argument, any encapsulation headers are returned in 176.Fa header , 177and the base64-decoded content and its length are returned via 178.Fa data 179and 180.Fa len , 181respectively. 182The 183.Fa name , 184.Fa header , 185and 186.Fa data 187pointers should be freed by the caller when no longer needed. 188.Pp 189The remaining functions are deprecated because the underlying PEM 190encryption format is obsolete and should be avoided. 191It uses an encryption format with an OpenSSL-specific key-derivation 192function, which employs MD5 with an iteration count of 1. 193Instead, private keys should be stored in PKCS#8 form, with a strong 194PKCS#5 v2.0 PBE; see 195.Xr PEM_write_PrivateKey 3 196and 197.Xr d2i_PKCS8PrivateKey_bio 3 . 198.Pp 199.Fn PEM_get_EVP_CIPHER_INFO 200can be used to determine the 201.Fa data 202returned by 203.Fn PEM_read 204or 205.Fn PEM_read_bio 206is encrypted and to retrieve the associated cipher and IV. 207The caller passes a pointer to a structure of type 208.Vt EVP_CIPHER_INFO 209via the 210.Fa cinfo 211argument and the 212.Fa header 213returned via 214.Fn PEM_read 215or 216.Fn PEM_read_bio . 217If the call is successful, 1 is returned and the cipher and IV are 218stored at the address pointed to by 219.Fa cinfo . 220When the header is malformed or not supported or when the cipher is 221unknown or some internal error happens, 0 is returned. 222.Pp 223.Fn PEM_do_header 224can then be used to decrypt the data if the header indicates encryption. 225The 226.Fa cinfo 227argument is a pointer to the structure initialized by the previous call 228to 229.Fn PEM_get_EVP_CIPHER_INFO . 230The 231.Fa data 232and 233.Fa len 234arguments are those returned by the previous call to 235.Fn PEM_read 236or 237.Fn PEM_read_bio . 238The 239.Fa cb 240and 241.Fa u 242arguments make it possible to override the default password prompt 243function as described in 244.Xr PEM_read_PrivateKey 3 . 245On successful completion, the 246.Fa data 247is decrypted in place, and 248.Fa len 249is updated to indicate the plaintext length. 250.Pp 251If the data is a priori known to not be encrypted, then neither 252.Fn PEM_do_header 253nor 254.Fn PEM_get_EVP_CIPHER_INFO 255need to be called. 256.Sh RETURN VALUES 257.Fn PEM_read 258and 259.Fn PEM_read_bio 260return 1 on success or 0 on failure. 261The latter includes the case when no more PEM objects remain in the 262input file. 263To distinguish end of file from more serious errors, the caller 264must peek at the error stack and check for 265.Dv PEM_R_NO_START_LINE , 266which indicates that no more PEM objects were found. 267See 268.Xr ERR_peek_last_error 3 269and 270.Xr ERR_GET_REASON 3 . 271.Pp 272.Fn PEM_get_EVP_CIPHER_INFO 273and 274.Fn PEM_do_header 275return 1 on success or 0 on failure. 276The 277.Fa data 278is likely meaningless if these functions fail. 279.Sh SEE ALSO 280.Xr crypto 3 , 281.Xr d2i_PKCS8PrivateKey_bio 3 , 282.Xr PEM_bytes_read_bio 3 , 283.Xr PEM_read_bio_PrivateKey 3 , 284.Xr PEM_write_bio_PKCS7_stream 3 285.Sh HISTORY 286.Fn PEM_write , 287.Fn PEM_read , 288and 289.Fn PEM_do_header 290appeared in SSLeay 0.4 or earlier. 291.Fn PEM_get_EVP_CIPHER_INFO 292first appeared in SSLeay 0.5.1. 293.Fn PEM_write_bio 294and 295.Fn PEM_read_bio 296first appeared in SSLeay 0.6.0. 297These functions have been available since 298.Ox 2.4 . 299