2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 # define OPENSSL_PEM_H
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
19 # include <openssl/e_os2.h>
20 # include <openssl/bio.h>
21 # include <openssl/safestack.h>
22 # include <openssl/evp.h>
23 # include <openssl/x509.h>
24 # include <openssl/pemerr.h>
30 # define PEM_BUFSIZE 1024
32 # define PEM_STRING_X509_OLD "X509 CERTIFICATE"
33 # define PEM_STRING_X509 "CERTIFICATE"
34 # define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
35 # define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
36 # define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
37 # define PEM_STRING_X509_CRL "X509 CRL"
38 # define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
39 # define PEM_STRING_PUBLIC "PUBLIC KEY"
40 # define PEM_STRING_RSA "RSA PRIVATE KEY"
41 # define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
42 # define PEM_STRING_DSA "DSA PRIVATE KEY"
43 # define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
44 # define PEM_STRING_PKCS7 "PKCS7"
45 # define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
46 # define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
47 # define PEM_STRING_PKCS8INF "PRIVATE KEY"
48 # define PEM_STRING_DHPARAMS "DH PARAMETERS"
49 # define PEM_STRING_DHXPARAMS "X9.42 DH PARAMETERS"
50 # define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
51 # define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
52 # define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
53 # define PEM_STRING_ECPARAMETERS "EC PARAMETERS"
54 # define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
55 # define PEM_STRING_PARAMETERS "PARAMETERS"
56 # define PEM_STRING_CMS "CMS"
58 # define PEM_TYPE_ENCRYPTED 10
59 # define PEM_TYPE_MIC_ONLY 20
60 # define PEM_TYPE_MIC_CLEAR 30
61 # define PEM_TYPE_CLEAR 40
64 * These macros make the PEM_read/PEM_write functions easier to maintain and
65 * write. Now they are all implemented with either: IMPLEMENT_PEM_rw(...) or
66 * IMPLEMENT_PEM_rw_cb(...)
69 # define PEM_write_fnsig(name, type, OUTTYPE, writename) \
70 int PEM_##writename##_##name(OUTTYPE *out, const type *x)
71 # define PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
72 int PEM_##writename##_##name(OUTTYPE *out, const type *x, \
73 const EVP_CIPHER *enc, \
74 const unsigned char *kstr, int klen, \
75 pem_password_cb *cb, void *u)
77 # ifdef OPENSSL_NO_STDIO
79 # define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/
80 # define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/
81 # ifndef OPENSSL_NO_DEPRECATED_3_0
82 # define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) /**/
84 # define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/
85 # ifndef OPENSSL_NO_DEPRECATED_3_0
86 # define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) /**/
90 # define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
91 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u) \
93 return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str, fp, \
97 # define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
98 PEM_write_fnsig(name, type, FILE, write) \
100 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1, str, out, \
101 x, NULL, NULL, 0, NULL, NULL); \
104 # ifndef OPENSSL_NO_DEPRECATED_3_0
105 # define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
106 IMPLEMENT_PEM_write_fp(name, type, str, asn1)
109 # define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
110 PEM_write_cb_fnsig(name, type, FILE, write) \
112 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1, str, out, \
113 x, enc, kstr, klen, cb, u); \
116 # ifndef OPENSSL_NO_DEPRECATED_3_0
117 # define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
118 IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
122 # define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
123 type *PEM_read_bio_##name(BIO *bp, type **x, \
124 pem_password_cb *cb, void *u) \
126 return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str, bp, \
127 (void **)x, cb, u); \
130 # define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
131 PEM_write_fnsig(name, type, BIO, write_bio) \
133 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1, str, out, \
134 x, NULL,NULL,0,NULL,NULL); \
137 # ifndef OPENSSL_NO_DEPRECATED_3_0
138 # define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
139 IMPLEMENT_PEM_write_bio(name, type, str, asn1)
142 # define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
143 PEM_write_cb_fnsig(name, type, BIO, write_bio) \
145 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1, str, out, \
146 x, enc, kstr, klen, cb, u); \
149 # ifndef OPENSSL_NO_DEPRECATED_3_0
150 # define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
151 IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)
154 # define IMPLEMENT_PEM_write(name, type, str, asn1) \
155 IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
156 IMPLEMENT_PEM_write_fp(name, type, str, asn1)
158 # ifndef OPENSSL_NO_DEPRECATED_3_0
159 # define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
160 IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
161 IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
164 # define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
165 IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
166 IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
168 # ifndef OPENSSL_NO_DEPRECATED_3_0
169 # define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
170 IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
171 IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
174 # define IMPLEMENT_PEM_read(name, type, str, asn1) \
175 IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
176 IMPLEMENT_PEM_read_fp(name, type, str, asn1)
178 # define IMPLEMENT_PEM_rw(name, type, str, asn1) \
179 IMPLEMENT_PEM_read(name, type, str, asn1) \
180 IMPLEMENT_PEM_write(name, type, str, asn1)
182 # ifndef OPENSSL_NO_DEPRECATED_3_0
183 # define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
184 IMPLEMENT_PEM_read(name, type, str, asn1) \
185 IMPLEMENT_PEM_write_const(name, type, str, asn1)
188 # define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
189 IMPLEMENT_PEM_read(name, type, str, asn1) \
190 IMPLEMENT_PEM_write_cb(name, type, str, asn1)
192 /* These are the same except they are for the declarations */
194 # if defined(OPENSSL_NO_STDIO)
196 # define DECLARE_PEM_read_fp(name, type) /**/
197 # define DECLARE_PEM_write_fp(name, type) /**/
198 # ifndef OPENSSL_NO_DEPRECATED_3_0
199 # define DECLARE_PEM_write_fp_const(name, type) /**/
201 # define DECLARE_PEM_write_cb_fp(name, type) /**/
204 # define DECLARE_PEM_read_fp(name, type) \
205 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
207 # define DECLARE_PEM_write_fp(name, type) \
208 PEM_write_fnsig(name, type, FILE, write);
210 # ifndef OPENSSL_NO_DEPRECATED_3_0
211 # define DECLARE_PEM_write_fp_const(name, type) \
212 PEM_write_fnsig(name, type, FILE, write);
215 # define DECLARE_PEM_write_cb_fp(name, type) \
216 PEM_write_cb_fnsig(name, type, FILE, write);
220 # define DECLARE_PEM_read_bio(name, type) \
221 type *PEM_read_bio_##name(BIO *bp, type **x, \
222 pem_password_cb *cb, void *u);
224 # define DECLARE_PEM_write_bio(name, type) \
225 PEM_write_fnsig(name, type, BIO, write_bio);
227 # ifndef OPENSSL_NO_DEPRECATED_3_0
228 # define DECLARE_PEM_write_bio_const(name, type) \
229 PEM_write_fnsig(name, type, BIO, write_bio);
232 # define DECLARE_PEM_write_cb_bio(name, type) \
233 PEM_write_cb_fnsig(name, type, BIO, write_bio);
235 # define DECLARE_PEM_write(name, type) \
236 DECLARE_PEM_write_bio(name, type) \
237 DECLARE_PEM_write_fp(name, type)
238 # ifndef OPENSSL_NO_DEPRECATED_3_0
239 # define DECLARE_PEM_write_const(name, type) \
240 DECLARE_PEM_write_bio_const(name, type) \
241 DECLARE_PEM_write_fp_const(name, type)
243 # define DECLARE_PEM_write_cb(name, type) \
244 DECLARE_PEM_write_cb_bio(name, type) \
245 DECLARE_PEM_write_cb_fp(name, type)
246 # define DECLARE_PEM_read(name, type) \
247 DECLARE_PEM_read_bio(name, type) \
248 DECLARE_PEM_read_fp(name, type)
249 # define DECLARE_PEM_rw(name, type) \
250 DECLARE_PEM_read(name, type) \
251 DECLARE_PEM_write(name, type)
252 # ifndef OPENSSL_NO_DEPRECATED_3_0
253 # define DECLARE_PEM_rw_const(name, type) \
254 DECLARE_PEM_read(name, type) \
255 DECLARE_PEM_write_const(name, type)
257 # define DECLARE_PEM_rw_cb(name, type) \
258 DECLARE_PEM_read(name, type) \
259 DECLARE_PEM_write_cb(name, type)
261 int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
262 int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *len,
263 pem_password_cb *callback, void *u);
265 int PEM_read_bio(BIO *bp, char **name, char **header,
266 unsigned char **data, long *len);
267 # define PEM_FLAG_SECURE 0x1
268 # define PEM_FLAG_EAY_COMPATIBLE 0x2
269 # define PEM_FLAG_ONLY_B64 0x4
270 int PEM_read_bio_ex(BIO *bp, char **name, char **header,
271 unsigned char **data, long *len, unsigned int flags);
272 int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
273 const char *name, BIO *bp, pem_password_cb *cb,
275 int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
276 const unsigned char *data, long len);
277 int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
278 const char *name, BIO *bp, pem_password_cb *cb,
280 void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
281 pem_password_cb *cb, void *u);
282 int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
283 const void *x, const EVP_CIPHER *enc,
284 const unsigned char *kstr, int klen,
285 pem_password_cb *cb, void *u);
287 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
288 pem_password_cb *cb, void *u);
289 int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
290 const unsigned char *kstr, int klen,
291 pem_password_cb *cd, void *u);
293 #ifndef OPENSSL_NO_STDIO
294 int PEM_read(FILE *fp, char **name, char **header,
295 unsigned char **data, long *len);
296 int PEM_write(FILE *fp, const char *name, const char *hdr,
297 const unsigned char *data, long len);
298 void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
299 pem_password_cb *cb, void *u);
300 int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
301 const void *x, const EVP_CIPHER *enc,
302 const unsigned char *kstr, int klen,
303 pem_password_cb *callback, void *u);
304 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
305 pem_password_cb *cb, void *u);
308 int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
309 int PEM_SignUpdate(EVP_MD_CTX *ctx, const unsigned char *d, unsigned int cnt);
310 int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
311 unsigned int *siglen, EVP_PKEY *pkey);
313 /* The default pem_password_cb that's used internally */
314 int PEM_def_callback(char *buf, int num, int rwflag, void *userdata);
315 void PEM_proc_type(char *buf, int type);
316 void PEM_dek_info(char *buf, const char *type, int len, const char *str);
318 # include <openssl/symhacks.h>
320 DECLARE_PEM_rw(X509, X509)
321 DECLARE_PEM_rw(X509_AUX, X509)
322 DECLARE_PEM_rw(X509_REQ, X509_REQ)
323 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
324 DECLARE_PEM_rw(X509_CRL, X509_CRL)
325 DECLARE_PEM_rw(X509_PUBKEY, X509_PUBKEY)
326 DECLARE_PEM_rw(PKCS7, PKCS7)
327 DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
328 DECLARE_PEM_rw(PKCS8, X509_SIG)
329 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
330 # ifndef OPENSSL_NO_RSA
331 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
332 DECLARE_PEM_rw(RSAPublicKey, RSA)
333 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
335 # ifndef OPENSSL_NO_DSA
336 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
337 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
338 DECLARE_PEM_rw(DSAparams, DSA)
340 # ifndef OPENSSL_NO_EC
341 DECLARE_PEM_rw(ECPKParameters, EC_GROUP)
342 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
343 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
345 # ifndef OPENSSL_NO_DH
346 DECLARE_PEM_rw(DHparams, DH)
347 DECLARE_PEM_write(DHxparams, DH)
349 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
350 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
352 int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
353 const EVP_CIPHER *enc,
354 const unsigned char *kstr, int klen,
355 pem_password_cb *cb, void *u);
357 /* Why do these take a signed char *kstr? */
358 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
359 const char *kstr, int klen,
360 pem_password_cb *cb, void *u);
361 int PEM_write_bio_PKCS8PrivateKey(BIO *, const EVP_PKEY *, const EVP_CIPHER *,
362 const char *kstr, int klen,
363 pem_password_cb *cb, void *u);
364 int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
365 const char *kstr, int klen,
366 pem_password_cb *cb, void *u);
367 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
368 const char *kstr, int klen,
369 pem_password_cb *cb, void *u);
370 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
373 # ifndef OPENSSL_NO_STDIO
374 int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
375 const char *kstr, int klen,
376 pem_password_cb *cb, void *u);
377 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
378 const char *kstr, int klen,
379 pem_password_cb *cb, void *u);
380 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
381 const char *kstr, int klen,
382 pem_password_cb *cb, void *u);
384 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
387 int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
388 const char *kstr, int klen,
389 pem_password_cb *cd, void *u);
391 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
392 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
394 # ifndef OPENSSL_NO_DSA
395 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
396 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
397 EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
398 EVP_PKEY *b2i_PublicKey_bio(BIO *in);
399 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk);
400 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk);
401 # ifndef OPENSSL_NO_RC4
402 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
403 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
404 pem_password_cb *cb, void *u);