mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 04:04:16 +00:00
use Signer for local destination
This commit is contained in:
parent
009af2eeae
commit
e45ee83746
40
Identity.cpp
40
Identity.cpp
@ -4,9 +4,10 @@
|
||||
#include <cryptopp/osrng.h>
|
||||
#include <cryptopp/dh.h>
|
||||
#include <cryptopp/dsa.h>
|
||||
#include "CryptoConst.h"
|
||||
#include "Identity.h"
|
||||
#include "base64.h"
|
||||
#include "CryptoConst.h"
|
||||
#include "RouterContext.h"
|
||||
#include "Identity.h"
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
@ -201,10 +202,22 @@ namespace data
|
||||
{
|
||||
m_Public = Identity (keys);
|
||||
memcpy (m_PrivateKey, keys.privateKey, 256); // 256
|
||||
memcpy (m_SigningPrivateKey, keys.signingPrivateKey, 20); // 20 - DSA
|
||||
memcpy (m_SigningPrivateKey, keys.signingPrivateKey, 20); // 20 - DSA
|
||||
delete m_Signer;
|
||||
CreateSigner ();
|
||||
return *this;
|
||||
}
|
||||
|
||||
PrivateKeys& PrivateKeys::operator=(const PrivateKeys& other)
|
||||
{
|
||||
m_Public = other.m_Public;
|
||||
memcpy (m_PrivateKey, other.m_PrivateKey, 256); // 256
|
||||
memcpy (m_SigningPrivateKey, other.m_SigningPrivateKey, 128); // 128
|
||||
delete m_Signer;
|
||||
CreateSigner ();
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t PrivateKeys::FromBuffer (const uint8_t * buf, size_t len)
|
||||
{
|
||||
size_t ret = m_Public.FromBuffer (buf, len);
|
||||
@ -213,6 +226,8 @@ namespace data
|
||||
size_t signingPrivateKeySize = m_Public.GetSignatureLen ()/2; // 20 for DSA
|
||||
memcpy (m_SigningPrivateKey, buf + ret, signingPrivateKeySize);
|
||||
ret += signingPrivateKeySize;
|
||||
delete m_Signer;
|
||||
CreateSigner ();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -227,12 +242,26 @@ namespace data
|
||||
return ret;
|
||||
}
|
||||
|
||||
void PrivateKeys::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
||||
{
|
||||
if (m_Signer)
|
||||
m_Signer->Sign (i2p::context.GetRandomNumberGenerator (), buf, len, signature);
|
||||
}
|
||||
|
||||
void PrivateKeys::CreateSigner ()
|
||||
{
|
||||
if (m_Public.GetSigningKeyType () == SIGNING_KEY_TYPE_ECDSA_SHA256_P256)
|
||||
m_Signer = new i2p::crypto::ECDSAP256Signer (m_SigningPrivateKey);
|
||||
else
|
||||
m_Signer = new i2p::crypto::DSASigner (m_SigningPrivateKey);
|
||||
}
|
||||
|
||||
PrivateKeys PrivateKeys::CreateRandomKeys (SigningKeyType type)
|
||||
{
|
||||
if (type == SIGNING_KEY_TYPE_ECDSA_SHA256_P256)
|
||||
{
|
||||
PrivateKeys keys;
|
||||
CryptoPP::AutoSeededRandomPool rnd;
|
||||
auto& rnd = i2p::context.GetRandomNumberGenerator ();
|
||||
// encryption
|
||||
uint8_t publicKey[256];
|
||||
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||
@ -241,6 +270,7 @@ namespace data
|
||||
uint8_t signingPublicKey[64];
|
||||
i2p::crypto::CreateECDSAP256RandomKeys (rnd, keys.m_SigningPrivateKey, signingPublicKey);
|
||||
keys.m_Public = IdentityEx (publicKey, signingPublicKey, SIGNING_KEY_TYPE_ECDSA_SHA256_P256);
|
||||
keys.CreateSigner ();
|
||||
return keys;
|
||||
}
|
||||
return PrivateKeys (i2p::data::CreateRandomKeys ()); // DSA-SHA1
|
||||
@ -249,7 +279,7 @@ namespace data
|
||||
Keys CreateRandomKeys ()
|
||||
{
|
||||
Keys keys;
|
||||
CryptoPP::AutoSeededRandomPool rnd;
|
||||
auto& rnd = i2p::context.GetRandomNumberGenerator ();
|
||||
// encryption
|
||||
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||
dh.GenerateKeyPair(rnd, keys.privateKey, keys.publicKey);
|
||||
|
20
Identity.h
20
Identity.h
@ -144,25 +144,33 @@ namespace data
|
||||
{
|
||||
public:
|
||||
|
||||
PrivateKeys () = default;
|
||||
PrivateKeys (const PrivateKeys& ) = default;
|
||||
PrivateKeys (const Keys& keys) { *this = keys; };
|
||||
PrivateKeys (): m_Signer (nullptr) {};
|
||||
PrivateKeys (const PrivateKeys& other): m_Signer (nullptr) { *this = other; };
|
||||
PrivateKeys (const Keys& keys): m_Signer (nullptr) { *this = keys; };
|
||||
PrivateKeys& operator=(const Keys& keys);
|
||||
|
||||
PrivateKeys& operator=(const PrivateKeys& other);
|
||||
~PrivateKeys () { delete m_Signer; };
|
||||
|
||||
const IdentityEx& GetPublic () const { return m_Public; };
|
||||
const uint8_t * GetPrivateKey () const { return m_PrivateKey; };
|
||||
const uint8_t * GetSigningPrivateKey () const { return m_SigningPrivateKey; };
|
||||
|
||||
void Sign (const uint8_t * buf, int len, uint8_t * signature) const;
|
||||
|
||||
size_t FromBuffer (const uint8_t * buf, size_t len);
|
||||
size_t ToBuffer (uint8_t * buf, size_t len) const;
|
||||
|
||||
static PrivateKeys CreateRandomKeys (SigningKeyType type = SIGNING_KEY_TYPE_DSA_SHA1);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void CreateSigner ();
|
||||
|
||||
private:
|
||||
|
||||
IdentityEx m_Public;
|
||||
uint8_t m_PrivateKey[256];
|
||||
uint8_t m_SigningPrivateKey[128]; // assume private key doesn't exceed 128 bytes
|
||||
i2p::crypto::Signer * m_Signer;
|
||||
};
|
||||
|
||||
#pragma pack()
|
||||
|
@ -506,8 +506,6 @@ namespace stream
|
||||
m_Service (service), m_LeaseSet (nullptr), m_IsPublic (false)
|
||||
{
|
||||
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys ();
|
||||
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
|
||||
CryptoPP::Integer (m_Keys.GetSigningPrivateKey (), 20));
|
||||
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
||||
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
|
||||
@ -530,8 +528,6 @@ namespace stream
|
||||
else
|
||||
LogPrint ("Can't open file ", fullPath);
|
||||
|
||||
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
|
||||
CryptoPP::Integer (m_Keys.GetSigningPrivateKey (), 20));
|
||||
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
||||
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
|
||||
@ -622,8 +618,7 @@ namespace stream
|
||||
|
||||
void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
||||
{
|
||||
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
|
||||
signer.SignMessage (i2p::context.GetRandomNumberGenerator (), buf, len, signature);
|
||||
m_Keys.Sign(buf, len, signature);
|
||||
}
|
||||
|
||||
StreamingDestinations destinations;
|
||||
|
@ -175,7 +175,6 @@ namespace stream
|
||||
i2p::data::LeaseSet * m_LeaseSet;
|
||||
bool m_IsPublic;
|
||||
|
||||
CryptoPP::DSA::PrivateKey m_SigningPrivateKey;
|
||||
std::function<void (Stream *)> m_Acceptor;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user