2#include <openssl/rsa.h>
3#include <openssl/rand.h>
4#include <openssl/sha.h>
5#include <openssl/pem.h>
6#include <openssl/err.h>
9#include <fc/io/stdio.hpp>
29 public_key::operator bool()
const {
return !!my; }
30 private_key::operator bool()
const {
return !!my; }
36 :my(
std::make_shared<
detail::pke_impl>() )
38 string pem =
"-----BEGIN RSA PUBLIC KEY-----\n";
40 for(
size_t i = 0; i < b64.size(); i += 64 )
41 pem += b64.substr( i, 64 ) +
"\n";
42 pem +=
"-----END RSA PUBLIC KEY-----\n";
45 BIO* mem = (BIO*)BIO_new_mem_buf( (
void*)pem.c_str(), pem.size() );
46 my->rsa = PEM_read_bio_RSAPublicKey(mem, NULL, NULL, NULL );
63 my =
p.my;
return *
this;
67 my = std::move(
p.my);
return *
this;
71 return 0 != RSA_verify( NID_sha1, (
const uint8_t*)&
digest, 20,
72 (
uint8_t*)&sig, 2048/8, my->rsa );
77 static_assert( sig.size() == 2048/8,
"" );
78 return 0 != RSA_verify( NID_sha1, (
const uint8_t*)&
digest, 20,
83 static_assert( sig.size() == 2048/8,
"" );
84 return 0 != RSA_verify( NID_sha256, (
const uint8_t*)&
digest, 32,
90 bytes out( RSA_size(my->rsa) );
91 int rtn = RSA_public_encrypt(
l,
93 (
unsigned char*)out.data(),
94 my->rsa, RSA_PKCS1_OAEP_PADDING );
105 bytes out( RSA_size(my->rsa) );
106 int rtn = RSA_public_encrypt( in.size(),
107 (
unsigned char*)in.data(),
108 (
unsigned char*)out.data(),
109 my->rsa, RSA_PKCS1_OAEP_PADDING );
110 fc::cerr<<
"rtn: "<<rtn<<
"\n";
120 bytes out( RSA_size(my->rsa) );
121 int rtn = RSA_public_decrypt( in.size(),
122 (
unsigned char*)in.data(),
123 (
unsigned char*)out.data(),
124 my->rsa, RSA_PKCS1_OAEP_PADDING );
135 if( !my ) {
return ba; }
137 BIO *mem = BIO_new(BIO_s_mem());
138 int e = PEM_write_bio_RSAPublicKey( mem, my->rsa );
145 uint32_t l = BIO_get_mem_data( mem, &dat );
147 fc::stringstream ss(
string( dat,
l ) );
148 fc::stringstream
key;
150 fc::getline( ss, tmp );
151 fc::getline( ss, tmp );
152 while( tmp.size() && tmp[0] !=
'-' )
155 fc::getline( ss, tmp );
157 auto str = key.str();
159 ba =
bytes( str.begin(), str.end() );
169 :my(
std::make_shared<
detail::pke_impl>() )
172 string pem =
"-----BEGIN RSA PRIVATE KEY-----\n";
174 for(
size_t i = 0; i < b64.size(); i += 64 )
175 pem += b64.substr( i, 64 ) +
"\n";
176 pem +=
"-----END RSA PRIVATE KEY-----\n";
179 BIO* mem = (BIO*)BIO_new_mem_buf( (
void*)pem.c_str(), pem.size() );
180 my->rsa = PEM_read_bio_RSAPrivateKey(mem, NULL, NULL, NULL );
183 FC_ASSERT( my->rsa,
"read private key" );
191 :my(
std::move(k.my) )
198 my =
p.my;
return *
this;
202 my = std::move(
p.my);
return *
this;
207 FC_ASSERT( (
size_t(RSA_size(my->rsa)) <=
sizeof(sig)),
"Invalid RSA size" );
210 20, (
unsigned char*)&sig, &slen, my->rsa ) )
220 sig.resize( RSA_size(my->rsa) );
224 20, (
unsigned char*)sig.data(), &slen, my->rsa ) )
234 sig.resize( RSA_size(my->rsa) );
238 32, (
unsigned char*)sig.data(), &slen, my->rsa ) )
250 out.resize( RSA_size(my->rsa) );
251 int rtn = RSA_private_encrypt( in.size(),
252 (
unsigned char*)in.data(),
253 (
unsigned char*)out.data(),
254 my->rsa, RSA_PKCS1_OAEP_PADDING );
267 out.resize( RSA_size(my->rsa) );
268 int rtn = RSA_private_decrypt(
l,
270 (
unsigned char*)out.data(),
271 my->rsa, RSA_PKCS1_OAEP_PADDING );
282 out.resize( RSA_size(my->rsa) );
283 int rtn = RSA_private_decrypt( in.size(),
284 (
unsigned char*)in.data(),
285 (
unsigned char*)out.data(),
286 my->rsa, RSA_PKCS1_OAEP_PADDING );
297 if( !my ) {
return ba; }
299 BIO *mem = BIO_new(BIO_s_mem());
300 int e = PEM_write_bio_RSAPrivateKey( mem, my->rsa, NULL, NULL, 0, NULL, NULL );
307 uint32_t l = BIO_get_mem_data( mem, &dat );
310 stringstream ss(
string( dat,
l ) );
313 fc::getline( ss, tmp );
314 fc::getline( ss, tmp );
316 while( tmp.size() && tmp[0] !=
'-' )
319 fc::getline( ss, tmp );
321 auto str = key.str();
323 ba =
bytes( str.begin(), str.end() );
331 static bool init =
true;
332 if(
init ) { ERR_load_crypto_strings();
init =
false; }
334 pub.my = std::make_shared<detail::pke_impl>();
336 pub.my->rsa = RSA_generate_key( 2048, 65537, NULL, NULL );
Used to generate a useful error report when an exception is thrown.
bytes encrypt(const bytes &) const
void sign(const sha1 &digest, array< char, 2048/8 > &sig) const
bytes decrypt(const char *bytes, size_t len) const
private_key & operator=(const private_key &p)
public_key & operator=(const public_key &p)
bool verify(const sha1 &digest, const array< char, 2048/8 > &sig) const
bytes encrypt(const char *data, size_t len) const
bytes decrypt(const bytes &) const
const char * data() const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
fc::sha256 digest(const T &value)
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
std::vector< char > bytes
void generate_key_pair(public_key &, private_key &)
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
std::string base64_decode(const std::string &encoded_string)