Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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> |
David Benjamin | 3815720 | 2018-06-15 17:31:19 -0400 | [diff] [blame] | 65 | #include <openssl/pkcs7.h> |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 66 | #include <openssl/stack.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 67 | #include <openssl/x509.h> |
| 68 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 69 | // For compatibility with open-iscsi, which assumes that it can get |
| 70 | // |OPENSSL_malloc| from pem.h or err.h |
Adam Langley | 7cb920b | 2016-05-20 08:28:56 -0700 | [diff] [blame] | 71 | #include <openssl/crypto.h> |
| 72 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 73 | #ifdef __cplusplus |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 74 | extern "C" { |
| 75 | #endif |
| 76 | |
| 77 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 78 | #define PEM_BUFSIZE 1024 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 79 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 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" |
David Benjamin | 8ebc0f5 | 2016-01-03 03:02:50 -0800 | [diff] [blame] | 93 | #define PEM_STRING_EC "EC PRIVATE KEY" |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 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" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 101 | #define PEM_STRING_ECDSA_PUBLIC "ECDSA PUBLIC KEY" |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 102 | #define PEM_STRING_ECPRIVATEKEY "EC PRIVATE KEY" |
| 103 | #define PEM_STRING_CMS "CMS" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 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 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 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(...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | |
| 115 | #ifdef OPENSSL_NO_FP_API |
| 116 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 117 | #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) // |
| 118 | #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) // |
| 119 | #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) // |
| 120 | #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) // |
| 121 | #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) // |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | |
| 123 | #else |
| 124 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 125 | #define IMPLEMENT_PEM_read_fp(name, type, str, asn1) \ |
| 126 | static void *pem_read_##name##_d2i(void **x, const unsigned char **inp, \ |
| 127 | long len) { \ |
| 128 | return d2i_##asn1((type **)x, inp, len); \ |
| 129 | } \ |
| 130 | OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ |
| 131 | pem_password_cb *cb, void *u) { \ |
| 132 | return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \ |
| 133 | cb, u); \ |
| 134 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 135 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 136 | #define IMPLEMENT_PEM_write_fp(name, type, str, asn1) \ |
| 137 | static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 138 | return i2d_##asn1((type *)x, outp); \ |
| 139 | } \ |
| 140 | OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) { \ |
| 141 | return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \ |
| 142 | NULL, NULL); \ |
| 143 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 145 | #define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) \ |
| 146 | static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 147 | return i2d_##asn1((const type *)x, outp); \ |
| 148 | } \ |
| 149 | OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) { \ |
| 150 | return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \ |
| 151 | NULL, 0, NULL, NULL); \ |
| 152 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 154 | #define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) \ |
| 155 | static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 156 | return i2d_##asn1((type *)x, outp); \ |
| 157 | } \ |
| 158 | OPENSSL_EXPORT int PEM_write_##name( \ |
| 159 | FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \ |
| 160 | pem_password_cb *cb, void *u) { \ |
| 161 | return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \ |
| 162 | cb, u); \ |
| 163 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 165 | #define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) \ |
| 166 | static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 167 | return i2d_##asn1((const type *)x, outp); \ |
| 168 | } \ |
| 169 | OPENSSL_EXPORT int PEM_write_##name( \ |
| 170 | FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \ |
| 171 | pem_password_cb *cb, void *u) { \ |
| 172 | return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, kstr, klen, \ |
| 173 | cb, u); \ |
| 174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
| 176 | #endif |
| 177 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 178 | #define IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ |
| 179 | static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \ |
| 180 | long len) { \ |
| 181 | return d2i_##asn1((type **)x, inp, len); \ |
| 182 | } \ |
| 183 | OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \ |
| 184 | pem_password_cb *cb, void *u) { \ |
| 185 | return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp, \ |
| 186 | (void **)x, cb, u); \ |
| 187 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 189 | #define IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ |
| 190 | static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 191 | return i2d_##asn1((type *)x, outp); \ |
| 192 | } \ |
| 193 | OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) { \ |
| 194 | return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL, \ |
| 195 | NULL, 0, NULL, NULL); \ |
| 196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 198 | #define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ |
| 199 | static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 200 | return i2d_##asn1((const type *)x, outp); \ |
| 201 | } \ |
| 202 | OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) { \ |
| 203 | return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \ |
| 204 | NULL, NULL, 0, NULL, NULL); \ |
| 205 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 207 | #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ |
| 208 | static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 209 | return i2d_##asn1((type *)x, outp); \ |
| 210 | } \ |
| 211 | OPENSSL_EXPORT int PEM_write_bio_##name( \ |
| 212 | BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \ |
| 213 | pem_password_cb *cb, void *u) { \ |
| 214 | return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc, \ |
| 215 | kstr, klen, cb, u); \ |
| 216 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
David Benjamin | 217bfd3 | 2018-09-22 16:49:56 -0500 | [diff] [blame] | 218 | #define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ |
| 219 | static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \ |
| 220 | return i2d_##asn1((const type *)x, outp); \ |
| 221 | } \ |
| 222 | OPENSSL_EXPORT int PEM_write_bio_##name( \ |
| 223 | BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \ |
| 224 | pem_password_cb *cb, void *u) { \ |
| 225 | return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x, \ |
| 226 | enc, kstr, klen, cb, u); \ |
| 227 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 228 | |
| 229 | #define IMPLEMENT_PEM_write(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 230 | IMPLEMENT_PEM_write_bio(name, type, str, asn1) \ |
| 231 | IMPLEMENT_PEM_write_fp(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
| 233 | #define IMPLEMENT_PEM_write_const(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 234 | IMPLEMENT_PEM_write_bio_const(name, type, str, asn1) \ |
| 235 | IMPLEMENT_PEM_write_fp_const(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
| 237 | #define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 238 | IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1) \ |
| 239 | IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
| 241 | #define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 242 | IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1) \ |
| 243 | IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
| 245 | #define IMPLEMENT_PEM_read(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 246 | IMPLEMENT_PEM_read_bio(name, type, str, asn1) \ |
| 247 | IMPLEMENT_PEM_read_fp(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
| 249 | #define IMPLEMENT_PEM_rw(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 250 | IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 251 | IMPLEMENT_PEM_write(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
| 253 | #define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 254 | IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 255 | IMPLEMENT_PEM_write_const(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
| 257 | #define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 258 | IMPLEMENT_PEM_read(name, type, str, asn1) \ |
| 259 | IMPLEMENT_PEM_write_cb(name, type, str, asn1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 261 | // These are the same except they are for the declarations |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
| 263 | #if defined(OPENSSL_NO_FP_API) |
| 264 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 265 | #define DECLARE_PEM_read_fp(name, type) // |
| 266 | #define DECLARE_PEM_write_fp(name, type) // |
| 267 | #define DECLARE_PEM_write_cb_fp(name, type) // |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | |
| 269 | #else |
| 270 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 271 | #define DECLARE_PEM_read_fp(name, type) \ |
| 272 | OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x, \ |
| 273 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 274 | |
| 275 | #define DECLARE_PEM_write_fp(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 276 | OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | |
| 278 | #define DECLARE_PEM_write_fp_const(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 279 | OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 281 | #define DECLARE_PEM_write_cb_fp(name, type) \ |
| 282 | OPENSSL_EXPORT int PEM_write_##name( \ |
| 283 | FILE *fp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \ |
| 284 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | |
| 286 | #endif |
| 287 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 288 | #define DECLARE_PEM_read_bio(name, type) \ |
| 289 | OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \ |
| 290 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | |
| 292 | #define DECLARE_PEM_write_bio(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 293 | OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
| 295 | #define DECLARE_PEM_write_bio_const(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 296 | OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 298 | #define DECLARE_PEM_write_cb_bio(name, type) \ |
| 299 | OPENSSL_EXPORT int PEM_write_bio_##name( \ |
| 300 | BIO *bp, type *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, \ |
| 301 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
| 304 | #define DECLARE_PEM_write(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 305 | DECLARE_PEM_write_bio(name, type) \ |
| 306 | DECLARE_PEM_write_fp(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
| 308 | #define DECLARE_PEM_write_const(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 309 | DECLARE_PEM_write_bio_const(name, type) \ |
| 310 | DECLARE_PEM_write_fp_const(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | |
| 312 | #define DECLARE_PEM_write_cb(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 313 | DECLARE_PEM_write_cb_bio(name, type) \ |
| 314 | DECLARE_PEM_write_cb_fp(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | |
| 316 | #define DECLARE_PEM_read(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 317 | DECLARE_PEM_read_bio(name, type) \ |
| 318 | DECLARE_PEM_read_fp(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | |
| 320 | #define DECLARE_PEM_rw(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 321 | DECLARE_PEM_read(name, type) \ |
| 322 | DECLARE_PEM_write(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | |
| 324 | #define DECLARE_PEM_rw_const(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 325 | DECLARE_PEM_read(name, type) \ |
| 326 | DECLARE_PEM_write_const(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | |
| 328 | #define DECLARE_PEM_rw_cb(name, type) \ |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 329 | DECLARE_PEM_read(name, type) \ |
| 330 | DECLARE_PEM_write_cb(name, type) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 332 | // "userdata": new with OpenSSL 0.9.4 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 335 | OPENSSL_EXPORT int PEM_get_EVP_CIPHER_INFO(char *header, |
| 336 | EVP_CIPHER_INFO *cipher); |
| 337 | OPENSSL_EXPORT int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, |
| 338 | long *len, pem_password_cb *callback, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | |
David Benjamin | 6d904d5 | 2020-10-09 16:27:23 -0400 | [diff] [blame] | 340 | // PEM_read_bio reads from |bp|, until the next PEM block. If one is found, it |
| 341 | // returns one and sets |*name|, |*header|, and |*data| to newly-allocated |
| 342 | // buffers containing the PEM type, the header block, and the decoded data, |
| 343 | // respectively. |*name| and |*header| are NUL-terminated C strings, while |
| 344 | // |*data| has |*len| bytes. The caller must release each of |*name|, |*header|, |
| 345 | // and |*data| with |OPENSSL_free| when done. If no PEM block is found, this |
| 346 | // function returns zero and pushes |PEM_R_NO_START_LINE| to the error queue. If |
| 347 | // one is found, but there is an error decoding it, it returns zero and pushes |
| 348 | // some other error to the error queue. |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 349 | OPENSSL_EXPORT int PEM_read_bio(BIO *bp, char **name, char **header, |
| 350 | unsigned char **data, long *len); |
David Benjamin | 6d904d5 | 2020-10-09 16:27:23 -0400 | [diff] [blame] | 351 | |
| 352 | // PEM_write_bio writes a PEM block to |bp|, containing |len| bytes from |data| |
| 353 | // as data. |name| and |hdr| are NUL-terminated C strings containing the PEM |
| 354 | // type and header block, respectively. This function returns zero on error and |
| 355 | // the number of bytes written on success. |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 356 | OPENSSL_EXPORT int PEM_write_bio(BIO *bp, const char *name, const char *hdr, |
| 357 | const unsigned char *data, long len); |
David Benjamin | 6d904d5 | 2020-10-09 16:27:23 -0400 | [diff] [blame] | 358 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 359 | OPENSSL_EXPORT int PEM_bytes_read_bio(unsigned char **pdata, long *plen, |
| 360 | char **pnm, const char *name, BIO *bp, |
| 361 | pem_password_cb *cb, void *u); |
| 362 | OPENSSL_EXPORT void *PEM_ASN1_read_bio(d2i_of_void *d2i, const char *name, |
| 363 | BIO *bp, void **x, pem_password_cb *cb, |
| 364 | void *u); |
| 365 | OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, |
| 366 | BIO *bp, void *x, const EVP_CIPHER *enc, |
| 367 | unsigned char *kstr, int klen, |
| 368 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 370 | OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio( |
| 371 | BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u); |
| 372 | OPENSSL_EXPORT int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, |
| 373 | EVP_CIPHER *enc, unsigned char *kstr, |
| 374 | int klen, pem_password_cb *cd, |
| 375 | void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 377 | OPENSSL_EXPORT int PEM_read(FILE *fp, char **name, char **header, |
| 378 | unsigned char **data, long *len); |
| 379 | OPENSSL_EXPORT int PEM_write(FILE *fp, const char *name, const char *hdr, |
| 380 | const unsigned char *data, long len); |
| 381 | OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, |
| 382 | void **x, pem_password_cb *cb, void *u); |
| 383 | OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, |
| 384 | void *x, const EVP_CIPHER *enc, |
| 385 | unsigned char *kstr, int klen, |
| 386 | pem_password_cb *callback, void *u); |
| 387 | OPENSSL_EXPORT STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, |
| 388 | STACK_OF(X509_INFO) *sk, |
| 389 | pem_password_cb *cb, |
| 390 | void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 392 | // PEM_def_callback treats |userdata| as a string and copies it into |buf|, |
| 393 | // assuming its |size| is sufficient. Returns the length of the string, or 0 |
| 394 | // if there is not enough room. If either |buf| or |userdata| is NULL, 0 is |
| 395 | // returned. Note that this is different from OpenSSL, which prompts for a |
| 396 | // password. |
| 397 | OPENSSL_EXPORT int PEM_def_callback(char *buf, int size, int rwflag, |
| 398 | void *userdata); |
| 399 | OPENSSL_EXPORT void PEM_proc_type(char *buf, int type); |
| 400 | OPENSSL_EXPORT void PEM_dek_info(char *buf, const char *type, int len, |
| 401 | char *str); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
| 403 | |
| 404 | DECLARE_PEM_rw(X509, X509) |
| 405 | |
| 406 | DECLARE_PEM_rw(X509_AUX, X509) |
| 407 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | DECLARE_PEM_rw(X509_REQ, X509_REQ) |
| 409 | DECLARE_PEM_write(X509_REQ_NEW, X509_REQ) |
| 410 | |
| 411 | DECLARE_PEM_rw(X509_CRL, X509_CRL) |
| 412 | |
David Benjamin | 3815720 | 2018-06-15 17:31:19 -0400 | [diff] [blame] | 413 | DECLARE_PEM_rw(PKCS7, PKCS7) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | DECLARE_PEM_rw(PKCS8, X509_SIG) |
| 415 | |
| 416 | DECLARE_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO) |
| 417 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | DECLARE_PEM_rw_cb(RSAPrivateKey, RSA) |
| 419 | |
| 420 | DECLARE_PEM_rw_const(RSAPublicKey, RSA) |
| 421 | DECLARE_PEM_rw(RSA_PUBKEY, RSA) |
| 422 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | #ifndef OPENSSL_NO_DSA |
| 424 | |
| 425 | DECLARE_PEM_rw_cb(DSAPrivateKey, DSA) |
| 426 | |
| 427 | DECLARE_PEM_rw(DSA_PUBKEY, DSA) |
| 428 | |
| 429 | DECLARE_PEM_rw_const(DSAparams, DSA) |
| 430 | |
| 431 | #endif |
| 432 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | DECLARE_PEM_rw_cb(ECPrivateKey, EC_KEY) |
| 434 | DECLARE_PEM_rw(EC_PUBKEY, EC_KEY) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
| 437 | DECLARE_PEM_rw_const(DHparams, DH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | |
| 440 | DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY) |
| 441 | |
| 442 | DECLARE_PEM_rw(PUBKEY, EVP_PKEY) |
| 443 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 444 | OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, |
| 445 | int nid, char *kstr, |
| 446 | int klen, |
| 447 | pem_password_cb *cb, |
| 448 | void *u); |
| 449 | OPENSSL_EXPORT int PEM_write_bio_PKCS8PrivateKey(BIO *, EVP_PKEY *, |
| 450 | const EVP_CIPHER *, char *, |
| 451 | int, pem_password_cb *, |
| 452 | void *); |
| 453 | OPENSSL_EXPORT int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, |
| 454 | const EVP_CIPHER *enc, char *kstr, |
| 455 | int klen, pem_password_cb *cb, |
| 456 | void *u); |
| 457 | OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid, |
| 458 | char *kstr, int klen, |
| 459 | pem_password_cb *cb, void *u); |
| 460 | OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, |
| 461 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 463 | OPENSSL_EXPORT int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, |
| 464 | const EVP_CIPHER *enc, char *kstr, |
| 465 | int klen, pem_password_cb *cb, |
| 466 | void *u); |
| 467 | OPENSSL_EXPORT int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid, |
| 468 | char *kstr, int klen, |
| 469 | pem_password_cb *cb, void *u); |
| 470 | OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid, |
| 471 | char *kstr, int klen, |
| 472 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 474 | OPENSSL_EXPORT EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, |
| 475 | pem_password_cb *cb, void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 477 | OPENSSL_EXPORT int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, |
| 478 | const EVP_CIPHER *enc, char *kstr, |
| 479 | int klen, pem_password_cb *cd, |
| 480 | void *u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 483 | #ifdef __cplusplus |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | } |
| 485 | #endif |
| 486 | |
David Benjamin | 689be0f | 2015-02-11 15:55:26 -0500 | [diff] [blame] | 487 | #define PEM_R_BAD_BASE64_DECODE 100 |
| 488 | #define PEM_R_BAD_DECRYPT 101 |
| 489 | #define PEM_R_BAD_END_LINE 102 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | #define PEM_R_BAD_IV_CHARS 103 |
David Benjamin | 689be0f | 2015-02-11 15:55:26 -0500 | [diff] [blame] | 491 | #define PEM_R_BAD_PASSWORD_READ 104 |
| 492 | #define PEM_R_CIPHER_IS_NULL 105 |
| 493 | #define PEM_R_ERROR_CONVERTING_PRIVATE_KEY 106 |
| 494 | #define PEM_R_NOT_DEK_INFO 107 |
| 495 | #define PEM_R_NOT_ENCRYPTED 108 |
| 496 | #define PEM_R_NOT_PROC_TYPE 109 |
| 497 | #define PEM_R_NO_START_LINE 110 |
| 498 | #define PEM_R_READ_KEY 111 |
| 499 | #define PEM_R_SHORT_HEADER 112 |
| 500 | #define PEM_R_UNSUPPORTED_CIPHER 113 |
| 501 | #define PEM_R_UNSUPPORTED_ENCRYPTION 114 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | |
David Benjamin | 5e549fb | 2020-10-09 16:18:33 -0400 | [diff] [blame] | 503 | #endif // OPENSSL_HEADER_PEM_H |