1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-25 16:28:15 +00:00
codecrypt/src/main.cpp

523 lines
13 KiB
C++
Raw Normal View History

2012-03-24 20:13:30 +00:00
2012-11-05 21:45:35 +00:00
/*
* This file is part of Codecrypt.
*
2017-06-26 12:28:54 +00:00
* Copyright (C) 2013-2017 Mirek Kratochvil <exa.exa@gmail.com>
2016-04-17 13:47:47 +00:00
*
2012-11-05 21:45:35 +00:00
* Codecrypt is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Codecrypt 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
2013-04-20 21:08:09 +00:00
#include "iohelpers.h"
2013-01-11 10:33:17 +00:00
void print_version()
{
out ("codecrypt " PACKAGE_VERSION);
2017-06-26 12:28:54 +00:00
out ("Copyright (C) 2013-2017 Mirek Kratochvil <exa.exa@gmail.com>");
2013-04-19 15:23:28 +00:00
out ("This is free software; see the source "
"for copying conditions. There is NO");
out ("warranty; not even for MERCHANTABILITY "
"or FITNESS FOR A PARTICULAR PURPOSE.");
2013-01-11 10:33:17 +00:00
}
void print_help (char*pname)
{
print_version();
outeol;
out ("Usage: " << pname << " [options]");
outeol;
2013-04-19 15:23:28 +00:00
out ("Common options:");
out (" -h, --help display this help");
out (" -V, --version display version information");
out (" -T, --test perform (probably nonexistent) testing/debugging stuff");
outeol;
out ("Global options:");
2017-09-10 15:29:23 +00:00
out (" -R, --in set input file, default is stdin");
out (" -o, --out set output file, default is stdout");
out (" -E, --err the same for stderr");
2013-04-19 15:23:28 +00:00
out (" -a, --armor use ascii-armored I/O");
out (" -y, --yes assume that answer is `yes' everytime");
outeol;
out ("Actions:");
out (" -s, --sign sign a message");
out (" -v, --verify verify a signed message");
out (" -e, --encrypt encrypt a message");
out (" -d, --decrypt decrypt an encrypted message");
outeol;
out ("Action options:");
out (" -r, --recipient encrypt for given user");
out (" -u, --user use specified secret key");
out (" -C, --clearsign work with cleartext signatures");
out (" -b, --detach-sign specify file with detached signature");
2014-03-07 07:44:23 +00:00
out (" -S, --symmetric enable symmetric mode of operation where encryption");
out (" is done using symmetric cipher and signatures are");
out (" hashes, and specify a filename of symmetric key or hashes");
2013-04-19 15:23:28 +00:00
outeol;
out ("Key management:");
2016-04-17 13:09:26 +00:00
out (" -g, --gen-key generate keys for specified algorithm");
out (" -g help list available cryptographic algorithms");
out (" -k, --list list the contents of keyring");
2013-04-19 15:23:28 +00:00
out (" -K, --list-secret");
2013-04-26 22:56:34 +00:00
out (" -i, --import import keys");
2013-04-19 15:23:28 +00:00
out (" -I, --import-secret");
2013-04-26 22:56:34 +00:00
out (" -p, --export export keys");
2013-04-19 15:23:28 +00:00
out (" -P, --export-secret");
out (" -x, --delete delete matching keys");
out (" -X, --delete-secret");
out (" -m, --rename rename matching keys");
out (" -M, --rename-secret");
2017-09-10 15:29:23 +00:00
out (" -L, --lock lock secrets");
out (" -U, --unlock unlock secrets");
2013-04-19 15:23:28 +00:00
outeol;
out ("Key management options:");
2013-04-20 09:58:28 +00:00
out (" -F, --filter only work with keys with matching names");
2013-04-26 22:56:34 +00:00
out (" -f, --fingerprint format full key IDs nicely for human eyes");
2017-09-10 15:29:23 +00:00
out (" -N, --name specify a new name for renaming or importing");
out (" -n, --no-action on import, only show what would be imported");
out (" -w, --with-lock specify the symmetric key for (un)locking the secrets");
2017-10-23 12:54:18 +00:00
out (" -w @SPEC ask for password and expand it to a symmetric key");
out(" of type SPEC for (un)locking the secret");
2017-09-10 15:29:23 +00:00
outeol;
out (" With -S and -w, using `@' as the key file name will cause the program to");
out (" interactively ask for a password and derive the symmetric key from it.");
2013-04-19 15:23:28 +00:00
outeol;
out ("Codecrypt eats data. Use it with caution.");
outeol;
2013-01-13 22:37:58 +00:00
}
/*
* testing
*/
void test()
{
2013-04-17 07:33:22 +00:00
/*
* Dear hacker,
* use this function for quicktesting your stuff.
* It gets executed by the -T parameter.
2013-04-17 07:33:22 +00:00
* Other places suck for that purpose.
*/
2013-01-11 10:33:17 +00:00
}
2013-01-13 22:37:58 +00:00
/*
* main module. Parse options, fire up stuff, pass commands to it.
*/
2013-01-11 10:33:17 +00:00
#include <getopt.h>
2017-10-23 12:33:47 +00:00
#include <stdlib.h>
#include "actions.h"
#include "algo_suite.h"
2013-04-17 07:33:22 +00:00
2013-01-11 10:33:17 +00:00
int main (int argc, char**argv)
2012-03-24 20:13:30 +00:00
{
//option variables
2013-04-18 08:45:05 +00:00
bool do_help = false,
do_version = false,
do_test = false,
has_opt = false,
opt_armor = false,
2013-04-20 09:58:28 +00:00
opt_yes = false,
2013-04-18 08:45:05 +00:00
opt_fingerprint = false,
opt_clearsign = false,
2013-04-18 08:45:05 +00:00
opt_import_no_action = false;
std::string recipient, user,
input, output, err_output,
2013-04-20 09:58:28 +00:00
name, filter,
2017-09-10 15:29:23 +00:00
withlock,
2013-04-20 09:58:28 +00:00
action_param,
2014-03-07 07:44:23 +00:00
detach_sign,
symmetric;
2013-01-11 10:33:17 +00:00
2013-04-20 09:58:28 +00:00
char action = 0;
2013-01-11 10:33:17 +00:00
int c, option_index;
for (;;) {
static struct option long_opts[] = {
{"help", 0, 0, 'h' },
{"version", 0, 0, 'V' },
2013-04-18 06:49:09 +00:00
{"test", 0, 0, 'T' },
//global options
{"armor", 0, 0, 'a' },
2013-04-19 15:23:28 +00:00
{"yes", 0, 0, 'y' },
2013-04-18 06:49:09 +00:00
{"recipient", 1, 0, 'r' },
{"user", 1, 0, 'u' },
//I/O redirection from default stdin/out
2013-04-20 09:58:28 +00:00
{"in", 1, 0, 'R' },
{"out", 1, 0, 'o' },
{"err", 1, 0, 'E' },
2013-04-18 06:49:09 +00:00
//keyring management
2013-04-20 09:58:28 +00:00
{"list", 0, 0, 'k' },
{"import", 0, 0, 'i' },
{"export", 0, 0, 'p' },
{"delete", 1, 0, 'x' },
2013-04-20 09:58:28 +00:00
{"rename", 1, 0, 'm' },
2013-04-18 06:49:09 +00:00
2013-04-20 09:58:28 +00:00
{"list-secret", 0, 0, 'K' },
{"import-secret", 0, 0, 'I' },
{"export-secret", 0, 0, 'P' },
{"delete-secret", 1, 0, 'X' },
2013-04-20 09:58:28 +00:00
{"rename-secret", 1, 0, 'M' },
2013-04-18 06:49:09 +00:00
2017-09-10 15:29:23 +00:00
{"lock", 0, 0, 'L' },
{"unlock", 0, 0, 'U' },
{"gen-key", 1, 0, 'g' },
2013-04-18 06:49:09 +00:00
{"name", 1, 0, 'N' },
2013-04-20 09:58:28 +00:00
{"filter", 1, 0, 'F' },
2013-04-18 06:49:09 +00:00
{"fingerprint", 0, 0, 'f' },
2013-04-18 08:45:05 +00:00
{"no-action", 0, 0, 'n' },
2013-04-18 06:49:09 +00:00
2017-09-10 15:29:23 +00:00
{"with-lock", 1, 0, 'w' },
2013-04-18 06:49:09 +00:00
//actions
{"sign", 0, 0, 's' },
{"verify", 0, 0, 'v' },
{"encrypt", 0, 0, 'e' },
{"decrypt", 0, 0, 'd' },
//action options
{"clearsign", 0, 0, 'C' },
2013-04-18 06:49:09 +00:00
{"detach-sign", 1, 0, 'b' },
2014-03-07 07:44:23 +00:00
{"symmetric", 1, 0, 'S' },
2013-04-18 06:49:09 +00:00
2013-01-11 10:33:17 +00:00
{0, 0, 0, 0 }
};
2013-04-20 09:58:28 +00:00
option_index = -1;
c = getopt_long
(argc, argv,
2017-09-10 15:29:23 +00:00
"hVTayr:u:R:o:E:kipx:m:KIPX:M:LUg:N:F:fnw:svedCb:S:",
long_opts, &option_index);
2013-01-11 10:33:17 +00:00
if (c == -1) break;
has_opt = true;
switch (c) {
case '?':
case ':':
case 'h':
do_help = true;
break;
2013-04-20 09:58:28 +00:00
#define read_flag(ch,var) case ch: var=true; break;
#define read_single_opt(ch,var,errmsg) \
case ch: if(var.length()) {progerr(errmsg); do_help=true;}\
else var=optarg; break;
#define read_action(ch) read_action_comb(ch,0,0)
#define read_action_comb(ch, hit, comb) \
case ch: \
if(hit && action==hit) { \
action=comb; \
if(optarg) action_param=optarg; \
} else if(action) { \
progerr("please specify a single action"); \
do_help=true; \
} else { \
action=ch; \
if(optarg) action_param=optarg; \
} break;
read_flag ('V', do_version)
read_flag ('T', do_test)
read_flag ('a', opt_armor)
read_flag ('y', opt_yes)
read_single_opt ('r', recipient,
"specify only one recipient")
read_single_opt ('u', user,
"specify only one local user")
read_single_opt ('R', input,
"cannot accept multiple inputs")
read_single_opt ('o', output,
"cannot accept multiple outputs")
read_single_opt ('E', err_output,
"cannot accept multiple error outputs")
2013-04-20 09:58:28 +00:00
read_action ('k')
read_action ('i')
read_action ('p')
read_action ('x')
read_action ('m')
read_action ('K')
read_action ('I')
read_action ('P')
read_action ('X')
read_action ('M')
2017-09-10 15:29:23 +00:00
read_action ('U')
2013-04-20 09:58:28 +00:00
read_single_opt ('N', name,
2017-09-10 15:29:23 +00:00
"specify a single name")
2013-04-20 09:58:28 +00:00
read_single_opt ('F', filter,
2017-09-10 15:29:23 +00:00
"specify a single filter string")
2013-04-20 09:58:28 +00:00
read_flag ('f', opt_fingerprint)
read_flag ('n', opt_import_no_action)
2017-09-10 15:29:23 +00:00
read_single_opt ('w', withlock,
"specify a single key lock")
2013-04-20 09:58:28 +00:00
/*
* combinations of s+e and d+v are possible. result is
2017-09-10 15:29:23 +00:00
* 'E' = "big encrypt with sig", 'D' "big decrypt
* with verify" and 'G' = "generate and lock"
2013-04-20 09:58:28 +00:00
*/
read_action_comb ('s', 'e', 'E')
read_action_comb ('e', 's', 'E')
2017-09-10 15:29:23 +00:00
read_action_comb ('v', 'd', 'D')
2013-04-20 09:58:28 +00:00
read_action_comb ('d', 'v', 'D')
2017-09-10 15:29:23 +00:00
read_action_comb ('g', 'L', 'G')
read_action_comb ('L', 'g', 'G')
read_flag ('C', opt_clearsign)
2013-04-20 09:58:28 +00:00
read_single_opt ('b', detach_sign,
"specify only one detach-sign file")
2014-03-07 07:44:23 +00:00
read_single_opt ('S', symmetric,
"specify only one symmetric parameter")
2013-04-20 09:58:28 +00:00
#undef read_flag
#undef read_single_opt
#undef read_action
2013-01-11 10:33:17 +00:00
default: //which doesn't just happen.
2013-04-20 09:58:28 +00:00
do_help = true;
2013-01-11 10:33:17 +00:00
break;
}
}
if (optind != argc) {
2013-04-20 21:44:18 +00:00
progerr ("unmatched non-option parameters");
2013-01-11 10:33:17 +00:00
do_help = true;
}
if ( (!has_opt) || do_help) {
print_help (argv[0]);
return 0;
}
if (do_version) {
print_version();
return 0;
}
2013-01-13 22:37:58 +00:00
2013-04-17 07:33:22 +00:00
/*
2016-01-12 22:20:07 +00:00
* initialization
2013-04-17 07:33:22 +00:00
*/
keyring KR;
algorithm_suite AS;
2013-04-17 07:33:22 +00:00
//register all available algorithms
fill_algorithm_suite (AS);
2017-10-23 12:33:47 +00:00
//default local user key from environment
if(user.empty()) {
const char*u=getenv("CCR_USER");
if(u) user=u;
}
2013-04-20 21:08:09 +00:00
/*
* cin/cout redirection
*/
int exitval = 0;
//handle the defaults
if (input == "-") input = "/dev/stdin";
if (output == "-") output = "/dev/stdout";
if (err_output == "-") err_output = "/dev/stderr";
//do the redirections
2015-10-31 21:58:17 +00:00
if (input.length() && !redirect_cin (input)) {
2013-04-20 21:08:09 +00:00
progerr ("could not open input file");
exitval = 1;
goto exit;
}
2015-10-31 21:58:17 +00:00
if (output.length() && !redirect_cout (output)) {
2013-04-20 21:08:09 +00:00
progerr ("could not redirect to output file");
exitval = 1;
goto exit;
}
if (err_output.length() && !redirect_cerr (err_output)) {
progerr ("could not redirect to error output file");
exitval = 1;
goto exit;
}
2013-04-17 07:33:22 +00:00
/*
* check the option flags and do whatever was requested
*/
2013-01-13 22:37:58 +00:00
if (do_test) {
test();
2013-04-20 21:08:09 +00:00
goto exit;
2013-04-17 07:33:22 +00:00
}
2015-10-31 21:58:17 +00:00
if (symmetric.length()) switch (action) {
2014-04-06 11:46:04 +00:00
case 'e':
2017-09-10 15:29:23 +00:00
case 'd':
2014-03-07 07:44:23 +00:00
case 's':
case 'v':
2017-09-10 15:29:23 +00:00
case 'g':
case 'G':
case 'L':
case 'U':
2014-03-07 07:44:23 +00:00
break;
default:
2017-09-10 15:29:23 +00:00
progerr ("specified action doesn't support"
" symmetric operation");
2014-03-07 07:44:23 +00:00
exitval = 1;
goto exit;
}
2013-04-20 12:19:32 +00:00
switch (action) {
case 'g':
2014-04-06 11:46:04 +00:00
exitval = action_gen_key (action_param, name,
2017-09-10 15:29:23 +00:00
symmetric, withlock,
opt_armor, false,
KR, AS);
break;
case 'G':
exitval = action_gen_key (action_param, name,
symmetric, withlock,
opt_armor, true,
2014-04-06 11:46:04 +00:00
KR, AS);
2013-04-20 12:19:32 +00:00
break;
case 'e':
2014-04-06 11:46:04 +00:00
exitval = action_encrypt (recipient, opt_armor, symmetric,
2017-09-10 15:29:23 +00:00
withlock, KR, AS);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'd':
2017-09-10 15:29:23 +00:00
exitval = action_decrypt (opt_armor, symmetric, withlock,
KR, AS);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 's':
2013-04-22 05:38:21 +00:00
exitval = action_sign (user, opt_armor, detach_sign,
2017-09-10 15:29:23 +00:00
opt_clearsign, symmetric, withlock, KR, AS);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'v':
2014-03-07 07:44:23 +00:00
exitval = action_verify (opt_armor, detach_sign, opt_clearsign,
2017-09-10 15:29:23 +00:00
opt_yes, symmetric, withlock, KR, AS);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'E':
2017-09-10 15:29:23 +00:00
exitval = action_sign_encrypt (user, recipient, withlock,
opt_armor, KR, AS);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'D':
2017-09-10 15:29:23 +00:00
exitval = action_decrypt_verify (opt_armor, opt_yes, withlock,
2013-04-22 07:23:47 +00:00
KR, AS);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'k':
2013-04-20 20:39:51 +00:00
exitval = action_list (opt_fingerprint, filter, KR);
break;
2013-04-20 12:19:32 +00:00
case 'i':
2013-04-20 20:39:51 +00:00
exitval = action_import (opt_armor, opt_import_no_action,
2013-04-21 15:47:00 +00:00
opt_yes, opt_fingerprint,
filter, name, KR);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'p':
2013-04-20 20:39:51 +00:00
exitval = action_export (opt_armor, filter, name, KR);
break;
2013-04-20 12:19:32 +00:00
case 'x':
2013-04-20 20:39:51 +00:00
exitval = action_delete (opt_yes, action_param, KR);
break;
2013-04-20 12:19:32 +00:00
case 'm':
2013-04-20 20:39:51 +00:00
exitval = action_rename (opt_yes, action_param, name, KR);
break;
2013-04-20 12:19:32 +00:00
case 'K':
2013-04-20 20:39:51 +00:00
exitval = action_list_sec (opt_fingerprint, filter, KR);
break;
2013-04-20 12:19:32 +00:00
case 'I':
2013-04-20 20:39:51 +00:00
exitval = action_import_sec (opt_armor, opt_import_no_action,
2013-04-21 15:47:00 +00:00
opt_yes, opt_fingerprint,
filter, name, KR);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'P':
2013-04-21 15:47:00 +00:00
exitval = action_export_sec (opt_armor, opt_yes,
filter, name, KR);
2013-04-20 20:39:51 +00:00
break;
2013-04-20 12:19:32 +00:00
case 'X':
2013-04-20 20:39:51 +00:00
exitval = action_delete_sec (opt_yes, action_param, KR);
break;
2013-04-20 12:19:32 +00:00
case 'M':
2013-04-20 20:39:51 +00:00
exitval = action_rename_sec (opt_yes, action_param, name, KR);
break;
2013-04-20 12:19:32 +00:00
2017-09-10 15:29:23 +00:00
case 'L':
exitval = action_lock_sec (opt_yes, filter,
symmetric, withlock,
2017-09-10 15:29:23 +00:00
opt_armor, KR);
break;
case 'U':
exitval = action_unlock_sec (opt_yes, filter,
symmetric, withlock,
2017-09-10 15:29:23 +00:00
opt_armor, KR);
break;
2013-04-20 12:19:32 +00:00
default:
progerr ("no action specified, use `--help'");
exitval = 1;
break;
}
2013-04-17 07:33:22 +00:00
/*
* all done.
* keyring is _not_ automatically saved here to prevent frequent
* rewriting and due the fact that everything that modifies it _must_
* also ensure and verify that it was written back correctly.
*/
2013-04-20 21:08:09 +00:00
exit:
2015-10-31 21:58:17 +00:00
if (!KR.close()) {
2013-04-20 21:44:18 +00:00
progerr ("could not close keyring, "
"something weird is going to happen.");
2013-01-13 22:37:58 +00:00
}
2013-04-20 12:19:32 +00:00
return exitval;
2012-11-05 21:14:48 +00:00
}
2012-05-22 20:48:48 +00:00