Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/include/openssl/pem.h
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef OPENSSL_HEADER_PEM_H
16
#define OPENSSL_HEADER_PEM_H
17
18
#include <openssl/base64.h>
19
#include <openssl/bio.h>
20
#include <openssl/cipher.h>
21
#include <openssl/digest.h>
22
#include <openssl/evp.h>
23
#include <openssl/pkcs7.h>
24
#include <openssl/stack.h>
25
#include <openssl/x509.h>
26
27
// For compatibility with open-iscsi, which assumes that it can get
28
// `OPENSSL_malloc` from pem.h or err.h
29
#include <openssl/crypto.h>
30
31
#if defined(__cplusplus)
32
extern "C" {
33
#endif
34
35
36
// PEM.
37
//
38
// This library contains functions for reading and writing data encoded in PEM
39
// format. This format originated in Privacy-Enhanced Mail (RFC 1421).
40
//
41
// As an exception to RFC 1421, generally PEM data is limited to 1 GiB by this
42
// library. This limit should not affect anyone in practice.
43
//
44
// TODO(crbug.com/42290574): Finish documenting this header.
45
46
47
0
#define PEM_BUFSIZE 1024
48
49
0
#define PEM_STRING_X509_OLD "X509 CERTIFICATE"
50
0
#define PEM_STRING_X509 "CERTIFICATE"
51
#define PEM_STRING_X509_PAIR "CERTIFICATE PAIR"
52
0
#define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE"
53
0
#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
54
0
#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
55
0
#define PEM_STRING_X509_CRL "X509 CRL"
56
0
#define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY"
57
#define PEM_STRING_PUBLIC "PUBLIC KEY"
58
0
#define PEM_STRING_RSA "RSA PRIVATE KEY"
59
#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
60
0
#define PEM_STRING_DSA "DSA PRIVATE KEY"
61
#define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY"
62
0
#define PEM_STRING_EC "EC PRIVATE KEY"
63
0
#define PEM_STRING_PKCS7 "PKCS7"
64
0
#define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA"
65
0
#define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY"
66
0
#define PEM_STRING_PKCS8INF "PRIVATE KEY"
67
#define PEM_STRING_DHPARAMS "DH PARAMETERS"
68
#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
69
#define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
70
#define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY"
71
0
#define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY"
72
#define PEM_STRING_CMS "CMS"
73
74
0
#define PEM_TYPE_ENCRYPTED 10
75
0
#define PEM_TYPE_MIC_ONLY 20
76
0
#define PEM_TYPE_MIC_CLEAR 30
77
#define PEM_TYPE_CLEAR 40
78
79
// These macros make the PEM_read/PEM_write functions easier to maintain and
80
// write. Now they are all implemented with either:
81
// IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
82
83
84
#define IMPLEMENT_PEM_read_fp(name, type, str, asn1)                         \
85
  static void *pem_read_##name##_d2i(void **x, const unsigned char **inp,    \
86
0
                                     long len) {                             \
87
0
    return d2i_##asn1((type **)x, inp, len);                                 \
88
0
  }                                                                          \
Unexecuted instantiation: ssl_x509.cc:pem_read_SSL_SESSION_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_X509_REQ_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_X509_CRL_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_PKCS7_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_RSAPublicKey_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_RSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_DSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_DSAparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_EC_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_DHparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_xaux.cc:pem_read_X509_AUX_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_PKCS8_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_PKCS8_PRIV_KEY_INFO_d2i(void**, unsigned char const**, long)
89
  OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x,                   \
90
0
                                       pem_password_cb *cb, void *u) {       \
91
0
    return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \
92
0
                                 cb, u);                                     \
93
0
  }
Unexecuted instantiation: PEM_read_SSL_SESSION
Unexecuted instantiation: PEM_read_X509_REQ
Unexecuted instantiation: PEM_read_X509_CRL
Unexecuted instantiation: PEM_read_PKCS7
Unexecuted instantiation: PEM_read_RSAPublicKey
Unexecuted instantiation: PEM_read_RSA_PUBKEY
Unexecuted instantiation: PEM_read_DSA_PUBKEY
Unexecuted instantiation: PEM_read_DSAparams
Unexecuted instantiation: PEM_read_EC_PUBKEY
Unexecuted instantiation: PEM_read_DHparams
Unexecuted instantiation: PEM_read_PUBKEY
Unexecuted instantiation: PEM_read_X509_AUX
Unexecuted instantiation: PEM_read_PKCS8
Unexecuted instantiation: PEM_read_PKCS8_PRIV_KEY_INFO
94
95
#define IMPLEMENT_PEM_write_fp(name, type, str, asn1)                        \
96
0
  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {   \
97
0
    return i2d_##asn1((type *)x, outp);                                      \
98
0
  }                                                                          \
