keycodes. boost stil can't handle dates
This commit is contained in:
1 parent
7d75d87e6b
commit
8a965d41ff
24 files changed
+747
-132
No files matched your search
@@ -4,6 +4,8 @@
|
||||
#include "CryptoInit.h"
|
||||
#include "iostreambio.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include <tbb/spin_mutex.h>
|
||||
#include <cstring>
|
||||
|
||||
@@ -13,6 +15,7 @@ namespace openssl
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/pem.h>
|
||||
}
|
||||
|
||||
@@ -26,9 +29,9 @@ struct PrivateKey::Pimp
|
||||
BTS_NO_COPY(Pimp);
|
||||
|
||||
const PasswordCallback* password;
|
||||
PasswordCallback default_password;
|
||||
PasswordCallback default_password;
|
||||
PublicKeyPtr public_key;
|
||||
|
||||
|
||||
Pimp(const PasswordCallback* pass) : password(pass), default_password(), public_key()
|
||||
{
|
||||
initialize();
|
||||
@@ -51,7 +54,7 @@ int password_callback(char *buf, int size, int rwflag, void *u)
|
||||
std::string data;
|
||||
if(u)
|
||||
{
|
||||
PasswordCallback* pwd = reinterpret_cast<PasswordCallback*>(u);
|
||||
PasswordCallback* pwd = reinterpret_cast<PasswordCallback*>(u);
|
||||
pwd->request_password(data, data);
|
||||
}
|
||||
else
|
||||
@@ -67,7 +70,7 @@ int password_callback(char *buf, int size, int rwflag, void *u)
|
||||
return sz;
|
||||
}
|
||||
|
||||
const PublicKeyPtr PrivateKey::public_key() const
|
||||
const PublicKeyPtr PrivateKey::public_key() const
|
||||
{
|
||||
if(!me->public_key)
|
||||
me->public_key = PublicKey::create(*this);
|
||||
@@ -83,7 +86,7 @@ const PrivateKeyPtr PrivateKey::create(int bits, AlgoType::type at, const Passwo
|
||||
openssl::RSA* rsa = openssl::RSA_new();
|
||||
openssl::BIGNUM* bn = openssl::BN_new();
|
||||
openssl::BN_set_word(bn, RSA_F4);
|
||||
int keyok = openssl::RSA_generate_key_ex(rsa, bits, bn, 0);
|
||||
int keyok = openssl::RSA_generate_key_ex(rsa, bits, bn, 0);
|
||||
openssl::BN_free(bn);
|
||||
bts_assert_msg(keyok != -1, "Unable to create key");
|
||||
int i = openssl::EVP_PKEY_assign_RSA(key, rsa);
|
||||
@@ -93,7 +96,7 @@ const PrivateKeyPtr PrivateKey::create(int bits, AlgoType::type at, const Passwo
|
||||
bts_assert_msg(false, "only support RSA");
|
||||
return PrivateKeyPtr(new PrivateKey(key, pass));
|
||||
}
|
||||
|
||||
|
||||
|
||||
const PrivateKeyPtr PrivateKey::read_file(const std::string& filename, const PasswordCallback* pass)
|
||||
{
|
||||
@@ -101,11 +104,11 @@ const PrivateKeyPtr PrivateKey::read_file(const std::string& filename, const Pas
|
||||
PrivateKeyPtr ret;
|
||||
openssl::BIO* bio = openssl::BIO_new_file(filename.c_str(), "r");
|
||||
bts_assert_msg(bio, "can't load keyfile");
|
||||
openssl::EVP_PKEY* key = openssl::PEM_read_bio_PrivateKey(bio, 0, password_callback, (void*)pass);
|
||||
openssl::EVP_PKEY* key = openssl::PEM_read_bio_PrivateKey(bio, 0, password_callback, (void*)pass);
|
||||
if(!key)
|
||||
//LOG(Error, Config) << "Invalid private key passphrase";
|
||||
LOG(Gen, Error, "Invalid private key passphrase");
|
||||
else
|
||||
ret.reset(new PrivateKey(key, pass));
|
||||
ret.reset(new PrivateKey(key, pass));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -125,7 +128,7 @@ const PrivateKeyPtr PrivateKey::read_stream(const std::istream& stream, const Pa
|
||||
|
||||
openssl::EVP_PKEY* key = openssl::PEM_read_bio_PrivateKey(bio, 0, password_callback, (void*)pass);
|
||||
if(!key)
|
||||
//LOG(Error, Config) << "Invalid private key passphrase";
|
||||
LOG(Gen, Error, "Invalid private key passphrase");
|
||||
else
|
||||
ret.reset(new PrivateKey(key, pass));
|
||||
return ret;
|
||||
@@ -135,7 +138,7 @@ const PrivateKeyPtr PrivateKey::read_stream(const std::istream& stream, const Pa
|
||||
PrivateKey::PrivateKey(openssl::EVP_PKEY* key, const PasswordCallback* pass) : me(new Pimp(pass)), _key(key)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
PrivateKey::~PrivateKey()
|
||||
{
|
||||
if(_key)
|
||||
@@ -146,8 +149,8 @@ typedef int encrypt_decrypt_init(openssl::EVP_PKEY_CTX *ctx);
|
||||
typedef int encrypt_decrypt(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen);
|
||||
|
||||
|
||||
bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_data,
|
||||
encrypt_decrypt_init* init_function, encrypt_decrypt* function,
|
||||
bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_data,
|
||||
encrypt_decrypt_init* init_function, encrypt_decrypt* function,
|
||||
openssl::EVP_PKEY* key, bool encrypt)
|
||||
{
|
||||
openssl::EVP_PKEY_CTX* ctx = openssl::EVP_PKEY_CTX_new(key, 0);
|
||||
@@ -171,7 +174,7 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da
|
||||
|
||||
in = new unsigned char[inlen];
|
||||
i = openssl::BIO_read(biostream.getBIO(), in, inlen);
|
||||
}
|
||||
}
|
||||
//BIO_flush(biostream.getBIO());
|
||||
BIO_free(biostream.getBIO());
|
||||
|
||||
@@ -183,7 +186,7 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da
|
||||
// delete[] in;
|
||||
|
||||
std::string header_name = "BTS", header_header = "";
|
||||
iostreambio biostream_out(const_cast<std::iostream*>(&out_data));
|
||||
iostreambio biostream_out(const_cast<std::iostream*>(&out_data));
|
||||
if(encrypt)
|
||||
i = openssl::PEM_write_bio(biostream_out.getBIO(), header_name.c_str(), const_cast<char*>(header_header.c_str()), out, outlen);
|
||||
else
|
||||
@@ -198,28 +201,30 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da
|
||||
int private_encrypt_func(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen)
|
||||
{
|
||||
openssl::EVP_PKEY *pk = openssl::EVP_PKEY_CTX_get0_pkey(ctx);
|
||||
auto rsa = openssl::EVP_PKEY_get0_RSA(pk);
|
||||
if(out == 0)
|
||||
{
|
||||
*outlen = openssl::RSA_size(pk->pkey.rsa);
|
||||
{
|
||||
*outlen = openssl::RSA_size(rsa);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return RSA_private_encrypt(inlen, const_cast<unsigned char *>(in), out, pk->pkey.rsa, RSA_PKCS1_PADDING);
|
||||
{
|
||||
return RSA_private_encrypt(inlen, const_cast<unsigned char *>(in), out, rsa, RSA_PKCS1_PADDING);
|
||||
}
|
||||
}
|
||||
|
||||
int public_decrypt_func(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen)
|
||||
{
|
||||
openssl::EVP_PKEY *pk = openssl::EVP_PKEY_CTX_get0_pkey(ctx);
|
||||
auto rsa = openssl::EVP_PKEY_get0_RSA(pk);
|
||||
if(out == 0)
|
||||
{
|
||||
*outlen = openssl::RSA_size(pk->pkey.rsa);
|
||||
*outlen = openssl::RSA_size(rsa);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int i = RSA_public_decrypt(inlen, const_cast<unsigned char *>(in), out, pk->pkey.rsa, RSA_PKCS1_PADDING);
|
||||
{
|
||||
int i = RSA_public_decrypt(inlen, const_cast<unsigned char *>(in), out, rsa, RSA_PKCS1_PADDING);
|
||||
out[i] = '\0';
|
||||
*outlen = i;
|
||||
return i;
|
||||
@@ -230,20 +235,20 @@ bool PrivateKey::private_encrypt(const std::iostream& in_data, std::iostream& ou
|
||||
{
|
||||
return encrypt_decrypt_function(in_data, out_data, 0, private_encrypt_func, _key, true);
|
||||
}
|
||||
|
||||
|
||||
bool PrivateKey::private_decrypt(const std::iostream& in_data, std::iostream& out_data) const
|
||||
{
|
||||
return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_decrypt_init, openssl::EVP_PKEY_decrypt, _key, false);
|
||||
return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_decrypt_init, openssl::EVP_PKEY_decrypt, _key, false);
|
||||
}
|
||||
|
||||
|
||||
bool PrivateKey::public_encrypt(const std::iostream& in_data, std::iostream& out_data) const
|
||||
{
|
||||
return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_encrypt_init, openssl::EVP_PKEY_encrypt, _key, true);
|
||||
}
|
||||
|
||||
|
||||
bool PrivateKey::public_decrypt(const std::iostream& in_data, std::iostream& out_data) const
|
||||
{
|
||||
return encrypt_decrypt_function(in_data, out_data, 0, public_decrypt_func, _key, false);
|
||||
return encrypt_decrypt_function(in_data, out_data, 0, public_decrypt_func, _key, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -251,7 +256,7 @@ bool PrivateKey::encrypt(const std::iostream& in_data, std::iostream& out_data)
|
||||
{
|
||||
return private_encrypt(in_data, out_data);
|
||||
}
|
||||
|
||||
|
||||
bool PrivateKey::decrypt(const std::iostream& in_data, std::iostream& out_data) const
|
||||
{
|
||||
return private_decrypt(in_data, out_data);
|
||||
@@ -274,7 +279,7 @@ void PrivateKey::write_file(const std::string& filename, CipherType::type cipher
|
||||
write(bio, cipher_type);
|
||||
BIO_free(bio);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PrivateKey::write_stream(std::iostream& stream, CipherType::type cipher_type) const
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user