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)
This commit is contained in:
Dr. David von Oheimb 2020-07-11 12:26:22 +02:00
parent fafa56a14f
commit 1202de4481
5 changed files with 29 additions and 11 deletions

View 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;
}

View 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,

View 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

View 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);

View 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