summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>2020-07-11 12:26:22 +0200
committerDr. David von Oheimb <David.von.Oheimb@siemens.com>2020-07-30 20:14:51 +0200
commit1202de4481df88d63a2a5cc1e9e0450a7e72f4ac (patch)
tree11e10160191b44e573f792531e811fbda5d8df6c
parentfafa56a14fc4787060818715c151e1ef7b25e72f (diff)
Add OSSL_CMP_MSG_write(), use it in apps/cmp.c
Reviewed-by: Paul Dale <paul.dale@oracle.com> (Merged from https://github.com/openssl/openssl/pull/12421)
-rw-r--r--apps/cmp.c10
-rw-r--r--crypto/cmp/cmp_msg.c18
-rw-r--r--doc/man3/OSSL_CMP_MSG_get0_header.pod10
-rw-r--r--include/openssl/cmp.h1
-rw-r--r--util/libcrypto.num1
5 files changed, 29 insertions, 11 deletions
diff --git a/apps/cmp.c b/apps/cmp.c
index 20e7f6ac84..e5f72cbea7 100644
--- a/apps/cmp.c
+++ b/apps/cmp.c
@@ -934,7 +934,6 @@ static X509_STORE *sk_X509_to_store(X509_STORE *store /* may be NULL */,
static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
{
char *file;
- BIO *bio;
if (msg == NULL || filenames == NULL) {
CMP_err("NULL arg to write_PKIMESSAGE");
@@ -947,17 +946,10 @@ static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
file = *filenames;
*filenames = next_item(file);
- bio = BIO_new_file(file, "wb");
- if (bio == NULL) {
- CMP_err1("Cannot open file '%s' for writing", file);
- return 0;
- }
- if (i2d_OSSL_CMP_MSG_bio(bio, msg) < 0) {
+ if (OSSL_CMP_MSG_write(file, msg) < 0) {
CMP_err1("Cannot write PKIMessage to file '%s'", file);
- BIO_free(bio);
return 0;
}
- BIO_free(bio);
return 1;
}
diff --git a/crypto/cmp/cmp_msg.c b/crypto/cmp/cmp_msg.c
index 2e24f49f3c..6d6e3bd2b6 100644
--- a/crypto/cmp/cmp_msg.c
+++ b/crypto/cmp/cmp_msg.c
@@ -1025,6 +1025,24 @@ OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file)
return msg;
}
+int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg)
+{
+ BIO *bio;
+ int res;
+
+ if (file == NULL || msg == NULL) {
+ CMPerr(0, CMP_R_NULL_ARGUMENT);
+ return -1;
+ }
+
+ bio = BIO_new_file(file, "wb");
+ if (bio == NULL)
+ return -2;
+ res = i2d_OSSL_CMP_MSG_bio(bio, msg);
+ BIO_free(bio);
+ return res;
+}
+
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg)
{
return ASN1_d2i_bio_of(OSSL_CMP_MSG, OSSL_CMP_MSG_new,
diff --git a/doc/man3/OSSL_CMP_MSG_get0_header.pod b/doc/man3/OSSL_CMP_MSG_get0_header.pod
index 0670fa31dc..8503b74b7c 100644
--- a/doc/man3/OSSL_CMP_MSG_get0_header.pod
+++ b/doc/man3/OSSL_CMP_MSG_get0_header.pod
@@ -6,6 +6,7 @@ OSSL_CMP_MSG_get0_header,
OSSL_CMP_MSG_update_transactionID,
OSSL_CMP_CTX_setup_CRM,
OSSL_CMP_MSG_read,
+OSSL_CMP_MSG_write,
d2i_OSSL_CMP_MSG_bio,
i2d_OSSL_CMP_MSG_bio
- function(s) manipulating CMP messages
@@ -18,6 +19,7 @@ i2d_OSSL_CMP_MSG_bio
int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg);
OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid);
OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file);
+ int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg);
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg);
int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg);
@@ -39,6 +41,8 @@ The I<rid> defines the request identifier to use, which typically is 0.
OSSL_CMP_MSG_read() loads a DER-encoded OSSL_CMP_MSG from B<file>.
+OSSL_CMP_MSG_write() stores the given OSSL_CMP_MSG to B<file> in DER encoding.
+
d2i_OSSL_CMP_MSG_bio() parses an ASN.1-encoded OSSL_CMP_MSG from the BIO I<bio>.
It assigns a pointer to the new structure to I<*msg> if I<msg> is not NULL.
@@ -62,8 +66,10 @@ d2i_OSSL_CMP_MSG_bio() returns the parsed message or NULL on error.
OSSL_CMP_MSG_read() and d2i_OSSL_CMP_MSG_bio()
return the parsed CMP message or NULL on error.
-i2d_OSSL_CMP_MSG_bio() and OSSL_CMP_MSG_update_transactionID()
-return 1 on success, 0 on error.
+OSSL_CMP_MSG_write() and i2d_OSSL_CMP_MSG_bio() return
+the number of bytes successfully encoded or a negative value if an error occurs.
+
+OSSL_CMP_MSG_update_transactionID() returns 1 on success, 0 on error.
=head1 HISTORY
diff --git a/include/openssl/cmp.h b/include/openssl/cmp.h
index 9dc8c95ad0..519117d622 100644
--- a/include/openssl/cmp.h
+++ b/include/openssl/cmp.h
@@ -356,6 +356,7 @@ OSSL_CMP_PKIHEADER *OSSL_CMP_MSG_get0_header(const OSSL_CMP_MSG *msg);
int OSSL_CMP_MSG_update_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg);
OSSL_CRMF_MSG *OSSL_CMP_CTX_setup_CRM(OSSL_CMP_CTX *ctx, int for_KUR, int rid);
OSSL_CMP_MSG *OSSL_CMP_MSG_read(const char *file);
+int OSSL_CMP_MSG_write(const char *file, const OSSL_CMP_MSG *msg);
OSSL_CMP_MSG *d2i_OSSL_CMP_MSG_bio(BIO *bio, OSSL_CMP_MSG **msg);
int i2d_OSSL_CMP_MSG_bio(BIO *bio, const OSSL_CMP_MSG *msg);
diff --git a/util/libcrypto.num b/util/libcrypto.num
index 1668253366..1a59d81624 100644
--- a/util/libcrypto.num
+++ b/util/libcrypto.num
@@ -4994,6 +4994,7 @@ OSSL_CMP_exec_RR_ses ? 3_0_0 EXIST::FUNCTION:CMP
OSSL_CMP_exec_GENM_ses ? 3_0_0 EXIST::FUNCTION:CMP
OSSL_CMP_MSG_http_perform ? 3_0_0 EXIST::FUNCTION:CMP
OSSL_CMP_MSG_read ? 3_0_0 EXIST::FUNCTION:CMP
+OSSL_CMP_MSG_write ? 3_0_0 EXIST::FUNCTION:CMP
EVP_PKEY_gen ? 3_0_0 EXIST::FUNCTION:
EVP_PKEY_CTX_set_rsa_keygen_bits ? 3_0_0 EXIST::FUNCTION:RSA
EVP_PKEY_CTX_set_rsa_keygen_pubexp ? 3_0_0 EXIST::FUNCTION:RSA