| 1 |
// Copyright (C) 1995-1997 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_PEM_H |
| 5 |
#define OPENSSL_HEADER_PEM_H |
| 6 |
|
| 7 |
#include <openssl/base64.h> |
| 8 |
#include <openssl/bio.h> |
| 9 |
#include <openssl/cipher.h> |
| 10 |
#include <openssl/digest.h> |
| 11 |
#include <openssl/evp.h> |
| 12 |
#include <openssl/pkcs7.h> |
| 13 |
#include <openssl/stack.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
|
| 16 |
// For compatibility with open-iscsi, which assumes that it can get |
| 17 |
// |OPENSSL_malloc| from pem.h or err.h |
| 18 |
#include <openssl/crypto.h> |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
extern "C" { |
| 22 |
#endif |
| 23 |
|
| 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_PAIR "CERTIFICATE PAIR" |
| 30 |
#define PEM_STRING_X509_TRUSTED "TRUSTED CERTIFICATE" |
| 31 |
#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST" |
| 32 |
#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST" |
| 33 |
#define PEM_STRING_X509_CRL "X509 CRL" |
| 34 |
#define PEM_STRING_EVP_PKEY "ANY PRIVATE KEY" |
| 35 |
#define PEM_STRING_PUBLIC "PUBLIC KEY" |
| 36 |
#define PEM_STRING_RSA "RSA PRIVATE KEY" |
| 37 |
#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY" |
| 38 |
#define PEM_STRING_DSA "DSA PRIVATE KEY" |
| 39 |
#define PEM_STRING_DSA_PUBLIC "DSA PUBLIC KEY" |
| 40 |
#define PEM_STRING_EC "EC PRIVATE KEY" |
| 41 |
#define PEM_STRING_PKCS7 "PKCS7" |
| 42 |
#define PEM_STRING_PKCS7_SIGNED "PKCS #7 SIGNED DATA" |
| 43 |
#define PEM_STRING_PKCS8 "ENCRYPTED PRIVATE KEY" |
| 44 |
#define PEM_STRING_PKCS8INF "PRIVATE KEY" |
| 45 |
#define PEM_STRING_DHPARAMS "DH PARAMETERS" |
| 46 |
#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS" |
| 47 |
#define PEM_STRING_DSAPARAMS "DSA PARAMETERS" |
| 48 |
#define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY" |
| 49 |
#define PEM_STRING_ECPARAMETERS "EC PARAMETERS" |
| 50 |
#define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY" |
| 51 |
#define PEM_STRING_PARAMETERS "PARAMETERS" |
| 52 |
#define PEM_STRING_CMS "CMS" |
| 53 |
|
| 54 |
// enc_type is one off |
| 55 |
#define PEM_TYPE_ENCRYPTED 10 |
| 56 |
#define PEM_TYPE_MIC_ONLY 20 |
| 57 |
#define PEM_TYPE_MIC_CLEAR 30 |
| 58 |
#define PEM_TYPE_CLEAR 40 |
| 59 |
|
| 60 |
// For compatibility with OpenSSL. First argument ignored. |
| 61 |
#define PEMerr(f, r) OPENSSL_PUT_ERROR(PEM, (r)) |
| 62 |
|
| 63 |
// These macros make the PEM_read/PEM_write functions easier to maintain and |
| 64 |
// write. Now they are all implemented with either: |
| 65 |
// IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...) |
| 66 |
|
| 67 |
|
| 68 |
#define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \ |
| 69 |
static void *pem_read_##name##_d2i(void **x, const unsigned char **inp, \ |
| 70 |
long len) { \ |
| 71 |
return d2i_##asn1((type **)x, inp, len); \ |
| 72 |
} \ |
| 73 |
OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ |
| 74 |
pem_password_cb *cb, void *u) { \ |
| 75 |
return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \ |
| 76 |
cb, u); \ |
| 77 |
} |
| 78 |
|
| 79 |
#define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \ |
| 80 |
static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 81 |
return i2d_##asn1((type *)x, outp); \ |
| 82 |
} \ |
| 83 |
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) { \ |
| 84 |
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \ |
| 85 |
NULL, NULL); \ |
| 86 |
} |
| 87 |
|
| 88 |
#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \ |
| 89 |
static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 90 |
return i2d_##asn1((const type *)x, outp); \ |
| 91 |
} \ |
| 92 |
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) { \ |
| 93 |
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \ |
| 94 |
NULL, 0, NULL, NULL); \ |
| 95 |
} |
| 96 |
|
| 97 |
#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \ |
| 98 |
static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 99 |
return i2d_##asn1((type *)x, outp); \ |
| 100 |
} \ |
| 101 |
OPENSSL_EXPORT int PEM_write_##name( \ |
| 102 |
FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ |
| 103 |
int pass_len, pem_password_cb *cb, void *u) { \ |
| 104 |
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass, \ |
| 105 |
pass_len, cb, u); \ |
| 106 |
} |
| 107 |
|
| 108 |
#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \ |
| 109 |
static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 110 |
return i2d_##asn1((const type *)x, outp); \ |
| 111 |
} \ |
| 112 |
OPENSSL_EXPORT int PEM_write_##name( \ |
| 113 |
FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ |
| 114 |
int pass_len, pem_password_cb *cb, void *u) { \ |
| 115 |
return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass, \ |
| 116 |
pass_len, cb, u); \ |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
#define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ |
| 121 |
static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \ |
| 122 |
long len) { \ |
| 123 |
return d2i_##asn1((type **)x, inp, len); \ |
| 124 |
} \ |
| 125 |
OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \ |
| 126 |
pem_password_cb *cb, void *u) { \ |
| 127 |
return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp, \ |
| 128 |
(void **)x, cb, u); \ |
| 129 |
} |
| 130 |
|
| 131 |
#define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ |
| 132 |
static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 133 |
return i2d_##asn1((type *)x, outp); \ |
| 134 |
} \ |
| 135 |
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) { \ |
| 136 |
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL, \ |
| 137 |
NULL, 0, NULL, NULL); \ |
| 138 |
} |
| 139 |
|
| 140 |
#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ |
| 141 |
static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 142 |
return i2d_##asn1((const type *)x, outp); \ |
| 143 |
} \ |
| 144 |
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) { \ |
| 145 |
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \ |
| 146 |
NULL, NULL, 0, NULL, NULL); \ |
| 147 |
} |
| 148 |
|
| 149 |
#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ |
| 150 |
static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 151 |
return i2d_##asn1((type *)x, outp); \ |
| 152 |
} \ |
| 153 |
OPENSSL_EXPORT int PEM_write_bio_##name( \ |
| 154 |
BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ |
| 155 |
int pass_len, pem_password_cb *cb, void *u) { \ |
| 156 |
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc, \ |
| 157 |
pass, pass_len, cb, u); \ |
| 158 |
} |
| 159 |
|
| 160 |
#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ |
| 161 |
static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 162 |
return i2d_##asn1((const type *)x, outp); \ |
| 163 |
} \ |
| 164 |
OPENSSL_EXPORT int PEM_write_bio_##name( \ |
| 165 |
BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ |
| 166 |
int pass_len, pem_password_cb *cb, void *u) { \ |
| 167 |
return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \ |
| 168 |
enc, pass, pass_len, cb, u); \ |
| 169 |
} |
| 170 |
|
| 171 |
#define IMPLEMENT_PEM_write(name, type, str, asn1) \ |
| 172 |
IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ |
| 173 |
IMPLEMENT_PEM_write_fp(name, type, str, asn1) |
| 174 |
|
| 175 |
#define IMPLEMENT_PEM_write_const(name, type, str, asn1) \ |
| 176 |
IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ |
| 177 |
IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) |
| 178 |
|
| 179 |
#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \ |
| 180 |
IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ |
| 181 |
IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) |
| 182 |
|
| 183 |
#define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \ |
| 184 |
IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ |
| 185 |
IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) |
| 186 |
|
| 187 |
#define IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 188 |
IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ |
| 189 |
IMPLEMENT_PEM_read_fp(name, type, str, asn1) |
| 190 |
|
| 191 |
#define IMPLEMENT_PEM_rw(name, type, str, asn1) \ |
| 192 |
IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 193 |
IMPLEMENT_PEM_write(name, type, str, asn1) |
| 194 |
|
| 195 |
#define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \ |
| 196 |
IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 197 |
IMPLEMENT_PEM_write_const(name, type, str, asn1) |
| 198 |
|
| 199 |
#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \ |
| 200 |
IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 201 |
IMPLEMENT_PEM_write_cb(name, type, str, asn1) |
| 202 |
|
| 203 |
// These are the same except they are for the declarations |
| 204 |
|
| 205 |
#define DECLARE_PEM_read_fp(name, type) \ |
| 206 |
OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ |
| 207 |
pem_password_cb *cb, void *u); |
| 208 |
|
| 209 |
#define DECLARE_PEM_write_fp(name, type) \ |
| 210 |
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x); |
| 211 |
|
| 212 |
#define DECLARE_PEM_write_fp_const(name, type) \ |
| 213 |
OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x); |
| 214 |
|
| 215 |
#define DECLARE_PEM_write_cb_fp(name, type) \ |
| 216 |
OPENSSL_EXPORT int PEM_write_##name( \ |
| 217 |
FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ |
| 218 |
int pass_len, pem_password_cb *cb, void *u); |
| 219 |
|
| 220 |
#define DECLARE_PEM_read_bio(name, type) \ |
| 221 |
OPENSSL_EXPORT 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 |
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x); |
| 226 |
|
| 227 |
#define DECLARE_PEM_write_bio_const(name, type) \ |
| 228 |
OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x); |
| 229 |
|
| 230 |
#define DECLARE_PEM_write_cb_bio(name, type) \ |
| 231 |
OPENSSL_EXPORT int PEM_write_bio_##name( \ |
| 232 |
BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \ |
| 233 |
int pass_len, pem_password_cb *cb, void *u); |
| 234 |
|
| 235 |
|
| 236 |
#define DECLARE_PEM_write(name, type) \ |
| 237 |
DECLARE_PEM_write_bio(name, type) \ |
| 238 |
DECLARE_PEM_write_fp(name, type) |
| 239 |
|
| 240 |
#define DECLARE_PEM_write_const(name, type) \ |
| 241 |
DECLARE_PEM_write_bio_const(name, type) \ |
| 242 |
DECLARE_PEM_write_fp_const(name, type) |
| 243 |
|
| 244 |
#define DECLARE_PEM_write_cb(name, type) \ |
| 245 |
DECLARE_PEM_write_cb_bio(name, type) \ |
| 246 |
DECLARE_PEM_write_cb_fp(name, type) |
| 247 |
|
| 248 |
#define DECLARE_PEM_read(name, type) \ |
| 249 |
DECLARE_PEM_read_bio(name, type) \ |
| 250 |
DECLARE_PEM_read_fp(name, type) |
| 251 |
|
| 252 |
#define DECLARE_PEM_rw(name, type) \ |
| 253 |
DECLARE_PEM_read(name, type) \ |
| 254 |
DECLARE_PEM_write(name, type) |
| 255 |
|
| 256 |
#define DECLARE_PEM_rw_const(name, type) \ |
| 257 |
DECLARE_PEM_read(name, type) \ |
| 258 |
DECLARE_PEM_write_const(name, type) |
| 259 |
|
| 260 |
#define DECLARE_PEM_rw_cb(name, type) \ |
| 261 |
DECLARE_PEM_read(name, type) \ |
| 262 |
DECLARE_PEM_write_cb(name, type) |
| 263 |
|
| 264 |
// "userdata": new with OpenSSL 0.9.4 |
| 265 |
typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata); |
| 266 |
|
| 267 |
OPENSSL_EXPORT int PEM_get_EVP_CIPHER_INFO(char *header, |
| 268 |
EVP_CIPHER_INFO *cipher); |
| 269 |
|
| 270 |
// PEM_do_header decrypts PEM-encoded data using the cipher info in |cipher|. |
| 271 |
// It processes |data| of length |len| using a password obtained via |callback| |
| 272 |
// (or the default callback provided via |PEM_def_callback| if NULL) with callback |
| 273 |
// data |u|. It then updates |len| with decrypted length. |
| 274 |
// Returns 1 on success or if |cipher| is NULL, 0 on failure. |
| 275 |
OPENSSL_EXPORT int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, |
| 276 |
long *len, pem_password_cb *callback, void *u); |
| 277 |
|
| 278 |
// PEM_read_bio reads from |bp|, until the next PEM block. If one is found, it |
| 279 |
// returns one and sets |*name|, |*header|, and |*data| to newly-allocated |
| 280 |
// buffers containing the PEM type, the header block, and the decoded data, |
| 281 |
// respectively. |*name| and |*header| are NUL-terminated C strings, while |
| 282 |
// |*data| has |*len| bytes. The caller must release each of |*name|, |*header|, |
| 283 |
// and |*data| with |OPENSSL_free| when done. If no PEM block is found, this |
| 284 |
// function returns zero and pushes |PEM_R_NO_START_LINE| to the error queue. If |
| 285 |
// one is found, but there is an error decoding it, it returns zero and pushes |
| 286 |
// some other error to the error queue. |
| 287 |
OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header, |
| 288 |
unsigned char **data, long *len); |
| 289 |
|
| 290 |
// PEM_write_bio writes a PEM block to |bp|, containing |len| bytes from |data| |
| 291 |
// as data. |name| and |hdr| are NUL-terminated C strings containing the PEM |
| 292 |
// type and header block, respectively. This function returns zero on error and |
| 293 |
// the number of bytes written on success. |
| 294 |
OPENSSL_EXPORT int PEM_write_bio(BIO *bp, const char *name, const char *hdr, |
| 295 |
const unsigned char *data, long len); |
| 296 |
|
| 297 |
// PEM_bytes_read_bio reads PEM-formatted data from |bp| for the data type given |
| 298 |
// in |name|. If a PEM block is found, it returns one and sets |*pnm| and |
| 299 |
// |*pdata| to newly-allocated buffers containing the PEM type and the decoded |
| 300 |
// data, respectively. |*pnm| is a NUL-terminated C string, while |*pdata| has |
| 301 |
// |*plen| bytes. The caller must release each of |*pnm| and |*pdata| with |
| 302 |
// |OPENSSL_free| when done. If no PEM block is found, this function returns |
| 303 |
// zero and pushes |PEM_R_NO_START_LINE| to the error queue. If one is found, |
| 304 |
// but there is an error decoding it, it returns zero and pushes some other |
| 305 |
// error to the error queue. |cb| is the callback to use when querying for |
| 306 |
// pass phrase used for encrypted PEM structures (normally only private keys) |
| 307 |
// and |u| is interpreted as the null terminated string to use as the |
| 308 |
// passphrase. |
| 309 |
OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen, |
| 310 |
char **pnm, const char *name, BIO *bp, |
| 311 |
pem_password_cb *cb, void *u); |
| 312 |
|
| 313 |
OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, |
| 314 |
BIO *bp, void **x, pem_password_cb *cb, |
| 315 |
void *u); |
| 316 |
|
| 317 |
// PEM_ASN1_write_bio writes ASN.1 structure |x| encoded by |i2d| to BIO |bp| in PEM format |
| 318 |
// with name |name|. If |enc| is non-NULL, encrypts data using cipher with password from |
| 319 |
// |pass| and |pass_len|, or via |callback| with user data |u| (uses PEM_def_callback if |
| 320 |
// callback is NULL). Returns 1 on success, 0 on failure. |
| 321 |
OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, |
| 322 |
BIO *bp, void *x, const EVP_CIPHER *enc, |
| 323 |
const unsigned char *pass, int pass_len, |
| 324 |
pem_password_cb *cb, void *u); |
| 325 |
|
| 326 |
// PEM_X509_INFO_read_bio reads PEM blocks from |bp| and decodes any |
| 327 |
// certificates, CRLs, and private keys found. It returns a |
| 328 |
// |STACK_OF(X509_INFO)| structure containing the results, or NULL on error. |
| 329 |
// |
| 330 |
// If |sk| is NULL, the result on success will be a newly-allocated |
| 331 |
// |STACK_OF(X509_INFO)| structure which should be released with |
| 332 |
// |sk_X509_INFO_pop_free| and |X509_INFO_free| when done. |
| 333 |
// |
| 334 |
// If |sk| is non-NULL, it appends the results to |sk| instead and returns |sk| |
| 335 |
// on success. In this case, the caller retains ownership of |sk| in both |
| 336 |
// success and failure. |
| 337 |
// |
| 338 |
// WARNING: If the input contains "TRUSTED CERTIFICATE" PEM blocks, this |
| 339 |
// function parses auxiliary properties as in |d2i_X509_AUX|. Passing untrusted |
| 340 |
// input to this function allows an attacker to influence those properties. See |
| 341 |
// |d2i_X509_AUX| for details. |
| 342 |
OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio( |
| 343 |
BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u); |
| 344 |
|
| 345 |
// PEM_X509_INFO_write_bio writes the contents of the |X509_INFO| structure |xi| |
| 346 |
// to the |BIO| object |bp| in PEM format. If the X509_INFO contains a |
| 347 |
// certificate (x509), it will be written after the private key (if any). Other |
| 348 |
// fields in X509_INFO (such as CRLs) are currently ignored. |
| 349 |
// |
| 350 |
// It returns 1 on success and 0 on failure. |
| 351 |
OPENSSL_EXPORT int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, |
| 352 |
EVP_CIPHER *enc, unsigned char *kstr, |
| 353 |
int klen, pem_password_cb *cd, |
| 354 |
void *u); |
| 355 |
|
| 356 |
// PEM_X509_INFO_read behaves like |PEM_X509_INFO_read_bio| but reads from a |
| 357 |
// |FILE|. |
| 358 |
OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, |
| 359 |
STACK_OF(X509_INFO) *sk, |
| 360 |
pem_password_cb *cb, |
| 361 |
void *u); |
| 362 |
|
| 363 |
OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header, |
| 364 |
unsigned char **data, long *len); |
| 365 |
OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr, |
| 366 |
const unsigned char *data, long len); |
| 367 |
OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, |
| 368 |
void **x, pem_password_cb *cb, void *u); |
| 369 |
OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, |
| 370 |
void *x, const EVP_CIPHER *enc, |
| 371 |
const unsigned char *pass, int pass_len, |
| 372 |
pem_password_cb *callback, void *u); |
| 373 |
|
| 374 |
// PEM_def_callback provides a password for PEM encryption/decryption operations. |
| 375 |
// This function is used as the default callback to provide a password for PEM |
| 376 |
// functions such as |PEM_do_header| and |PEM_ASN1_write_bio|. |
| 377 |
// If |userdata| is non-NULL, it treats |userdata| as a string and copies it |
| 378 |
// into |buf|, assuming |size| is sufficient. If |userdata| is NULL, it prompts |
| 379 |
// the user for a password using the prompt from EVP_get_pw_prompt() (or default |
| 380 |
// "Enter PEM pass phrase:"). For encryption (|rwflag|=1), a minimum password |
| 381 |
// length is enforced, while for decryption (|rwflag|=0) any password length is |
| 382 |
// accepted. Returns the length of the password (excluding null |
| 383 |
// terminator) on success, or 0 on error or if |buf| is null, if |buf| is too small, |
| 384 |
// or |size| is negative, or |size| is smaller than user input length. |
| 385 |
OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag, |
| 386 |
void *userdata); |
| 387 |
|
| 388 |
|
| 389 |
DECLARE_PEM_rw(X509, X509) |
| 390 |
|
| 391 |
// TODO(crbug.com/boringssl/426): When documenting these, copy the warning |
| 392 |
// about auxiliary properties from |PEM_X509_INFO_read_bio|. |
| 393 |
DECLARE_PEM_rw(X509_AUX, X509) |
| 394 |
|
| 395 |
DECLARE_PEM_rw(X509_REQ, X509_REQ) |
| 396 |
DECLARE_PEM_write(X509_REQ_NEW, X509_REQ) |
| 397 |
|
| 398 |
DECLARE_PEM_rw(X509_CRL, X509_CRL) |
| 399 |
|
| 400 |
DECLARE_PEM_rw(PKCS7, PKCS7) |
| 401 |
DECLARE_PEM_rw(PKCS8, X509_SIG) |
| 402 |
|
| 403 |
DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) |
| 404 |
|
| 405 |
DECLARE_PEM_rw_cb(RSAPrivateKey, RSA) |
| 406 |
|
| 407 |
DECLARE_PEM_rw_const(RSAPublicKey, RSA) |
| 408 |
DECLARE_PEM_rw(RSA_PUBKEY, RSA) |
| 409 |
|
| 410 |
#ifndef OPENSSL_NO_DSA |
| 411 |
|
| 412 |
DECLARE_PEM_rw_cb(DSAPrivateKey, DSA) |
| 413 |
|
| 414 |
DECLARE_PEM_rw(DSA_PUBKEY, DSA) |
| 415 |
|
| 416 |
DECLARE_PEM_rw_const(DSAparams, DSA) |
| 417 |
|
| 418 |
#endif |
| 419 |
|
| 420 |
DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY) |
| 421 |
DECLARE_PEM_rw(EC_PUBKEY, EC_KEY) |
| 422 |
|
| 423 |
|
| 424 |
DECLARE_PEM_rw_const(DHparams, DH) |
| 425 |
|
| 426 |
|
| 427 |
DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY) |
| 428 |
|
| 429 |
DECLARE_PEM_rw(PUBKEY, EVP_PKEY) |
| 430 |
|
| 431 |
OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, |
| 432 |
int nid, const char *pass, |
| 433 |
int pass_len, |
| 434 |
pem_password_cb *cb, |
| 435 |
void *u); |
| 436 |
OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, |
| 437 |
const EVP_CIPHER *enc, |
| 438 |
const char *pass, int pass_len, |
| 439 |
pem_password_cb *cb, void *u); |
| 440 |
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, |
| 441 |
const EVP_CIPHER *enc, |
| 442 |
const char *pass, int pass_len, |
| 443 |
pem_password_cb *cb, void *u); |
| 444 |
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, |
| 445 |
int nid, const char *pass, |
| 446 |
int pass_len, |
| 447 |
pem_password_cb *cb, void *u); |
| 448 |
OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, |
| 449 |
pem_password_cb *cb, void *u); |
| 450 |
|
| 451 |
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, |
| 452 |
const EVP_CIPHER *enc, |
| 453 |
const char *pass, int pass_len, |
| 454 |
pem_password_cb *cb, void *u); |
| 455 |
OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, |
| 456 |
int nid, const char *pass, |
| 457 |
int pass_len, pem_password_cb *cb, |
| 458 |
void *u); |
| 459 |
OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, |
| 460 |
int nid, const char *pass, |
| 461 |
int pass_len, |
| 462 |
pem_password_cb *cb, void *u); |
| 463 |
|
| 464 |
OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, |
| 465 |
pem_password_cb *cb, void *u); |
| 466 |
|
| 467 |
OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, |
| 468 |
const EVP_CIPHER *enc, |
| 469 |
const char *pass, int pass_len, |
| 470 |
pem_password_cb *cd, void *u); |
| 471 |
|
| 472 |
// PEM_read_bio_Parameters is a generic PEM deserialization function that |
| 473 |
// parses the public "parameters" in |bio| and returns a corresponding |
| 474 |
// |EVP_PKEY|. If |*pkey| is non-null, the original |*pkey| is freed and the |
| 475 |
// returned |EVP_PKEY| is also written to |*pkey|. |*pkey| must be either NULL |
| 476 |
// or an allocated value, passing in an uninitialized pointer is undefined |
| 477 |
// behavior. This is only supported with |EVP_PKEY_EC|, |EVP_PKEY_DH|, and |
| 478 |
// |EVP_PKEY_DSA|. |
| 479 |
OPENSSL_EXPORT EVP_PKEY *PEM_read_bio_Parameters(BIO *bio, EVP_PKEY **pkey); |
| 480 |
|
| 481 |
// PEM_write_bio_Parameters is a generic PEM serialization function that parses |
| 482 |
// the public "parameters" of |pkey| to |bio|. It returns 1 on success or 0 on |
| 483 |
// failure. This is only supported with |EVP_PKEY_EC|, |EVP_PKEY_DH|, and |
| 484 |
// |EVP_PKEY_DSA|. |
| 485 |
OPENSSL_EXPORT int PEM_write_bio_Parameters(BIO *bio, EVP_PKEY *pkey); |
| 486 |
|
| 487 |
// PEM_read_bio_ECPKParameters deserializes the PEM file written in |bio| |
| 488 |
// according to |ECPKParameters| in RFC 3279. It returns the |EC_GROUP| |
| 489 |
// corresponding to deserialized output and also writes it to |out_group|. Only |
| 490 |
// deserialization of namedCurves or explicitly-encoded versions of namedCurves |
| 491 |
// are supported. |
| 492 |
OPENSSL_EXPORT EC_GROUP *PEM_read_bio_ECPKParameters(BIO *bio, |
| 493 |
EC_GROUP **out_group, |
| 494 |
pem_password_cb *cb, |
| 495 |
void *u); |
| 496 |
|
| 497 |
// PEM_write_bio_ECPKParameters serializes |group| as a PEM file to |out| |
| 498 |
// according to |ECPKParameters| in RFC 3279. Only serialization of namedCurves |
| 499 |
// are supported. |
| 500 |
OPENSSL_EXPORT int PEM_write_bio_ECPKParameters(BIO *out, |
| 501 |
const EC_GROUP *group); |
| 502 |
|
| 503 |
// PEM_write_bio_PrivateKey_traditional calls |PEM_ASN1_write_bio| to write |
| 504 |
// out |x|'s private key in the "traditional" ASN1 format. Use |
| 505 |
// |PEM_write_bio_PrivateKey| instead. |
| 506 |
OPENSSL_EXPORT int PEM_write_bio_PrivateKey_traditional( |
| 507 |
BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, |
| 508 |
pem_password_cb *cb, void *u); |
| 509 |
|
| 510 |
#ifdef __cplusplus |
| 511 |
} // extern "C" |
| 512 |
#endif |
| 513 |
|
| 514 |
#define PEM_R_BAD_BASE64_DECODE 100 |
| 515 |
#define PEM_R_BAD_DECRYPT 101 |
| 516 |
#define PEM_R_BAD_END_LINE 102 |
| 517 |
#define PEM_R_BAD_IV_CHARS 103 |
| 518 |
#define PEM_R_BAD_PASSWORD_READ 104 |
| 519 |
#define PEM_R_CIPHER_IS_NULL 105 |
| 520 |
#define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 106 |
| 521 |
#define PEM_R_NOT_DEK_INFO 107 |
| 522 |
#define PEM_R_NOT_ENCRYPTED 108 |
| 523 |
#define PEM_R_NOT_PROC_TYPE 109 |
| 524 |
#define PEM_R_NO_START_LINE 110 |
| 525 |
#define PEM_R_READ_KEY 111 |
| 526 |
#define PEM_R_SHORT_HEADER 112 |
| 527 |
#define PEM_R_UNSUPPORTED_CIPHER 113 |
| 528 |
#define PEM_R_UNSUPPORTED_ENCRYPTION 114 |
| 529 |
#define PEM_R_PROBLEMS_GETTING_PASSWORD 115 |
| 530 |
|
| 531 |
#endif // OPENSSL_HEADER_PEM_H |