2019-11-18 08:44:23 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2019-11-18 08:44:23 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2020-09-14 16:42:05 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2019-11-18 08:44:23 +08:00
|
|
|
#include <openssl/pem.h>
|
2020-08-17 03:25:08 +08:00
|
|
|
#include <openssl/encoder.h>
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2020-09-14 16:42:05 +08:00
|
|
|
/*
|
|
|
|
* Selectors, named according to the ASN.1 names used throughout libcrypto.
|
|
|
|
*
|
|
|
|
* Note that these are not absolutely mandatory, they are rather a wishlist
|
|
|
|
* of sorts. The provider implementations are free to make choices that
|
|
|
|
* make sense for them, based on these selectors.
|
|
|
|
* For example, the EC backend is likely to really just output the private
|
|
|
|
* key to a PKCS#8 structure, even thought PEM_SELECTION_PrivateKey specifies
|
|
|
|
* the public key as well. This is fine, as long as the corresponding
|
|
|
|
* decoding operation can return an object that contains what libcrypto
|
|
|
|
* expects.
|
|
|
|
*/
|
2020-10-09 19:02:58 +08:00
|
|
|
# define PEM_SELECTION_PUBKEY EVP_PKEY_PUBLIC_KEY
|
|
|
|
# define PEM_SELECTION_PrivateKey EVP_PKEY_KEYPAIR
|
|
|
|
# define PEM_SELECTION_Parameters EVP_PKEY_KEY_PARAMETERS
|
2020-09-14 16:42:05 +08:00
|
|
|
|
2020-10-17 14:55:39 +08:00
|
|
|
/*
|
|
|
|
* Properties, named according to the ASN.1 names used throughout libcrypto.
|
|
|
|
*/
|
|
|
|
# define PEM_STRUCTURE_PUBKEY "SubjectPublicKeyInfo"
|
|
|
|
# define PEM_STRUCTURE_PrivateKey "pkcs8"
|
|
|
|
# define PEM_STRUCTURE_Parameters "type-specific"
|
|
|
|
|
2020-10-04 22:34:31 +08:00
|
|
|
# define PEM_STRUCTURE_RSAPrivateKey "type-specific"
|
|
|
|
# define PEM_STRUCTURE_RSAPublicKey "type-specific"
|
|
|
|
|
2020-08-17 03:25:08 +08:00
|
|
|
/* Alternative IMPLEMENT macros for provided encoders */
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2020-09-14 17:30:14 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_body_vars(type, asn1, pq) \
|
2019-11-18 08:44:23 +08:00
|
|
|
int ret = 0; \
|
2020-09-14 16:42:05 +08:00
|
|
|
OSSL_ENCODER_CTX *ctx = \
|
2021-02-11 23:57:37 +08:00
|
|
|
OSSL_ENCODER_CTX_new_for_##type(x, PEM_SELECTION_##asn1, \
|
2020-10-17 14:55:39 +08:00
|
|
|
"PEM", PEM_STRUCTURE_##asn1, \
|
2020-09-14 17:30:14 +08:00
|
|
|
(pq)); \
|
2019-11-18 08:44:23 +08:00
|
|
|
\
|
2020-09-14 16:42:05 +08:00
|
|
|
if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { \
|
2020-08-17 03:25:08 +08:00
|
|
|
OSSL_ENCODER_CTX_free(ctx); \
|
2019-11-18 08:44:23 +08:00
|
|
|
goto legacy; \
|
|
|
|
}
|
|
|
|
# define IMPLEMENT_PEM_provided_write_body_pass() \
|
|
|
|
ret = 1; \
|
|
|
|
if (kstr == NULL && cb == NULL) { \
|
|
|
|
if (u != NULL) { \
|
|
|
|
kstr = u; \
|
|
|
|
klen = strlen(u); \
|
|
|
|
} else { \
|
|
|
|
cb = PEM_def_callback; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (enc != NULL) { \
|
|
|
|
ret = 0; \
|
2020-08-17 03:25:08 +08:00
|
|
|
if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \
|
|
|
|
NULL)) { \
|
2019-11-18 08:44:23 +08:00
|
|
|
ret = 1; \
|
|
|
|
if (kstr != NULL \
|
2020-08-17 03:25:08 +08:00
|
|
|
&& !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen)) \
|
2019-11-18 08:44:23 +08:00
|
|
|
ret = 0; \
|
|
|
|
else if (cb != NULL \
|
2020-09-14 16:42:05 +08:00
|
|
|
&& !OSSL_ENCODER_CTX_set_pem_password_cb(ctx, \
|
|
|
|
cb, u)) \
|
2019-11-18 08:44:23 +08:00
|
|
|
ret = 0; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (!ret) { \
|
2020-08-17 03:25:08 +08:00
|
|
|
OSSL_ENCODER_CTX_free(ctx); \
|
2019-11-18 08:44:23 +08:00
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
# define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \
|
2020-08-17 03:25:08 +08:00
|
|
|
ret = OSSL_ENCODER_to_##outtype(ctx, out); \
|
|
|
|
OSSL_ENCODER_CTX_free(ctx); \
|
2019-11-18 08:44:23 +08:00
|
|
|
return ret
|
|
|
|
# define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
|
|
|
|
writename) \
|
|
|
|
legacy: \
|
|
|
|
return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
|
2020-08-17 03:25:08 +08:00
|
|
|
x, NULL, NULL, 0, NULL, NULL)
|
2019-11-18 08:44:23 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
|
|
|
|
writename) \
|
|
|
|
legacy: \
|
2020-09-14 17:30:14 +08:00
|
|
|
return PEM_ASN1_##writename##((i2d_of_void *)i2d_##asn1, str, out, \
|
|
|
|
x, enc, kstr, klen, cb, u)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_to(name, TYPE, type, str, asn1, \
|
2019-11-18 08:44:23 +08:00
|
|
|
OUTTYPE, outtype, writename) \
|
2021-02-11 23:57:37 +08:00
|
|
|
PEM_write_fnsig(name, TYPE, OUTTYPE, writename) \
|
2019-11-18 08:44:23 +08:00
|
|
|
{ \
|
2020-09-14 17:30:14 +08:00
|
|
|
IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
|
|
|
|
writename); \
|
|
|
|
} \
|
2021-02-11 23:57:37 +08:00
|
|
|
PEM_write_ex_fnsig(name, TYPE, OUTTYPE, writename) \
|
2020-09-14 17:30:14 +08:00
|
|
|
{ \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
|
2019-11-18 08:44:23 +08:00
|
|
|
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
|
|
|
|
writename); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_cb_to(name, TYPE, type, str, asn1, \
|
2019-11-18 08:44:23 +08:00
|
|
|
OUTTYPE, outtype, writename) \
|
2021-02-11 23:57:37 +08:00
|
|
|
PEM_write_cb_fnsig(name, TYPE, OUTTYPE, writename) \
|
2019-11-18 08:44:23 +08:00
|
|
|
{ \
|
2020-09-14 17:30:14 +08:00
|
|
|
IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
|
2019-11-18 08:44:23 +08:00
|
|
|
IMPLEMENT_PEM_provided_write_body_pass(); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
|
|
|
|
writename); \
|
2020-09-14 17:30:14 +08:00
|
|
|
} \
|
2021-02-11 23:57:37 +08:00
|
|
|
PEM_write_ex_cb_fnsig(name, TYPE, OUTTYPE, writename) \
|
2020-09-14 17:30:14 +08:00
|
|
|
{ \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_pass(); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
|
|
|
|
IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
|
|
|
|
writename); \
|
2019-11-18 08:44:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef OPENSSL_NO_STDIO
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_fp(name, TYPE, type, str, asn1)
|
|
|
|
# define IMPLEMENT_PEM_provided_write_cb_fp(name, TYPE, type, str, asn1)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
|
|
|
# else
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_fp(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_to(name, TYPE, type, str, asn1, FILE, fp, write)
|
|
|
|
# define IMPLEMENT_PEM_provided_write_cb_fp(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_cb_to(name, TYPE, type, str, asn1, FILE, fp, write)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
|
|
|
# endif
|
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_bio(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_to(name, TYPE, type, str, asn1, BIO, bio, write_bio)
|
|
|
|
# define IMPLEMENT_PEM_provided_write_cb_bio(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_cb_to(name, TYPE, type, str, asn1, BIO, bio, write_bio)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_bio(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_fp(name, TYPE, type, str, asn1)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_write_cb(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_cb_bio(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_cb_fp(name, TYPE, type, str, asn1)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_rw(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_read(name, TYPE, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write(name, TYPE, type, str, asn1)
|
2019-11-18 08:44:23 +08:00
|
|
|
|
2021-02-11 23:57:37 +08:00
|
|
|
# define IMPLEMENT_PEM_provided_rw_cb(name, TYPE, type, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_read(name, TYPE, str, asn1) \
|
|
|
|
IMPLEMENT_PEM_provided_write_cb(name, TYPE, type, str, asn1)
|
2019-11-18 08:44:23 +08:00
|
|
|
|