Unexecuted instantiation: ssl_x509.cc:pem_write_SSL_SESSION_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_X509_REQ_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_X509_REQ_NEW_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_X509_CRL_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_PKCS7_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_RSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_EC_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_xaux.cc:pem_write_X509_AUX_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_PKCS8_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_PKCS8_PRIV_KEY_INFO_i2d(void const*, unsigned char**)
99
0
  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) {                   \
100
0
    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \
101
0
                          NULL, NULL);                                       \
102
0
  }
103
104
#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)                 \
105
0
  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {  \
106
0
    return i2d_##asn1((const type *)x, outp);                               \
107
0
  }                                                                         \
Unexecuted instantiation: pem_all.cc:pem_write_RSAPublicKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DSAparams_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DHparams_i2d(void const*, unsigned char**)
108
0
  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) {            \
109
0
    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \
110
0
                          NULL, 0, NULL, NULL);                             \
111
0
  }
112
113
#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)                   \
114
0
  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
115
0
    return i2d_##asn1((type *)x, outp);                                    \
116
0
  }                                                                        \
117
  OPENSSL_EXPORT int PEM_write_##name(                                     \
118
      FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
119
0
      int pass_len, pem_password_cb *cb, void *u) {                        \
120
0
    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass,   \
121
0
                          pass_len, cb, u);                                \
122
0
  }
123
124
#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)             \
125
0
  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
126
0
    return i2d_##asn1((const type *)x, outp);                              \
127
0
  }                                                                        \
Unexecuted instantiation: pem_all.cc:pem_write_RSAPrivateKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DSAPrivateKey_i2d(void const*, unsigned char**)
128
  OPENSSL_EXPORT int PEM_write_##name(                                     \
129
      FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
130
0
      int pass_len, pem_password_cb *cb, void *u) {                        \
131
0
    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass,   \
132
0
                          pass_len, cb, u);                                \
133
0
  }
Unexecuted instantiation: PEM_write_RSAPrivateKey
Unexecuted instantiation: PEM_write_DSAPrivateKey
134
135
136
#define IMPLEMENT_PEM_read_bio(name, type, str, asn1)                         \
137
  static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \
138
0
                                         long len) {                          \
139
0
    return d2i_##asn1((type **)x, inp, len);                                  \
140
0
  }                                                                           \
Unexecuted instantiation: ssl_x509.cc:pem_read_bio_SSL_SESSION_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_X509_REQ_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_X509_CRL_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_PKCS7_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_RSAPublicKey_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_RSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_DSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_DSAparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_EC_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_DHparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_xaux.cc:pem_read_bio_X509_AUX_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_bio_PKCS8_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_bio_PKCS8_PRIV_KEY_INFO_d2i(void**, unsigned char const**, long)
141
  OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x,                 \
142
0
                                           pem_password_cb *cb, void *u) {    \
143
0
    return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp,      \
144
0
                                     (void **)x, cb, u);                      \
145
0
  }
Unexecuted instantiation: PEM_read_bio_SSL_SESSION
Unexecuted instantiation: PEM_read_bio_X509_REQ
Unexecuted instantiation: PEM_read_bio_X509_CRL
Unexecuted instantiation: PEM_read_bio_PKCS7
Unexecuted instantiation: PEM_read_bio_RSAPublicKey
Unexecuted instantiation: PEM_read_bio_RSA_PUBKEY
Unexecuted instantiation: PEM_read_bio_DSA_PUBKEY
Unexecuted instantiation: PEM_read_bio_DSAparams
Unexecuted instantiation: PEM_read_bio_EC_PUBKEY
Unexecuted instantiation: PEM_read_bio_DHparams
Unexecuted instantiation: PEM_read_bio_PUBKEY
Unexecuted instantiation: PEM_read_bio_X509_AUX
Unexecuted instantiation: PEM_read_bio_PKCS8
Unexecuted instantiation: PEM_read_bio_PKCS8_PRIV_KEY_INFO
146
147
#define IMPLEMENT_PEM_write_bio(name, type, str, asn1)                         \
148
0
  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
149
0
    return i2d_##asn1((type *)x, outp);                                        \
150
0
  }                                                                            \
