From 6714dd1c3b47eaf7cf2176e13bc826cef4cecea4 Mon Sep 17 00:00:00 2001 From: "git@daemon.de" Date: Mon, 20 Jan 2014 16:07:01 +0100 Subject: [PATCH] prepared recipient list support --- include/pcp/plist.h | 55 +++++++++++++++++++++++++++++++++++ src/encryption.c | 71 +++++++++++++++++++++++++++++---------------- src/encryption.h | 3 +- src/pcp.c | 41 +++++++++++++++----------- src/pcp.h | 1 + 5 files changed, 128 insertions(+), 43 deletions(-) create mode 100644 include/pcp/plist.h diff --git a/include/pcp/plist.h b/include/pcp/plist.h new file mode 100644 index 0000000..815b286 --- /dev/null +++ b/include/pcp/plist.h @@ -0,0 +1,55 @@ +/* + This file is part of Pretty Curved Privacy (pcp1). + + Copyright (C) 2013 T. von Dein. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + You can contact me by mail: . +*/ + + +// used for recipient- or keyid lists + +#ifndef _HAVE_PCP_PLIST_H +#define _HAVE_PCP_PLIST_H + +#include + +struct _plist_t { + char *value; + struct _plist_t *next; +}; + +typedef struct _plist_t plist_t; + +static inline void p_add(plist_t **lst, char *value) { + plist_t *newitem; + plist_t *lst_iter = *lst; + + newitem = (plist_t *)malloc(sizeof(plist_t)); + newitem->value = malloc(strlen(value) + 1); + strncpy(newitem->value, value, strlen(value) + 1); + newitem->next = NULL; + + if ( lst_iter != NULL ) { + while (lst_iter->next != NULL ) + lst_iter = lst_iter->next; + lst_iter->next = newitem; + } + else + *lst = newitem; +} + +#endif // _HAVE_PCP_PLIST_H diff --git a/src/encryption.c b/src/encryption.c index 7b868a1..ae53f1a 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -93,39 +93,62 @@ int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd) { -int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, char *recipient) { +int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *recipient) { FILE *in = NULL; FILE *out = NULL; + pcp_pubkey_t *pubhash = NULL; // FIXME: add free() + pcp_pubkey_t *tmp = NULL; pcp_pubkey_t *pub = NULL; pcp_key_t *secret = NULL; int self = 0; - // look if we've got that key - HASH_FIND_STR(pcppubkey_hash, id, pub); - if(pub == NULL) { - // FIXME: use recipient to lookup by name or email - // self-encryption: look if its a secret one - pcp_key_t *s = NULL; - HASH_FIND_STR(pcpkey_hash, id, s); - if(s != NULL) { - pub = pcpkey_pub_from_secret(s); - self = 1; + // FIXME: mk id a plist_t with loop as well + if(id != NULL) { + // lookup by id + HASH_FIND_STR(pcppubkey_hash, id, tmp); + if(tmp == NULL) { + // FIXME: use recipient to lookup by name or email + // self-encryption: look if its a secret one + pcp_key_t *s = NULL; + HASH_FIND_STR(pcpkey_hash, id, s); + if(s != NULL) { + tmp = pcpkey_pub_from_secret(s); + HASH_ADD_STR( pubhash, id, tmp); + self = 1; + } + else { + fatal("Could not find a public key with id 0x%s in vault %s!\n", + id, vault->filename); + goto erren3; + } } else { - fatal("Could not find a public key with id 0x%s in vault %s!\n", - id, vault->filename); - goto erren3; + // found one by id, copy into local hash + pcp_pubkey_t *pub = ucmalloc(sizeof(pcp_pubkey_t)); + memcpy(pub, tmp, sizeof(pcp_pubkey_t)); + HASH_ADD_STR( pubhash, id, tmp); } } + else if(recipient != NULL) { + // lookup by recipient list + // FIXME: implement, iterate through global hashlist + // copy matches into temporary pubhash + } + else { + fatal("id or recipient list required!\n"); + goto erren3; + } - secret = pcpkey_new(); // DEPRECATED: pcp_find_primary_secret(); + + // we're using a random secret keypair on our side + secret = pcpkey_new(); if(infile == NULL) in = stdin; else { if((in = fopen(infile, "rb")) == NULL) { fatal("Could not open input file %s\n", infile); - goto erren3; + goto erren2; } } @@ -134,24 +157,22 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, char *recipi else { if((out = fopen(outfile, "wb+")) == NULL) { fatal("Could not open output file %s\n", outfile); - goto erren3; + goto erren2; } } - if(debug) { - fprintf(stderr, "Using secret key:\n"); - pcp_dumpkey(secret); - fprintf(stderr, "Using publickey:\n"); - pcp_dumppubkey(pub); - } - - size_t clen = pcp_encrypt_file(in, out, secret, pub, self); + size_t clen = pcp_encrypt_file(in, out, secret, pubhash, self); if(clen > 0) { fprintf(stderr, "Encrypted %d bytes for 0x%s successfully\n", (int)clen, id); return 0; } + erren2: + free(pubhash); + free(tmp); + free(pub); + erren3: return 1; diff --git a/src/encryption.h b/src/encryption.h index a41446e..cfbee84 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -34,8 +34,9 @@ #include "z85.h" #include "keyprint.h" #include "keyhash.h" +#include "plist.h" int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd); -int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, char *recipient); +int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *recipient); #endif // _HAVE_ENCRYPTION_H diff --git a/src/pcp.c b/src/pcp.c index 88ff60d..7042c91 100644 --- a/src/pcp.c +++ b/src/pcp.c @@ -52,7 +52,7 @@ int main (int argc, char **argv) { char *keyid = NULL; char *id = NULL; char *xpass = NULL; - char *recipient = NULL; + plist_t *recipient = NULL; FILE *in; PCP_EXIT = 0; @@ -203,8 +203,7 @@ int main (int argc, char **argv) { strncpy(xpass, optarg, strlen(optarg)+1); break; case 'r': - recipient = ucmalloc(strlen(optarg)+1); - strncpy(recipient, optarg, strlen(optarg)+1); + p_add(&recipient, optarg); userec = 1; break; @@ -265,7 +264,10 @@ int main (int argc, char **argv) { if(id == NULL) break; } - pcp_exportpublic(id, recipient, xpass, outfile); + if (recipient != NULL) + pcp_exportpublic(id, recipient->value, xpass, outfile); + else + pcp_exportpublic(id, NULL, xpass, outfile); if(xpass != NULL) free(xpass); if(recipient != NULL) @@ -325,28 +327,33 @@ int main (int argc, char **argv) { break; case PCP_MODE_ENCRYPT: - if(useid) { + if(useid == 1 && userec == 0) { + // one dst, FIXME: make id a list as well id = pcp_normalize_id(keyid); + pcpencrypt(id, infile, outfile, xpass, NULL); } - if(useid == 0 && userec == 1) { - id = pcp_find_id_byrec(recipient); + else if(useid == 0 && userec == 1) { + // multiple dst + pcpencrypt(id, infile, outfile, xpass, recipient); } - if(useid == 0 && userec == 0) { + else if(useid == 0 && userec == 0) { + // self mode pcp_key_t *k = pcp_find_primary_secret(); id = ucmalloc(17); memcpy(id, k->id, 17); + pcpencrypt(id, infile, outfile, xpass, NULL); } - if(id != NULL) { - pcpencrypt(id, infile, outfile, xpass, recipient); - free(id); - if(xpass != NULL) - free(xpass); - if(recipient != NULL) - free(recipient); - } else { - fatal("You need to specify a key id (--keyid) or a recipient (--recipient)!\n"); + // -i and -r specified + fatal("You can't specify both -i and -r, use either -i or -r!\n"); } + if(id != NULL) + free(id); + if(xpass != NULL) + free(xpass); + if(recipient != NULL) + free(recipient); + break; case PCP_MODE_DECRYPT: diff --git a/src/pcp.h b/src/pcp.h index 17a9fa0..87249d8 100644 --- a/src/pcp.h +++ b/src/pcp.h @@ -46,6 +46,7 @@ #include "encryption.h" #include "signature.h" #include "keyhash.h" +#include "plist.h" // operation modi // perl -e '$x=0; while ($x<100000) { $x++; $x *= 1.7; printf "0x%08X: %d\n", $x, $x }'