From 56fec770071713bf800e7e9f3905973703105ec5 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 21 May 2013 17:33:22 +0200 Subject: pem: Write PEM data directly to a buffer --- tools/extract-openssl.c | 29 +++++++++++++++++------------ tools/extract-pem.c | 32 ++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 24 deletions(-) (limited to 'tools') diff --git a/tools/extract-openssl.c b/tools/extract-openssl.c index 2b8005a..91a9965 100644 --- a/tools/extract-openssl.c +++ b/tools/extract-openssl.c @@ -313,33 +313,34 @@ p11_extract_openssl_bundle (P11KitIter *iter, p11_extract_info *ex) { p11_save_file *file; + p11_buffer output; p11_buffer buf; char *comment; bool ret = true; - size_t length; bool first; CK_RV rv; - char *pem; file = p11_save_open_file (ex->destination, ex->flags); if (!file) return false; first = true; + p11_buffer_init (&output, 0); while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { p11_buffer_init (&buf, 1024); + if (!p11_buffer_reset (&output, 2048)) + return_val_if_reached (false); if (prepare_pem_contents (ex, &buf)) { - pem = p11_pem_write (buf.data, buf.len, "TRUSTED CERTIFICATE", &length); - return_val_if_fail (pem != NULL, false); + if (!p11_pem_write (buf.data, buf.len, "TRUSTED CERTIFICATE", &output)) + return_val_if_reached (false); comment = p11_extract_info_comment (ex, first); first = false; ret = p11_save_write (file, comment, -1) && - p11_save_write (file, pem, length); + p11_save_write (file, output.data, output.len); - free (pem); free (comment); } @@ -349,6 +350,8 @@ p11_extract_openssl_bundle (P11KitIter *iter, break; } + p11_buffer_uninit (&output); + if (rv != CKR_OK && rv != CKR_CANCEL) { p11_message ("failed to find certificates: %s", p11_kit_strerror (rv)); ret = false; @@ -584,11 +587,10 @@ p11_extract_openssl_directory (P11KitIter *iter, const char *filename; p11_save_file *file; p11_save_dir *dir; + p11_buffer output; p11_buffer buf; bool ret = true; char *name; - size_t length; - char *pem; CK_RV rv; #ifdef OS_UNIX @@ -600,14 +602,17 @@ p11_extract_openssl_directory (P11KitIter *iter, return false; p11_buffer_init (&buf, 0); + p11_buffer_init (&output, 0); while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { if (!p11_buffer_reset (&buf, 1024)) return_val_if_reached (false); + if (!p11_buffer_reset (&output, 2048)) + return_val_if_reached (false); if (prepare_pem_contents (ex, &buf)) { - pem = p11_pem_write (buf.data, buf.len, "TRUSTED CERTIFICATE", &length); - return_val_if_fail (pem != NULL, false); + if (!p11_pem_write (buf.data, buf.len, "TRUSTED CERTIFICATE", &output)) + return_val_if_reached (false); name = p11_extract_info_filename (ex); return_val_if_fail (name != NULL, false); @@ -645,12 +650,11 @@ p11_extract_openssl_directory (P11KitIter *iter, #endif /* OS_UNIX */ if (ret) - ret = p11_save_write_and_finish (file, pem, length); + ret = p11_save_write_and_finish (file, output.data, output.len); else p11_save_finish_file (file, false); free (name); - free (pem); } if (!ret) @@ -658,6 +662,7 @@ p11_extract_openssl_directory (P11KitIter *iter, } p11_buffer_uninit (&buf); + p11_buffer_uninit (&output); if (rv != CKR_OK && rv != CKR_CANCEL) { p11_message ("failed to find certificates: %s", p11_kit_strerror (rv)); diff --git a/tools/extract-pem.c b/tools/extract-pem.c index a1a0865..0bae3cb 100644 --- a/tools/extract-pem.c +++ b/tools/extract-pem.c @@ -50,34 +50,38 @@ p11_extract_pem_bundle (P11KitIter *iter, p11_extract_info *ex) { char *comment; + p11_buffer buf; p11_save_file *file; bool ret = true; bool first = true; - size_t length; CK_RV rv; - char *pem; file = p11_save_open_file (ex->destination, ex->flags); if (!file) return false; + p11_buffer_init (&buf, 0); while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - pem = p11_pem_write (ex->cert_der, ex->cert_len, "CERTIFICATE", &length); - return_val_if_fail (pem != NULL, false); + if (!p11_buffer_reset (&buf, 2048)) + return_val_if_reached (false); + + if (!p11_pem_write (ex->cert_der, ex->cert_len, "CERTIFICATE", &buf)) + return_val_if_reached (false); comment = p11_extract_info_comment (ex, first); first = false; ret = p11_save_write (file, comment, -1) && - p11_save_write (file, pem, length); + p11_save_write (file, buf.data, buf.len); free (comment); - free (pem); if (!ret) break; } + p11_buffer_uninit (&buf); + if (rv != CKR_OK && rv != CKR_CANCEL) { p11_message ("failed to find certificates: %s", p11_kit_strerror (rv)); ret = false; @@ -98,19 +102,22 @@ p11_extract_pem_directory (P11KitIter *iter, { p11_save_file *file; p11_save_dir *dir; + p11_buffer buf; bool ret = true; char *filename; - size_t length; - char *pem; CK_RV rv; dir = p11_save_open_directory (ex->destination, ex->flags); if (dir == NULL) return false; + p11_buffer_init (&buf, 0); while ((rv = p11_kit_iter_next (iter)) == CKR_OK) { - pem = p11_pem_write (ex->cert_der, ex->cert_len, "CERTIFICATE", &length); - return_val_if_fail (pem != NULL, false); + if (!p11_buffer_reset (&buf, 2048)) + return_val_if_reached (false); + + if (!p11_pem_write (ex->cert_der, ex->cert_len, "CERTIFICATE", &buf)) + return_val_if_reached (false); filename = p11_extract_info_filename (ex); return_val_if_fail (filename != NULL, false); @@ -118,13 +125,14 @@ p11_extract_pem_directory (P11KitIter *iter, file = p11_save_open_file_in (dir, filename, ".pem", NULL); free (filename); - ret = p11_save_write_and_finish (file, pem, length); - free (pem); + ret = p11_save_write_and_finish (file, buf.data, buf.len); if (!ret) break; } + p11_buffer_uninit (&buf); + if (rv != CKR_OK && rv != CKR_CANCEL) { p11_message ("failed to find certificates: %s", p11_kit_strerror (rv)); ret = false; -- cgit v1.1