Unexecuted instantiation: ssl_x509.cc:pem_write_bio_SSL_SESSION_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_X509_REQ_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_X509_REQ_NEW_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_X509_CRL_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_PKCS7_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_RSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_EC_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_xaux.cc:pem_write_bio_X509_AUX_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_bio_PKCS8_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_bio_PKCS8_PRIV_KEY_INFO_i2d(void const*, unsigned char**)
151
0
  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) {                  \
152
0
    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL,    \
153
0
                              NULL, 0, NULL, NULL);                            \
154
0
  }
155
156
#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1)                   \
157
0
  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
158
0
    return i2d_##asn1((const type *)x, outp);                                  \
159
0
  }                                                                            \
Unexecuted instantiation: pem_all.cc:pem_write_bio_RSAPublicKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DSAparams_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DHparams_i2d(void const*, unsigned char**)
160
0
  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) {            \
161
0
    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x,  \
162
0
                              NULL, NULL, 0, NULL, NULL);                      \
163
0
  }
164
165
#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)                      \
166
0
  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
167
0
    return i2d_##asn1((type *)x, outp);                                        \
168
0
  }                                                                            \
169
  OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
170
      BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass,      \
171
0
      int pass_len, pem_password_cb *cb, void *u) {                            \
172
0
    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc,     \
173
0
                              pass, pass_len, cb, u);                          \
174
0
  }
175
176
#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1)                \
177
0
  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
178
0
    return i2d_##asn1((const type *)x, outp);                                  \
179
0
  }                                                                            \
Unexecuted instantiation: pem_all.cc:pem_write_bio_RSAPrivateKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DSAPrivateKey_i2d(void const*, unsigned char**)
180
  OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
181
      BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass,      \
182
0
      int pass_len, pem_password_cb *cb, void *u) {                            \
183
0
    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x,  \
184
0
                              enc, pass, pass_len, cb, u);                     \
185
0
  }
Unexecuted instantiation: PEM_write_bio_RSAPrivateKey
Unexecuted instantiation: PEM_write_bio_DSAPrivateKey
186
187
#define IMPLEMENT_PEM_write(name, type, str, asn1) \
188
  IMPLEMENT_PEM_write_bio(name, type, str, asn1)   \
189
  IMPLEMENT_PEM_write_fp(name, type, str, asn1)
190
191
#define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
192
  IMPLEMENT_PEM_write_bio_const(name, type, str, asn1)   \
193
  IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
194
195
#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
196
  IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)   \
197
  IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
198
199
#define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
200
  IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1)   \
201
  IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
202
203
#define IMPLEMENT_PEM_read(name, type, str, asn1) \
204
  IMPLEMENT_PEM_read_bio(name, type, str, asn1)   \
205
  IMPLEMENT_PEM_read_fp(name, type, str, asn1)
206
207
#define IMPLEMENT_PEM_rw(name, type, str, asn1) \
208
  IMPLEMENT_PEM_read(name, type, str, asn1)     \
209
  IMPLEMENT_PEM_write(name, type, str, asn1)
210
211
#define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
212
  IMPLEMENT_PEM_read(name, type, str, asn1)           \
213
  IMPLEMENT_PEM_write_const(name, type, str, asn1)
214
215
#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
216
  IMPLEMENT_PEM_read(name, type, str, asn1)        \
217
  IMPLEMENT_PEM_write_cb(name, type, str, asn1)
218
219
// These are the same except they are for the declarations
220
221
#define DECLARE_PEM_read_fp(name, type)                    \
222
  OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \
223
                                       pem_password_cb *cb, void *u);
224
225
#define DECLARE_PEM_write_fp(name, type) \
226
  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x);
227
228
#define DECLARE_PEM_write_fp_const(name, type) \
229
  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x);
230
231
#define DECLARE_PEM_write_cb_fp(name, type)                                \
232
  OPENSSL_EXPORT int PEM_write_##name(                                     \
233
      FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
234
      int pass_len, pem_password_cb *cb, void *u);
235
236
#define DECLARE_PEM_read_bio(name, type)                      \
237
  OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \
238
                                           pem_password_cb *cb, void *u);
239
240
#define DECLARE_PEM_write_bio(name, type) \
241
  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x);
242
243
#define DECLARE_PEM_write_bio_const(name, type) \
244
  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x);
245
246
#define DECLARE_PEM_write_cb_bio(name, type)                              \
247
  OPENSSL_EXPORT int PEM_write_bio_##name(                                \
248
      BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
249
      int pass_len, pem_password_cb *cb, void *u);
250
251
252
#define DECLARE_PEM_write(name, type) \
253
  DECLARE_PEM_write_bio(name, type)   \
254
  DECLARE_PEM_write_fp(name, type)
255
256
#define DECLARE_PEM_write_const(name, type) \
257
  DECLARE_PEM_write_bio_const(name, type)   \
