From 6d3309d99045be7c49dfaeaa3d6fe518e85bc449 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 31 Aug 2011 21:06:00 +0200 Subject: [PATCH] egg: Make OpenSSL PEM routines return outer block as well. * We expose this in GcrParser so that we can reparse a block that needs a password. --- egg/egg-openssl.c | 32 +++++++++++++++++++++++++++----- egg/egg-openssl.h | 9 +++++++-- egg/tests/test-openssl.c | 9 +++++++-- gcr/gcr-parser.c | 9 +++++++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/egg/egg-openssl.c b/egg/egg-openssl.c index c95c4ad..03b3e7f 100644 --- a/egg/egg-openssl.c +++ b/egg/egg-openssl.c @@ -92,7 +92,10 @@ parse_header_lines (const gchar *hbeg, const gchar *hend, GHashTable **result) } static const gchar* -pem_find_begin (const gchar *data, gsize n_data, GQuark *type) +pem_find_begin (const gchar *data, + gsize n_data, + GQuark *type, + const gchar **outer) { const gchar *pref, *suff; gchar *stype; @@ -114,6 +117,9 @@ pem_find_begin (const gchar *data, gsize n_data, GQuark *type) if (memchr (pref, '\n', suff - pref)) return NULL; + if (outer) + *outer = pref; + if (type) { *type = 0; pref += PEM_PREF_BEGIN_L; @@ -129,7 +135,10 @@ pem_find_begin (const gchar *data, gsize n_data, GQuark *type) } static const gchar* -pem_find_end (const gchar *data, gsize n_data, GQuark type) +pem_find_end (const gchar *data, + gsize n_data, + GQuark type, + const gchar **outer) { const gchar *stype; const gchar *pref; @@ -156,6 +165,13 @@ pem_find_end (const gchar *data, gsize n_data, GQuark type) if (strncmp ((gchar*)data, PEM_SUFF, PEM_SUFF_L) != 0) return NULL; + if (outer != NULL) { + data += PEM_SUFF_L; + if (isspace (data[0])) + data++; + *outer = data; + } + /* The beginning of this ---END--- */ return pref; } @@ -238,6 +254,7 @@ egg_openssl_pem_parse (gconstpointer data, gsize n_data, EggOpensslPemCallback callback, gpointer user_data) { const gchar *beg, *end; + const gchar *outer_beg, *outer_end; guint nfound = 0; guchar *decoded = NULL; gsize n_decoded = 0; @@ -251,20 +268,25 @@ egg_openssl_pem_parse (gconstpointer data, gsize n_data, while (n_data > 0) { /* This returns the first character after the PEM BEGIN header */ - beg = pem_find_begin ((const gchar*)data, n_data, &type); + beg = pem_find_begin ((const gchar*)data, n_data, &type, &outer_beg); if (!beg) break; g_assert (type); /* This returns the character position before the PEM END header */ - end = pem_find_end ((const gchar*)beg, n_data - ((const gchar*)beg - (const gchar *)data), type); + end = pem_find_end ((const gchar*)beg, n_data - ((const gchar*)beg - (const gchar *)data), + type, &outer_end); if (!end) break; if (beg != end) { if (pem_parse_block (beg, end - beg, &decoded, &n_decoded, &headers)) { - (callback) (type, decoded, n_decoded, headers, user_data); + g_assert (outer_end > outer_beg); + (callback) (type, + decoded, n_decoded, + outer_beg, outer_end - outer_beg, + headers, user_data); ++nfound; egg_secure_free (decoded); if (headers) diff --git a/egg/egg-openssl.h b/egg/egg-openssl.h index 1539c4d..5914fdb 100644 --- a/egg/egg-openssl.h +++ b/egg/egg-openssl.h @@ -26,8 +26,13 @@ #include -typedef void (*EggOpensslPemCallback) (GQuark type, const guchar *data, gsize n_data, - GHashTable *headers, gpointer user_data); +typedef void (*EggOpensslPemCallback) (GQuark type, + const guchar *data, + gsize n_data, + const gchar *outer, + gsize n_outer, + GHashTable *headers, + gpointer user_data); GHashTable* egg_openssl_headers_new (void); diff --git a/egg/tests/test-openssl.c b/egg/tests/test-openssl.c index e80c1b1..6721f74 100644 --- a/egg/tests/test-openssl.c +++ b/egg/tests/test-openssl.c @@ -71,8 +71,13 @@ copy_each_key_value (gpointer key, gpointer value, gpointer user_data) } static void -parse_reference (GQuark type, const guchar *data, gsize n_data, - GHashTable *headers, gpointer user_data) +parse_reference (GQuark type, + const guchar *data, + gsize n_data, + const gchar *outer, + gsize n_outer, + GHashTable *headers, + gpointer user_data) { Test *test = user_data; gboolean res; diff --git a/gcr/gcr-parser.c b/gcr/gcr-parser.c index cc2ae57..8361738 100644 --- a/gcr/gcr-parser.c +++ b/gcr/gcr-parser.c @@ -1288,8 +1288,13 @@ typedef struct { } HandlePemArgs; static void -handle_pem_data (GQuark type, const guchar *data, gsize n_data, - GHashTable *headers, gpointer user_data) +handle_pem_data (GQuark type, + const guchar *data, + gsize n_data, + const gchar *outer, + gsize n_outer, + GHashTable *headers, + gpointer user_data) { HandlePemArgs *args = (HandlePemArgs*)user_data; gint res = GCR_ERROR_FAILURE; -- 2.7.4