3b21ab8234bfc55c52b5c306d83e8c30e414c8bd
[openssl.git] / include / openssl / pem.h
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 HEADER_PEM_H
11 # define HEADER_PEM_H
12
13 # include <openssl/e_os2.h>
14 # include <openssl/bio.h>
15 # include <openssl/safestack.h>
16 # include <openssl/evp.h>
17 # include <openssl/x509.h>
18 # include <openssl/pem2.h>
19 # include <openssl/pemerr.h>
20
21 #ifdef  __cplusplus
22 extern "C" {
23 #endif
24
25 # define PEM_BUFSIZE             1024
26
27 # define PEM_STRING_X509_OLD     "X509 CERTIFICATE"
28 # define PEM_STRING_X509         "CERTIFICATE"
29 # define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
30 # define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
31 # define PEM_STRING_X509_REQ     "CERTIFICATE REQUEST"
32 # define PEM_STRING_X509_CRL     "X509 CRL"
33 # define PEM_STRING_EVP_PKEY     "ANY PRIVATE KEY"
34 # define PEM_STRING_PUBLIC       "PUBLIC KEY"
35 # define PEM_STRING_RSA          "RSA PRIVATE KEY"
36 # define PEM_STRING_RSA_PUBLIC   "RSA PUBLIC KEY"
37 # define PEM_STRING_DSA          "DSA PRIVATE KEY"
38 # define PEM_STRING_DSA_PUBLIC   "DSA PUBLIC KEY"
39 # define PEM_STRING_PKCS7        "PKCS7"
40 # define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
41 # define PEM_STRING_PKCS8        "ENCRYPTED PRIVATE KEY"
42 # define PEM_STRING_PKCS8INF     "PRIVATE KEY"
43 # define PEM_STRING_DHPARAMS     "DH PARAMETERS"
44 # define PEM_STRING_DHXPARAMS    "X9.42 DH PARAMETERS"
45 # define PEM_STRING_SSL_SESSION  "SSL SESSION PARAMETERS"
46 # define PEM_STRING_DSAPARAMS    "DSA PARAMETERS"
47 # define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
48 # define PEM_STRING_ECPARAMETERS "EC PARAMETERS"
49 # define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
50 # define PEM_STRING_PARAMETERS   "PARAMETERS"
51 # define PEM_STRING_CMS          "CMS"
52
53 # define PEM_TYPE_ENCRYPTED      10
54 # define PEM_TYPE_MIC_ONLY       20
55 # define PEM_TYPE_MIC_CLEAR      30
56 # define PEM_TYPE_CLEAR          40
57
58 /*
59  * These macros make the PEM_read/PEM_write functions easier to maintain and
60  * write. Now they are all implemented with either: IMPLEMENT_PEM_rw(...) or
61  * IMPLEMENT_PEM_rw_cb(...)
62  */
63
64 # ifdef OPENSSL_NO_STDIO
65
66 #  define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/
67 #  define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/
68 #  define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) /**/
69 #  define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/
70 #  define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) /**/
71 # else
72
73 #  define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
74 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
75 { \
76 return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \
77 }
78
79 #  define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
80 int PEM_write_##name(FILE *fp, type *x) \
81 { \
82 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \
83 }
84
85 #  define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
86 int PEM_write_##name(FILE *fp, const type *x) \
87 { \
88 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \
89 }
90
91 #  define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
92 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
93              unsigned char *kstr, int klen, pem_password_cb *cb, \
94                   void *u) \
95         { \
96         return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
97         }
98
99 #  define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
100 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
101              unsigned char *kstr, int klen, pem_password_cb *cb, \
102                   void *u) \
103         { \
104         return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
105         }
106
107 # endif
108
109 # define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
110 type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
111 { \
112 return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \
113 }
114
115 # define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
116 int PEM_write_bio_##name(BIO *bp, type *x) \
117 { \
118 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \
119 }
120
121 # define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
122 int PEM_write_bio_##name(BIO *bp, const type *x) \
123 { \
124 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \
125 }
126
127 # define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
128 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
129              unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
130         { \
131         return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \
132         }
133
134 # define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
135 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
136              unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
137         { \
138         return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \
139         }
140
141 # define IMPLEMENT_PEM_write(name, type, str, asn1) \
142         IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
143         IMPLEMENT_PEM_write_fp(name, type, str, asn1)
144
145 # define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
146         IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
147         IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
148
149 # define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
150         IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
151         IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
152
153 # define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
154         IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
155         IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
156
157 # define IMPLEMENT_PEM_read(name, type, str, asn1) \
158         IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
159         IMPLEMENT_PEM_read_fp(name, type, str, asn1)
160
161 # define IMPLEMENT_PEM_rw(name, type, str, asn1) \
162         IMPLEMENT_PEM_read(name, type, str, asn1) \
163         IMPLEMENT_PEM_write(name, type, str, asn1)
164
165 # define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
166         IMPLEMENT_PEM_read(name, type, str, asn1) \
167         IMPLEMENT_PEM_write_const(name, type, str, asn1)
168
169 # define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
170         IMPLEMENT_PEM_read(name, type, str, asn1) \
171         IMPLEMENT_PEM_write_cb(name, type, str, asn1)
172
173 /* These are the same except they are for the declarations */
174
175 # if defined(OPENSSL_NO_STDIO)
176
177 #  define DECLARE_PEM_read_fp(name, type) /**/
178 #  define DECLARE_PEM_write_fp(name, type) /**/
179 #  define DECLARE_PEM_write_fp_const(name, type) /**/
180 #  define DECLARE_PEM_write_cb_fp(name, type) /**/
181 # else
182
183 #  define DECLARE_PEM_read_fp(name, type) \
184         type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
185
186 #  define DECLARE_PEM_write_fp(name, type) \
187         int PEM_write_##name(FILE *fp, type *x);
188
189 #  define DECLARE_PEM_write_fp_const(name, type) \
190         int PEM_write_##name(FILE *fp, const type *x);
191
192 #  define DECLARE_PEM_write_cb_fp(name, type) \
193         int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
194              unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
195
196 # endif
197
198 #  define DECLARE_PEM_read_bio(name, type) \
199         type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
200
201 #  define DECLARE_PEM_write_bio(name, type) \
202         int PEM_write_bio_##name(BIO *bp, type *x);
203
204 #  define DECLARE_PEM_write_bio_const(name, type) \
205         int PEM_write_bio_##name(BIO *bp, const type *x);
206
207 #  define DECLARE_PEM_write_cb_bio(name, type) \
208         int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
209              unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
210
211 # define DECLARE_PEM_write(name, type) \
212         DECLARE_PEM_write_bio(name, type) \
213         DECLARE_PEM_write_fp(name, type)
214 # define DECLARE_PEM_write_const(name, type) \
215         DECLARE_PEM_write_bio_const(name, type) \
216         DECLARE_PEM_write_fp_const(name, type)
217 # define DECLARE_PEM_write_cb(name, type) \
218         DECLARE_PEM_write_cb_bio(name, type) \
219         DECLARE_PEM_write_cb_fp(name, type)
220 # define DECLARE_PEM_read(name, type) \
221         DECLARE_PEM_read_bio(name, type) \
222         DECLARE_PEM_read_fp(name, type)
223 # define DECLARE_PEM_rw(name, type) \
224         DECLARE_PEM_read(name, type) \
225         DECLARE_PEM_write(name, type)
226 # define DECLARE_PEM_rw_const(name, type) \
227         DECLARE_PEM_read(name, type) \
228         DECLARE_PEM_write_const(name, type)
229 # define DECLARE_PEM_rw_cb(name, type) \
230         DECLARE_PEM_read(name, type) \
231         DECLARE_PEM_write_cb(name, type)
232 typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata);
233
234 int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
235 int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *len,
236                   pem_password_cb *callback, void *u);
237
238 int PEM_read_bio(BIO *bp, char **name, char **header,
239                  unsigned char **data, long *len);
240 #   define PEM_FLAG_SECURE             0x1
241 #   define PEM_FLAG_EAY_COMPATIBLE     0x2
242 #   define PEM_FLAG_ONLY_B64           0x4
243 int PEM_read_bio_ex(BIO *bp, char **name, char **header,
244                     unsigned char **data, long *len, unsigned int flags);
245 int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
246                               const char *name, BIO *bp, pem_password_cb *cb,
247                               void *u);
248 int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
249                   const unsigned char *data, long len);
250 int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
251                        const char *name, BIO *bp, pem_password_cb *cb,
252                        void *u);
253 void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
254                         pem_password_cb *cb, void *u);
255 int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
256                        const EVP_CIPHER *enc, unsigned char *kstr, int klen,
257                        pem_password_cb *cb, void *u);
258
259 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
260                                             pem_password_cb *cb, void *u);
261 int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
262                             unsigned char *kstr, int klen,
263                             pem_password_cb *cd, void *u);
264
265 #ifndef OPENSSL_NO_STDIO
266 int PEM_read(FILE *fp, char **name, char **header,
267              unsigned char **data, long *len);
268 int PEM_write(FILE *fp, const char *name, const char *hdr,
269               const unsigned char *data, long len);
270 void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
271                     pem_password_cb *cb, void *u);
272 int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
273                    void *x, const EVP_CIPHER *enc, unsigned char *kstr,
274                    int klen, pem_password_cb *callback, void *u);
275 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
276                                         pem_password_cb *cb, void *u);
277 #endif
278
279 int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
280 int PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *d, unsigned int cnt);
281 int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
282                   unsigned int *siglen, EVP_PKEY *pkey);
283
284 int PEM_def_callback(char *buf, int num, int w, void *key);
285 void PEM_proc_type(char *buf, int type);
286 void PEM_dek_info(char *buf, const char *type, int len, char *str);
287
288 # include <openssl/symhacks.h>
289
290 DECLARE_PEM_rw(X509, X509)
291 DECLARE_PEM_rw(X509_AUX, X509)
292 DECLARE_PEM_rw(X509_REQ, X509_REQ)
293 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
294 DECLARE_PEM_rw(X509_CRL, X509_CRL)
295 DECLARE_PEM_rw(PKCS7, PKCS7)
296 DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
297 DECLARE_PEM_rw(PKCS8, X509_SIG)
298 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
299 # ifndef OPENSSL_NO_RSA
300 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
301 DECLARE_PEM_rw_const(RSAPublicKey, RSA)
302 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
303 # endif
304 # ifndef OPENSSL_NO_DSA
305 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
306 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
307 DECLARE_PEM_rw_const(DSAparams, DSA)
308 # endif
309 # ifndef OPENSSL_NO_EC
310 DECLARE_PEM_rw_const(ECPKParameters, EC_GROUP)
311 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
312 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
313 # endif
314 # ifndef OPENSSL_NO_DH
315 DECLARE_PEM_rw_const(DHparams, DH)
316 DECLARE_PEM_write_const(DHxparams, DH)
317 # endif
318 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
319 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
320
321 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
322                                          const EVP_CIPHER *enc,
323                                          unsigned char *kstr, int klen,
324                                          pem_password_cb *cb, void *u);
325
326 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
327                                       char *kstr, int klen,
328                                       pem_password_cb *cb, void *u);
329 int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, const EVP_CIPHER *,
330                                   char *, int, pem_password_cb *, void *);
331 int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
332                             char *kstr, int klen,
333                             pem_password_cb *cb, void *u);
334 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
335                                 char *kstr, int klen,
336                                 pem_password_cb *cb, void *u);
337 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
338                                   void *u);
339
340 # ifndef OPENSSL_NO_STDIO
341 int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
342                            char *kstr, int klen,
343                            pem_password_cb *cb, void *u);
344 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
345                                char *kstr, int klen,
346                                pem_password_cb *cb, void *u);
347 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
348                                   char *kstr, int klen,
349                                   pem_password_cb *cb, void *u);
350
351 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
352                                  void *u);
353
354 int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
355                               char *kstr, int klen, pem_password_cb *cd,
356                               void *u);
357 # endif
358 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
359 int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x);
360
361 # ifndef OPENSSL_NO_DSA
362 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
363 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
364 EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
365 EVP_PKEY *b2i_PublicKey_bio(BIO *in);
366 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk);
367 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk);
368 #  ifndef OPENSSL_NO_RC4
369 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
370 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
371                 pem_password_cb *cb, void *u);
372 #  endif
373 # endif
374
375 # ifdef  __cplusplus
376 }
377 # endif
378 #endif