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"
|
|
|
|
|
|
|
|
|
|
int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd) {
|
|
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
|
|
|
|
pcp_key_t *secret = NULL;
|
|
|
|
|
|
|
|
|
|
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(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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(secret->secret[0] == 0) {
|
|
|
|
|
// encrypted, decrypt it
|
|
|
|
|
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 errde3;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
size_t dlen = pcp_decrypt_file(in, out, secret);
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
if(dlen > 0) {
|
2014-01-19 23:57:11 +01:00
|
|
|
fprintf(stderr, "Decrypted %d bytes successfully\n",
|
|
|
|
|
(int)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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-02 11:02:36 +01:00
|
|
|
int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, char *recipient) {
|
2013-10-28 22:50:05 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = 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-01-20 10:16:05 +01:00
|
|
|
int self = 0;
|
2013-10-28 22:50:05 +01:00
|
|
|
|
|
|
|
|
// look if we've got that key
|
2013-11-29 20:01:42 +01:00
|
|
|
HASH_FIND_STR(pcppubkey_hash, id, pub);
|
|
|
|
|
if(pub == NULL) {
|
2014-01-19 23:57:11 +01:00
|
|
|
// FIXME: use recipient to lookup by name or email
|
2013-11-07 13:36:02 +01:00
|
|
|
// self-encryption: look if its a secret one
|
|
|
|
|
pcp_key_t *s = NULL;
|
|
|
|
|
HASH_FIND_STR(pcpkey_hash, id, s);
|
|
|
|
|
if(s != NULL) {
|
2013-11-29 20:01:42 +01:00
|
|
|
pub = pcpkey_pub_from_secret(s);
|
2014-01-20 10:16:05 +01:00
|
|
|
self = 1;
|
2013-11-07 13:36:02 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fatal("Could not find a public key with id 0x%s in vault %s!\n",
|
2013-10-28 22:50:05 +01:00
|
|
|
id, vault->filename);
|
2013-11-07 13:36:02 +01:00
|
|
|
goto erren3;
|
|
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-19 23:57:11 +01:00
|
|
|
secret = pcpkey_new(); // DEPRECATED: pcp_find_primary_secret();
|
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 erren3;
|
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 erren3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
|
fprintf(stderr, "Using secret key:\n");
|
2013-11-12 16:58:59 +01:00
|
|
|
pcp_dumpkey(secret);
|
2013-10-28 22:50:05 +01:00
|
|
|
fprintf(stderr, "Using publickey:\n");
|
2013-11-29 20:01:42 +01:00
|
|
|
pcp_dumppubkey(pub);
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
size_t clen = pcp_encrypt_file(in, out, secret, pub, self);
|
2014-01-19 23:57:11 +01:00
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
if(clen > 0) {
|
|
|
|
|
fprintf(stderr, "Encrypted %d bytes for 0x%s successfully\n", (int)clen, id);
|
|
|
|
|
return 0;
|
2014-01-19 23:57:11 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
|
|
|
|
erren3:
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|