2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * TODO(v3.0): the IMPLEMENT macros in include/openssl/pem.h should be
15 #include <openssl/core_dispatch.h>
16 #include <openssl/pem.h>
17 #include <openssl/encoder.h>
20 * Selectors, named according to the ASN.1 names used throughout libcrypto.
22 * Note that these are not absolutely mandatory, they are rather a wishlist
23 * of sorts. The provider implementations are free to make choices that
24 * make sense for them, based on these selectors.
25 * For example, the EC backend is likely to really just output the private
26 * key to a PKCS#8 structure, even thought PEM_SELECTION_PrivateKey specifies
27 * the public key as well. This is fine, as long as the corresponding
28 * decoding operation can return an object that contains what libcrypto
31 # define PEM_SELECTION_PUBKEY EVP_PKEY_PUBLIC_KEY
32 # define PEM_SELECTION_PrivateKey EVP_PKEY_KEYPAIR
33 # define PEM_SELECTION_Parameters EVP_PKEY_KEY_PARAMETERS
36 * Properties, named according to the ASN.1 names used throughout libcrypto.
38 # define PEM_STRUCTURE_PUBKEY "SubjectPublicKeyInfo"
39 # define PEM_STRUCTURE_PrivateKey "pkcs8"
40 # define PEM_STRUCTURE_Parameters "type-specific"
42 # define PEM_STRUCTURE_RSAPrivateKey "type-specific"
43 # define PEM_STRUCTURE_RSAPublicKey "type-specific"
45 /* Alternative IMPLEMENT macros for provided encoders */
47 # define IMPLEMENT_PEM_provided_write_body_vars(type, asn1, pq) \
49 OSSL_ENCODER_CTX *ctx = \
50 OSSL_ENCODER_CTX_new_by_##type(x, PEM_SELECTION_##asn1, \
51 "PEM", PEM_STRUCTURE_##asn1, \
54 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { \
55 OSSL_ENCODER_CTX_free(ctx); \
58 # define IMPLEMENT_PEM_provided_write_body_pass() \
60 if (kstr == NULL && cb == NULL) { \
65 cb = PEM_def_callback; \
70 if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \
74 && !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen)) \
77 && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx, \
83 OSSL_ENCODER_CTX_free(ctx); \
86 # define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \
87 ret = OSSL_ENCODER_to_##outtype(ctx, out); \
88 OSSL_ENCODER_CTX_free(ctx); \
90 # define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
93 return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
94 x, NULL, NULL, 0, NULL, NULL)
95 # define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
98 return PEM_ASN1_##writename##((i2d_of_void *)i2d_##asn1, str, out, \
99 x, enc, kstr, klen, cb, u)
101 # define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \
102 OUTTYPE, outtype, writename) \
103 PEM_write_fnsig(name, type, OUTTYPE, writename) \
105 IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
106 IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
107 IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
110 PEM_write_ex_fnsig(name, type, OUTTYPE, writename) \
112 IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
113 IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
114 IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
119 # define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, \
120 OUTTYPE, outtype, writename) \
121 PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
123 IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
124 IMPLEMENT_PEM_provided_write_body_pass(); \
125 IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
126 IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
129 PEM_write_ex_cb_fnsig(name, type, OUTTYPE, writename) \
131 IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
132 IMPLEMENT_PEM_provided_write_body_pass(); \
133 IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
134 IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
138 # ifdef OPENSSL_NO_STDIO
140 # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
141 # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
145 # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) \
146 IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write)
147 # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) \
148 IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write)
152 # define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
153 IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio)
154 # define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
155 IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio)
157 # define IMPLEMENT_PEM_provided_write(name, type, str, asn1) \
158 IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
159 IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
161 # define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) \
162 IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
163 IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
165 # define IMPLEMENT_PEM_provided_rw(name, type, str, asn1) \
166 IMPLEMENT_PEM_read(name, type, str, asn1) \
167 IMPLEMENT_PEM_provided_write(name, type, str, asn1)
169 # define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1) \
170 IMPLEMENT_PEM_read(name, type, str, asn1) \
171 IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)