2013-11-08 09:40:51 +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>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "signature.h"
|
|
|
|
|
#include "defines.h"
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
int pcpsign(char *infile, char *outfile, char *passwd, int z85) {
|
2013-11-08 09:40:51 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
|
|
|
|
pcp_key_t *secret = NULL;
|
|
|
|
|
|
|
|
|
|
secret = pcp_find_primary_secret();
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal("Could not find a secret key in vault %s!\n", vault->filename);
|
|
|
|
|
goto errs1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(infile == NULL)
|
|
|
|
|
in = stdin;
|
|
|
|
|
else {
|
|
|
|
|
if((in = fopen(infile, "rb")) == NULL) {
|
|
|
|
|
fatal("Could not open input file %s\n", infile);
|
2014-01-23 15:40:06 +01:00
|
|
|
goto errs1;
|
2013-11-08 09:40:51 +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-23 15:40:06 +01:00
|
|
|
goto errs1;
|
2013-11-08 09:40:51 +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)
|
2014-01-23 15:40:06 +01:00
|
|
|
goto errs1;
|
2013-11-08 09:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
size_t sigsize = pcp_ed_sign_buffered(in, out, secret, z85);
|
2013-11-19 17:17:30 +01:00
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
if(sigsize == 0)
|
|
|
|
|
goto errs1;
|
2013-11-08 09:40:51 +01:00
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
fprintf(stderr, "Signed %ld bytes successfully\n", sigsize);
|
2013-11-08 09:40:51 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
errs1:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
int pcpverify(char *infile, char *id) {
|
2013-11-08 09:40:51 +01:00
|
|
|
FILE *in = NULL;
|
2013-11-29 20:01:42 +01:00
|
|
|
pcp_pubkey_t *pub = NULL;
|
2014-01-23 15:40:06 +01:00
|
|
|
unsigned char *message = NULL;
|
2013-11-08 09:40:51 +01:00
|
|
|
|
|
|
|
|
if(infile == NULL)
|
|
|
|
|
in = stdin;
|
|
|
|
|
else {
|
|
|
|
|
if((in = fopen(infile, "rb")) == NULL) {
|
|
|
|
|
fatal("Could not open input file %s\n", infile);
|
|
|
|
|
goto errv1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
if(id != NULL)
|
|
|
|
|
HASH_FIND_STR(pcppubkey_hash, id, pub);
|
2013-11-08 09:40:51 +01:00
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
if(pub != NULL) {
|
2014-01-23 23:36:57 +01:00
|
|
|
message = pcp_ed_verify_buffered(in, pub);
|
2014-01-23 15:40:06 +01:00
|
|
|
if(message != NULL) {
|
|
|
|
|
fprintf(stderr, "Signature verified (signed by %s <%s>).\n", pub->owner, pub->mail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2014-01-23 23:36:57 +01:00
|
|
|
// put public key as pub, so verify iterates over our keys
|
|
|
|
|
message = pcp_ed_verify_buffered(in, pub);
|
|
|
|
|
if(message != NULL) {
|
|
|
|
|
fprintf(stderr, "Signature verified (signed by %s <%s>).\n", pub->owner, pub->mail);
|
2014-01-23 15:40:06 +01:00
|
|
|
}
|
2013-11-08 09:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
if(message == NULL) {
|
2014-01-23 23:36:57 +01:00
|
|
|
fprintf(stderr, "Could not verify signature\n");
|
2014-01-23 15:40:06 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
free(message);
|
|
|
|
|
|
2013-11-08 09:40:51 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
errv4:
|
|
|
|
|
|
|
|
|
|
errv1:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2013-11-19 17:17:30 +01:00
|
|
|
|