2013-11-04 17:43:22 +01:00
|
|
|
/*
|
|
|
|
|
This file is part of Pretty Curved Privacy (pcp1).
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2013 T.Linden.
|
|
|
|
|
|
|
|
|
|
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>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
#include "encryption.h"
|
|
|
|
|
|
2014-01-27 16:12:43 +01:00
|
|
|
int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, int verify) {
|
2013-10-28 22:50:05 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
|
|
|
|
pcp_key_t *secret = NULL;
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *symkey = NULL;
|
2014-01-22 23:20:30 +01:00
|
|
|
size_t dlen;
|
|
|
|
|
uint8_t head;
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
if(infile == NULL)
|
|
|
|
|
in = stdin;
|
|
|
|
|
else {
|
|
|
|
|
if((in = fopen(infile, "rb")) == NULL) {
|
|
|
|
|
fatal("Could not open input file %s\n", infile);
|
2014-01-20 10:16:05 +01:00
|
|
|
goto errde3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(outfile == NULL)
|
|
|
|
|
out = stdout;
|
|
|
|
|
else {
|
|
|
|
|
if((out = fopen(outfile, "wb+")) == NULL) {
|
|
|
|
|
fatal("Could not open output file %s\n", outfile);
|
2014-01-20 10:16:05 +01:00
|
|
|
goto errde3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 20:41:16 +01:00
|
|
|
/* determine crypt mode */
|
2014-01-22 23:20:30 +01:00
|
|
|
fread(&head, 1, 1, in);
|
|
|
|
|
if(!feof(in) && !ferror(in)) {
|
|
|
|
|
if(head == PCP_SYM_CIPHER) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* symetric mode */
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *salt = ucmalloc(90);
|
2014-01-22 23:20:30 +01:00
|
|
|
char stsalt[] = PBP_COMPAT_SALT;
|
|
|
|
|
memcpy(salt, stsalt, 90);
|
|
|
|
|
|
|
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
|
|
|
|
pcp_readpass(&passphrase,
|
|
|
|
|
"Enter passphrase for symetric decryption", NULL, 1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
passphrase = ucmalloc(strlen(passwd)+1);
|
|
|
|
|
strncpy(passphrase, passwd, strlen(passwd)+1);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 12:19:17 +01:00
|
|
|
symkey = pcp_scrypt(passphrase, crypto_secretbox_KEYBYTES, salt, 90);
|
2014-01-22 23:20:30 +01:00
|
|
|
free(salt);
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* asymetric mode */
|
2014-01-22 23:20:30 +01:00
|
|
|
if(useid) {
|
|
|
|
|
HASH_FIND_STR(pcpkey_hash, id, secret);
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal("Could not find a secret key with id 0x%s in vault %s!\n",
|
|
|
|
|
id, vault->filename);
|
|
|
|
|
goto errde3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
secret = pcp_find_primary_secret();
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal("Could not find a secret key in vault %s!\n", id, vault->filename);
|
|
|
|
|
goto errde3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(secret->secret[0] == 0) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* encrypted, decrypt it */
|
2014-01-22 23:20:30 +01:00
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
|
|
|
|
pcp_readpass(&passphrase,
|
|
|
|
|
"Enter passphrase to decrypt your secret key", NULL, 1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
passphrase = ucmalloc(strlen(passwd)+1);
|
|
|
|
|
strncpy(passphrase, passwd, strlen(passwd)+1);
|
|
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-22 23:20:30 +01:00
|
|
|
secret = pcpkey_decrypt(secret, passphrase);
|
|
|
|
|
if(secret == NULL)
|
|
|
|
|
goto errde3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fatal("Could not determine input file type\n");
|
|
|
|
|
goto errde3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-15 13:10:51 +01:00
|
|
|
Pcpstream *pin = ps_new_file(in);
|
|
|
|
|
Pcpstream *pout = ps_new_file(out);
|
|
|
|
|
|
2014-01-22 23:20:30 +01:00
|
|
|
if(symkey == NULL)
|
2014-02-15 13:10:51 +01:00
|
|
|
dlen = pcp_decrypt_stream(pin, pout, secret, NULL, verify);
|
2014-01-22 23:20:30 +01:00
|
|
|
else
|
2014-02-15 13:10:51 +01:00
|
|
|
dlen = pcp_decrypt_stream(pin, pout, NULL, symkey, verify);
|
|
|
|
|
|
|
|
|
|
ps_close(pin);
|
|
|
|
|
ps_close(pout);
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
if(dlen > 0) {
|
2014-01-27 16:12:43 +01:00
|
|
|
if(verify)
|
|
|
|
|
fprintf(stderr, "Decrypted and Verified %ld bytes successfully\n", dlen);
|
|
|
|
|
else
|
|
|
|
|
fprintf(stderr, "Decrypted %ld bytes successfully\n", dlen);
|
2014-01-20 10:16:05 +01:00
|
|
|
return 0;
|
2013-11-02 11:02:36 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
errde3:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-27 16:12:43 +01:00
|
|
|
int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *recipient, int signcrypt) {
|
2013-10-28 22:50:05 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
2014-02-05 20:41:16 +01:00
|
|
|
pcp_pubkey_t *pubhash = NULL; /* FIXME: add free() */
|
2014-01-20 16:07:01 +01:00
|
|
|
pcp_pubkey_t *tmp = NULL;
|
2013-11-29 20:01:42 +01:00
|
|
|
pcp_pubkey_t *pub = NULL;
|
2013-10-28 22:50:05 +01:00
|
|
|
pcp_key_t *secret = NULL;
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *symkey = NULL;
|
2014-01-20 10:16:05 +01:00
|
|
|
int self = 0;
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-22 23:20:30 +01:00
|
|
|
if(id == NULL && recipient == NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* self mode */
|
2014-01-22 23:20:30 +01:00
|
|
|
self = 1;
|
|
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
|
|
|
|
pcp_readpass(&passphrase,
|
|
|
|
|
"Enter passphrase for symetric encryption", "Repeat passphrase", 1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
passphrase = ucmalloc(strlen(passwd)+1);
|
|
|
|
|
strncpy(passphrase, passwd, strlen(passwd)+1);
|
|
|
|
|
}
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *salt = ucmalloc(90); /* FIXME: use random salt, concat it with result afterwards */
|
2014-01-22 23:20:30 +01:00
|
|
|
char stsalt[] = PBP_COMPAT_SALT;
|
|
|
|
|
memcpy(salt, stsalt, 90);
|
2014-02-03 12:19:17 +01:00
|
|
|
symkey = pcp_scrypt(passphrase, crypto_secretbox_KEYBYTES, salt, 90);
|
2014-01-22 23:20:30 +01:00
|
|
|
free(salt);
|
|
|
|
|
}
|
2014-02-05 13:09:20 +01:00
|
|
|
else if(id != NULL && recipient == NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* lookup by id */
|
2014-01-20 16:07:01 +01:00
|
|
|
HASH_FIND_STR(pcppubkey_hash, id, tmp);
|
|
|
|
|
if(tmp == NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* self-encryption: look if its a secret one */
|
2014-01-20 16:07:01 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2013-11-07 13:36:02 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* found one by id, copy into local hash */
|
2014-01-21 16:11:04 +01:00
|
|
|
pub = ucmalloc(sizeof(pcp_pubkey_t));
|
2014-01-20 16:07:01 +01:00
|
|
|
memcpy(pub, tmp, sizeof(pcp_pubkey_t));
|
|
|
|
|
HASH_ADD_STR( pubhash, id, tmp);
|
2013-11-07 13:36:02 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
2014-01-20 16:07:01 +01:00
|
|
|
else if(recipient != NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* lookup by recipient list */
|
|
|
|
|
/* iterate through global hashlist */
|
|
|
|
|
/* copy matches into temporary pubhash */
|
2014-01-21 16:11:04 +01:00
|
|
|
plist_t *rec;
|
|
|
|
|
pcphash_iteratepub(tmp) {
|
|
|
|
|
rec = recipient->first;
|
2014-02-05 13:09:20 +01:00
|
|
|
while (rec != NULL) {
|
2014-01-21 16:11:04 +01:00
|
|
|
_lc(rec->value);
|
|
|
|
|
if(strnstr(tmp->mail, rec->value, 255) != NULL || strnstr(tmp->owner, rec->value, 255) != NULL) {
|
|
|
|
|
pub = ucmalloc(sizeof(pcp_pubkey_t));
|
|
|
|
|
memcpy(pub, tmp, sizeof(pcp_pubkey_t));
|
|
|
|
|
HASH_ADD_STR( pubhash, id, tmp);
|
2014-02-05 20:41:16 +01:00
|
|
|
/* fprintf(stderr, " => found a matching key %s\n", tmp->id); */
|
2014-01-21 16:11:04 +01:00
|
|
|
}
|
|
|
|
|
rec = rec->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(HASH_COUNT(pubhash) == 0) {
|
|
|
|
|
fatal("no matching key found for specified recipient(s)!\n");
|
|
|
|
|
goto erren3;
|
|
|
|
|
}
|
2014-01-20 16:07:01 +01:00
|
|
|
}
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-22 23:20:30 +01:00
|
|
|
if(self != 1) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* we're using a random secret keypair on our side */
|
2014-01-22 16:23:30 +01:00
|
|
|
#ifdef PCP_ASYM_ADD_SENDER_PUB
|
2014-01-22 23:20:30 +01:00
|
|
|
secret = pcpkey_new();
|
2014-01-22 16:23:30 +01:00
|
|
|
#else
|
2014-01-22 23:20:30 +01:00
|
|
|
secret = pcp_find_primary_secret();
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal("Could not find a secret key in vault %s!\n", id, vault->filename);
|
|
|
|
|
goto erren2;
|
2014-01-22 16:23:30 +01:00
|
|
|
}
|
2014-01-22 23:20:30 +01:00
|
|
|
|
|
|
|
|
if(secret->secret[0] == 0) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* encrypted, decrypt it */
|
2014-01-22 23:20:30 +01:00
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
|
|
|
|
pcp_readpass(&passphrase,
|
|
|
|
|
"Enter passphrase to decrypt your secret key", NULL, 1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
passphrase = ucmalloc(strlen(passwd)+1);
|
|
|
|
|
strncpy(passphrase, passwd, strlen(passwd)+1);
|
|
|
|
|
}
|
|
|
|
|
secret = pcpkey_decrypt(secret, passphrase);
|
|
|
|
|
if(secret == NULL)
|
|
|
|
|
goto erren2;
|
2014-01-22 16:23:30 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
|
|
|
|
if(infile == NULL)
|
|
|
|
|
in = stdin;
|
|
|
|
|
else {
|
|
|
|
|
if((in = fopen(infile, "rb")) == NULL) {
|
|
|
|
|
fatal("Could not open input file %s\n", infile);
|
2014-01-20 16:07:01 +01:00
|
|
|
goto erren2;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(outfile == NULL)
|
|
|
|
|
out = stdout;
|
|
|
|
|
else {
|
|
|
|
|
if((out = fopen(outfile, "wb+")) == NULL) {
|
|
|
|
|
fatal("Could not open output file %s\n", outfile);
|
2014-01-20 16:07:01 +01:00
|
|
|
goto erren2;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
size_t clen = 0;
|
2014-01-22 23:20:30 +01:00
|
|
|
|
2014-02-15 13:10:51 +01:00
|
|
|
Pcpstream *pin = ps_new_file(in);
|
|
|
|
|
Pcpstream *pout = ps_new_file(out);
|
|
|
|
|
|
2014-01-22 23:20:30 +01:00
|
|
|
if(self == 1)
|
2014-02-15 13:10:51 +01:00
|
|
|
clen = pcp_encrypt_stream_sym(pin, pout, symkey, 0, NULL);
|
2014-01-22 23:20:30 +01:00
|
|
|
else
|
2014-02-15 13:10:51 +01:00
|
|
|
clen = pcp_encrypt_stream(pin, pout, secret, pubhash, signcrypt);
|
|
|
|
|
|
|
|
|
|
ps_close(pin);
|
|
|
|
|
ps_close(pout);
|
2014-01-19 23:57:11 +01:00
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
if(clen > 0) {
|
2014-01-22 23:20:30 +01:00
|
|
|
if(id == NULL && recipient == NULL)
|
|
|
|
|
fprintf(stderr, "Encrypted %ld bytes symetrically\n", clen);
|
|
|
|
|
else if(id != NULL)
|
|
|
|
|
fprintf(stderr, "Encrypted %ld bytes for 0x%s successfully\n", clen, id);
|
2014-01-21 16:11:04 +01:00
|
|
|
else {
|
2014-01-22 23:20:30 +01:00
|
|
|
fprintf(stderr, "Encrypted %ld bytes for:\n", clen);
|
2014-01-21 16:11:04 +01:00
|
|
|
pcp_pubkey_t *cur, *t;
|
|
|
|
|
HASH_ITER(hh, pubhash, cur, t) {
|
|
|
|
|
fprintf(stderr, "%s <%s>\n", cur->owner, cur->mail);
|
|
|
|
|
}
|
|
|
|
|
free(t);
|
|
|
|
|
free(cur);
|
|
|
|
|
}
|
2014-01-27 16:12:43 +01:00
|
|
|
if(signcrypt)
|
|
|
|
|
fprintf(stderr, "Signed encrypted file successfully\n");
|
2014-01-20 10:16:05 +01:00
|
|
|
return 0;
|
2014-01-19 23:57:11 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-20 16:07:01 +01:00
|
|
|
erren2:
|
2014-02-05 20:41:16 +01:00
|
|
|
free(pubhash); /* FIXME: it's a uthash, dont use free() but func instead */
|
2014-01-20 16:07:01 +01:00
|
|
|
free(tmp);
|
|
|
|
|
free(pub);
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
erren3:
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|