summaryrefslogtreecommitdiffstats
path: root/crypto/pem
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2020-09-14 11:30:14 +0200
committerRichard Levitte <levitte@openssl.org>2020-12-05 11:09:20 +0100
commit9256e8a2487697c347f8e54db69509dc662b26f7 (patch)
treea7756aa28aeaa909797fa1cce59aa6874b8dbddf /crypto/pem
parent030da84412c5e01c070a580ad237e713c2057626 (diff)
PEM: Add a more generic way to implement PEM _ex functions for libctx
This also adds the following functions, for completeness: PEM_write_PrivateKey_ex(), PEM_write_bio_PrivateKey_ex(), PEM_write_PUBKEY_ex, PEM_write_bio_PUBKEY_ex Fixes #13542 Reviewed-by: Shane Lontis <shane.lontis@oracle.com> (Merged from https://github.com/openssl/openssl/pull/13547)
Diffstat (limited to 'crypto/pem')
-rw-r--r--crypto/pem/pem_local.h27
-rw-r--r--crypto/pem/pem_pkey.c25
2 files changed, 38 insertions, 14 deletions
diff --git a/crypto/pem/pem_local.h b/crypto/pem/pem_local.h
index 728fefb33a..3f54644e89 100644
--- a/crypto/pem/pem_local.h
+++ b/crypto/pem/pem_local.h
@@ -44,12 +44,12 @@
/* Alternative IMPLEMENT macros for provided encoders */
-# define IMPLEMENT_PEM_provided_write_body_vars(type, asn1) \
+# define IMPLEMENT_PEM_provided_write_body_vars(type, asn1, pq) \
int ret = 0; \
OSSL_ENCODER_CTX *ctx = \
OSSL_ENCODER_CTX_new_by_##type(x, PEM_SELECTION_##asn1, \
"PEM", PEM_STRUCTURE_##asn1, \
- NULL); \
+ (pq)); \
\
if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { \
OSSL_ENCODER_CTX_free(ctx); \
@@ -95,14 +95,21 @@
# define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
writename) \
legacy: \
- return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
- x, enc, kstr, klen, cb, u)
+ return PEM_ASN1_##writename##((i2d_of_void *)i2d_##asn1, str, out, \
+ x, enc, kstr, klen, cb, u)
# define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \
OUTTYPE, outtype, writename) \
PEM_write_fnsig(name, type, OUTTYPE, writename) \
{ \
- IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
+ 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); \
+ } \
+ PEM_write_ex_fnsig(name, type, OUTTYPE, writename) \
+ { \
+ IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
writename); \
@@ -113,11 +120,19 @@
OUTTYPE, outtype, writename) \
PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
{ \
- IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
+ IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
IMPLEMENT_PEM_provided_write_body_pass(); \
IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
writename); \
+ } \
+ PEM_write_ex_cb_fnsig(name, type, OUTTYPE, writename) \
+ { \
+ 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); \
}
# ifdef OPENSSL_NO_STDIO
diff --git a/crypto/pem/pem_pkey.c b/crypto/pem/pem_pkey.c
index 717fb4ef4d..c71bc24bb2 100644
--- a/crypto/pem/pem_pkey.c
+++ b/crypto/pem/pem_pkey.c
@@ -151,9 +151,9 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
}
-PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
+PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
{
- IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey);
+ IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey, propq);
IMPLEMENT_PEM_provided_write_body_pass();
IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
@@ -165,6 +165,12 @@ PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
}
+PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
+{
+ return PEM_write_bio_PrivateKey_ex(out, x, enc, kstr, klen, cb, u,
+ NULL, NULL);
+}
+
/*
* Note: there is no way to tell a provided pkey encoder to use "traditional"
* encoding. Therefore, if the pkey is provided, we try to take a copy
@@ -212,7 +218,7 @@ EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
{
char pem_str[80];
- IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters);
+ IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters, NULL);
IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
@@ -249,20 +255,23 @@ EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
}
-int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
- const unsigned char *kstr, int klen,
- pem_password_cb *cb, void *u)
+PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, FILE, write)
{
BIO *b;
int ret;
- if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
+ if ((b = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
return 0;
}
- ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
+ ret = PEM_write_bio_PrivateKey_ex(b, x, enc, kstr, klen, cb, u,
+ libctx, propq);
BIO_free(b);
return ret;
}
+PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, FILE, write)
+{
+ return PEM_write_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, NULL, NULL);
+}
#endif