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-26 16:02:47 +01:00
|
|
|
|
|
|
|
|
int pcpsign(char *infile, char *outfile, char *passwd, int z85, int detach) {
|
2013-11-08 09:40:51 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
|
|
|
|
pcp_key_t *secret = NULL;
|
|
|
|
|
|
|
|
|
|
secret = pcp_find_primary_secret();
|
2014-01-26 11:57:23 +01:00
|
|
|
|
2013-11-08 09:40:51 +01:00
|
|
|
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) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* encrypted, decrypt it */
|
2013-11-08 09:40:51 +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)
|
2014-01-23 15:40:06 +01:00
|
|
|
goto errs1;
|
2013-11-08 09:40:51 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-26 16:02:47 +01:00
|
|
|
size_t sigsize;
|
|
|
|
|
if(detach == 1)
|
|
|
|
|
sigsize = pcp_ed_detachsign_buffered(in, out, secret);
|
|
|
|
|
else
|
|
|
|
|
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-26 16:02:47 +01:00
|
|
|
int pcpverify(char *infile, char *sigfile, char *id, int detach) {
|
2013-11-08 09:40:51 +01:00
|
|
|
FILE *in = NULL;
|
2014-01-26 16:02:47 +01:00
|
|
|
FILE *sigfd = NULL;
|
2013-11-29 20:01:42 +01:00
|
|
|
pcp_pubkey_t *pub = 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-26 16:02:47 +01:00
|
|
|
if(sigfile != NULL) {
|
|
|
|
|
if((sigfd = fopen(sigfile, "rb")) == NULL) {
|
|
|
|
|
fatal("Could not open signature file %s\n", sigfile);
|
|
|
|
|
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-26 16:02:47 +01:00
|
|
|
if(detach)
|
|
|
|
|
pub = pcp_ed_detachverify_buffered(in, sigfd, pub);
|
|
|
|
|
else
|
2014-01-28 12:19:25 +01:00
|
|
|
pub = pcp_ed_verify_buffered(in, pub);
|
2014-01-26 16:02:47 +01:00
|
|
|
|
|
|
|
|
if(pub != NULL)
|
|
|
|
|
fprintf(stderr, "Signature verified (signed by %s <%s>).\n", pub->owner, pub->mail);
|
|
|
|
|
|
2013-11-08 09:40:51 +01:00
|
|
|
|
|
|
|
|
errv1:
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2013-11-19 17:17:30 +01:00
|
|
|
|