pem: remove index argument
PATCH v2 1/6 pem: remove index argument
This argument seems to always be hard coded to zero.
--- ell/pem.c | 41 ++++++++++++++++++----------------------- ell/pem.h | 5 ++--- 2 files changed, 20 insertions(+), 26 deletions(-)
ell/pem.c +18 -23
@@ -124,7 +124,7 @@ static bool is_end_boundary(const uint8_t *buf, size_t buf_len,return true; } -static uint8_t *pem_load_buffer (const uint8_t *buf,size_t buf_len,int index, +static uint8_t *pem_load_buffer (const uint8_t *buf,size_t buf_len,char **type_label,size_t *len,const uint8_t **endp) {
@@ -149,24 +149,19 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len, int index,if (label) base64_data = eol; }else if (is_end_boundary (buf, eol - buf, label, label_len)) { -if (index ==0 ) { - data =l_base64_decode ( - (const char *) base64_data, - buf - base64_data, len); -if (!data) -return NULL ; - - *type_label =l_strndup ((const char *) label, - label_len); + data =l_base64_decode ( + (const char *) base64_data, + buf - base64_data, len); +if (!data) +return NULL ; -if (endp) - *endp = eol +1 ; + *type_label =l_strndup ((const char *) label, + label_len); -return data; - } +if (endp) + *endp = eol +1 ; - base64_data =NULL ; - index--; +return data; }if (eol == buf + buf_len)
@@ -189,10 +184,10 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len, int index, } LIB_EXPORT uint8_t *l_pem_load_buffer(const uint8_t *buf, size_t buf_len, -int index,char **type_label, +char **type_label,size_t *out_len) { -return pem_load_buffer (buf, buf_len, index, type_label, out_len,NULL ); +return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ); }struct pem_file_info {
@@ -232,7 +227,7 @@ static void pem_file_close(struct pem_file_info *info)close (info->fd); } -LIB_EXPORT uint8_t *l_pem_load_file(const char *filename, int index, +LIB_EXPORT uint8_t *l_pem_load_file(const char *filename,char **type_label,size_t *len) {struct pem_file_info file;
@@ -241,7 +236,7 @@ LIB_EXPORT uint8_t *l_pem_load_file(const char *filename, int index,if (pem_file_open (&file, filename) <0 )return NULL ; - result =pem_load_buffer (file.data, file.st.st_size, index, + result =pem_load_buffer (file.data, file.st.st_size, type_label, len,NULL );pem_file_close (&file);return result;
@@ -300,7 +295,7 @@ LIB_EXPORT struct l_queue *l_pem_load_certificate_list_from_data(char *label;struct l_cert *cert; - der =pem_load_buffer (ptr, end - ptr,0 , &label, &der_len, &ptr); + der =pem_load_buffer (ptr, end - ptr, &label, &der_len, &ptr);if (!der ||strcmp (label,"CERTIFICATE" )) {if (der)
@@ -470,7 +465,7 @@ LIB_EXPORT struct l_key *l_pem_load_private_key_from_data(const void *buf,if (encrypted) *encrypted = false; - content =pem_load_buffer (buf, buf_len,0 , &label, &len,NULL ); + content =pem_load_buffer (buf, buf_len, &label, &len,NULL );if (!content)return NULL ;
@@ -505,7 +500,7 @@ LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename,if (encrypted) *encrypted = false; - content =l_pem_load_file (filename,0 , &label, &len); + content =l_pem_load_file (filename, &label, &len);if (!content)return NULL ;
ell/pem.h +2 -3
@@ -33,9 +33,8 @@ struct l_cert;struct l_certchain ;uint8_t *l_pem_load_buffer (const uint8_t *buf,size_t buf_len, -int index,char **type_label,size_t *out_len); -uint8_t *l_pem_load_file (const char *filename,int index, -char **type_label,size_t *len); +char **type_label,size_t *out_len); +uint8_t *l_pem_load_file (const char *filename,char **type_label,size_t *len);struct l_certchain *l_pem_load_certificate_chain (const char *filename); struct l_certchain *l_pem_load_certificate_chain_from_data(const void *buf,
Signature · 1 line
2.17.1
PATCH v2 2/6 unit: remove index from pem APIs
--- unit/test-key.c | 2 +- unit/test-pem.c | 2 +- unit/test-tls.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
unit/test-key.c +1 -1
@@ -399,7 +399,7 @@ static struct l_cert *load_cert_file(const char *filename)char *label;struct l_cert *cert; - der =l_pem_load_file (filename,0 , &label, &len); + der =l_pem_load_file (filename, &label, &len);if (!der)return NULL ;
unit/test-pem.c +1 -1
@@ -230,7 +230,7 @@ static void test_pem(const void *data)size_t decoded_size; decoded =l_pem_load_buffer ((const uint8_t *) test->input, -strlen (test->input),0 , +strlen (test->input), &label, &decoded_size);if (!test->valid) {
unit/test-tls.c +1 -1
@@ -214,7 +214,7 @@ static struct l_cert *load_cert_file(const char *filename)char *label;struct l_cert *cert; - der =l_pem_load_file (filename,0 , &label, &len); + der =l_pem_load_file (filename, &label, &len);if (!der)return NULL ;
Signature · 1 line
2.17.1
PATCH v2 3/6 pem: convert l_pem_load_buffer to take void*
In reply to James PrestwoodThis handles both cases of PEM's being loaded as text data and as raw bytes. is_{start,end}_boundary was also converted.
--- ell/pem.c | 55 ++++++++++++++++++++++++++++++------------------------- ell/pem.h | 4 ++-- 2 files changed, 32 insertions(+), 27 deletions(-)
ell/pem.c +30 -25
@@ -49,11 +49,12 @@#define PEM_START_BOUNDARY "-----BEGIN "#define PEM_END_BOUNDARY "-----END " -static const uint8_t *is_start_boundary (const uint8_t *buf,size_t buf_len, +static const void *is_start_boundary (const void *buf,size_t buf_len,size_t *label_len) {const uint8_t *start, *end, *ptr;int prev_special, special; +const uint8_t *buf_ptr = buf;if (buf_len <strlen (PEM_START_BOUNDARY ))return NULL ;
@@ -74,7 +75,7 @@ static const uint8_t *is_start_boundary(const uint8_t *buf, size_t buf_len, end = start; prev_special =1 ; -while (end < buf + buf_len &&l_ascii_isprint (*end)) { +while (end < buf_ptr + buf_len &&l_ascii_isprint (*end)) { special = *end ==' ' || *end =='-' ;if (prev_special && special)
@@ -89,11 +90,11 @@ static const uint8_t *is_start_boundary(const uint8_t *buf, size_t buf_len, end--;/* Check we have a "-----" (RFC7468 section 2) */ -if (end +5 > buf + buf_len ||memcmp (end,"-----" ,5 )) +if (end +5 > buf_ptr + buf_len ||memcmp (end,"-----" ,5 ))return NULL ;/* Check all remaining characters are horizontal whitespace (WSP) */ -for (ptr = end +5 ; ptr < buf + buf_len; ptr++) +for (ptr = end +5 ; ptr < buf_ptr + buf_len; ptr++)if (*ptr !=' ' && *ptr !='\t' )return NULL ;
@@ -102,33 +103,36 @@ static const uint8_t *is_start_boundary(const uint8_t *buf, size_t buf_len,return start; } -static bool is_end_boundary (const uint8_t *buf,size_t buf_len, -const uint8_t *label,size_t label_len) +static bool is_end_boundary (const void *buf,size_t buf_len, +const char *label,size_t label_len) { +const uint8_t *buf_ptr = buf;size_t len =strlen (PEM_END_BOUNDARY ) + label_len +5 ;if (buf_len < len)return false; -if (memcmp (buf,PEM_END_BOUNDARY ,strlen (PEM_END_BOUNDARY )) || -memcmp (buf +strlen (PEM_END_BOUNDARY ), +if (memcmp (buf_ptr,PEM_END_BOUNDARY ,strlen (PEM_END_BOUNDARY )) || +memcmp (buf_ptr +strlen (PEM_END_BOUNDARY ), label, label_len) || -memcmp (buf + (len -5 ),"-----" ,5 )) +memcmp (buf_ptr + (len -5 ),"-----" ,5 ))return false;/* Check all remaining characters are horizontal whitespace (WSP) */ for (; len < buf_len; len++) -if (buf[len] !=' ' && buf[len] !='\t' ) +if (buf_ptr[len] !=' ' && buf_ptr[len] !='\t' )return false;return true; } -static uint8_t *pem_load_buffer (const uint8_t *buf,size_t buf_len, +static uint8_t *pem_load_buffer (const void *buf,size_t buf_len,char **type_label,size_t *len,const uint8_t **endp) { -const uint8_t *base64_data =NULL , *label =NULL , *eol; +const char *buf_ptr = buf; +const char *base64_data =NULL , *eol; +const char *label =NULL ;uint8_t *data;size_t label_len =0 ;
@@ -139,19 +143,21 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len,* are not confused for actual PEM "textual encoding". */ while (buf_len) { -for (eol = buf; eol < buf + buf_len; eol++) +for (eol = buf_ptr; eol < buf_ptr + buf_len; eol++)if (*eol =='\r' || *eol =='\n' )break ;if (!base64_data) { - label =is_start_boundary (buf, eol - buf, &label_len); + label =is_start_boundary (buf, eol - buf_ptr, + &label_len);if (label) base64_data = eol; - }else if (is_end_boundary (buf, eol - buf, label, label_len)) { + }else if (is_end_boundary (buf_ptr, eol - buf_ptr, label, + label_len)) { data =l_base64_decode ( (const char *) base64_data, - buf - base64_data, len); + buf_ptr - base64_data, len);if (!data)return NULL ;
@@ -159,19 +165,19 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len, label_len);if (endp) - *endp = eol +1 ; + *endp = (uint8_t *)eol +1 ;return data; } -if (eol == buf + buf_len) +if (eol == buf_ptr + buf_len)break ; - buf_len -= eol +1 - buf; - buf = eol +1 ; + buf_len -= eol +1 - buf_ptr; + buf_ptr = eol +1 ; -if (buf_len && *eol =='\r' && *buf =='\n' ) { - buf++; +if (buf_len && *eol =='\r' && *buf_ptr =='\n' ) { + buf_ptr++; buf_len--; } }
@@ -183,9 +189,8 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len,return NULL ; } -LIB_EXPORT uint8_t *l_pem_load_buffer(const uint8_t *buf, size_t buf_len, -char **type_label, -size_t *out_len) +LIB_EXPORT uint8_t *l_pem_load_buffer(const void *buf, size_t buf_len, +char **type_label,size_t *out_len) {return pem_load_buffer (buf, buf_len, type_label, out_len,NULL ); }
ell/pem.h +2 -2
@@ -32,8 +32,8 @@ struct l_key;struct l_cert ;struct l_certchain ; -uint8_t *l_pem_load_buffer (const uint8_t *buf,size_t buf_len, -char **type_label,size_t *out_len); +uint8_t *l_pem_load_buffer (const void *buf,size_t buf_len,char **type_label, +size_t *out_len);uint8_t *l_pem_load_file (const char *filename,char **type_label,size_t *len);struct l_certchain *l_pem_load_certificate_chain (const char *filename);
Signature · 1 line
2.17.1
PATCH v2 4/6 pem: refactor to expose pem iterator privately
In reply to James PrestwoodThis refactors pem.c to expose a new internal API, pem_next. This API will allow iteration/parsing of PEM(s). Other APIs in pem.c were updated to use pem_next.
pem_next behaves similar to pem_load_buffer, except it does not do any base64 decoding. Instead it just returns the start of the PEM as a string, and the length as an out parameter. This allows simple PEM iteration loops. Strictly for pem.c usage we decode the base64 string in the loop, but settings.c will need this for parsing PEMs from settings files and returning the PEM as a full string, including the start/end tags.
--- ell/pem-private.h | 31 ++++++++++++++ ell/pem.c | 103 ++++++++++++++++++++++++++++++---------------- 2 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 ell/pem-private.h
ell/pem-private.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* +* Embedded Linux library +* +* Copyright (C) 2019 Intel Corporation. All rights reserved. +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#define _GNU_SOURCE +#include <sys/types.h> + +const char *pem_next (const void *buf,size_t buf_len,char **type_label, +size_t *len);
ell/pem.c +67 -36
173 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 5/6 settings: introduce extended groups concept
In reply to James PrestwoodThis patch introduces a new extended format for settings files. This new format allows raw data to be provided under a given group rather than key/value pairs. A group using an extended format will contain both the extended type and group name in the format, for example:
[@pem(a)certificate]
Where 'pem' is the type and 'certificate' is the name. The type of format requires an extension specific parser implementation in settings as the parser must know when to start/stop parsing data. This allows for any textual characters to be present after the group is declared, including special characters like [] (its completely up to the parser to decide whats valid and what is not).
Three new l_settings APIs were added:
l_settings_has_embedded_groupl_settings_get_embedded_groupsl_settings_get_embedded_value
In this patch, the 'pem' extension type is also introduced, and for now is the only extended type. The pem extended type expects a base64 encoded PEM (or list of PEMs) after the group. The pem parser will stop after the last PEM is found. Examples can be found in unit/test-settings.c
--- ell/ell.sym | 3 + ell/settings.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++--- ell/settings.h | 7 ++ 3 files changed, 230 insertions(+), 11 deletions(-)
ell/ell.sym +3 -0
@@ -425,6 +425,9 @@ global: l_settings_set_float; l_settings_remove_group; l_settings_remove_key; + l_settings_has_embedded_group; + l_settings_get_embedded_groups; + l_settings_get_embedded_value; /* signal */ l_signal_create; l_signal_remove;
ell/settings.c +220 -11
298 lines not on this page · raw message
ell/settings.h +7 -0
@@ -123,6 +123,13 @@ bool l_settings_remove_key(struct l_settings *settings, const char *group_name, const char *key);bool l_settings_remove_group (struct l_settings *settings,const char *group_name); + +char **l_settings_get_embedded_groups (struct l_settings *settings); +bool l_settings_has_embedded_group (struct l_settings *settings, +const char *group); +const char *l_settings_get_embedded_value (struct l_settings *settings, +const char *group_name, +const char **out_type); #ifdef __cplusplus }#endif
Signature · 1 line
2.17.1
PATCH v2 6/6 unit/test-settings: add pem extended type tests
In reply to James Prestwood--- unit/settings.test | 28 +++++ unit/test-settings.c | 238 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 264 insertions(+), 2 deletions(-)
unit/settings.test +28 -0
@@ -20,3 +20,31 @@ StringList=Foo,Bar,Baz StringListEmpty= StringListOne=FooBarBaz StringWithSpaces=Bar B Q + +[@pem(a)certificate] +-----BEGIN CERTIFICATE----- +MIIEajCCA1KgAwIBAgIUKIOXQXEyHZsOFw/+ihDBNGTQnWUwDQYJKoZIhvcNAQEL +BQAweDE1MDMGA1UECgwsSW50ZXJuYXRpb25hbCBVbmlvbiBvZiBFeGFtcGxlIE9y +Z2FuaXphdGlvbnMxHzAdBgNVBAMMFkNlcnRpZmljYXRlIGlzc3VlciBndXkxHjAc +BgkqhkiG9w0BCQEWD2NhQG1haWwuZXhhbXBsZTAeFw0xOTA5MTYxNzEyNThaFw00 +NzAyMDExNzEyNThaMHgxNTAzBgNVBAoMLEludGVybmF0aW9uYWwgVW5pb24gb2Yg +RXhhbXBsZSBPcmdhbml6YXRpb25zMR8wHQYDVQQDDBZDZXJ0aWZpY2F0ZSBpc3N1 +ZXIgZ3V5MR4wHAYJKoZIhvcNAQkBFg9jYUBtYWlsLmV4YW1wbGUwggEiMA0G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GpaijDicA2YqlK +-----END CERTIFICATE-----
unit/test-settings.c +236 -2
274 lines not on this page · raw message
Signature · 1 line
2.17.1