]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add OSSL_CMP_MSG_write(), use it in apps/cmp.c
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Sat, 11 Jul 2020 10:26:22 +0000 (12:26 +0200)
committerDr. David von Oheimb <David.von.Oheimb@siemens.com>
Thu, 30 Jul 2020 18:14:51 +0000 (20:14 +0200)
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12421)

apps/cmp.c
crypto/cmp/cmp_msg.c
doc/man3/OSSL_CMP_MSG_get0_header.pod
include/openssl/cmp.h
util/libcrypto.num

index 20e7f6ac843d7fe0501862213e41f90162e28710..e5f72cbea78acd3400abe2cc0f08d900888eb22e 100644 (file)
@@ -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;
 }
 
index 2e24f49f3c12b50ee047f49586fa258fd27e8d6e..6d6e3bd2b66cb7e18e17ed5e648fe909bc10880f 100644 (file)
@@ -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,
index 0670fa31dc5bbf754b95e7861262c500653059e7..8503b74b7c0fb0d51ef5588cb2fea91abfd636c9 100644 (file)
@@ -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
 
index 9dc8c95ad02de1e994b4a3cca55def1f71f9267d..519117d622fb8f3801a41c32420a00cad3f778db 100644 (file)
@@ -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);
 
index 16682533663af2ce950a64a2b8487ab6f6cdcaf8..1a59d816249fd79b44c7a99373ceaaf52a80abb7 100644 (file)
@@ -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