blob: f39989e13337e52b966777306fb7cc893ffe9708 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#ifndef OPENSSL_HEADER_PEM_H
58#define OPENSSL_HEADER_PEM_H
59
60#include <openssl/base64.h>
61#include <openssl/bio.h>
62#include <openssl/cipher.h>
63#include <openssl/digest.h>
64#include <openssl/evp.h>
David Benjamin38157202018-06-15 17:31:19 -040065#include <openssl/pkcs7.h>
David Benjamin5e549fb2020-10-09 16:18:33 -040066#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -070067#include <openssl/x509.h>
68
David Benjamin5e549fb2020-10-09 16:18:33 -040069// For compatibility with open-iscsi, which assumes that it can get
70// |OPENSSL_malloc| from pem.h or err.h
Adam Langley7cb920b2016-05-20 08:28:56 -070071#include <openssl/crypto.h>
72
David Benjamin5e549fb2020-10-09 16:18:33 -040073#ifdef __cplusplus
Adam Langley95c29f32014-06-20 12:00:00 -070074extern "C" {
75#endif
76
77
David Benjamin5e549fb2020-10-09 16:18:33 -040078#define PEM_BUFSIZE 1024
Adam Langley95c29f32014-06-20 12:00:00 -070079
David Benjamin5e549fb2020-10-09 16:18:33 -040080#define PEM_STRING_X509_OLD "X509 CERTIFICATE"
81#define PEM_STRING_X509 "CERTIFICATE"
82#define PEM_STRING_X509_PAIR "CERTIFICATE PAIR"
83#define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
84#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
85#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
86#define PEM_STRING_X509_CRL "X509 CRL"
87#define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
88#define PEM_STRING_PUBLIC "PUBLIC KEY"
89#define PEM_STRING_RSA "RSA PRIVATE KEY"
90#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
91#define PEM_STRING_DSA "DSA PRIVATE KEY"
92#define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
David Benjamin8ebc0f52016-01-03 03:02:50 -080093#define PEM_STRING_EC "EC PRIVATE KEY"
David Benjamin5e549fb2020-10-09 16:18:33 -040094#define PEM_STRING_PKCS7 "PKCS7"
95#define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
96#define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
97#define PEM_STRING_PKCS8INF "PRIVATE KEY"
98#define PEM_STRING_DHPARAMS "DH PARAMETERS"
99#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
100#define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
Adam Langley95c29f32014-06-20 12:00:00 -0700101#define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
David Benjamin5e549fb2020-10-09 16:18:33 -0400102#define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
103#define PEM_STRING_CMS "CMS"
Adam Langley95c29f32014-06-20 12:00:00 -0700104
David Benjamin5e549fb2020-10-09 16:18:33 -0400105// enc_type is one off
106#define PEM_TYPE_ENCRYPTED 10
107#define PEM_TYPE_MIC_ONLY 20
108#define PEM_TYPE_MIC_CLEAR 30
109#define PEM_TYPE_CLEAR 40
Adam Langley95c29f32014-06-20 12:00:00 -0700110
David Benjamin5e549fb2020-10-09 16:18:33 -0400111// These macros make the PEM_read/PEM_write functions easier to maintain and
112// write. Now they are all implemented with either:
113// IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
Adam Langley95c29f32014-06-20 12:00:00 -0700114
115#ifdef OPENSSL_NO_FP_API
116
David Benjamin5e549fb2020-10-09 16:18:33 -0400117#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) //
118#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) //
119#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) //
120#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) //
121#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) //
Adam Langley95c29f32014-06-20 12:00:00 -0700122
123#else
124
David Benjamin217bfd32018-09-22 16:49:56 -0500125#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
126 static void *pem_read_##name##_d2i(void **x, const unsigned char **inp, \
127 long len) { \
128 return d2i_##asn1((type **)x, inp, len); \
129 } \
130 OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \
131 pem_password_cb *cb, void *u) { \
132 return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \
133 cb, u); \
134 }
Adam Langley95c29f32014-06-20 12:00:00 -0700135
David Benjamin217bfd32018-09-22 16:49:56 -0500136#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
137 static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
138 return i2d_##asn1((type *)x, outp); \
139 } \
140 OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) { \
141 return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \
142 NULL, NULL); \
143 }
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin217bfd32018-09-22 16:49:56 -0500145#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
146 static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
147 return i2d_##asn1((const type *)x, outp); \
148 } \
149 OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) { \
150 return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \
151 NULL, 0, NULL, NULL); \
152 }
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamin217bfd32018-09-22 16:49:56 -0500154#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
155 static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
156 return i2d_##asn1((type *)x, outp); \
157 } \
158 OPENSSL_EXPORT int PEM_write_##name( \
159 FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
160 pem_password_cb *cb, void *u) { \
161 return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \
162 cb, u); \
163 }
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamin217bfd32018-09-22 16:49:56 -0500165#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
166 static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
167 return i2d_##asn1((const type *)x, outp); \
168 } \
169 OPENSSL_EXPORT int PEM_write_##name( \
170 FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
171 pem_password_cb *cb, void *u) { \
172 return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \
173 cb, u); \
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
176#endif
177
David Benjamin217bfd32018-09-22 16:49:56 -0500178#define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
179 static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \
180 long len) { \
181 return d2i_##asn1((type **)x, inp, len); \
182 } \
183 OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \
184 pem_password_cb *cb, void *u) { \
185 return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp, \
186 (void **)x, cb, u); \
187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
David Benjamin217bfd32018-09-22 16:49:56 -0500189#define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
190 static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
191 return i2d_##asn1((type *)x, outp); \
192 } \
193 OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) { \
194 return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL, \
195 NULL, 0, NULL, NULL); \
196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjamin217bfd32018-09-22 16:49:56 -0500198#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
199 static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
200 return i2d_##asn1((const type *)x, outp); \
201 } \
202 OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) { \
203 return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \
204 NULL, NULL, 0, NULL, NULL); \
205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin217bfd32018-09-22 16:49:56 -0500207#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
208 static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
209 return i2d_##asn1((type *)x, outp); \
210 } \
211 OPENSSL_EXPORT int PEM_write_bio_##name( \
212 BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
213 pem_password_cb *cb, void *u) { \
214 return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc, \
215 kstr, klen, cb, u); \
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin217bfd32018-09-22 16:49:56 -0500218#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
219 static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
220 return i2d_##asn1((const type *)x, outp); \
221 } \
222 OPENSSL_EXPORT int PEM_write_bio_##name( \
223 BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
224 pem_password_cb *cb, void *u) { \
225 return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \
226 enc, kstr, klen, cb, u); \
227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
229#define IMPLEMENT_PEM_write(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400230 IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
231 IMPLEMENT_PEM_write_fp(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700232
233#define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400234 IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
235 IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700236
237#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400238 IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
239 IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700240
241#define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400242 IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
243 IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700244
245#define IMPLEMENT_PEM_read(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400246 IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
247 IMPLEMENT_PEM_read_fp(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700248
249#define IMPLEMENT_PEM_rw(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400250 IMPLEMENT_PEM_read(name, type, str, asn1) \
251 IMPLEMENT_PEM_write(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700252
253#define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400254 IMPLEMENT_PEM_read(name, type, str, asn1) \
255 IMPLEMENT_PEM_write_const(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700256
257#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400258 IMPLEMENT_PEM_read(name, type, str, asn1) \
259 IMPLEMENT_PEM_write_cb(name, type, str, asn1)
Adam Langley95c29f32014-06-20 12:00:00 -0700260
David Benjamin5e549fb2020-10-09 16:18:33 -0400261// These are the same except they are for the declarations
Adam Langley95c29f32014-06-20 12:00:00 -0700262
263#if defined(OPENSSL_NO_FP_API)
264
David Benjamin5e549fb2020-10-09 16:18:33 -0400265#define DECLARE_PEM_read_fp(name, type) //
266#define DECLARE_PEM_write_fp(name, type) //
267#define DECLARE_PEM_write_cb_fp(name, type) //
Adam Langley95c29f32014-06-20 12:00:00 -0700268
269#else
270
David Benjamin5e549fb2020-10-09 16:18:33 -0400271#define DECLARE_PEM_read_fp(name, type) \
272 OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \
273 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700274
275#define DECLARE_PEM_write_fp(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400276 OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x);
Adam Langley95c29f32014-06-20 12:00:00 -0700277
278#define DECLARE_PEM_write_fp_const(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400279 OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x);
Adam Langley95c29f32014-06-20 12:00:00 -0700280
David Benjamin5e549fb2020-10-09 16:18:33 -0400281#define DECLARE_PEM_write_cb_fp(name, type) \
282 OPENSSL_EXPORT int PEM_write_##name( \
283 FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
284 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700285
286#endif
287
David Benjamin5e549fb2020-10-09 16:18:33 -0400288#define DECLARE_PEM_read_bio(name, type) \
289 OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \
290 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700291
292#define DECLARE_PEM_write_bio(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400293 OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x);
Adam Langley95c29f32014-06-20 12:00:00 -0700294
295#define DECLARE_PEM_write_bio_const(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400296 OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x);
Adam Langley95c29f32014-06-20 12:00:00 -0700297
David Benjamin5e549fb2020-10-09 16:18:33 -0400298#define DECLARE_PEM_write_cb_bio(name, type) \
299 OPENSSL_EXPORT int PEM_write_bio_##name( \
300 BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
301 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langley95c29f32014-06-20 12:00:00 -0700303
304#define DECLARE_PEM_write(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400305 DECLARE_PEM_write_bio(name, type) \
306 DECLARE_PEM_write_fp(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700307
308#define DECLARE_PEM_write_const(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400309 DECLARE_PEM_write_bio_const(name, type) \
310 DECLARE_PEM_write_fp_const(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700311
312#define DECLARE_PEM_write_cb(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400313 DECLARE_PEM_write_cb_bio(name, type) \
314 DECLARE_PEM_write_cb_fp(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700315
316#define DECLARE_PEM_read(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400317 DECLARE_PEM_read_bio(name, type) \
318 DECLARE_PEM_read_fp(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700319
320#define DECLARE_PEM_rw(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400321 DECLARE_PEM_read(name, type) \
322 DECLARE_PEM_write(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700323
324#define DECLARE_PEM_rw_const(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400325 DECLARE_PEM_read(name, type) \
326 DECLARE_PEM_write_const(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700327
328#define DECLARE_PEM_rw_cb(name, type) \
David Benjamin5e549fb2020-10-09 16:18:33 -0400329 DECLARE_PEM_read(name, type) \
330 DECLARE_PEM_write_cb(name, type)
Adam Langley95c29f32014-06-20 12:00:00 -0700331
David Benjamin5e549fb2020-10-09 16:18:33 -0400332// "userdata": new with OpenSSL 0.9.4
Adam Langley95c29f32014-06-20 12:00:00 -0700333typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
Adam Langley95c29f32014-06-20 12:00:00 -0700334
David Benjamin5e549fb2020-10-09 16:18:33 -0400335OPENSSL_EXPORT int PEM_get_EVP_CIPHER_INFO(char *header,
336 EVP_CIPHER_INFO *cipher);
337OPENSSL_EXPORT int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data,
338 long *len, pem_password_cb *callback, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700339
David Benjamin6d904d52020-10-09 16:27:23 -0400340// PEM_read_bio reads from |bp|, until the next PEM block. If one is found, it
341// returns one and sets |*name|, |*header|, and |*data| to newly-allocated
342// buffers containing the PEM type, the header block, and the decoded data,
343// respectively. |*name| and |*header| are NUL-terminated C strings, while
344// |*data| has |*len| bytes. The caller must release each of |*name|, |*header|,
345// and |*data| with |OPENSSL_free| when done. If no PEM block is found, this
346// function returns zero and pushes |PEM_R_NO_START_LINE| to the error queue. If
347// one is found, but there is an error decoding it, it returns zero and pushes
348// some other error to the error queue.
David Benjamin5e549fb2020-10-09 16:18:33 -0400349OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header,
350 unsigned char **data, long *len);
David Benjamin6d904d52020-10-09 16:27:23 -0400351
352// PEM_write_bio writes a PEM block to |bp|, containing |len| bytes from |data|
353// as data. |name| and |hdr| are NUL-terminated C strings containing the PEM
354// type and header block, respectively. This function returns zero on error and
355// the number of bytes written on success.
David Benjamin5e549fb2020-10-09 16:18:33 -0400356OPENSSL_EXPORT int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
357 const unsigned char *data, long len);
David Benjamin6d904d52020-10-09 16:27:23 -0400358
David Benjamin5e549fb2020-10-09 16:18:33 -0400359OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen,
360 char **pnm, const char *name, BIO *bp,
361 pem_password_cb *cb, void *u);
362OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name,
363 BIO *bp, void **x, pem_password_cb *cb,
364 void *u);
365OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name,
366 BIO *bp, void *x, const EVP_CIPHER *enc,
367 unsigned char *kstr, int klen,
368 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700369
David Benjamin5e549fb2020-10-09 16:18:33 -0400370OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(
371 BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
372OPENSSL_EXPORT int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi,
373 EVP_CIPHER *enc, unsigned char *kstr,
374 int klen, pem_password_cb *cd,
375 void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700376
David Benjamin5e549fb2020-10-09 16:18:33 -0400377OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header,
378 unsigned char **data, long *len);
379OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr,
380 const unsigned char *data, long len);
381OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp,
382 void **x, pem_password_cb *cb, void *u);
383OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
384 void *x, const EVP_CIPHER *enc,
385 unsigned char *kstr, int klen,
386 pem_password_cb *callback, void *u);
387OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp,
388 STACK_OF(X509_INFO) *sk,
389 pem_password_cb *cb,
390 void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700391
David Benjamin5e549fb2020-10-09 16:18:33 -0400392// PEM_def_callback treats |userdata| as a string and copies it into |buf|,
393// assuming its |size| is sufficient. Returns the length of the string, or 0
394// if there is not enough room. If either |buf| or |userdata| is NULL, 0 is
395// returned. Note that this is different from OpenSSL, which prompts for a
396// password.
397OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag,
398 void *userdata);
399OPENSSL_EXPORT void PEM_proc_type(char *buf, int type);
400OPENSSL_EXPORT void PEM_dek_info(char *buf, const char *type, int len,
401 char *str);
Adam Langley95c29f32014-06-20 12:00:00 -0700402
403
404DECLARE_PEM_rw(X509, X509)
405
406DECLARE_PEM_rw(X509_AUX, X509)
407
Adam Langley95c29f32014-06-20 12:00:00 -0700408DECLARE_PEM_rw(X509_REQ, X509_REQ)
409DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
410
411DECLARE_PEM_rw(X509_CRL, X509_CRL)
412
David Benjamin38157202018-06-15 17:31:19 -0400413DECLARE_PEM_rw(PKCS7, PKCS7)
Adam Langley95c29f32014-06-20 12:00:00 -0700414DECLARE_PEM_rw(PKCS8, X509_SIG)
415
416DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
417
Adam Langley95c29f32014-06-20 12:00:00 -0700418DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
419
420DECLARE_PEM_rw_const(RSAPublicKey, RSA)
421DECLARE_PEM_rw(RSA_PUBKEY, RSA)
422
Adam Langley95c29f32014-06-20 12:00:00 -0700423#ifndef OPENSSL_NO_DSA
424
425DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
426
427DECLARE_PEM_rw(DSA_PUBKEY, DSA)
428
429DECLARE_PEM_rw_const(DSAparams, DSA)
430
431#endif
432
Adam Langley95c29f32014-06-20 12:00:00 -0700433DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
434DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langley95c29f32014-06-20 12:00:00 -0700436
437DECLARE_PEM_rw_const(DHparams, DH)
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langley95c29f32014-06-20 12:00:00 -0700439
440DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
441
442DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
443
David Benjamin5e549fb2020-10-09 16:18:33 -0400444OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x,
445 int nid, char *kstr,
446 int klen,
447 pem_password_cb *cb,
448 void *u);
449OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *,
450 const EVP_CIPHER *, char *,
451 int, pem_password_cb *,
452 void *);
453OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x,
454 const EVP_CIPHER *enc, char *kstr,
455 int klen, pem_password_cb *cb,
456 void *u);
457OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
458 char *kstr, int klen,
459 pem_password_cb *cb, void *u);
460OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x,
461 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700462
David Benjamin5e549fb2020-10-09 16:18:33 -0400463OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x,
464 const EVP_CIPHER *enc, char *kstr,
465 int klen, pem_password_cb *cb,
466 void *u);
467OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
468 char *kstr, int klen,
469 pem_password_cb *cb, void *u);
470OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
471 char *kstr, int klen,
472 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700473
David Benjamin5e549fb2020-10-09 16:18:33 -0400474OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x,
475 pem_password_cb *cb, void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjamin5e549fb2020-10-09 16:18:33 -0400477OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x,
478 const EVP_CIPHER *enc, char *kstr,
479 int klen, pem_password_cb *cd,
480 void *u);
Adam Langley95c29f32014-06-20 12:00:00 -0700481
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamin5e549fb2020-10-09 16:18:33 -0400483#ifdef __cplusplus
Adam Langley95c29f32014-06-20 12:00:00 -0700484}
485#endif
486
David Benjamin689be0f2015-02-11 15:55:26 -0500487#define PEM_R_BAD_BASE64_DECODE 100
488#define PEM_R_BAD_DECRYPT 101
489#define PEM_R_BAD_END_LINE 102
Adam Langley95c29f32014-06-20 12:00:00 -0700490#define PEM_R_BAD_IV_CHARS 103
David Benjamin689be0f2015-02-11 15:55:26 -0500491#define PEM_R_BAD_PASSWORD_READ 104
492#define PEM_R_CIPHER_IS_NULL 105
493#define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 106
494#define PEM_R_NOT_DEK_INFO 107
495#define PEM_R_NOT_ENCRYPTED 108
496#define PEM_R_NOT_PROC_TYPE 109
497#define PEM_R_NO_START_LINE 110
498#define PEM_R_READ_KEY 111
499#define PEM_R_SHORT_HEADER 112
500#define PEM_R_UNSUPPORTED_CIPHER 113
501#define PEM_R_UNSUPPORTED_ENCRYPTION 114
Adam Langley95c29f32014-06-20 12:00:00 -0700502
David Benjamin5e549fb2020-10-09 16:18:33 -0400503#endif // OPENSSL_HEADER_PEM_H