2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2019-01-12 21:43:48 +00:00
|
|
|
// Copyright (C) 2019 University of California
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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.
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2002-09-26 18:11:06 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-09-26 18:11:06 +00:00
|
|
|
|
|
|
|
|
2002-06-14 05:49:34 +00:00
|
|
|
// utility program for encryption.
|
|
|
|
//
|
|
|
|
// -genkey n private_keyfile public_keyfile
|
2002-07-05 05:33:40 +00:00
|
|
|
// create a key pair with n bits (512 <= n <= 1024)
|
2002-06-14 05:49:34 +00:00
|
|
|
// write it in hex notation
|
|
|
|
// -sign file private_keyfile
|
|
|
|
// create a signature for a given file
|
|
|
|
// write it in hex notation
|
2005-12-14 23:43:50 +00:00
|
|
|
// -sign_string string private_keyfile
|
|
|
|
// create a signature for a given string
|
|
|
|
// write it in hex notation
|
2002-06-14 05:49:34 +00:00
|
|
|
// -verify file signature_file public_keyfile
|
2023-07-17 21:40:45 +00:00
|
|
|
// verify a file signature
|
|
|
|
// -verify_string string signature_file public_keyfile
|
|
|
|
// verify a string signature
|
2002-07-05 05:33:40 +00:00
|
|
|
// -test_crypt private_keyfile public_keyfile
|
2002-06-14 05:49:34 +00:00
|
|
|
// test encrypt/decrypt
|
2008-09-04 15:13:54 +00:00
|
|
|
// -convkey o2b/b2o priv/pub input_file output_file
|
|
|
|
// convert keys between BOINC and OpenSSL format
|
|
|
|
// -cert_verify file signature_file certificate_dir
|
|
|
|
// verify a signature using a directory of certificates
|
2002-06-14 05:49:34 +00:00
|
|
|
|
2006-02-16 04:05:38 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2006-02-16 04:05:38 +00:00
|
|
|
#endif
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2005-08-01 22:44:40 +00:00
|
|
|
#include <cstring>
|
2002-06-14 05:49:34 +00:00
|
|
|
|
2008-09-04 15:13:54 +00:00
|
|
|
#include "openssl/bio.h"
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/conf.h>
|
|
|
|
#include <openssl/engine.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2002-06-14 05:49:34 +00:00
|
|
|
#include "crypt.h"
|
2008-09-04 15:13:54 +00:00
|
|
|
#include "md5_file.h"
|
2002-06-14 05:49:34 +00:00
|
|
|
|
2005-02-16 23:17:43 +00:00
|
|
|
void die(const char* p) {
|
2002-07-09 00:10:58 +00:00
|
|
|
fprintf(stderr, "Error: %s\n", p);
|
2008-05-09 21:27:22 +00:00
|
|
|
exit(2);
|
2002-06-14 05:49:34 +00:00
|
|
|
}
|
|
|
|
|
2010-10-05 23:06:20 +00:00
|
|
|
void usage() {
|
|
|
|
fprintf(stderr,
|
2023-07-17 21:40:45 +00:00
|
|
|
"Usage: crypt_prog options\n\n"
|
|
|
|
"Options:\n\n"
|
|
|
|
"-genkey n private_keyfile public_keyfile\n"
|
|
|
|
" create an n-bit key pair\n"
|
|
|
|
"-sign file private_keyfile\n"
|
|
|
|
" create a signature for a given file, write to stdout\n"
|
|
|
|
"-sign_string string private_keyfile\n"
|
|
|
|
" create a signature for a given string\n"
|
|
|
|
"-verify file signature_file public_keyfile\n"
|
|
|
|
" verify a file signature\n"
|
|
|
|
"-verify_string string signature_file public_keyfile\n"
|
|
|
|
" verify a string signature\n"
|
|
|
|
"-test_crypt private_keyfile public_keyfile\n"
|
|
|
|
" test encrypt/decrypt functions\n"
|
|
|
|
"-convkey o2b/b2o priv/pub input_file output_file\n"
|
|
|
|
" convert keys between BOINC and OpenSSL format\n"
|
|
|
|
"-cert_verify file signature certificate_dir\n"
|
|
|
|
" verify a signature using a directory of certificates\n"
|
|
|
|
);
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
|
|
|
|
2005-08-01 22:44:40 +00:00
|
|
|
unsigned int random_int() {
|
|
|
|
unsigned int n;
|
2006-02-16 04:05:38 +00:00
|
|
|
#if defined(_WIN32)
|
2006-06-30 20:45:16 +00:00
|
|
|
#if defined(__CYGWIN32__)
|
|
|
|
HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL");
|
|
|
|
#else
|
2008-02-12 15:28:31 +00:00
|
|
|
HMODULE hLib=LoadLibrary("ADVAPI32.DLL");
|
2006-06-30 20:45:16 +00:00
|
|
|
#endif
|
2006-02-16 04:05:38 +00:00
|
|
|
if (!hLib) {
|
|
|
|
die("Can't load ADVAPI32.DLL");
|
|
|
|
}
|
|
|
|
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
|
2016-07-18 14:25:31 +00:00
|
|
|
(BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
|
2006-02-16 04:05:38 +00:00
|
|
|
if (pfn) {
|
|
|
|
char buff[32];
|
|
|
|
ULONG ulCbBuff = sizeof(buff);
|
|
|
|
if(pfn(buff,ulCbBuff)) {
|
|
|
|
// use buff full of random goop
|
|
|
|
memcpy(&n,buff,sizeof(n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FreeLibrary(hLib);
|
|
|
|
#else
|
2005-08-01 22:44:40 +00:00
|
|
|
FILE* f = fopen("/dev/random", "r");
|
|
|
|
if (!f) {
|
2006-02-16 04:05:38 +00:00
|
|
|
die("can't open /dev/random\n");
|
2005-08-01 22:44:40 +00:00
|
|
|
}
|
2015-11-04 16:55:53 +00:00
|
|
|
if (1 != fread(&n, sizeof(n), 1, f)) {
|
|
|
|
die("couldn't read from /dev/random\n");
|
|
|
|
}
|
2012-02-13 08:41:48 +00:00
|
|
|
fclose(f);
|
2006-02-16 04:05:38 +00:00
|
|
|
#endif
|
2005-08-01 22:44:40 +00:00
|
|
|
return n;
|
|
|
|
}
|
2004-10-06 21:49:21 +00:00
|
|
|
|
2002-10-04 20:26:23 +00:00
|
|
|
int main(int argc, char** argv) {
|
2002-06-14 05:49:34 +00:00
|
|
|
R_RSA_PUBLIC_KEY public_key;
|
|
|
|
R_RSA_PRIVATE_KEY private_key;
|
2008-09-04 15:13:54 +00:00
|
|
|
int i, n, retval;
|
2002-06-14 05:49:34 +00:00
|
|
|
bool is_valid;
|
|
|
|
DATA_BLOCK signature, in, out;
|
|
|
|
unsigned char signature_buf[256], buf[256], buf2[256];
|
|
|
|
FILE *f, *fpriv, *fpub;
|
2005-12-14 23:43:50 +00:00
|
|
|
char cbuf[256];
|
2016-06-27 16:26:27 +00:00
|
|
|
#ifdef HAVE_OPAQUE_RSA_DSA_DH
|
2016-07-18 14:01:26 +00:00
|
|
|
RSA *rsa_key = RSA_new();
|
2016-06-27 16:26:27 +00:00
|
|
|
#else
|
2008-09-04 15:13:54 +00:00
|
|
|
RSA rsa_key;
|
2016-06-27 16:26:27 +00:00
|
|
|
#endif
|
2008-09-04 15:13:54 +00:00
|
|
|
RSA *rsa_key_;
|
2016-07-18 14:25:31 +00:00
|
|
|
BIO *bio_out=NULL;
|
2008-09-04 15:13:54 +00:00
|
|
|
BIO *bio_err=NULL;
|
|
|
|
char *certpath;
|
2010-06-25 18:54:37 +00:00
|
|
|
bool b2o=false; // boinc key to openssl key ?
|
|
|
|
bool kpriv=false; // private key ?
|
2016-07-18 14:01:26 +00:00
|
|
|
BIGNUM *e;
|
2002-06-14 05:49:34 +00:00
|
|
|
|
|
|
|
if (argc == 1) {
|
2010-10-05 23:06:20 +00:00
|
|
|
usage();
|
2002-06-14 05:49:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "-genkey")) {
|
2005-08-01 22:44:40 +00:00
|
|
|
if (argc < 5) {
|
2010-10-05 23:06:20 +00:00
|
|
|
usage();
|
2005-08-01 22:44:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-08-26 22:57:17 +00:00
|
|
|
printf("creating keys in %s and %s\n", argv[3], argv[4]);
|
2002-06-14 05:49:34 +00:00
|
|
|
n = atoi(argv[2]);
|
|
|
|
|
2005-08-01 22:44:40 +00:00
|
|
|
srand(random_int());
|
2016-07-18 14:01:26 +00:00
|
|
|
e = BN_new();
|
|
|
|
retval = BN_set_word(e, (unsigned long)65537);
|
|
|
|
if (retval != 1) {
|
|
|
|
die("BN_set_word");
|
|
|
|
}
|
|
|
|
RSA *rp = RSA_new();
|
|
|
|
retval = RSA_generate_key_ex(rp, n, e, NULL);
|
|
|
|
if (retval != 1) {
|
|
|
|
die("RSA_generate_key_ex");
|
|
|
|
}
|
2005-08-01 22:44:40 +00:00
|
|
|
openssl_to_keys(rp, n, private_key, public_key);
|
2002-06-14 05:49:34 +00:00
|
|
|
fpriv = fopen(argv[3], "w");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
fpub = fopen(argv[4], "w");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
print_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
print_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
2005-08-01 22:44:40 +00:00
|
|
|
|
2002-06-14 05:49:34 +00:00
|
|
|
} else if (!strcmp(argv[1], "-sign")) {
|
2010-10-05 23:06:20 +00:00
|
|
|
if (argc < 4) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-06-14 05:49:34 +00:00
|
|
|
fpriv = fopen(argv[3], "r");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
if (retval) die("scan_key_hex\n");
|
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = sign_file(argv[2], private_key, signature);
|
|
|
|
print_hex_data(stdout, signature);
|
2005-12-14 23:43:50 +00:00
|
|
|
} else if (!strcmp(argv[1], "-sign_string")) {
|
2010-10-05 23:06:20 +00:00
|
|
|
if (argc < 4) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-12-14 23:43:50 +00:00
|
|
|
fpriv = fopen(argv[3], "r");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
if (retval) die("scan_key_hex\n");
|
|
|
|
generate_signature(argv[2], cbuf, private_key);
|
|
|
|
puts(cbuf);
|
2002-06-14 05:49:34 +00:00
|
|
|
} else if (!strcmp(argv[1], "-verify")) {
|
2010-10-05 23:06:20 +00:00
|
|
|
if (argc < 5) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-06-14 05:49:34 +00:00
|
|
|
fpub = fopen(argv[4], "r");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
|
|
|
if (retval) die("read_public_key");
|
|
|
|
f = fopen(argv[3], "r");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!f) die("fopen");
|
2002-06-14 05:49:34 +00:00
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = scan_hex_data(f, signature);
|
|
|
|
if (retval) die("scan_hex_data");
|
2012-02-06 22:57:36 +00:00
|
|
|
|
|
|
|
char md5_buf[64];
|
|
|
|
double size;
|
|
|
|
retval = md5_file(argv[2], md5_buf, size);
|
|
|
|
if (retval) die("md5_file");
|
2012-02-06 06:06:44 +00:00
|
|
|
retval = check_file_signature(
|
2023-07-17 21:40:45 +00:00
|
|
|
md5_buf, public_key, signature, is_valid
|
|
|
|
);
|
2012-02-06 22:57:36 +00:00
|
|
|
if (retval) die("check_file_signature");
|
2008-05-09 21:27:22 +00:00
|
|
|
if (is_valid) {
|
2023-07-17 21:40:45 +00:00
|
|
|
printf("signature is valid\n");
|
2008-05-09 21:27:22 +00:00
|
|
|
} else {
|
2023-07-17 21:40:45 +00:00
|
|
|
printf("signature is invalid\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[1], "-verify_string")) {
|
|
|
|
if (argc < 5) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fpub = fopen(argv[4], "r");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
|
|
|
if (retval) die("read_public_key");
|
|
|
|
f = fopen(argv[3], "r");
|
|
|
|
if (!f) die("fopen");
|
|
|
|
int n = fread(cbuf, 1, 256, f);
|
|
|
|
cbuf[n] = 0;
|
|
|
|
|
|
|
|
retval = check_string_signature(argv[2], cbuf, public_key, is_valid);
|
|
|
|
if (retval) die("check_string_signature");
|
|
|
|
if (is_valid) {
|
|
|
|
printf("signature is valid\n");
|
|
|
|
} else {
|
|
|
|
printf("signature is invalid\n");
|
2008-05-09 21:27:22 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-06-14 05:49:34 +00:00
|
|
|
} else if (!strcmp(argv[1], "-test_crypt")) {
|
2010-10-05 23:06:20 +00:00
|
|
|
if (argc < 4) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-06-14 05:49:34 +00:00
|
|
|
fpriv = fopen(argv[2], "r");
|
|
|
|
if (!fpriv) die("fopen");
|
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
|
|
|
if (retval) die("scan_key_hex\n");
|
|
|
|
fpub = fopen(argv[3], "r");
|
|
|
|
if (!fpub) die("fopen");
|
|
|
|
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
|
|
|
if (retval) die("read_public_key");
|
2008-02-06 19:32:51 +00:00
|
|
|
strcpy((char*)buf2, "encryption test successful");
|
2005-02-16 23:17:43 +00:00
|
|
|
in.data = buf2;
|
2002-06-14 05:49:34 +00:00
|
|
|
in.len = strlen((char*)in.data);
|
|
|
|
out.data = buf;
|
2005-08-11 17:41:08 +00:00
|
|
|
encrypt_private(private_key, in, out);
|
2002-06-14 05:49:34 +00:00
|
|
|
in = out;
|
|
|
|
out.data = buf2;
|
|
|
|
decrypt_public(public_key, in, out);
|
2005-08-01 22:44:40 +00:00
|
|
|
printf("out: %s\n", out.data);
|
2008-09-04 15:13:54 +00:00
|
|
|
} else if (!strcmp(argv[1], "-cert_verify")) {
|
|
|
|
if (argc < 6)
|
|
|
|
die("usage: crypt_prog -cert_verify file signature_file certificate_dir ca_dir \n");
|
|
|
|
|
|
|
|
f = fopen(argv[3], "r");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!f) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = scan_hex_data(f, signature);
|
|
|
|
if (retval) die("cannot scan_hex_data");
|
|
|
|
certpath = check_validity(argv[4], argv[2], signature.data, argv[5]);
|
|
|
|
if (certpath == NULL) {
|
2021-10-01 07:36:00 +00:00
|
|
|
die("signature cannot be verified.\n\n");
|
2008-09-04 15:13:54 +00:00
|
|
|
} else {
|
2021-10-01 07:36:00 +00:00
|
|
|
printf("signature verified using certificate '%s'.\n\n", certpath);
|
2008-09-04 15:13:54 +00:00
|
|
|
free(certpath);
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2016-07-18 14:25:31 +00:00
|
|
|
// this converts, but an executable signed with sign_executable,
|
|
|
|
// and signature converted to OpenSSL format cannot be verified with
|
|
|
|
// OpenSSL
|
2008-09-04 15:13:54 +00:00
|
|
|
} else if (!strcmp(argv[1], "-convsig")) {
|
2010-10-05 23:06:20 +00:00
|
|
|
if (argc < 5) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[2], "b2o") == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
b2o = true;
|
2010-10-05 23:06:20 +00:00
|
|
|
} else if (strcmp(argv[2], "o2b") == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
b2o = false;
|
2010-10-05 23:06:20 +00:00
|
|
|
} else {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("either 'o2b' or 'b2o' must be defined for -convsig\n");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
if (b2o) {
|
|
|
|
f = fopen(argv[3], "r");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!f) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = scan_hex_data(f, signature);
|
|
|
|
fclose(f);
|
2010-10-05 23:06:20 +00:00
|
|
|
f = fopen(argv[4], "w+");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!f) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
print_raw_data(f, signature);
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
f = fopen(argv[3], "r");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!f) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
signature.data = signature_buf;
|
|
|
|
signature.len = 256;
|
|
|
|
retval = scan_raw_data(f, signature);
|
|
|
|
fclose(f);
|
2010-10-05 23:06:20 +00:00
|
|
|
f = fopen(argv[4], "w+");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!f) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
print_hex_data(f, signature);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[1], "-convkey")) {
|
2010-10-05 23:06:20 +00:00
|
|
|
if (argc < 6) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[2], "b2o") == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
b2o = true;
|
2010-10-05 23:06:20 +00:00
|
|
|
} else if (strcmp(argv[2], "o2b") == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
b2o = false;
|
2010-10-05 23:06:20 +00:00
|
|
|
} else {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("either 'o2b' or 'b2o' must be defined for -convkey\n");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
|
|
|
if (strcmp(argv[3], "pub") == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
kpriv = false;
|
2010-10-05 23:06:20 +00:00
|
|
|
} else if (strcmp(argv[3], "priv") == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
kpriv = true;
|
2010-10-05 23:06:20 +00:00
|
|
|
} else {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("either 'pub' or 'priv' must be defined for -convkey\n");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
OpenSSL_add_all_algorithms();
|
2016-07-18 14:25:31 +00:00
|
|
|
ERR_load_crypto_strings();
|
|
|
|
ENGINE_load_builtin_engines();
|
|
|
|
if (bio_err == NULL) {
|
|
|
|
bio_err = BIO_new_fp(stdout, BIO_NOCLOSE);
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
//enc=EVP_get_cipherbyname("des");
|
|
|
|
//if (enc == NULL)
|
|
|
|
// die("could not get cypher.\n");
|
2021-10-01 07:36:00 +00:00
|
|
|
// no encryption yet.
|
2008-09-04 15:13:54 +00:00
|
|
|
bio_out=BIO_new(BIO_s_file());
|
2016-07-18 14:25:31 +00:00
|
|
|
if (BIO_write_filename(bio_out,argv[5]) <= 0) {
|
|
|
|
perror(argv[5]);
|
2008-09-04 15:13:54 +00:00
|
|
|
die("could not create output file.\n");
|
|
|
|
}
|
|
|
|
if (b2o) {
|
|
|
|
rsa_key_ = RSA_new();
|
|
|
|
if (kpriv) {
|
|
|
|
fpriv = fopen(argv[4], "r");
|
2010-10-05 23:06:20 +00:00
|
|
|
if (!fpriv) {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("fopen");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2015-11-04 16:49:43 +00:00
|
|
|
retval = scan_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
2008-09-04 15:13:54 +00:00
|
|
|
fclose(fpriv);
|
2015-11-04 16:49:43 +00:00
|
|
|
if (retval) die("scan_key_hex\n");
|
2016-06-27 16:26:27 +00:00
|
|
|
#ifdef HAVE_OPAQUE_RSA_DSA_DH
|
|
|
|
private_to_openssl(private_key, rsa_key);
|
|
|
|
#else
|
2008-09-04 15:13:54 +00:00
|
|
|
private_to_openssl(private_key, &rsa_key);
|
2016-06-27 16:26:27 +00:00
|
|
|
#endif
|
2010-10-05 23:06:20 +00:00
|
|
|
|
2008-09-04 15:13:54 +00:00
|
|
|
//i = PEM_write_bio_RSAPrivateKey(bio_out, &rsa_key,
|
2016-07-18 14:25:31 +00:00
|
|
|
// enc, NULL, 0, pass_cb, NULL);
|
|
|
|
// no encryption yet.
|
|
|
|
|
2008-09-04 15:13:54 +00:00
|
|
|
//i = PEM_write_bio_RSAPrivateKey(bio_out, &rsa_key,
|
2016-07-18 14:25:31 +00:00
|
|
|
// NULL, NULL, 0, pass_cb, NULL);
|
2008-09-04 15:13:54 +00:00
|
|
|
fpriv = fopen(argv[5], "w+");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!fpriv) die("fopen");
|
2016-06-27 16:26:27 +00:00
|
|
|
#ifdef HAVE_OPAQUE_RSA_DSA_DH
|
|
|
|
PEM_write_RSAPrivateKey(fpriv, rsa_key, NULL, NULL, 0, 0, NULL);
|
|
|
|
#else
|
2008-09-04 15:13:54 +00:00
|
|
|
PEM_write_RSAPrivateKey(fpriv, &rsa_key, NULL, NULL, 0, 0, NULL);
|
2016-06-27 16:26:27 +00:00
|
|
|
#endif
|
2008-09-04 15:13:54 +00:00
|
|
|
fclose(fpriv);
|
2016-07-18 14:25:31 +00:00
|
|
|
//if (i == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
// ERR_print_errors(bio_err);
|
|
|
|
// die("could not write key file.\n");
|
2016-07-18 14:25:31 +00:00
|
|
|
//}
|
2008-09-04 15:13:54 +00:00
|
|
|
} else {
|
|
|
|
fpub = fopen(argv[4], "r");
|
2010-10-05 23:06:20 +00:00
|
|
|
if (!fpub) {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("fopen");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2015-11-04 16:49:43 +00:00
|
|
|
retval = scan_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
2008-09-04 15:13:54 +00:00
|
|
|
fclose(fpub);
|
2015-11-04 16:49:43 +00:00
|
|
|
if (retval) die("scan_key_hex\n");
|
2008-09-04 15:13:54 +00:00
|
|
|
fpub = fopen(argv[5], "w+");
|
2010-10-05 23:06:20 +00:00
|
|
|
if (!fpub) {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("fopen");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
public_to_openssl(public_key, rsa_key_);
|
2016-07-18 14:25:31 +00:00
|
|
|
i = PEM_write_RSA_PUBKEY(fpub, rsa_key_);
|
|
|
|
if (i == 0) {
|
2008-09-04 15:13:54 +00:00
|
|
|
ERR_print_errors(bio_err);
|
|
|
|
die("could not write key file.\n");
|
2016-07-18 14:25:31 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
fclose(fpub);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// o2b
|
2014-06-27 00:42:48 +00:00
|
|
|
rsa_key_ = RSA_new();
|
2010-10-05 23:06:20 +00:00
|
|
|
if (rsa_key_ == NULL) {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("could not allocate memory for RSA structure.\n");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
if (kpriv) {
|
|
|
|
fpriv = fopen (argv[4], "r");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!fpriv) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
rsa_key_ = PEM_read_RSAPrivateKey(fpriv, NULL, NULL, NULL);
|
|
|
|
fclose(fpriv);
|
|
|
|
if (rsa_key_ == NULL) {
|
|
|
|
ERR_print_errors(bio_err);
|
|
|
|
die("could not load private key.\n");
|
|
|
|
}
|
|
|
|
openssl_to_private(rsa_key_, &private_key);
|
|
|
|
fpriv = fopen(argv[5], "w");
|
2010-10-05 23:06:20 +00:00
|
|
|
if (!fpriv) {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("fopen");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
|
|
|
print_key_hex(fpriv, (KEY*)&private_key, sizeof(private_key));
|
2008-09-04 15:13:54 +00:00
|
|
|
} else {
|
|
|
|
fpub = fopen (argv[4], "r");
|
2015-11-04 16:45:00 +00:00
|
|
|
if (!fpub) die("fopen");
|
2008-09-04 15:13:54 +00:00
|
|
|
rsa_key_ = PEM_read_RSA_PUBKEY(fpub, NULL, NULL, NULL);
|
|
|
|
fclose(fpub);
|
|
|
|
if (rsa_key_ == NULL) {
|
|
|
|
ERR_print_errors(bio_err);
|
|
|
|
die("could not load public key.\n");
|
|
|
|
}
|
|
|
|
openssl_to_keys(rsa_key_, 1024, private_key, public_key);
|
|
|
|
//openssl_to_public(rsa_key_, &public_key);
|
|
|
|
public_to_openssl(public_key, rsa_key_); //
|
|
|
|
fpub = fopen(argv[5], "w");
|
2010-10-05 23:06:20 +00:00
|
|
|
if (!fpub) {
|
2008-09-04 15:13:54 +00:00
|
|
|
die("fopen");
|
2010-10-05 23:06:20 +00:00
|
|
|
}
|
2008-09-04 15:13:54 +00:00
|
|
|
print_key_hex(fpub, (KEY*)&public_key, sizeof(public_key));
|
|
|
|
}
|
|
|
|
}
|
2002-06-14 05:49:34 +00:00
|
|
|
} else {
|
2010-10-05 23:06:20 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
2002-06-14 05:49:34 +00:00
|
|
|
}
|
2002-10-04 20:26:23 +00:00
|
|
|
return 0;
|
2002-06-14 05:49:34 +00:00
|
|
|
}
|