1 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #ifndef OPENSSL_HEADER_PEM_H
58 #define OPENSSL_HEADER_PEM_H
59 
60 #include <openssl/base64.h>
61 #include <openssl/bio.h>
62 #include <openssl/cipher.h>
63 #include <openssl/digest.h>
64 #include <openssl/evp.h>
65 #include <openssl/pkcs7.h>
66 #include <openssl/stack.h>
67 #include <openssl/x509.h>
68 
69 // For compatibility with open-iscsi, which assumes that it can get
70 // |OPENSSL_malloc| from pem.h or err.h
71 #include <openssl/crypto.h>
72 
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 
77 
78 #define PEM_BUFSIZE 1024
79 
80 #define PEM_STRING_X509_OLD "X509 CERTIFICATE"
81 #define PEM_STRING_X509 "CERTIFICATE"
82 #define PEM_STRING_X509_PAIR "CERTIFICATE PAIR"
83 #define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
84 #define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
85 #define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
86 #define PEM_STRING_X509_CRL "X509 CRL"
87 #define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
88 #define PEM_STRING_PUBLIC "PUBLIC KEY"
89 #define PEM_STRING_RSA "RSA PRIVATE KEY"
90 #define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
91 #define PEM_STRING_DSA "DSA PRIVATE KEY"
92 #define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
93 #define PEM_STRING_EC "EC PRIVATE KEY"
94 #define PEM_STRING_PKCS7 "PKCS7"
95 #define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
96 #define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
97 #define PEM_STRING_PKCS8INF "PRIVATE KEY"
98 #define PEM_STRING_DHPARAMS "DH PARAMETERS"
99 #define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
100 #define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
101 #define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
102 #define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
103 #define PEM_STRING_CMS "CMS"
104 
105 // enc_type is one off
106 #define PEM_TYPE_ENCRYPTED 10
107 #define PEM_TYPE_MIC_ONLY 20
108 #define PEM_TYPE_MIC_CLEAR 30
109 #define PEM_TYPE_CLEAR 40
110 
111 // These macros make the PEM_read/PEM_write functions easier to maintain and
112 // write. Now they are all implemented with either:
113 // IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
114 
115 
116 #define IMPLEMENT_PEM_read_fp(name, type, str, asn1)                         \
117   static void *pem_read_##name##_d2i(void **x, const unsigned char **inp,    \
118                                      long len) {                             \
119     return d2i_##asn1((type **)x, inp, len);                                 \
120   }                                                                          \
121   OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x,                   \
122                                        pem_password_cb *cb, void *u) {       \
123     return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \
124                                  cb, u);                                     \
125   }
126 
127 #define IMPLEMENT_PEM_write_fp(name, type, str, asn1)                        \
128   static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {   \
129     return i2d_##asn1((type *)x, outp);                                      \
130   }                                                                          \
131   OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) {                   \
132     return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \
133                           NULL, NULL);                                       \
134   }
135 
136 #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)                 \
137   static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {  \
138     return i2d_##asn1((const type *)x, outp);                               \
139   }                                                                         \
140   OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) {            \
141     return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \
142                           NULL, 0, NULL, NULL);                             \
143   }
144 
145 #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)                       \
146   static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {     \
147     return i2d_##asn1((type *)x, outp);                                        \
148   }                                                                            \
149   OPENSSL_EXPORT int PEM_write_##name(                                         \
150       FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
151       pem_password_cb *cb, void *u) {                                          \
152     return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \
153                           cb, u);                                              \
154   }
155 
156 #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)                 \
157   static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {     \
158     return i2d_##asn1((const type *)x, outp);                                  \
159   }                                                                            \
160   OPENSSL_EXPORT int PEM_write_##name(                                         \
161       FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
162       pem_password_cb *cb, void *u) {                                          \
163     return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \
164                           cb, u);                                              \
165   }
166 
167 
168 #define IMPLEMENT_PEM_read_bio(name, type, str, asn1)                         \
169   static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \
170                                          long len) {                          \
171     return d2i_##asn1((type **)x, inp, len);                                  \
172   }                                                                           \
173   OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x,                 \
174                                            pem_password_cb *cb, void *u) {    \
175     return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp,      \
176                                      (void **)x, cb, u);                      \
177   }
178 
179 #define IMPLEMENT_PEM_write_bio(name, type, str, asn1)                         \
180   static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
181     return i2d_##asn1((type *)x, outp);                                        \
182   }                                                                            \
183   OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) {                  \
184     return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL,    \
185                               NULL, 0, NULL, NULL);                            \
186   }
187 
188 #define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1)                   \
189   static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
190     return i2d_##asn1((const type *)x, outp);                                  \
191   }                                                                            \
192   OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) {            \
193     return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x,  \
194                               NULL, NULL, 0, NULL, NULL);                      \
195   }
196 
197 #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)                      \
198   static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
199     return i2d_##asn1((type *)x, outp);                                        \
200   }                                                                            \
201   OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
202       BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen,  \
203       pem_password_cb *cb, void *u) {                                          \
204     return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc,     \
205                               kstr, klen, cb, u);                              \
206   }
207 
208 #define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1)                \
209   static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
210     return i2d_##asn1((const type *)x, outp);                                  \
211   }                                                                            \
212   OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
213       BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen,  \
214       pem_password_cb *cb, void *u) {                                          \
215     return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x,  \
216                               enc, kstr, klen, cb, u);                         \
217   }
218 
219 #define IMPLEMENT_PEM_write(name, type, str, asn1) \
220   IMPLEMENT_PEM_write_bio(name, type, str, asn1)   \
221   IMPLEMENT_PEM_write_fp(name, type, str, asn1)
222 
223 #define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
224   IMPLEMENT_PEM_write_bio_const(name, type, str, asn1)   \
225   IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
226 
227 #define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
228   IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)   \
229   IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
230 
231 #define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
232   IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1)   \
233   IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
234 
235 #define IMPLEMENT_PEM_read(name, type, str, asn1) \
236   IMPLEMENT_PEM_read_bio(name, type, str, asn1)   \
237   IMPLEMENT_PEM_read_fp(name, type, str, asn1)
238 
239 #define IMPLEMENT_PEM_rw(name, type, str, asn1) \
240   IMPLEMENT_PEM_read(name, type, str, asn1)     \
241   IMPLEMENT_PEM_write(name, type, str, asn1)
242 
243 #define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
244   IMPLEMENT_PEM_read(name, type, str, asn1)           \
245   IMPLEMENT_PEM_write_const(name, type, str, asn1)
246 
247 #define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
248   IMPLEMENT_PEM_read(name, type, str, asn1)        \
249   IMPLEMENT_PEM_write_cb(name, type, str, asn1)
250 
251 // These are the same except they are for the declarations
252 
253 #define DECLARE_PEM_read_fp(name, type)                    \
254   OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \
255                                        pem_password_cb *cb, void *u);
256 
257 #define DECLARE_PEM_write_fp(name, type) \
258   OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x);
259 
260 #define DECLARE_PEM_write_fp_const(name, type) \
261   OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x);
262 
263 #define DECLARE_PEM_write_cb_fp(name, type)                                    \
264   OPENSSL_EXPORT int PEM_write_##name(                                         \
265       FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
266       pem_password_cb *cb, void *u);
267 
268 #define DECLARE_PEM_read_bio(name, type)                      \
269   OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \
270                                            pem_password_cb *cb, void *u);
271 
272 #define DECLARE_PEM_write_bio(name, type) \
273   OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x);
274 
275 #define DECLARE_PEM_write_bio_const(name, type) \
276   OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x);
277 
278 #define DECLARE_PEM_write_cb_bio(name, type)                                  \
279   OPENSSL_EXPORT int PEM_write_bio_##name(                                    \
280       BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \
281       pem_password_cb *cb, void *u);
282 
283 
284 #define DECLARE_PEM_write(name, type) \
285   DECLARE_PEM_write_bio(name, type)   \
286   DECLARE_PEM_write_fp(name, type)
287 
288 #define DECLARE_PEM_write_const(name, type) \
289   DECLARE_PEM_write_bio_const(name, type)   \
290   DECLARE_PEM_write_fp_const(name, type)
291 
292 #define DECLARE_PEM_write_cb(name, type) \
293   DECLARE_PEM_write_cb_bio(name, type)   \
294   DECLARE_PEM_write_cb_fp(name, type)
295 
296 #define DECLARE_PEM_read(name, type) \
297   DECLARE_PEM_read_bio(name, type)   \
298   DECLARE_PEM_read_fp(name, type)
299 
300 #define DECLARE_PEM_rw(name, type) \
301   DECLARE_PEM_read(name, type)     \
302   DECLARE_PEM_write(name, type)
303 
304 #define DECLARE_PEM_rw_const(name, type) \
305   DECLARE_PEM_read(name, type)           \
306   DECLARE_PEM_write_const(name, type)
307 
308 #define DECLARE_PEM_rw_cb(name, type) \
309   DECLARE_PEM_read(name, type)        \
310   DECLARE_PEM_write_cb(name, type)
311 
312 // "userdata": new with OpenSSL 0.9.4
313 typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
314 
315 OPENSSL_EXPORT int PEM_get_EVP_CIPHER_INFO(char *header,
316                                            EVP_CIPHER_INFO *cipher);
317 OPENSSL_EXPORT int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data,
318                                  long *len, pem_password_cb *callback, void *u);
319 
320 // PEM_read_bio reads from |bp|, until the next PEM block. If one is found, it
321 // returns one and sets |*name|, |*header|, and |*data| to newly-allocated
322 // buffers containing the PEM type, the header block, and the decoded data,
323 // respectively. |*name| and |*header| are NUL-terminated C strings, while
324 // |*data| has |*len| bytes. The caller must release each of |*name|, |*header|,
325 // and |*data| with |OPENSSL_free| when done. If no PEM block is found, this
326 // function returns zero and pushes |PEM_R_NO_START_LINE| to the error queue. If
327 // one is found, but there is an error decoding it, it returns zero and pushes
328 // some other error to the error queue.
329 OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header,
330                                 unsigned char **data, long *len);
331 
332 // PEM_write_bio writes a PEM block to |bp|, containing |len| bytes from |data|
333 // as data. |name| and |hdr| are NUL-terminated C strings containing the PEM
334 // type and header block, respectively. This function returns zero on error and
335 // the number of bytes written on success.
336 OPENSSL_EXPORT int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
337                                  const unsigned char *data, long len);
338 
339 OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen,
340                                       char **pnm, const char *name, BIO *bp,
341                                       pem_password_cb *cb, void *u);
342 OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name,
343                                        BIO *bp, void **x, pem_password_cb *cb,
344                                        void *u);
345 OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name,
346                                       BIO *bp, void *x, const EVP_CIPHER *enc,
347                                       unsigned char *kstr, int klen,
348                                       pem_password_cb *cb, void *u);
349 
350 OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(
351     BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
352 OPENSSL_EXPORT int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi,
353                                            EVP_CIPHER *enc, unsigned char *kstr,
354                                            int klen, pem_password_cb *cd,
355                                            void *u);
356 
357 OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header,
358                             unsigned char **data, long *len);
359 OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr,
360                              const unsigned char *data, long len);
361 OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp,
362                                    void **x, pem_password_cb *cb, void *u);
363 OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
364                                   void *x, const EVP_CIPHER *enc,
365                                   unsigned char *kstr, int klen,
366                                   pem_password_cb *callback, void *u);
367 OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp,
368                                                        STACK_OF(X509_INFO) *sk,
369                                                        pem_password_cb *cb,
370                                                        void *u);
371 
372 // PEM_def_callback treats |userdata| as a string and copies it into |buf|,
373 // assuming its |size| is sufficient. Returns the length of the string, or 0
374 // if there is not enough room. If either |buf| or |userdata| is NULL, 0 is
375 // returned. Note that this is different from OpenSSL, which prompts for a
376 // password.
377 OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag,
378                                     void *userdata);
379 OPENSSL_EXPORT void PEM_proc_type(char *buf, int type);
380 OPENSSL_EXPORT void PEM_dek_info(char *buf, const char *type, int len,
381                                  char *str);
382 
383 
384 DECLARE_PEM_rw(X509, X509)
385 
386 DECLARE_PEM_rw(X509_AUX, X509)
387 
388 DECLARE_PEM_rw(X509_REQ, X509_REQ)
389 DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
390 
391 DECLARE_PEM_rw(X509_CRL, X509_CRL)
392 
393 DECLARE_PEM_rw(PKCS7, PKCS7)
394 DECLARE_PEM_rw(PKCS8, X509_SIG)
395 
396 DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
397 
398 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
399 
400 DECLARE_PEM_rw_const(RSAPublicKey, RSA)
401 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
402 
403 #ifndef OPENSSL_NO_DSA
404 
405 DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
406 
407 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
408 
409 DECLARE_PEM_rw_const(DSAparams, DSA)
410 
411 #endif
412 
413 DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
414 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
415 
416 
417 DECLARE_PEM_rw_const(DHparams, DH)
418 
419 
420 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
421 
422 DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
423 
424 OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x,
425                                                      int nid, char *kstr,
426                                                      int klen,
427                                                      pem_password_cb *cb,
428                                                      void *u);
429 OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *,
430                                                  const EVP_CIPHER *, char *,
431                                                  int, pem_password_cb *,
432                                                  void *);
433 OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x,
434                                            const EVP_CIPHER *enc, char *kstr,
435                                            int klen, pem_password_cb *cb,
436                                            void *u);
437 OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
438                                                char *kstr, int klen,
439                                                pem_password_cb *cb, void *u);
440 OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x,
441                                                  pem_password_cb *cb, void *u);
442 
443 OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x,
444                                           const EVP_CIPHER *enc, char *kstr,
445                                           int klen, pem_password_cb *cb,
446                                           void *u);
447 OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
448                                               char *kstr, int klen,
449                                               pem_password_cb *cb, void *u);
450 OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
451                                                  char *kstr, int klen,
452                                                  pem_password_cb *cb, void *u);
453 
454 OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x,
455                                                 pem_password_cb *cb, void *u);
456 
457 OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x,
458                                              const EVP_CIPHER *enc, char *kstr,
459                                              int klen, pem_password_cb *cd,
460                                              void *u);
461 
462 
463 #ifdef __cplusplus
464 }
465 #endif
466 
467 #define PEM_R_BAD_BASE64_DECODE 100
468 #define PEM_R_BAD_DECRYPT 101
469 #define PEM_R_BAD_END_LINE 102
470 #define PEM_R_BAD_IV_CHARS 103
471 #define PEM_R_BAD_PASSWORD_READ 104
472 #define PEM_R_CIPHER_IS_NULL 105
473 #define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 106
474 #define PEM_R_NOT_DEK_INFO 107
475 #define PEM_R_NOT_ENCRYPTED 108
476 #define PEM_R_NOT_PROC_TYPE 109
477 #define PEM_R_NO_START_LINE 110
478 #define PEM_R_READ_KEY 111
479 #define PEM_R_SHORT_HEADER 112
480 #define PEM_R_UNSUPPORTED_CIPHER 113
481 #define PEM_R_UNSUPPORTED_ENCRYPTION 114
482 
483 #endif  // OPENSSL_HEADER_PEM_H
484