]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/openssl/pem.h
Add X509 related libctx changes.
[thirdparty/openssl.git] / include / openssl / pem.h
1 /*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #ifndef OPENSSL_PEM_H
11 # define OPENSSL_PEM_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 # define HEADER_PEM_H
17 # endif
18
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>
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 # define PEM_BUFSIZE 1024
31
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"
57
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
62
63 /*
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(...)
67 */
68
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)
76
77 # ifdef OPENSSL_NO_STDIO
78
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) /**/
83 # endif
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) /**/
87 # endif
88 # else
89
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) \
92 { \
93 return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str, fp, \
94 (void **)x, cb, u); \
95 }
96
97 # define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
98 PEM_write_fnsig(name, type, FILE, write) \
99 { \
100 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1, str, out, \
101 x, NULL, NULL, 0, NULL, NULL); \
102 }
103
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)
107 # endif
108
109 # define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
110 PEM_write_cb_fnsig(name, type, FILE, write) \
111 { \
112 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1, str, out, \
113 x, enc, kstr, klen, cb, u); \
114 }
115
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)
119 # endif
120 # endif
121
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) \
125 { \
126 return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str, bp, \
127 (void **)x, cb, u); \
128 }
129
130 # define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
131 PEM_write_fnsig(name, type, BIO, write_bio) \
132 { \
133 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1, str, out, \
134 x, NULL,NULL,0,NULL,NULL); \
135 }
136
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)
140 # endif
141
142 # define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
143 PEM_write_cb_fnsig(name, type, BIO, write_bio) \
144 { \
145 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1, str, out, \
146 x, enc, kstr, klen, cb, u); \
147 }
148
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)
152 # endif
153
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)
157
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)
162 # endif
163
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)
167
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)
172 # endif
173
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)
177
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)
181
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)
186 # endif
187
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)
191
192 /* These are the same except they are for the declarations */
193
194 # if defined(OPENSSL_NO_STDIO)
195
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) /**/
200 # endif
201 # define DECLARE_PEM_write_cb_fp(name, type) /**/
202 # else
203
204 # define DECLARE_PEM_read_fp(name, type) \
205 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
206
207 # define DECLARE_PEM_write_fp(name, type) \
208 PEM_write_fnsig(name, type, FILE, write);
209
210 # ifndef OPENSSL_NO_DEPRECATED_3_0
211 # define DECLARE_PEM_write_fp_const(name, type) \
212 PEM_write_fnsig(name, type, FILE, write);
213 # endif
214
215 # define DECLARE_PEM_write_cb_fp(name, type) \
216 PEM_write_cb_fnsig(name, type, FILE, write);
217
218 # endif
219
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);
223
224 # define DECLARE_PEM_write_bio(name, type) \
225 PEM_write_fnsig(name, type, BIO, write_bio);
226
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);
230 # endif
231
232 # define DECLARE_PEM_write_cb_bio(name, type) \
233 PEM_write_cb_fnsig(name, type, BIO, write_bio);
234
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)
242 # endif
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)
256 # endif
257 # define DECLARE_PEM_rw_cb(name, type) \
258 DECLARE_PEM_read(name, type) \
259 DECLARE_PEM_write_cb(name, type)
260
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);
264
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,
274 void *u);
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,
279 void *u);
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);
286
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 STACK_OF(X509_INFO)
290 *PEM_X509_INFO_read_bio_with_libctx(BIO *bp, STACK_OF(X509_INFO) *sk,
291 pem_password_cb *cb, void *u,
292 OPENSSL_CTX *libctx, const char *propq);
293
294 int PEM_X509_INFO_write_bio(BIO *bp, const X509_INFO *xi, EVP_CIPHER *enc,
295 const unsigned char *kstr, int klen,
296 pem_password_cb *cd, void *u);
297
298 #ifndef OPENSSL_NO_STDIO
299 int PEM_read(FILE *fp, char **name, char **header,
300 unsigned char **data, long *len);
301 int PEM_write(FILE *fp, const char *name, const char *hdr,
302 const unsigned char *data, long len);
303 void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
304 pem_password_cb *cb, void *u);
305 int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
306 const void *x, const EVP_CIPHER *enc,
307 const unsigned char *kstr, int klen,
308 pem_password_cb *callback, void *u);
309 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
310 pem_password_cb *cb, void *u);
311 STACK_OF(X509_INFO)
312 *PEM_X509_INFO_read_with_libctx(FILE *fp, STACK_OF(X509_INFO) *sk,
313 pem_password_cb *cb, void *u,
314 OPENSSL_CTX *libctx, const char *propq);
315 #endif
316
317 int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
318 int PEM_SignUpdate(EVP_MD_CTX *ctx, const unsigned char *d, unsigned int cnt);
319 int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
320 unsigned int *siglen, EVP_PKEY *pkey);
321
322 /* The default pem_password_cb that's used internally */
323 int PEM_def_callback(char *buf, int num, int rwflag, void *userdata);
324 void PEM_proc_type(char *buf, int type);
325 void PEM_dek_info(char *buf, const char *type, int len, const char *str);
326
327 # include <openssl/symhacks.h>
328
329 DECLARE_PEM_rw(X509, X509)
330 DECLARE_PEM_rw(X509_AUX, X509)
331 DECLARE_PEM_rw(X509_REQ, X509_REQ)
332 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
333 DECLARE_PEM_rw(X509_CRL, X509_CRL)
334 DECLARE_PEM_rw(X509_PUBKEY, X509_PUBKEY)
335 DECLARE_PEM_rw(PKCS7, PKCS7)
336 DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
337 DECLARE_PEM_rw(PKCS8, X509_SIG)
338 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
339 # ifndef OPENSSL_NO_RSA
340 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
341 DECLARE_PEM_rw(RSAPublicKey, RSA)
342 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
343 # endif
344 # ifndef OPENSSL_NO_DSA
345 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
346 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
347 DECLARE_PEM_rw(DSAparams, DSA)
348 # endif
349 # ifndef OPENSSL_NO_EC
350 DECLARE_PEM_rw(ECPKParameters, EC_GROUP)
351 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
352 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
353 # endif
354 # ifndef OPENSSL_NO_DH
355 DECLARE_PEM_rw(DHparams, DH)
356 DECLARE_PEM_write(DHxparams, DH)
357 # endif
358 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
359 EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
360 void *u, OPENSSL_CTX *libctx,
361 const char *propq);
362 # ifndef OPENSSL_NO_STDIO
363 EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
364 void *u, OPENSSL_CTX *libctx,
365 const char *propq);
366 # endif
367 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
368
369 int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
370 const EVP_CIPHER *enc,
371 const unsigned char *kstr, int klen,
372 pem_password_cb *cb, void *u);
373
374 /* Why do these take a signed char *kstr? */
375 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
376 const char *kstr, int klen,
377 pem_password_cb *cb, void *u);
378 int PEM_write_bio_PKCS8PrivateKey(BIO *, const EVP_PKEY *, const EVP_CIPHER *,
379 const char *kstr, int klen,
380 pem_password_cb *cb, void *u);
381 int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
382 const char *kstr, int klen,
383 pem_password_cb *cb, void *u);
384 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
385 const char *kstr, int klen,
386 pem_password_cb *cb, void *u);
387 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
388 void *u);
389
390 # ifndef OPENSSL_NO_STDIO
391 int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
392 const char *kstr, int klen,
393 pem_password_cb *cb, void *u);
394 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
395 const char *kstr, int klen,
396 pem_password_cb *cb, void *u);
397 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
398 const char *kstr, int klen,
399 pem_password_cb *cb, void *u);
400
401 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
402 void *u);
403
404 int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
405 const char *kstr, int klen,
406 pem_password_cb *cd, void *u);
407 # endif
408 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
409 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
410
411 # ifndef OPENSSL_NO_DSA
412 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
413 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
414 EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
415 EVP_PKEY *b2i_PublicKey_bio(BIO *in);
416 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk);
417 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk);
418 # ifndef OPENSSL_NO_RC4
419 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
420 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
421 pem_password_cb *cb, void *u);
422 # endif
423 # endif
424
425 # ifdef __cplusplus
426 }
427 # endif
428 #endif