diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cipherpack/cipherpack.cpp | 92 |
1 files changed, 51 insertions, 41 deletions
diff --git a/examples/cipherpack/cipherpack.cpp b/examples/cipherpack/cipherpack.cpp index 8f16576..f3c3a02 100644 --- a/examples/cipherpack/cipherpack.cpp +++ b/examples/cipherpack/cipherpack.cpp @@ -18,64 +18,74 @@ extern "C" { using namespace elevator; -static const std::string opt_deconly("-deconly"); +static void print_usage(const char* progname) { + fprintf(stderr, "Usage %s pack <enc-pub-key> <sign-sec-key> <sign-sec-key-passphrase> <input-filename> <header-filename> " + "<file-version> <file-version-parent> <output-filename>\n", progname); + fprintf(stderr, "Usage %s unpack <sign-pub-key> <dec-sec-key> <dec-sec-key-passphrase> <input-source> <output-filename>\n", progname); +} int main(int argc, char *argv[]) { - bool dec_only = false; - std::string fname_payload; - fprintf(stderr, "Called Elevate::Crypt %s with %d arguments: ", (argc>0?argv[0]:"exe"), argc-1); + fprintf(stderr, "Called %s with %d arguments: ", (argc>0?argv[0]:"exe"), argc-1); for(int i=0; i<argc; i++) { fprintf(stderr, "%s ", argv[i]); } fprintf(stderr, "\n"); - argc--; // main int argi = 0; - if( 0 == argc ) { - fprintf(stderr, "Elevate::Crypt Usage %s [-deconly] <input>\n", argv[0]); + if( 1+1 >= argc ) { + print_usage(argv[0]); return -1; } - if( 1 <= argc && opt_deconly== argv[argi+1] ) { - dec_only = true; - fprintf(stderr, "Called Elevate::Crypt %s decrypt-only\n", argv[0]); - argi++; - argc--; - } - if( 1 <= argc ) { - fname_payload = argv[++argi]; - fprintf(stderr, "Called Elevate::Crypt %s input %s\n", argv[0], fname_payload.c_str()); - argc--; - } - const bool overwrite = true; - const std::string enc_pub_key_fname("../keys/terminal_rsa.pub.pem"); - const std::string dec_sec_key_fname("../keys/terminal_rsa"); - const std::string dec_sec_key_passphrase(""); - const std::string sign_pub_key_fname("../keys/host_rsa.pub.pem"); - const std::string sign_sec_key_fname("../keys/host_rsa"); - const std::string sign_sec_key_passphrase(""); - const std::string fname_encrypted = dec_only ? fname_payload : fname_payload+".enc"; - const std::string fname_decrypted = dec_only ? "out.dec" : fname_encrypted+".dec"; + const std::string command = argv[++argi]; - bool res_enc; - if( dec_only ) { - res_enc = true; - } else { - res_enc = Cipherpack::encryptThenSign_RSA1(enc_pub_key_fname, sign_sec_key_fname, sign_sec_key_passphrase, fname_payload, fname_encrypted, overwrite); - jau::PLAIN_PRINT(true, "Encrypt1 result: Output encrypted file %s: Result %d\n", fname_encrypted.c_str(), res_enc); + if( command == "pack") { + if( 8+1+1 != argc ) { + print_usage(argv[0]); + return -1; + } + const std::string enc_pub_key_fname = argv[++argi]; + const std::string sign_sec_key_fname = argv[++argi]; + std::string sign_sec_key_passphrase = argv[++argi]; + const std::string fname_input = argv[++argi]; + const std::string fname_header = argv[++argi]; + uint64_t payload_version = (uint64_t)atoll(argv[++argi]); + uint64_t payload_version_parent = (uint64_t)atoll(argv[++argi]); + const std::string fname_output = argv[++argi]; + + Cipherpack::PackInfo pinfo = Cipherpack::encryptThenSign_RSA1(enc_pub_key_fname, sign_sec_key_fname, sign_sec_key_passphrase, + fname_input, fname_header, + payload_version, payload_version_parent, + fname_output, overwrite); + jau::PLAIN_PRINT(true, "Pack: Encrypted %s to %s\n", fname_input.c_str(), fname_output.c_str()); + jau::PLAIN_PRINT(true, "Pack: %s\n", pinfo.toString().c_str()); + return pinfo.isValid() ? 0 : -1; } - if( res_enc ) { + if( command == "unpack") { + if( 5+1+1 != argc ) { + print_usage(argv[0]); + return -1; + } + const std::string sign_pub_key_fname = argv[++argi]; + const std::string dec_sec_key_fname = argv[++argi]; + std::string dec_sec_key_passphrase = argv[++argi]; + const std::string source = argv[++argi]; + const std::string fname_output = argv[++argi]; + std::unique_ptr<Botan::DataSource> enc_stream; - const std::string proto = fname_encrypted.substr(0, 5); + const std::string proto = source.substr(0, 5); if( proto == "http:" ) { - enc_stream = std::make_unique<DataSource_Http>(fname_encrypted); + enc_stream = std::make_unique<DataSource_Http>(source); } else { - enc_stream = std::make_unique<Botan::DataSource_Stream>(fname_encrypted, true /* use_binary */); + enc_stream = std::make_unique<Botan::DataSource_Stream>(source, true /* use_binary */); } - bool res_dec = Cipherpack::checkSignThenDecrypt_RSA1(sign_pub_key_fname, dec_sec_key_fname, dec_sec_key_passphrase, *enc_stream, fname_decrypted, overwrite); - jau::PLAIN_PRINT(true, "Decrypted1 result: Output decrypted file %s: Result %d\n", fname_decrypted.c_str(), res_dec); + Cipherpack::PackInfo pinfo = Cipherpack::checkSignThenDecrypt_RSA1(sign_pub_key_fname, dec_sec_key_fname, dec_sec_key_passphrase, + *enc_stream, fname_output, overwrite); + // dec_sec_key_passphrase.resize(0); + jau::PLAIN_PRINT(true, "Unpack: Decypted %s to %s\n", source.c_str(), fname_output.c_str()); + jau::PLAIN_PRINT(true, "Unpack: %s\n", pinfo.toString().c_str()); + return pinfo.isValid() ? 0 : -1; } - - return 0; + return -1; } |