SERIALIZER: add hooks in PEM_write_bio_ and PEM_write_fp_ routines
[openssl.git] / include / openssl / pem.h
1 /*
2  * Copyright 1995-2018 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 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);
292
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);
306 #endif
307
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);
312
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);
317
318 # include <openssl/symhacks.h>
319
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(PKCS7, PKCS7)
326 DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
327 DECLARE_PEM_rw(PKCS8, X509_SIG)
328 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
329 # ifndef OPENSSL_NO_RSA
330 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
331 DECLARE_PEM_rw(RSAPublicKey, RSA)
332 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
333 # endif
334 # ifndef OPENSSL_NO_DSA
335 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
336 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
337 DECLARE_PEM_rw(DSAparams, DSA)
338 # endif
339 # ifndef OPENSSL_NO_EC
340 DECLARE_PEM_rw(ECPKParameters, EC_GROUP)
341 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
342 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
343 # endif
344 # ifndef OPENSSL_NO_DH
345 DECLARE_PEM_rw(DHparams, DH)
346 DECLARE_PEM_write(DHxparams, DH)
347 # endif
348 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
349 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
350
351 int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
352                                          const EVP_CIPHER *enc,
353                                          const unsigned char *kstr, int klen,
354                                          pem_password_cb *cb, void *u);
355
356 /* Why do these take a signed char *kstr? */
357 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
358                                       const char *kstr, int klen,
359                                       pem_password_cb *cb, void *u);
360 int PEM_write_bio_PKCS8PrivateKey(BIO *, const EVP_PKEY *, const EVP_CIPHER *,
361                                   const char *kstr, int klen,
362                                   pem_password_cb *cb, void *u);
363 int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
364                             const char *kstr, int klen,
365                             pem_password_cb *cb, void *u);
366 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
367                                 const char *kstr, int klen,
368                                 pem_password_cb *cb, void *u);
369 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
370                                   void *u);
371
372 # ifndef OPENSSL_NO_STDIO
373 int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
374                            const char *kstr, int klen,
375                            pem_password_cb *cb, void *u);
376 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
377                                const char *kstr, int klen,
378                                pem_password_cb *cb, void *u);
379 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
380                                   const char *kstr, int klen,
381                                   pem_password_cb *cb, void *u);
382
383 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
384                                  void *u);
385
386 int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
387                               const char *kstr, int klen,
388                               pem_password_cb *cd, void *u);
389 # endif
390 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
391 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x);
392
393 # ifndef OPENSSL_NO_DSA
394 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
395 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
396 EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
397 EVP_PKEY *b2i_PublicKey_bio(BIO *in);
398 int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk);
399 int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk);
400 #  ifndef OPENSSL_NO_RC4
401 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
402 int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
403                 pem_password_cb *cb, void *u);
404 #  endif
405 # endif
406
407 # ifdef  __cplusplus
408 }
409 # endif
410 #endif