Update source files for deprecation at 3.0
[oweals/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 # ifdef OPENSSL_NO_STDIO
70
71 #  define IMPLEMENT_PEM_read_fp(name, type, str, asn1) /**/
72 #  define IMPLEMENT_PEM_write_fp(name, type, str, asn1) /**/
73 #  define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) /**/
74 #  define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) /**/
75 #  define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) /**/
76 # else
77
78 #  define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \
79 type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u)\
80 { \
81 return PEM_ASN1_read((d2i_of_void *)d2i_##asn1, str,fp,(void **)x,cb,u); \
82 }
83
84 #  define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \
85 int PEM_write_##name(FILE *fp, type *x) \
86 { \
87 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,NULL,NULL,0,NULL,NULL); \
88 }
89
90 #  define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \
91 int PEM_write_##name(FILE *fp, const type *x) \
92 { \
93 return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,(void *)x,NULL,NULL,0,NULL,NULL); \
94 }
95
96 #  define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \
97 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
98              unsigned char *kstr, int klen, pem_password_cb *cb, \
99                   void *u) \
100         { \
101         return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
102         }
103
104 #  define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \
105 int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
106              unsigned char *kstr, int klen, pem_password_cb *cb, \
107                   void *u) \
108         { \
109         return PEM_ASN1_write((i2d_of_void *)i2d_##asn1,str,fp,x,enc,kstr,klen,cb,u); \
110         }
111
112 # endif
113
114 # define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
115 type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u)\
116 { \
117 return PEM_ASN1_read_bio((d2i_of_void *)d2i_##asn1, str,bp,(void **)x,cb,u); \
118 }
119
120 # define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
121 int PEM_write_bio_##name(BIO *bp, type *x) \
122 { \
123 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,NULL,NULL,0,NULL,NULL); \
124 }
125
126 # define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
127 int PEM_write_bio_##name(BIO *bp, const type *x) \
128 { \
129 return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,NULL,NULL,0,NULL,NULL); \
130 }
131
132 # define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
133 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
134              unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
135         { \
136         return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,x,enc,kstr,klen,cb,u); \
137         }
138
139 # define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
140 int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
141              unsigned char *kstr, int klen, pem_password_cb *cb, void *u) \
142         { \
143         return PEM_ASN1_write_bio((i2d_of_void *)i2d_##asn1,str,bp,(void *)x,enc,kstr,klen,cb,u); \
144         }
145
146 # define IMPLEMENT_PEM_write(name, type, str, asn1) \
147         IMPLEMENT_PEM_write_bio(name, type, str, asn1) \
148         IMPLEMENT_PEM_write_fp(name, type, str, asn1)
149
150 # define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
151         IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \
152         IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
153
154 # define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
155         IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \
156         IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
157
158 # define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
159         IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \
160         IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
161
162 # define IMPLEMENT_PEM_read(name, type, str, asn1) \
163         IMPLEMENT_PEM_read_bio(name, type, str, asn1) \
164         IMPLEMENT_PEM_read_fp(name, type, str, asn1)
165
166 # define IMPLEMENT_PEM_rw(name, type, str, asn1) \
167         IMPLEMENT_PEM_read(name, type, str, asn1) \
168         IMPLEMENT_PEM_write(name, type, str, asn1)
169
170 # define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
171         IMPLEMENT_PEM_read(name, type, str, asn1) \
172         IMPLEMENT_PEM_write_const(name, type, str, asn1)
173
174 # define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
175         IMPLEMENT_PEM_read(name, type, str, asn1) \
176         IMPLEMENT_PEM_write_cb(name, type, str, asn1)
177
178 /* These are the same except they are for the declarations */
179
180 # if defined(OPENSSL_NO_STDIO)
181
182 #  define DECLARE_PEM_read_fp(name, type) /**/
183 #  define DECLARE_PEM_write_fp(name, type) /**/
184 #  define DECLARE_PEM_write_fp_const(name, type) /**/
185 #  define DECLARE_PEM_write_cb_fp(name, type) /**/
186 # else
187
188 #  define DECLARE_PEM_read_fp(name, type) \
189         type *PEM_read_##name(FILE *fp, type **x, pem_password_cb *cb, void *u);
190
191 #  define DECLARE_PEM_write_fp(name, type) \
192         int PEM_write_##name(FILE *fp, type *x);
193
194 #  define DECLARE_PEM_write_fp_const(name, type) \
195         int PEM_write_##name(FILE *fp, const type *x);
196
197 #  define DECLARE_PEM_write_cb_fp(name, type) \
198         int PEM_write_##name(FILE *fp, type *x, const EVP_CIPHER *enc, \
199              unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
200
201 # endif
202
203 #  define DECLARE_PEM_read_bio(name, type) \
204         type *PEM_read_bio_##name(BIO *bp, type **x, pem_password_cb *cb, void *u);
205
206 #  define DECLARE_PEM_write_bio(name, type) \
207         int PEM_write_bio_##name(BIO *bp, type *x);
208
209 #  define DECLARE_PEM_write_bio_const(name, type) \
210         int PEM_write_bio_##name(BIO *bp, const type *x);
211
212 #  define DECLARE_PEM_write_cb_bio(name, type) \
213         int PEM_write_bio_##name(BIO *bp, type *x, const EVP_CIPHER *enc, \
214              unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
215
216 # define DECLARE_PEM_write(name, type) \
217         DECLARE_PEM_write_bio(name, type) \
218         DECLARE_PEM_write_fp(name, type)
219 # define DECLARE_PEM_write_const(name, type) \
220         DECLARE_PEM_write_bio_const(name, type) \
221         DECLARE_PEM_write_fp_const(name, type)
222 # define DECLARE_PEM_write_cb(name, type) \
223         DECLARE_PEM_write_cb_bio(name, type) \
224         DECLARE_PEM_write_cb_fp(name, type)
225 # define DECLARE_PEM_read(name, type) \
226         DECLARE_PEM_read_bio(name, type) \
227         DECLARE_PEM_read_fp(name, type)
228 # define DECLARE_PEM_rw(name, type) \
229         DECLARE_PEM_read(name, type) \
230         DECLARE_PEM_write(name, type)
231 # define DECLARE_PEM_rw_const(name, type) \
232         DECLARE_PEM_read(name, type) \
233         DECLARE_PEM_write_const(name, type)
234 # define DECLARE_PEM_rw_cb(name, type) \
235         DECLARE_PEM_read(name, type) \
236         DECLARE_PEM_write_cb(name, type)
237 typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata);
238
239 int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
240 int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *len,
241                   pem_password_cb *callback, void *u);
242
243 int PEM_read_bio(BIO *bp, char **name, char **header,
244                  unsigned char **data, long *len);
245 #   define PEM_FLAG_SECURE             0x1
246 #   define PEM_FLAG_EAY_COMPATIBLE     0x2
247 #   define PEM_FLAG_ONLY_B64           0x4
248 int PEM_read_bio_ex(BIO *bp, char **name, char **header,
249                     unsigned char **data, long *len, unsigned int flags);
250 int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm,
251                               const char *name, BIO *bp, pem_password_cb *cb,
252                               void *u);
253 int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
254                   const unsigned char *data, long len);
255 int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm,
256                        const char *name, BIO *bp, pem_password_cb *cb,
257                        void *u);
258 void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, BIO *bp, void **x,
259                         pem_password_cb *cb, void *u);
260 int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
261                        const EVP_CIPHER *enc, unsigned char *kstr, int klen,
262                        pem_password_cb *cb, void *u);
263
264 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
265                                             pem_password_cb *cb, void *u);
266 int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
267                             unsigned char *kstr, int klen,
268                             pem_password_cb *cd, void *u);
269
270 #ifndef OPENSSL_NO_STDIO
271 int PEM_read(FILE *fp, char **name, char **header,
272              unsigned char **data, long *len);
273 int PEM_write(FILE *fp, const char *name, const char *hdr,
274               const unsigned char *data, long len);
275 void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x,
276                     pem_password_cb *cb, void *u);
277 int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
278                    void *x, const EVP_CIPHER *enc, unsigned char *kstr,
279                    int klen, pem_password_cb *callback, void *u);
280 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
281                                         pem_password_cb *cb, void *u);
282 #endif
283
284 int PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
285 int PEM_SignUpdate(EVP_MD_CTX *ctx, unsigned char *d, unsigned int cnt);
286 int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
287                   unsigned int *siglen, EVP_PKEY *pkey);
288
289 /* The default pem_password_cb that's used internally */
290 int PEM_def_callback(char *buf, int num, int rwflag, void *userdata);
291 void PEM_proc_type(char *buf, int type);
292 void PEM_dek_info(char *buf, const char *type, int len, char *str);
293
294 # include <openssl/symhacks.h>
295
296 DECLARE_PEM_rw(X509, X509)
297 DECLARE_PEM_rw(X509_AUX, X509)
298 DECLARE_PEM_rw(X509_REQ, X509_REQ)
299 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
300 DECLARE_PEM_rw(X509_CRL, X509_CRL)
301 DECLARE_PEM_rw(PKCS7, PKCS7)
302 DECLARE_PEM_rw(NETSCAPE_CERT_SEQUENCE, NETSCAPE_CERT_SEQUENCE)
303 DECLARE_PEM_rw(PKCS8, X509_SIG)
304 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
305 # ifndef OPENSSL_NO_RSA
306 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
307 DECLARE_PEM_rw_const(RSAPublicKey, RSA)
308 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
309 # endif
310 # ifndef OPENSSL_NO_DSA
311 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
312 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
313 DECLARE_PEM_rw_const(DSAparams, DSA)
314 # endif
315 # ifndef OPENSSL_NO_EC
316 DECLARE_PEM_rw_const(ECPKParameters, EC_GROUP)
317 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
318 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
319 # endif
320 # ifndef OPENSSL_NO_DH
321 DECLARE_PEM_rw_const(DHparams, DH)
322 DECLARE_PEM_write_const(DHxparams, DH)
323 # endif
324 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
325 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
326
327 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
328                                          const EVP_CIPHER *enc,
329                                          unsigned char *kstr, int klen,
330                                          pem_password_cb *cb, void *u);
331
332 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
333                                       char *kstr, int klen,
334                                       pem_password_cb *cb, void *u);
335 int PEM_write_bio_PKCS8PrivateKey(BIO *, const EVP_PKEY *, const EVP_CIPHER *,
336                                   char *, int, pem_password_cb *, void *);
337 int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
338                             char *kstr, int klen,
339                             pem_password_cb *cb, void *u);
340 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
341                                 char *kstr, int klen,
342                                 pem_password_cb *cb, void *u);
343 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
344                                   void *u);
345
346 # ifndef OPENSSL_NO_STDIO
347 int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
348                            char *kstr, int klen,
349                            pem_password_cb *cb, void *u);
350 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
351                                char *kstr, int klen,
352                                pem_password_cb *cb, void *u);
353 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
354                                   char *kstr, int klen,
355                                   pem_password_cb *cb, void *u);
356
357 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
358                                  void *u);
359
360 int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
361                               char *kstr, int klen, pem_password_cb *cd,
362                               void *u);
363 # endif
364 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x);
365 int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x);
366
367 # ifndef OPENSSL_NO_DSA
368 EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length);
369 EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length);
370 EVP_PKEY *b2i_PrivateKey_bio(BIO *in);
371 EVP_PKEY *b2i_PublicKey_bio(BIO *in);
372 int i2b_PrivateKey_bio(BIO *out, EVP_PKEY *pk);
373 int i2b_PublicKey_bio(BIO *out, EVP_PKEY *pk);
374 #  ifndef OPENSSL_NO_RC4
375 EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u);
376 int i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel,
377                 pem_password_cb *cb, void *u);
378 #  endif
379 # endif
380
381 # ifdef  __cplusplus
382 }
383 # endif
384 #endif