mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
prepared recipient list support
This commit is contained in:
55
include/pcp/plist.h
Normal file
55
include/pcp/plist.h
Normal 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
|
||||||
@@ -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 *in = NULL;
|
||||||
FILE *out = NULL;
|
FILE *out = NULL;
|
||||||
|
pcp_pubkey_t *pubhash = NULL; // FIXME: add free()
|
||||||
|
pcp_pubkey_t *tmp = NULL;
|
||||||
pcp_pubkey_t *pub = NULL;
|
pcp_pubkey_t *pub = NULL;
|
||||||
pcp_key_t *secret = NULL;
|
pcp_key_t *secret = NULL;
|
||||||
int self = 0;
|
int self = 0;
|
||||||
|
|
||||||
// look if we've got that key
|
// FIXME: mk id a plist_t with loop as well
|
||||||
HASH_FIND_STR(pcppubkey_hash, id, pub);
|
if(id != NULL) {
|
||||||
if(pub == NULL) {
|
// lookup by id
|
||||||
// FIXME: use recipient to lookup by name or email
|
HASH_FIND_STR(pcppubkey_hash, id, tmp);
|
||||||
// self-encryption: look if its a secret one
|
if(tmp == NULL) {
|
||||||
pcp_key_t *s = NULL;
|
// FIXME: use recipient to lookup by name or email
|
||||||
HASH_FIND_STR(pcpkey_hash, id, s);
|
// self-encryption: look if its a secret one
|
||||||
if(s != NULL) {
|
pcp_key_t *s = NULL;
|
||||||
pub = pcpkey_pub_from_secret(s);
|
HASH_FIND_STR(pcpkey_hash, id, s);
|
||||||
self = 1;
|
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 {
|
else {
|
||||||
fatal("Could not find a public key with id 0x%s in vault %s!\n",
|
// found one by id, copy into local hash
|
||||||
id, vault->filename);
|
pcp_pubkey_t *pub = ucmalloc(sizeof(pcp_pubkey_t));
|
||||||
goto erren3;
|
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)
|
if(infile == NULL)
|
||||||
in = stdin;
|
in = stdin;
|
||||||
else {
|
else {
|
||||||
if((in = fopen(infile, "rb")) == NULL) {
|
if((in = fopen(infile, "rb")) == NULL) {
|
||||||
fatal("Could not open input file %s\n", infile);
|
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 {
|
else {
|
||||||
if((out = fopen(outfile, "wb+")) == NULL) {
|
if((out = fopen(outfile, "wb+")) == NULL) {
|
||||||
fatal("Could not open output file %s\n", outfile);
|
fatal("Could not open output file %s\n", outfile);
|
||||||
goto erren3;
|
goto erren2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug) {
|
size_t clen = pcp_encrypt_file(in, out, secret, pubhash, self);
|
||||||
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);
|
|
||||||
|
|
||||||
if(clen > 0) {
|
if(clen > 0) {
|
||||||
fprintf(stderr, "Encrypted %d bytes for 0x%s successfully\n", (int)clen, id);
|
fprintf(stderr, "Encrypted %d bytes for 0x%s successfully\n", (int)clen, id);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
erren2:
|
||||||
|
free(pubhash);
|
||||||
|
free(tmp);
|
||||||
|
free(pub);
|
||||||
|
|
||||||
erren3:
|
erren3:
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@@ -34,8 +34,9 @@
|
|||||||
#include "z85.h"
|
#include "z85.h"
|
||||||
#include "keyprint.h"
|
#include "keyprint.h"
|
||||||
#include "keyhash.h"
|
#include "keyhash.h"
|
||||||
|
#include "plist.h"
|
||||||
|
|
||||||
int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd);
|
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
|
#endif // _HAVE_ENCRYPTION_H
|
||||||
|
|||||||
41
src/pcp.c
41
src/pcp.c
@@ -52,7 +52,7 @@ int main (int argc, char **argv) {
|
|||||||
char *keyid = NULL;
|
char *keyid = NULL;
|
||||||
char *id = NULL;
|
char *id = NULL;
|
||||||
char *xpass = NULL;
|
char *xpass = NULL;
|
||||||
char *recipient = NULL;
|
plist_t *recipient = NULL;
|
||||||
FILE *in;
|
FILE *in;
|
||||||
|
|
||||||
PCP_EXIT = 0;
|
PCP_EXIT = 0;
|
||||||
@@ -203,8 +203,7 @@ int main (int argc, char **argv) {
|
|||||||
strncpy(xpass, optarg, strlen(optarg)+1);
|
strncpy(xpass, optarg, strlen(optarg)+1);
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
recipient = ucmalloc(strlen(optarg)+1);
|
p_add(&recipient, optarg);
|
||||||
strncpy(recipient, optarg, strlen(optarg)+1);
|
|
||||||
userec = 1;
|
userec = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -265,7 +264,10 @@ int main (int argc, char **argv) {
|
|||||||
if(id == NULL)
|
if(id == NULL)
|
||||||
break;
|
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)
|
if(xpass != NULL)
|
||||||
free(xpass);
|
free(xpass);
|
||||||
if(recipient != NULL)
|
if(recipient != NULL)
|
||||||
@@ -325,28 +327,33 @@ int main (int argc, char **argv) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PCP_MODE_ENCRYPT:
|
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);
|
id = pcp_normalize_id(keyid);
|
||||||
|
pcpencrypt(id, infile, outfile, xpass, NULL);
|
||||||
}
|
}
|
||||||
if(useid == 0 && userec == 1) {
|
else if(useid == 0 && userec == 1) {
|
||||||
id = pcp_find_id_byrec(recipient);
|
// 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();
|
pcp_key_t *k = pcp_find_primary_secret();
|
||||||
id = ucmalloc(17);
|
id = ucmalloc(17);
|
||||||
memcpy(id, k->id, 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 {
|
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;
|
break;
|
||||||
|
|
||||||
case PCP_MODE_DECRYPT:
|
case PCP_MODE_DECRYPT:
|
||||||
|
|||||||
@@ -46,6 +46,7 @@
|
|||||||
#include "encryption.h"
|
#include "encryption.h"
|
||||||
#include "signature.h"
|
#include "signature.h"
|
||||||
#include "keyhash.h"
|
#include "keyhash.h"
|
||||||
|
#include "plist.h"
|
||||||
|
|
||||||
// operation modi
|
// operation modi
|
||||||
// perl -e '$x=0; while ($x<100000) { $x++; $x *= 1.7; printf "0x%08X: %d\n", $x, $x }'
|
// perl -e '$x=0; while ($x<100000) { $x++; $x *= 1.7; printf "0x%08X: %d\n", $x, $x }'
|
||||||
|
|||||||
Reference in New Issue
Block a user