prepared recipient list support

This commit is contained in:
git@daemon.de
2014-01-20 16:07:01 +01:00
parent 67ba04f3bd
commit 6714dd1c3b
5 changed files with 128 additions and 43 deletions

55
include/pcp/plist.h Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
You can contact me by mail: <tlinden AT cpan DOT org>.
*/
// used for recipient- or keyid lists
#ifndef _HAVE_PCP_PLIST_H
#define _HAVE_PCP_PLIST_H
#include <stdlib.h>
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

View File

@@ -93,22 +93,27 @@ 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: 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) {
pub = pcpkey_pub_from_secret(s);
tmp = pcpkey_pub_from_secret(s);
HASH_ADD_STR( pubhash, id, tmp);
self = 1;
}
else {
@@ -117,15 +122,33 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, char *recipi
goto erren3;
}
}
else {
// 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;

View File

@@ -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

View File

@@ -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);
else {
// -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);
}
else {
fatal("You need to specify a key id (--keyid) or a recipient (--recipient)!\n");
}
break;
case PCP_MODE_DECRYPT:

View File

@@ -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 }'