258
  DECLARE_PEM_write_fp_const(name, type)
259
260
#define DECLARE_PEM_write_cb(name, type) \
261
  DECLARE_PEM_write_cb_bio(name, type)   \
262
  DECLARE_PEM_write_cb_fp(name, type)
263
264
#define DECLARE_PEM_read(name, type) \
265
  DECLARE_PEM_read_bio(name, type)   \
266
  DECLARE_PEM_read_fp(name, type)
267
268
#define DECLARE_PEM_rw(name, type) \
269
  DECLARE_PEM_read(name, type)     \
270
  DECLARE_PEM_write(name, type)
271
272
#define DECLARE_PEM_rw_const(name, type) \
273
  DECLARE_PEM_read(name, type)           \
274
  DECLARE_PEM_write_const(name, type)
275
276
#define DECLARE_PEM_rw_cb(name, type) \
277
  DECLARE_PEM_read(name, type)        \
278
  DECLARE_PEM_write_cb(name, type)
279
280
// "userdata": new with OpenSSL 0.9.4
281
typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
282
283
// PEM_read_bio reads from `bp`, until the next PEM block. If one is found, it
284
// returns one and sets `*name`, `*header`, and `*data` to newly-allocated
285
// buffers containing the PEM type, the header block, and the decoded data,
286
// respectively. `*name` and `*header` are NUL-terminated C strings, while
287
// `*data` has `*len` bytes. The caller must release each of `*name`, `*header`,
288
// and `*data` with `OPENSSL_free` when done. If no PEM block is found, this
289
// function returns zero and pushes `PEM_R_NO_START_LINE` to the error queue. If
290
// one is found, but there is an error decoding it, it returns zero and pushes
291
// some other error to the error queue.
292
OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header,
293
                                unsigned char **data, long *len);
294
295
// PEM_write_bio writes a PEM block to `bp`, containing `len` bytes from `data`
296
// as data. `name` and `hdr` are NUL-terminated C strings containing the PEM
297
// type and header block, respectively. This function returns zero on error and
298
// the number of bytes written on success.
299
OPENSSL_EXPORT int PEM_write_bio(BIO *bp, const char *name, const char *hdr,
300
                                 const unsigned char *data, long len);
301
302
OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen,
303
                                      char **pnm, const char *name, BIO *bp,
304
                                      pem_password_cb *cb, void *u);
305
OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name,
306
                                       BIO *bp, void **x, pem_password_cb *cb,
307
                                       void *u);
308
OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name,
309
                                      BIO *bp, void *x, const EVP_CIPHER *enc,
310
                                      const unsigned char *pass, int pass_len,
311
                                      pem_password_cb *cb, void *u);
312
313
// PEM_X509_INFO_read_bio reads PEM blocks from `bp` and decodes any
314
// certificates, CRLs, and private keys found. It returns a
315
// `STACK_OF(X509_INFO)` structure containing the results, or NULL on error.
316
//
317
// If `sk` is NULL, the result on success will be a newly-allocated
318
// `STACK_OF(X509_INFO)` structure which should be released with
319
// `sk_X509_INFO_pop_free` and `X509_INFO_free` when done.
320
//
321
// If `sk` is non-NULL, it appends the results to `sk` instead and returns `sk`
322
// on success. In this case, the caller retains ownership of `sk` in both
323
// success and failure.
324
//
325
// This function will decrypt any encrypted certificates in `bp`, using `cb`,
326
// but it will not decrypt encrypted private keys. Encrypted private keys are
327
// instead represented as placeholder `X509_INFO` objects with an empty `x_pkey`
328
// field. This allows this function to be used with inputs with unencrypted
329
// certificates, but encrypted passwords, without knowing the password. However,
330
// it also means that this function cannot be used to decrypt the private key
331
// when the password is known.
332
//
333
// WARNING: If the input contains "TRUSTED CERTIFICATE" PEM blocks, this
334
// function parses auxiliary properties as in `d2i_X509_AUX`. Passing untrusted
335
// input to this function allows an attacker to influence those properties. See
336
// `d2i_X509_AUX` for details.
337
OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(
338
    BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u);
339
340
// PEM_X509_INFO_read behaves like `PEM_X509_INFO_read_bio` but reads from a
341
// `FILE`.
342
OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp,
343
                                                       STACK_OF(X509_INFO) *sk,
344
                                                       pem_password_cb *cb,
345
                                                       void *u);
346
347
OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header,
348
                            unsigned char **data, long *len);
349
OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr,
350
                             const unsigned char *data, long len);
351
OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp,
352
                                   void **x, pem_password_cb *cb, void *u);
