1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <botan/botan.h>
#include <botan/gost_3410.h>
#include <botan/gost_3411.h>
#include "dnssecinfra.hh"
using namespace Botan;
/* Государственный гимн Российской Федерации
(Gosudarstvenny Gimn Rossiyskoy Federatsii)
"The National Anthem of the Russian Federation"
~ Rossiya - svyashchennaya nasha derzhava, ~
~ Rossiya - lyubimaya nasha strana. ~
~ Moguchaya volya, velikaya slava - ~
~ Tvoyo dostoyanye na vse vremena! ~
*/
class GOSTDNSCryptoKeyEngine : public DNSCryptoKeyEngine
{
public:
explicit GOSTDNSCryptoKeyEngine(unsigned int algorithm) : DNSCryptoKeyEngine(algorithm) {}
// XXX FIXME NEEDS COPY CONSTRUCTOR SO WE DON'T SHARE KEYS
~GOSTDNSCryptoKeyEngine(){}
void create(unsigned int bits);
string getName() const { return "Botan 1.10 GOST"; }
storvector_t convertToISCVector() const;
std::string getPubKeyHash() const;
std::string sign(const std::string& hash) const;
std::string hash(const std::string& hash) const;
bool verify(const std::string& hash, const std::string& signature) const;
std::string getPublicKeyString() const;
int getBits() const;
void fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& content);
void fromPublicKeyString(const std::string& content);
void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw)
{}
static DNSCryptoKeyEngine* maker(unsigned int algorithm)
{
return new GOSTDNSCryptoKeyEngine(algorithm);
}
private:
shared_ptr<GOST_3410_PrivateKey> d_key;
shared_ptr<GOST_3410_PublicKey> d_pubkey;
};
/*
~ Slav'sya, Otechestvo nashe svobodnoye, ~
~ Bratskikh narodov soyuz vekovoy, ~
~ Predkami dannaya mudrost' narodnaya! ~
~ Slav'sya, strana! My gordimsya toboy! ~
*/
void GOSTDNSCryptoKeyEngine::create(unsigned int bits)
{
AutoSeeded_RNG rng;
EC_Domain_Params params("1.2.643.2.2.35.1");
d_key = shared_ptr<GOST_3410_PrivateKey>(new GOST_3410_PrivateKey(rng, params));
}
int GOSTDNSCryptoKeyEngine::getBits() const
{
return 256;
}
/*
~ Ot yuzhnykh morey do polyarnogo kraya ~
~ Raskinulis' nashi lesa i polya. ~
~ Odna ty na svete! Odna ty takaya - ~
~ Khranimaya Bogom rodnaya zemlya! ~
*/
DNSCryptoKeyEngine::storvector_t GOSTDNSCryptoKeyEngine::convertToISCVector() const
{
storvector_t storvect;
storvect.push_back(make_pair("Algorithm", "12 (ECC-GOST)"));
unsigned char asn1Prefix[]=
{0x30, 0x45, 0x02, 0x01, 0x00, 0x30, 0x1c, 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02,
0x13, 0x30, 0x12, 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01, 0x06, 0x07,
0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01, 0x04, 0x22, 0x04, 0x20}; // this is DER, fixed for a 32 byte key
SecureVector<byte> buffer=BigInt::encode(d_key->private_value());
string gostasn1((const char*)asn1Prefix, sizeof(asn1Prefix));
gostasn1.append((const char*)&*buffer.begin(), (const char*)&*buffer.end());
storvect.push_back(make_pair("GostAsn1", gostasn1));
return storvect;
}
/*
~ Slav'sya, Otechestvo nashe svobodnoye, ~
~ Bratskikh narodov soyuz vekovoy, ~
~ Predkami dannaya mudrost' narodnaya! ~
~ Slav'sya, strana! My gordimsya toboy! ~
*/
void GOSTDNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap )
{
drc.d_algorithm = pdns_stou(stormap["algorithm"]);
string privateKey=stormap["gostasn1"];
//cerr<<"PrivateKey.size() = "<<privateKey.size()<<endl;
//cerr<<makeHexDump(string(privateKey.c_str(), 39))<<endl;
string rawKey(privateKey.c_str()+39, privateKey.length()-39);
for(size_t i = 0; i < rawKey.size() / 2; ++i)
{
std::swap(rawKey[i], rawKey[rawKey.size()-1-i]);
}
BigInt bigint((byte*)rawKey.c_str(), rawKey.size());
EC_Group params("1.2.643.2.2.35.1");
AutoSeeded_RNG rng;
d_key=shared_ptr<GOST_3410_PrivateKey>(new GOST_3410_PrivateKey(rng, params, bigint));
//cerr<<"Is the just imported key on the curve? " << d_key->public_point().on_the_curve()<<endl;
//cerr<<"Is the just imported key zero? " << d_key->public_point().is_zero()<<endl;
const BigInt&x = d_key->private_value();
SecureVector<byte> buffer=BigInt::encode(x);
// cerr<<"And out again! "<<makeHexDump(string((const char*)buffer.begin(), (const char*)buffer.end()))<<endl;
}
namespace {
BigInt decode_le(const byte msg[], size_t msg_len)
{
SecureVector<byte> msg_le(msg, msg_len);
for(size_t i = 0; i != msg_le.size() / 2; ++i)
std::swap(msg_le[i], msg_le[msg_le.size()-1-i]);
return BigInt(&msg_le[0], msg_le.size());
}
}
void GOSTDNSCryptoKeyEngine::fromPublicKeyString(const std::string& input)
{
BigInt x, y;
x=decode_le((const byte*)input.c_str(), input.length()/2);
y=decode_le((const byte*)input.c_str() + input.length()/2, input.length()/2);
EC_Domain_Params params("1.2.643.2.2.35.1");
PointGFp point(params.get_curve(), x,y);
d_pubkey = shared_ptr<GOST_3410_PublicKey>(new GOST_3410_PublicKey(params, point));
d_key.reset();
}
std::string GOSTDNSCryptoKeyEngine::getPubKeyHash() const
{
const BigInt&x = d_key->private_value();
SecureVector<byte> buffer=BigInt::encode(x);
return string((const char*)buffer.begin(), (const char*)buffer.end());
}
std::string GOSTDNSCryptoKeyEngine::getPublicKeyString() const
{
const BigInt&x =d_key->public_point().get_affine_x();
const BigInt&y =d_key->public_point().get_affine_y();
size_t part_size = std::max(x.bytes(), y.bytes());
MemoryVector<byte> bits(2*part_size);
x.binary_encode(&bits[part_size - x.bytes()]);
y.binary_encode(&bits[2*part_size - y.bytes()]);
// Keys are stored in little endian format (WTF)
for(size_t i = 0; i != part_size / 2; ++i)
{
std::swap(bits[i], bits[part_size-1-i]);
std::swap(bits[part_size+i], bits[2*part_size-1-i]);
}
return string((const char*)bits.begin(), (const char*)bits.end());
}
/*
~ Shirokiy prostor dlya mechty i dlya zhizni. ~
~ Gryadushchiye nam otkryvayut goda. ~
~ Nam silu dayot nasha vernost' Otchizne. ~
~ Tak bylo, tak yest' i tak budet vsegda! ~
*/
std::string GOSTDNSCryptoKeyEngine::sign(const std::string& msg) const
{
GOST_3410_Signature_Operation ops(*d_key);
AutoSeeded_RNG rng;
string hash= this->hash(msg);
SecureVector<byte> signature=ops.sign((byte*)hash.c_str(), hash.length(), rng);
#if BOTAN_VERSION_CODE <= BOTAN_VERSION_CODE_FOR(1,9,12) // see http://bit.ly/gTytUf
string reversed((const char*)signature.begin()+ signature.size()/2, signature.size()/2);
reversed.append((const char*)signature.begin(), signature.size()/2);
return reversed;
#else
return string((const char*)signature.begin(), (const char*) signature.end());
#endif
}
std::string GOSTDNSCryptoKeyEngine::hash(const std::string& orig) const
{
SecureVector<byte> result;
GOST_34_11 hasher;
result= hasher.process(orig);
return string((const char*)result.begin(), (const char*) result.end());
}
bool GOSTDNSCryptoKeyEngine::verify(const std::string& message, const std::string& signature) const
{
string hash = this->hash(message);
GOST_3410_PublicKey* pk;
if(d_pubkey) {
pk =d_pubkey.get();
}
else
pk = d_key.get();
GOST_3410_Verification_Operation ops(*pk);
#if BOTAN_VERSION_CODE <= BOTAN_VERSION_CODE_FOR(1,9,12) // see http://bit.ly/gTytUf
string rsignature(signature.substr(32));
rsignature.append(signature.substr(0,32));
return ops.verify ((byte*)hash.c_str(), hash.length(), (byte*)rsignature.c_str(), rsignature.length());
#else
return ops.verify ((byte*)hash.c_str(), hash.length(), (byte*)signature.c_str(), signature.length());
#endif
}
/*
~ Slav'sya, Otechestvo nashe svobodnoye, ~
~ Bratskikh narodov soyuz vekovoy, ~
~ Predkami dannaya mudrost' narodnaya! ~
~ Slav'sya, strana! My gordimsya toboy! ~
*/
//////////////////////////////
namespace {
struct LoaderStruct
{
LoaderStruct()
{
new Botan::LibraryInitializer("thread_safe=true");
// this leaks, but is fine
Botan::global_state().set_default_allocator("malloc"); // the other Botan allocator slows down for us
DNSCryptoKeyEngine::report(12, &GOSTDNSCryptoKeyEngine::maker);
}
} loaderBotan110;
}
|