From dbdaf38185d7c91f9c62d981af60c04d9fbd224f Mon Sep 17 00:00:00 2001 From: "git@daemon.de" Date: Wed, 29 Jan 2014 12:53:42 +0100 Subject: [PATCH] use a struct to fetch in a pbp key (pbp_pubkey_t) instead of manual parsing --- TODO | 9 +++------ include/pcp/key.h | 12 ++++++++++++ libtool | 12 ++++++------ src/keymgmt.c | 48 +++++++++++++++++++++-------------------------- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/TODO b/TODO index fa760b1..f293e2a 100644 --- a/TODO +++ b/TODO @@ -6,13 +6,10 @@ Bug: pcp_z85_decode() fails if after end marker follows something, even whitespa key++: normalize id and lc() -sym decrypt uses vault, which it shouldn't - - allow signing using an alternate secret key, like in pcpdecrypt() -support export/import from/to pbp - malloc() new pointers in functions only if not NULL, e.g. pcp_gennonce() -generalize file i/0, open+close only in src/, print msg if using stdin or stdout \ No newline at end of file +generalize file i/0, open+close only in src/, print msg if using stdin or stdout + +put the key import and export stuff into the lib, support from/to file and string \ No newline at end of file diff --git a/include/pcp/key.h b/include/pcp/key.h index 55caab1..b02ce97 100644 --- a/include/pcp/key.h +++ b/include/pcp/key.h @@ -104,8 +104,20 @@ struct _pcp_pubkey_t { UT_hash_handle hh; }; +// the PBP public key format +struct _pbp_pubkey_t { + byte sig[crypto_sign_BYTES]; + byte sigpub[crypto_box_PUBLICKEYBYTES]; + byte edpub[crypto_sign_PUBLICKEYBYTES]; + byte pub[crypto_box_PUBLICKEYBYTES]; + char iso_ctime[32]; + char iso_expire[32]; + char name[1024]; +}; + typedef struct _pcp_key_t pcp_key_t; typedef struct _pcp_pubkey_t pcp_pubkey_t; +typedef struct _pbp_pubkey_t pbp_pubkey_t; #define PCP_RAW_KEYSIZE sizeof(pcp_key_t) - sizeof(UT_hash_handle) #define PCP_RAW_PUBKEYSIZE sizeof(pcp_pubkey_t) - sizeof(UT_hash_handle) diff --git a/libtool b/libtool index 9002541..08021ec 100755 --- a/libtool +++ b/libtool @@ -2,7 +2,7 @@ # libtool - Provide generalized library-building support services. # Generated automatically by config.status (pcp) 0.2.0 -# Libtool was configured on host io: +# Libtool was configured on host r4: # NOTE: Changes made to this file will be lost: look at ltmain.sh. # # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, @@ -66,13 +66,13 @@ PATH_SEPARATOR=":" # The host system. host_alias= -host=amd64-unknown-freebsd9.0 -host_os=freebsd9.0 +host=amd64-unknown-freebsd9.1 +host_os=freebsd9.1 # The build system. build_alias= -build=amd64-unknown-freebsd9.0 -build_os=freebsd9.0 +build=amd64-unknown-freebsd9.1 +build_os=freebsd9.1 # A sed program that does not truncate output. SED="/usr/bin/sed" @@ -164,7 +164,7 @@ lock_old_archive_extraction=no LTCC="gcc" # LTCC compiler flags. -LTCFLAGS="-I/usr/local/include -I/usr/local/include" +LTCFLAGS="-g -O2 -I/usr/local/include" # Take the output of nm and produce a listing of raw symbols and C names. global_symbol_pipe="sed -n -e 's/^.*[ ]\\([ABCDGIRSTW][ABCDGIRSTW]*\\)[ ][ ]*\\([_A-Za-z][_A-Za-z0-9]*\\)\$/\\1 \\2 \\2/p' | sed '/ __gnu_lto/d'" diff --git a/src/keymgmt.c b/src/keymgmt.c index 90ee1a6..3a44739 100644 --- a/src/keymgmt.c +++ b/src/keymgmt.c @@ -376,33 +376,31 @@ int pcp_importsecret (vault_t *vault, FILE *in) { int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { pcp_pubkey_t *pub = NULL; if(pbpcompat == 1) { - size_t bufsize = 1024; - unsigned char in_buf[bufsize]; + char *date = NULL; + char *parts = NULL; + int pnum; + pbp_pubkey_t *b = ucmalloc(sizeof(pbp_pubkey_t)); pub = ucmalloc(sizeof(pcp_pubkey_t)); - char *date = ucmalloc(19); - char *tmp = ucmalloc(1024); + + // fetch the key from the file + if(fread(b, 1, sizeof(pbp_pubkey_t), in) < sizeof(pbp_pubkey_t) - 1024) { + fatal("PBP key seems to be too small, maybe it's not a PBP key\n"); + goto errimp1; + } - bufsize = fread(&in_buf, 1, crypto_sign_BYTES, in); - - fread(&in_buf, 1, crypto_box_PUBLICKEYBYTES, in); // ignored currently - fread(pub->edpub, 1, crypto_sign_PUBLICKEYBYTES, in); - fread(pub->pub, 1, crypto_box_PUBLICKEYBYTES, in); - - fread(date, 1, 19, in); + // parse the date + date = ucmalloc(19); + memcpy(date, b->iso_ctime, 18); date[19] = '\0'; - fread(&in_buf, 1, 44, in); // ignore validity date - - bufsize = fread(tmp, 1, 1024, in); - tmp[bufsize] = '\0'; - struct tm c; if(strptime(date, "%Y-%m-%dT%H:%M:%S", &c) == NULL) { fatal("Failed to parse creation time in PBP public key file (<%s>)\n", date); goto errimp1; } - char *parts = strtok (tmp, "<>"); - int pnum = 0; + // parse the name + parts = strtok (b->name, "<>"); + pnum = 0; while (parts != NULL) { if(pnum == 0) memcpy(pub->owner, parts, strlen(parts)); @@ -413,27 +411,23 @@ int pcp_importpublic (vault_t *vault, FILE *in, int pbpcompat) { } free(parts); - if(sscanf(tmp, "%s|%s", pub->owner, pub->mail) == 0) { - if(sscanf(tmp, "%s", pub->owner) == 0) { - fatal("Failed to parse owner in PBP public key file\n"); - goto errimp1; - } - } - + // fill in the fields pub->ctime = (long)mktime(&c); pub->type = PCP_KEY_TYPE_PUBLIC; pub->version = PCP_KEY_VERSION; pub->serial = arc4random(); memcpy(pub->id, pcp_getpubkeyid(pub), 17); + memcpy(pub->pub, b->pub, crypto_box_PUBLICKEYBYTES); + memcpy(pub->edpub, b->edpub, crypto_sign_PUBLICKEYBYTES); + free(b); free(date); - free(tmp); goto kimp; errimp1: free(date); - free(tmp); free(pub); + free(b); return 1; } else {