353
OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
354
                                  void *x, const EVP_CIPHER *enc,
355
                                  const unsigned char *pass, int pass_len,
356
                                  pem_password_cb *callback, void *u);
357
358
// PEM_def_callback treats `userdata` as a string and copies it into `buf`,
359
// assuming its `size` is sufficient. Returns the length of the string, or -1 on
360
// error. Error cases the buffer being too small, or `buf` and `userdata` being
361
// NULL. Note that this is different from OpenSSL, which prompts for a password.
362
OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag,
363
                                    void *userdata);
364
365
366
DECLARE_PEM_rw(X509, X509)
367
368
// TODO(crbug.com/boringssl/426): When documenting these, copy the warning
369
// about auxiliary properties from `PEM_X509_INFO_read_bio`.
370
371
DECLARE_PEM_rw(X509_AUX, X509)
372
373
DECLARE_PEM_rw(X509_REQ, X509_REQ)
374
DECLARE_PEM_write(X509_REQ_NEW, X509_REQ)
375
376
DECLARE_PEM_rw(X509_CRL, X509_CRL)
377
378
DECLARE_PEM_rw(PKCS7, PKCS7)
379
DECLARE_PEM_rw(PKCS8, X509_SIG)
380
381
DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
382
383
DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
384
385
DECLARE_PEM_rw_const(RSAPublicKey, RSA)
386
DECLARE_PEM_rw(RSA_PUBKEY, RSA)
387
388
#ifndef OPENSSL_NO_DSA
389
390
DECLARE_PEM_rw_cb(DSAPrivateKey, DSA)
391
392
DECLARE_PEM_rw(DSA_PUBKEY, DSA)
393
394
DECLARE_PEM_rw_const(DSAparams, DSA)
395
396
#endif
397
398
DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY)
399
DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
400
401
402
DECLARE_PEM_rw_const(DHparams, DH)
403
404
405
DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
406
407
DECLARE_PEM_rw(PUBKEY, EVP_PKEY)
408
409
OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x,
410
                                                     int nid, const char *pass,
411
                                                     int pass_len,
412
                                                     pem_password_cb *cb,
413
                                                     void *u);
414
OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x,
415
                                                 const EVP_CIPHER *enc,
416
                                                 const char *pass, int pass_len,
417
                                                 pem_password_cb *cb, void *u);
418
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x,
419
                                           const EVP_CIPHER *enc,
420
                                           const char *pass, int pass_len,
421
                                           pem_password_cb *cb, void *u);
422
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x,
423
                                               int nid, const char *pass,
424
                                               int pass_len,
425
                                               pem_password_cb *cb, void *u);
426
OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x,
427
                                                 pem_password_cb *cb, void *u);
428
429
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x,
430
                                          const EVP_CIPHER *enc,
431
                                          const char *pass, int pass_len,
432
                                          pem_password_cb *cb, void *u);
433
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x,
434
                                              int nid, const char *pass,
435
                                              int pass_len, pem_password_cb *cb,
436
                                              void *u);
437
OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x,
438
                                                 int nid, const char *pass,
439
                                                 int pass_len,
440
                                                 pem_password_cb *cb, void *u);
441
442
OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x,
443
                                                pem_password_cb *cb, void *u);
444
445
OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x,
446
                                             const EVP_CIPHER *enc,
447
                                             const char *pass, int pass_len,
448
                                             pem_password_cb *cd, void *u);
449
450
451
#if defined(__cplusplus)
452
}  // extern C
453
#endif
454
455
#define PEM_R_BAD_BASE64_DECODE 100
456
#define PEM_R_BAD_DECRYPT 101
457
#define PEM_R_BAD_END_LINE 102
458
#define PEM_R_BAD_IV_CHARS 103
459
#define PEM_R_BAD_PASSWORD_READ 104
460
#define PEM_R_CIPHER_IS_NULL 105
461
#define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 106
462
#define PEM_R_NOT_DEK_INFO 107
463
#define PEM_R_NOT_ENCRYPTED 108
464
#define PEM_R_NOT_PROC_TYPE 109
465
0
#define PEM_R_NO_START_LINE 110
466
#define PEM_R_READ_KEY 111
467
#define PEM_R_SHORT_HEADER 112
468
#define PEM_R_UNSUPPORTED_CIPHER 113
469
#define PEM_R_UNSUPPORTED_ENCRYPTION 114
470
#define PEM_R_UNSUPPORTED_PROC_TYPE_VERSION 115
471
#define PEM_R_NO_DATA 116
472
473
#endif  // OPENSSL_HEADER_PEM_H