Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::public_key Class Reference

#include <pke.hpp>

Public Member Functions

 public_key ()
 
 public_key (const bytes &d)
 
 public_key (const public_key &k)
 
 public_key (public_key &&k)
 
 ~public_key ()
 
 operator bool () const
 
public_keyoperator= (const public_key &p)
 
public_keyoperator= (public_key &&p)
 
bool verify (const sha1 &digest, const array< char, 2048/8 > &sig) const
 
bool verify (const sha1 &digest, const signature &sig) const
 
bool verify (const sha256 &digest, const signature &sig) const
 
bytes encrypt (const char *data, size_t len) const
 
bytes encrypt (const bytes &) const
 
bytes decrypt (const bytes &) const
 
bytes serialize () const
 

Friends

void generate_key_pair (public_key &pub, private_key &priv)
 

Detailed Description

Definition at line 19 of file pke.hpp.

Constructor & Destructor Documentation

◆ public_key() [1/4]

fc::public_key::public_key ( )

Definition at line 32 of file pke.cpp.

33 {}

◆ public_key() [2/4]

fc::public_key::public_key ( const bytes & d)
explicit

Definition at line 35 of file pke.cpp.

36 :my( std::make_shared<detail::pke_impl>() )
37 {
38 string pem = "-----BEGIN RSA PUBLIC KEY-----\n";
39 auto b64 = fc::base64_encode( (const unsigned char*)d.data(), d.size() );
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";
43 // fc::cerr<<pem;
44
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 );
47 BIO_free(mem);
48 }
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:92
CK_ULONG d
Here is the call graph for this function:

◆ public_key() [3/4]

fc::public_key::public_key ( const public_key & k)

Definition at line 49 of file pke.cpp.

50 :my(k.my)
51 {
52 }

◆ public_key() [4/4]

fc::public_key::public_key ( public_key && k)

Definition at line 54 of file pke.cpp.

55 :my(std::move(k.my))
56 {
57 }

◆ ~public_key()

fc::public_key::~public_key ( )

Definition at line 59 of file pke.cpp.

59{ }

Member Function Documentation

◆ decrypt()

bytes fc::public_key::decrypt ( const bytes & in) const

Definition at line 117 of file pke.cpp.

118 {
119 FC_ASSERT( my && my->rsa );
120 bytes out( RSA_size(my->rsa) );//, char(0) );
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 );
125 if( rtn >= 0 ) {
126 out.resize(rtn);
127 return out;
128 }
129 FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
130 }
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
std::string string
Definition string.hpp:10
std::vector< char > bytes
Definition alt_bn128.hpp:10

◆ encrypt() [1/2]

bytes fc::public_key::encrypt ( const bytes & in) const

Definition at line 102 of file pke.cpp.

103 {
104 FC_ASSERT( my && my->rsa );
105 bytes out( RSA_size(my->rsa) ); //, char(0) );
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";
111 if( rtn >= 0 ) {
112 out.resize(rtn);
113 return out;
114 }
115 FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
116 }

◆ encrypt() [2/2]

bytes fc::public_key::encrypt ( const char * data,
size_t len ) const

Definition at line 87 of file pke.cpp.

88 {
89 FC_ASSERT( my && my->rsa );
90 bytes out( RSA_size(my->rsa) ); //, char(0) );
91 int rtn = RSA_public_encrypt( l,
92 (unsigned char*)b,
93 (unsigned char*)out.data(),
94 my->rsa, RSA_PKCS1_OAEP_PADDING );
95 if( rtn >= 0 ) {
96 out.resize(rtn);
97 return out;
98 }
99 FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
100 }
int l

◆ operator bool()

fc::public_key::operator bool ( ) const

Definition at line 29 of file pke.cpp.

29{ return !!my; }

◆ operator=() [1/2]

public_key & fc::public_key::operator= ( const public_key & p)

Definition at line 61 of file pke.cpp.

62 {
63 my = p.my; return *this;
64 }
const mie::Vuint & p
Definition bn.cpp:27

◆ operator=() [2/2]

public_key & fc::public_key::operator= ( public_key && p)

Definition at line 65 of file pke.cpp.

66 {
67 my = std::move(p.my); return *this;
68 }

◆ serialize()

bytes fc::public_key::serialize ( ) const

Definition at line 132 of file pke.cpp.

133 {
134 bytes ba;
135 if( !my ) { return ba; }
136
137 BIO *mem = BIO_new(BIO_s_mem());
138 int e = PEM_write_bio_RSAPublicKey( mem, my->rsa );
139 if( e != 1 )
140 {
141 BIO_free(mem);
142 FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
143 }
144 char* dat;
145 uint32_t l = BIO_get_mem_data( mem, &dat );
146
147 fc::stringstream ss( string( dat, l ) );
148 fc::stringstream key;
149 fc::string tmp;
150 fc::getline( ss, tmp );
151 fc::getline( ss, tmp );
152 while( tmp.size() && tmp[0] != '-' )
153 {
154 key << tmp;
155 fc::getline( ss, tmp );
156 }
157 auto str = key.str();
158 str = fc::base64_decode( str );
159 ba = bytes( str.begin(), str.end() );
160
161 BIO_free(mem);
162 return ba;
163 }
return str
Definition CLI11.hpp:1359
static const Segment ss(Segment::ss)
std::string base64_decode(const std::string &encoded_string)
Definition base64.cpp:152
unsigned int uint32_t
Definition stdint.h:126
uint8_t key[16]
Definition yubico_otp.c:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ verify() [1/3]

bool fc::public_key::verify ( const sha1 & digest,
const array< char, 2048/8 > & sig ) const

Definition at line 69 of file pke.cpp.

70 {
71 return 0 != RSA_verify( NID_sha1, (const uint8_t*)&digest, 20,
72 (uint8_t*)&sig, 2048/8, my->rsa );
73 }
fc::sha256 digest(const T &value)
Definition digest.hpp:9
unsigned char uint8_t
Definition stdint.h:124
Here is the call graph for this function:

◆ verify() [2/3]

bool fc::public_key::verify ( const sha1 & digest,
const signature & sig ) const

Definition at line 75 of file pke.cpp.

76 {
77 static_assert( sig.size() == 2048/8, "" );
78 return 0 != RSA_verify( NID_sha1, (const uint8_t*)&digest, 20,
79 (uint8_t*)sig.data(), 2048/8, my->rsa );
80 }
const char * data() const
Definition sha256.cpp:31
Here is the call graph for this function:

◆ verify() [3/3]

bool fc::public_key::verify ( const sha256 & digest,
const signature & sig ) const

Definition at line 81 of file pke.cpp.

82 {
83 static_assert( sig.size() == 2048/8, "" );
84 return 0 != RSA_verify( NID_sha256, (const uint8_t*)&digest, 32,
85 (uint8_t*)sig.data(), 2048/8, my->rsa );
86 }
Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ generate_key_pair

void generate_key_pair ( public_key & pub,
private_key & priv )
friend

Definition at line 329 of file pke.cpp.

330 {
331 static bool init = true;
332 if( init ) { ERR_load_crypto_strings(); init = false; }
333
334 pub.my = std::make_shared<detail::pke_impl>();
335 priv.my = pub.my;
336 pub.my->rsa = RSA_generate_key( 2048, 65537, NULL, NULL );
337 }
void init()
Definition lib_test.cpp:3
bool pub

The documentation for this class was generated from the following files: