Changeset 1951
- Timestamp:
- 01/31/11 22:59:45 (2 years ago)
- Location:
- trunk/pdns/pdns
- Files:
-
- 1 added
- 10 modified
-
Makefile.am (modified) (2 diffs)
-
botan18signers.cc (modified) (7 diffs)
-
botan19signers.cc (modified) (9 diffs)
-
botansigners.cc (added)
-
communicator.cc (modified) (1 diff)
-
dnssecinfra.cc (modified) (5 diffs)
-
dnssecinfra.hh (modified) (3 diffs)
-
dnssecsigner.cc (modified) (2 diffs)
-
mastercommunicator.cc (modified) (1 diff)
-
pdnssec.cc (modified) (1 diff)
-
polarrsakeyinfra.cc (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pdns/pdns/Makefile.am
r1949 r1951 48 48 49 49 if BOTAN19 50 pdns_server_SOURCES += botan19signers.cc 50 pdns_server_SOURCES += botan19signers.cc botansigners.cc 51 51 pdns_server_LDADD += -lbotan -lgmp 52 52 endif 53 53 54 54 if BOTAN18 55 pdns_server_SOURCES += botan18signers.cc 55 pdns_server_SOURCES += botan18signers.cc botansigners.cc 56 56 pdns_server_LDADD += -lbotan -lgmp 57 57 endif … … 72 72 73 73 if BOTAN19 74 pdnssec_SOURCES += botan19signers.cc 74 pdnssec_SOURCES += botan19signers.cc botansigners.cc 75 75 pdnssec_LDADD += -lbotan -lgmp 76 76 endif 77 77 78 78 if BOTAN18 79 pdnssec_SOURCES += botan18signers.cc 79 pdnssec_SOURCES += botan18signers.cc botansigners.cc 80 80 pdnssec_LDADD += -lbotan -lgmp 81 81 endif -
trunk/pdns/pdns/botan18signers.cc
r1947 r1951 11 11 using namespace Botan; 12 12 13 //////////////////////////////14 15 13 class ECDSADNSPrivateKey : public DNSPrivateKey 16 14 { … … 23 21 std::string sign(const std::string& hash) const; 24 22 std::string hash(const std::string& hash) const; 25 bool verify(const std::string& hash, const std::string& signature) const;23 bool verify(const std::string& msg, const std::string& signature) const; 26 24 std::string getPublicKeyString() const; 27 25 int getBits() const; 28 void fromISC String(DNSKEYRecordContent& drc, const std::string& content);26 void fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap); 29 27 void fromPublicKeyString(unsigned int algorithm, const std::string& content); 30 28 void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw) … … 60 58 } 61 59 d_key = shared_ptr<ECDSA_PrivateKey>(new ECDSA_PrivateKey(rng, getECParams((bits == 256) ? 13 : 14))); 62 63 60 64 61 PKCS8_Encoder* pk8e= d_key->pkcs8_encoder(); … … 106 103 } 107 104 108 void ECDSADNSPrivateKey::fromISC String(DNSKEYRecordContent& drc, const std::string& content)105 void ECDSADNSPrivateKey::fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap ) 109 106 { 110 107 /*Private-key-format: v1.2 111 108 Algorithm: 13 (ECDSAP256SHA256) 112 109 PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ= */ 113 114 istringstream input(content); 115 string sline, key, value, privateKey; 116 while(getline(input, sline)) { 117 tie(key,value)=splitField(sline, ':'); 118 trim(value); 119 if(pdns_iequals(key,"Private-key-format")) {} 120 else if(key=="Algorithm") 121 drc.d_algorithm = atoi(value.c_str()); 122 else if(key=="PrivateKey") { 123 Pipe pipe(new Base64_Decoder); 124 pipe.process_msg(value); 125 privateKey=pipe.read_all_as_string(); 126 } 127 else 128 throw runtime_error("Unknown field '"+key+"' in Private Key Representation of ECDSA"); 129 } 130 d_algorithm = drc.d_algorithm; 110 111 d_algorithm = drc.d_algorithm = atoi(stormap["algorithm"].c_str()); 112 string privateKey = stormap["privatekey"]; 113 131 114 BigInt bigint((byte*)privateKey.c_str(), privateKey.length()); 132 115 … … 150 133 151 134 MemoryVector<byte> tmp((byte*)noIdea.c_str(), noIdea.length()); 152 cerr<<"key_bits"<<endl;153 135 p8e->key_bits(tmp); 154 cerr<<"Done reading"<<endl;155 136 delete p8e; 156 137 } … … 193 174 } 194 175 195 196 std::string ECDSADNSPrivateKey::sign(const std::string& hash) const 176 std::string ECDSADNSPrivateKey::sign(const std::string& msg) const 197 177 { 198 178 AutoSeeded_RNG rng; 179 string hash = this->hash(msg); 199 180 SecureVector<byte> signature=d_key->sign((byte*)hash.c_str(), hash.length(), rng); 200 181 … … 217 198 } 218 199 219 220 bool ECDSADNSPrivateKey::verify(const std::string& hash, const std::string& signature) const 221 { 222 ECDSA_PublicKey* key; 223 if(d_key) 224 key = d_key.get(); 225 else 226 key = d_pubkey.get(); 227 200 bool ECDSADNSPrivateKey::verify(const std::string& msg, const std::string& signature) const 201 { 202 string hash = this->hash(msg); 203 ECDSA_PublicKey* key = d_key ? d_key.get() : d_pubkey.get(); 228 204 return key->verify((byte*)hash.c_str(), hash.length(), (byte*)signature.c_str(), signature.length()); 229 205 } 230 231 206 namespace { 232 207 struct LoaderStruct -
trunk/pdns/pdns/botan19signers.cc
r1920 r1951 33 33 std::string getPublicKeyString() const; 34 34 int getBits() const; 35 void fromISC String(DNSKEYRecordContent& drc, const std::string& content);35 void fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& content); 36 36 void fromPublicKeyString(unsigned int algorithm, const std::string& content); 37 37 void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw) … … 106 106 107 107 108 void GOSTDNSPrivateKey::fromISC String(DNSKEYRecordContent& drc, const std::string& content)108 void GOSTDNSPrivateKey::fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap ) 109 109 { 110 istringstream input(content); 111 string sline, key, value, privateKey; 112 while(getline(input, sline)) { 113 tie(key,value)=splitField(sline, ':'); 114 trim(value); 115 if(pdns_iequals(key,"Private-key-format")) {} 116 else if(key=="Algorithm") 117 drc.d_algorithm = atoi(value.c_str()); 118 else if(key=="GostAsn1") { 119 Pipe pipe(new Base64_Decoder); 120 pipe.process_msg(value); 121 privateKey=pipe.read_all_as_string(); 122 } 123 else 124 throw runtime_error("Unknown field '"+key+"' in Private Key Representation of GOST"); 125 } 110 drc.d_algorithm = atoi(stormap["algorithm"].c_str()); 111 string privateKey=stormap["gostasn1"]; 126 112 //cerr<<"PrivateKey.size() = "<<privateKey.size()<<endl; 127 113 //cerr<<makeHexDump(string(privateKey.c_str(), 39))<<endl; … … 207 193 */ 208 194 209 std::string GOSTDNSPrivateKey::sign(const std::string& hash) const195 std::string GOSTDNSPrivateKey::sign(const std::string& msg) const 210 196 { 211 197 GOST_3410_Signature_Operation ops(*d_key); 212 198 AutoSeeded_RNG rng; 199 200 string hash= this->hash(msg); 201 213 202 SecureVector<byte> signature=ops.sign((byte*)hash.c_str(), hash.length(), rng); 214 203 … … 232 221 233 222 234 bool GOSTDNSPrivateKey::verify(const std::string& hash, const std::string& signature) const 235 { 223 bool GOSTDNSPrivateKey::verify(const std::string& message, const std::string& signature) const 224 { 225 string hash = this->hash(message); 236 226 GOST_3410_PublicKey* pk; 237 227 if(d_pubkey) { … … 274 264 std::string getPublicKeyString() const; 275 265 int getBits() const; 276 void fromISC String(DNSKEYRecordContent& drc, const std::string& content);266 void fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap); 277 267 void fromPublicKeyString(unsigned int algorithm, const std::string& content); 278 268 void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw) … … 352 342 } 353 343 354 void ECDSADNSPrivateKey::fromISC String(DNSKEYRecordContent& drc, const std::string& content)344 void ECDSADNSPrivateKey::fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap) 355 345 { 356 346 /*Private-key-format: v1.2 … … 358 348 PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ= */ 359 349 360 istringstream input(content); 361 string sline, key, value, privateKey; 362 while(getline(input, sline)) { 363 tie(key,value)=splitField(sline, ':'); 364 trim(value); 365 if(pdns_iequals(key,"Private-key-format")) {} 366 else if(key=="Algorithm") 367 drc.d_algorithm = atoi(value.c_str()); 368 else if(key=="PrivateKey") { 369 Pipe pipe(new Base64_Decoder); 370 pipe.process_msg(value); 371 privateKey=pipe.read_all_as_string(); 372 } 373 else 374 throw runtime_error("Unknown field '"+key+"' in Private Key Representation of ECDSA"); 375 } 350 drc.d_algorithm = atoi(stormap["algorithm"].c_str()); 351 string privateKey=stormap["privatekey"]; 352 376 353 d_algorithm = drc.d_algorithm; 377 354 BigInt bigint((byte*)privateKey.c_str(), privateKey.length()); … … 379 356 EC_Domain_Params params=getECParams(drc.d_algorithm); 380 357 d_key=shared_ptr<ECDSA_PrivateKey>(new ECDSA_PrivateKey(params, bigint)); 381 382 358 } 383 359 … … 418 394 419 395 420 std::string ECDSADNSPrivateKey::sign(const std::string& hash) const 421 { 396 std::string ECDSADNSPrivateKey::sign(const std::string& msg) const 397 { 398 string hash = this->hash(msg); 422 399 ECDSA_Signature_Operation ops(*d_key); 423 400 AutoSeeded_RNG rng; -
trunk/pdns/pdns/communicator.cc
r1859 r1951 1 1 /* 2 2 PowerDNS Versatile Database Driven Nameserver 3 Copyright (C) 2002-20 09PowerDNS.COM BV3 Copyright (C) 2002-2011 PowerDNS.COM BV 4 4 5 5 This program is free software; you can redistribute it and/or modify -
trunk/pdns/pdns/dnssecinfra.cc
r1916 r1951 13 13 #include <boost/assign/std/vector.hpp> // for 'operator+=()' 14 14 #include <boost/assign/list_inserter.hpp> 15 #include "base64.hh" 15 16 16 17 using namespace boost; … … 20 21 DNSPrivateKey* DNSPrivateKey::makeFromISCFile(DNSKEYRecordContent& drc, const char* fname) 21 22 { 22 string sline, isc , key, value;23 string sline, isc; 23 24 FILE *fp=fopen(fname, "r"); 24 25 if(!fp) { 25 26 throw runtime_error("Unable to read file '"+string(fname)+"' for generating DNS Private Key"); 26 27 } 27 int algorithm=0;28 28 29 while(stringfgets(fp, sline)) { 30 isc += sline; 31 } 32 fclose(fp); 33 return makeFromISCString(drc, isc); 34 } 35 36 DNSPrivateKey* DNSPrivateKey::makeFromISCString(DNSKEYRecordContent& drc, const std::string& content) 37 { 38 int algorithm = 0; 39 string sline, key, value, raw; 40 istringstream str(content); 41 map<string, string> stormap; 42 while(getline(str, sline)) { 29 43 tie(key,value)=splitField(sline, ':'); 30 if(pdns_iequals(key,"algorithm")) 44 trim(value); 45 if(pdns_iequals(key,"algorithm")) { 31 46 algorithm = atoi(value.c_str()); 32 isc.append(sline); 33 } 34 fclose(fp); 35 47 stormap["algorithm"]=lexical_cast<string>(algorithm); 48 continue; 49 } 50 else if(pdns_iequals(key, "Private-key-format")) 51 continue; 52 raw.clear(); 53 B64Decode(value, raw); 54 stormap[toLower(key)]=raw; 55 } 36 56 DNSPrivateKey* dpk=make(algorithm); 37 dpk->fromISC String(drc, isc);57 dpk->fromISCMap(drc, stormap); 38 58 return dpk; 39 59 } 60 40 61 41 62 DNSPrivateKey* DNSPrivateKey::make(unsigned int algo) … … 50 71 } 51 72 52 void DNSPrivateKey::report(unsigned int algo, maker_t* maker) 53 { 73 void DNSPrivateKey::report(unsigned int algo, maker_t* maker, bool fallback) 74 { 75 if(getMakers().count(algo) && fallback) { 76 return; 77 } 54 78 getMakers()[algo]=maker; 55 }56 DNSPrivateKey* DNSPrivateKey::makeFromISCString(DNSKEYRecordContent& drc, const std::string& content)57 {58 int algorithm = 0;59 string sline, key, value;60 istringstream str(content);61 while(getline(str, sline)) {62 tie(key,value)=splitField(sline, ':');63 if(pdns_iequals(key,"algorithm")) {64 algorithm = atoi(value.c_str());65 break;66 }67 }68 DNSPrivateKey* dpk=make(algorithm);69 dpk->fromISCString(drc, content);70 return dpk;71 79 } 72 80 … … 104 112 } 105 113 106 string get HashForRRSET(const std::string& qname, const RRSIGRecordContent& rrc, vector<shared_ptr<DNSRecordContent> >& signRecords)114 string getMessageForRRSET(const std::string& qname, const RRSIGRecordContent& rrc, vector<shared_ptr<DNSRecordContent> >& signRecords) 107 115 { 108 116 sort(signRecords.begin(), signRecords.end(), sharedDNSSECCompare); … … 126 134 } 127 135 128 shared_ptr<DNSPrivateKey> dpk(DNSPrivateKey::make(rrc.d_algorithm)); 129 return dpk->hash(toHash); 136 return toHash; 130 137 } 131 138 -
trunk/pdns/pdns/dnssecinfra.hh
r1932 r1951 20 20 virtual int getBits() const =0; 21 21 22 virtual void fromISC String(DNSKEYRecordContent& drc, const std::string& content)=0;22 virtual void fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap)=0; 23 23 virtual void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw)=0; 24 24 virtual void fromPublicKeyString(unsigned algorithm, const std::string& content) … … 35 35 typedef DNSPrivateKey* maker_t(unsigned int algorithm); 36 36 37 static void report(unsigned int algorithm, maker_t* maker );37 static void report(unsigned int algorithm, maker_t* maker, bool fallback=false); 38 38 private: 39 39 … … 87 87 88 88 bool sharedDNSSECCompare(const boost::shared_ptr<DNSRecordContent>& a, const shared_ptr<DNSRecordContent>& b); 89 string get HashForRRSET(const std::string& qname, const RRSIGRecordContent& rrc, std::vector<boost::shared_ptr<DNSRecordContent> >& signRecords);89 string getMessageForRRSET(const std::string& qname, const RRSIGRecordContent& rrc, std::vector<boost::shared_ptr<DNSRecordContent> >& signRecords); 90 90 91 91 DSRecordContent makeDSFromDNSKey(const std::string& qname, const DNSKEYRecordContent& drc, int digest=1); -
trunk/pdns/pdns/dnssecsigner.cc
r1932 r1951 115 115 rrc.d_tag = drc.getTag(); 116 116 rrc.d_algorithm = drc.d_algorithm; 117 string realhash=getHashForRRSET(signQName, rrc, toSign); // this is what we sign118 117 119 pair<string, string> lookup(rc->getPubKeyHash(), realhash); 118 string msg=getMessageForRRSET(signQName, rrc, toSign); // this is what we will hash & sign 119 120 pair<string, string> lookup(rc->getPubKeyHash(), msg); // we key on the whole message now! 120 121 121 122 { … … 130 131 } 131 132 132 rrc.d_signature = rc->sign(realhash); 133 //DTime dt; 134 //dt.set(); 135 rrc.d_signature = rc->sign(msg); 136 //cerr<<dt.udiff()<<endl; 133 137 134 138 Lock l(&g_signatures_lock); -
trunk/pdns/pdns/mastercommunicator.cc
r1865 r1951 1 1 /* 2 2 PowerDNS Versatile Database Driven Nameserver 3 Copyright (C) 2002-20 09PowerDNS.COM BV3 Copyright (C) 2002-2011 PowerDNS.COM BV 4 4 5 5 This program is free software; you can redistribute it and/or modify -
trunk/pdns/pdns/pdnssec.cc
r1939 r1951 209 209 } 210 210 211 string hash = getHashForRRSET(qname, rrc, toSign); 211 string msg = getMessageForRRSET(qname, rrc, toSign); 212 DNSPrivateKey* dpk = DNSPrivateKey::make(rrc.d_algorithm); 213 string hash = dpk->sign(msg); 212 214 cerr<<"Verify: "<<DNSPrivateKey::makeFromPublicKeyString(drc.d_algorithm, drc.d_key)->verify(hash, rrc.d_signature)<<endl; 213 215 if(dsrc.d_digesttype) { -
trunk/pdns/pdns/polarrsakeyinfra.cc
r1915 r1951 93 93 return mpi_size(&d_context.N)*8; 94 94 } 95 void fromISC String(DNSKEYRecordContent& drc, const std::string& content);95 void fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap); 96 96 void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw); 97 97 void fromPublicKeyString(unsigned int algorithm, const std::string& raw); … … 145 145 } 146 146 147 std::string RSADNSPrivateKey::sign(const std::string& hash) const 148 { 147 std::string RSADNSPrivateKey::sign(const std::string& msg) const 148 { 149 string hash = this->hash(msg); 149 150 unsigned char signature[mpi_size(&d_context.N)]; 150 151 int hashKind; … … 168 169 } 169 170 170 bool RSADNSPrivateKey::verify(const std::string& hash, const std::string& signature) const171 bool RSADNSPrivateKey::verify(const std::string& msg, const std::string& signature) const 171 172 { 172 173 int hashKind; 174 string hash=this->hash(msg); 173 175 if(hash.size()==20) 174 176 hashKind= SIG_RSA_SHA1; … … 177 179 else 178 180 hashKind = SIG_RSA_SHA512; 181 182 179 183 180 184 int ret=rsa_pkcs1_verify(const_cast<rsa_context*>(&d_context), RSA_PUBLIC, … … 248 252 249 253 250 void RSADNSPrivateKey::fromISC String(DNSKEYRecordContent& drc, const std::string& content)254 void RSADNSPrivateKey::fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap) 251 255 { 252 256 string sline; 253 257 string key,value; 254 map<string, mpi*> places; 258 typedef map<string, mpi*> places_t; 259 places_t places; 255 260 256 261 rsa_init(&d_context, RSA_PKCS_V15, 0, NULL, NULL ); … … 264 269 places["Exponent2"]=&d_context.DQ; 265 270 places["Coefficient"]=&d_context.QP; 266 267 string modulus, exponent; 268 istringstream str(content); 269 unsigned char decoded[1024]; 270 while(getline(str, sline)) { 271 tie(key,value)=splitField(sline, ':'); 272 trim(value); 273 274 if(places.count(key)) { 275 if(places[key]) { 276 int len=sizeof(decoded); 277 if(base64_decode(decoded, &len, (unsigned char*)value.c_str(), value.length()) < 0) { 278 cerr<<"Error base64 decoding '"<<value<<"'\n"; 279 exit(1); 280 } 281 // B64Decode(value, decoded); 282 // cerr<<key<<" decoded.length(): "<<8*len<<endl; 283 mpi_read_binary(places[key], decoded, len); 284 if(key=="Modulus") 285 modulus.assign((const char*)decoded,len); 286 if(key=="PublicExponent") 287 exponent.assign((const char*)decoded,len); 288 } 289 } 290 else { 291 if(key == "Algorithm") 292 drc.d_algorithm = atoi(value.c_str()); 293 else if(key != "Private-key-format") 294 cerr<<"Unknown field '"<<key<<"'\n"; 295 } 296 } 271 272 drc.d_algorithm = atoi(stormap["algorithm"].c_str()); 273 274 string raw; 275 BOOST_FOREACH(const places_t::value_type& val, places) { 276 raw=stormap[toLower(val.first)]; 277 mpi_read_binary(val.second, (unsigned char*) raw.c_str(), raw.length()); 278 } 279 297 280 d_context.len = ( mpi_msb( &d_context.N ) + 7 ) >> 3; // no clue what this does 298 299 if(exponent.length() < 255) 300 drc.d_key.assign(1, (char) (unsigned int) exponent.length()); 301 else { 302 drc.d_key.assign(1, 0); 303 uint16_t len=htons(exponent.length()); 304 drc.d_key.append((char*)&len, 2); 305 } 306 drc.d_key.append(exponent); 307 drc.d_key.append(modulus); 281 drc.d_key = this->getPublicKeyString(); 308 282 drc.d_protocol=3; 309 283 } … … 394 368 return keystring; 395 369 } 370 396 371 namespace { 397 372 struct LoaderStruct … … 399 374 LoaderStruct() 400 375 { 401 DNSPrivateKey::report(5, &RSADNSPrivateKey::maker );402 DNSPrivateKey::report(7, &RSADNSPrivateKey::maker );403 DNSPrivateKey::report(8, &RSADNSPrivateKey::maker );404 DNSPrivateKey::report(10, &RSADNSPrivateKey::maker );376 DNSPrivateKey::report(5, &RSADNSPrivateKey::maker, true); 377 DNSPrivateKey::report(7, &RSADNSPrivateKey::maker, true); 378 DNSPrivateKey::report(8, &RSADNSPrivateKey::maker, true); 379 DNSPrivateKey::report(10, &RSADNSPrivateKey::maker, true); 405 380 } 406 381 } loader; 407 382 } 383