Changeset 1913

Show
Ignore:
Timestamp:
01/26/11 22:01:22 (2 years ago)
Author:
ahu
Message:

finish up support for GOST, including DS with digest type=3, plus abstract out relevant hashes to the signer objects.
Plus update the formatting of the Russian anthem in botan19signers.cc ;-)

Location:
trunk/pdns/pdns
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/botan19signers.cc

    r1912 r1913  
     1// utf-8 UTF-8 utf8 UTF8 
    12#include <botan/botan.h> 
    23#include <botan/ecdsa.h> 
    34#include <botan/gost_3410.h> 
     5#include <botan/gost_3411.h> 
     6#include <botan/sha2_32.h> 
     7#include <botan/sha2_64.h> 
    48#include <botan/pubkey.h> 
    59#include <botan/look_pk.h> 
     
    1216    "The National Anthem of the Russian Federation" 
    1317     
    14  ~      Rossiya - svyashchennaya nasha derzhava,  ~ 
     18 ~  Rossiya - svyashchennaya nasha derzhava,  ~ 
    1519 ~  Rossiya - lyubimaya nasha strana.         ~ 
    1620 ~  Moguchaya volya, velikaya slava -         ~ 
     
    2529  std::string getPubKeyHash() const; 
    2630  std::string sign(const std::string& hash) const;  
     31  std::string hash(const std::string& hash) const;  
    2732  bool verify(const std::string& hash, const std::string& signature) const; 
    2833  std::string getPublicKeyString() const; 
     
    184189} 
    185190 
     191std::string GOSTDNSPrivateKey::hash(const std::string& orig) const 
     192{ 
     193  SecureVector<byte> result; 
     194   
     195  GOST_34_11 hasher; 
     196  result= hasher.process(orig); 
     197   
     198  return string((const char*)result.begin(), (const char*) result.end()); 
     199} 
     200 
    186201bool GOSTDNSPrivateKey::verify(const std::string& hash, const std::string& signature) const 
    187202{ 
     
    207222  std::string getPubKeyHash() const; 
    208223  std::string sign(const std::string& hash) const;  
     224  std::string hash(const std::string& hash) const;  
    209225  bool verify(const std::string& hash, const std::string& signature) const; 
    210226  std::string getPublicKeyString() const; 
     
    344360} 
    345361 
     362std::string ECDSADNSPrivateKey::hash(const std::string& orig) const 
     363{ 
     364  SecureVector<byte> result; 
     365  if(getBits() == 256) { // SHA256 
     366    SHA_256 hasher; 
     367    result= hasher.process(orig); 
     368  } 
     369  else { // SHA384 
     370    SHA_384 hasher; 
     371    result = hasher.process(orig); 
     372  } 
     373   
     374  return string((const char*)result.begin(), (const char*) result.end()); 
     375} 
     376 
     377 
    346378bool ECDSADNSPrivateKey::verify(const std::string& hash, const std::string& signature) const 
    347379{ 
  • trunk/pdns/pdns/dnssecinfra.cc

    r1909 r1913  
    1010#include "dnssecinfra.hh"  
    1111#include "dnsseckeeper.hh" 
    12  
    13 #include <polarssl/sha1.h> 
    14 #include <polarssl/sha2.h> 
    15 #include <polarssl/sha4.h> 
     12#include "polarssl/sha1.h" 
    1613#include <boost/assign/std/vector.hpp> // for 'operator+=()' 
    1714#include <boost/assign/list_inserter.hpp> 
     
    122119  } 
    123120   
    124   // algorithm 12 needs special GOST hash 
    125    
    126   if(rrc.d_algorithm <= 7 ) {  // RSASHA1 
    127     unsigned char hash[20]; 
    128     sha1((unsigned char*)toHash.c_str(), toHash.length(), hash); 
    129     return string((char*)hash, sizeof(hash)); 
    130   } else if(rrc.d_algorithm == 8 || rrc.d_algorithm == 13) { // RSASHA256 or ECDSAP256 
    131     unsigned char hash[32]; 
    132     sha2((unsigned char*)toHash.c_str(), toHash.length(), hash, 0); 
    133     return string((char*)hash, sizeof(hash)); 
    134   } else if(rrc.d_algorithm == 10) { // RSASHA512 
    135     unsigned char hash[64]; 
    136     sha4((unsigned char*)toHash.c_str(), toHash.length(), hash, 0); 
    137     return string((char*)hash, sizeof(hash)); 
    138   } else if(rrc.d_algorithm == 14) { // ECDSAP384 
    139     unsigned char hash[48]; 
    140     sha4((unsigned char*)toHash.c_str(), toHash.length(), hash, 1); // == 384 
    141     return string((char*)hash, sizeof(hash)); 
    142   } 
    143   else { 
    144     cerr<<"No idea how to hash for algorithm "<<(int)rrc.d_algorithm<<endl; 
    145     exit(1); 
    146   } 
     121  shared_ptr<DNSPrivateKey> dpk(DNSPrivateKey::make(rrc.d_algorithm)); 
     122  return dpk->hash(toHash); 
    147123} 
    148124 
     
    153129  toHash.append(const_cast<DNSKEYRecordContent&>(drc).serialize("", true, true)); 
    154130 
    155   unsigned char hash[32]; 
    156   if(digest==1) 
    157     sha1((unsigned char*)toHash.c_str(), toHash.length(), hash); 
    158   else 
    159     sha2((unsigned char*)toHash.c_str(), toHash.length(), hash, 0); 
    160  
     131   
    161132  DSRecordContent dsrc; 
     133  if(digest==1) { 
     134    shared_ptr<DNSPrivateKey> dpk(DNSPrivateKey::make(5)); // gives us SHA1 
     135    dsrc.d_digest = dpk->hash(toHash); 
     136  } 
     137  else if(digest == 2) { 
     138    shared_ptr<DNSPrivateKey> dpk(DNSPrivateKey::make(8)); // gives us SHA256 
     139    dsrc.d_digest = dpk->hash(toHash); 
     140  } 
     141  else if(digest == 3) { 
     142    shared_ptr<DNSPrivateKey> dpk(DNSPrivateKey::make(12)); // gives us GOST 
     143    dsrc.d_digest = dpk->hash(toHash); 
     144  } 
     145   
    162146  dsrc.d_algorithm= drc.d_algorithm; 
    163147  dsrc.d_digesttype=digest; 
    164148  dsrc.d_tag=const_cast<DNSKEYRecordContent&>(drc).getTag(); 
    165   dsrc.d_digest.assign((const char*)hash, digest == 1 ? 20 : 32); 
    166149  return dsrc; 
    167150} 
  • trunk/pdns/pdns/dnssecinfra.hh

    r1909 r1913  
    1515    virtual std::string getPubKeyHash()const =0; 
    1616    virtual std::string sign(const std::string& hash) const =0; 
     17    virtual std::string hash(const std::string& hash) const =0; 
    1718    virtual std::string getPublicKeyString()const =0; 
    1819    virtual int getBits() const =0; 
  • trunk/pdns/pdns/pdnssec.cc

    r1912 r1913  
    207207        cout<<"KSK DNSKEY = "<<zone<<" IN DNSKEY "<< value.first.getDNSKEY().getZoneRepresentation() << endl; 
    208208        cout<<"DS = "<<zone<<" IN DS "<<makeDSFromDNSKey(zone, value.first.getDNSKEY(), 1).getZoneRepresentation() << endl; 
    209         cout<<"DS = "<<zone<<" IN DS "<<makeDSFromDNSKey(zone, value.first.getDNSKEY(), 2).getZoneRepresentation() << endl << endl; 
     209        cout<<"DS = "<<zone<<" IN DS "<<makeDSFromDNSKey(zone, value.first.getDNSKEY(), 2).getZoneRepresentation() << endl; 
     210        cout<<"DS = "<<zone<<" IN DS "<<makeDSFromDNSKey(zone, value.first.getDNSKEY(), 3).getZoneRepresentation() << endl << endl; 
    210211      } 
    211212    } 
  • trunk/pdns/pdns/polarrsakeyinfra.cc

    r1909 r1913  
    33#include <polarssl/sha1.h> 
    44#include <polarssl/sha2.h> 
     5#include <polarssl/sha4.h> 
    56#include <polarssl/havege.h> 
    67#include <boost/assign/std/vector.hpp> // for 'operator+=()' 
     
    1617{ 
    1718public: 
    18   RSADNSPrivateKey() 
     19  explicit RSADNSPrivateKey(unsigned int algorithm) : d_algorithm(algorithm) 
    1920  { 
    2021    memset(&d_context, 0, sizeof(d_context)); 
     
    3738  RSADNSPrivateKey(const RSADNSPrivateKey& orig)  
    3839  { 
     40    d_algorithm = orig.d_algorithm; 
     41     
    3942    d_context.ver = orig.d_context.ver; 
    4043    d_context.len = orig.d_context.len; 
     
    5154  RSADNSPrivateKey& operator=(const RSADNSPrivateKey& orig)  
    5255  { 
     56    d_algorithm = orig.d_algorithm; 
     57     
    5358    d_context.ver = orig.d_context.ver; 
    5459    d_context.len = orig.d_context.len; 
     
    8186  std::string getPubKeyHash() const; 
    8287  std::string sign(const std::string& hash) const;  
     88  std::string hash(const std::string& hash) const;  
    8389  std::string getPublicKeyString() const; 
    8490  int getBits() const 
     
    8995  void fromPEMString(DNSKEYRecordContent& drc, const std::string& raw); 
    9096 
    91   static DNSPrivateKey* maker(unsigned int) 
    92   { 
    93     return new RSADNSPrivateKey(); 
     97  static DNSPrivateKey* maker(unsigned int algorithm) 
     98  { 
     99    return new RSADNSPrivateKey(algorithm); 
    94100  } 
    95101 
    96102private: 
    97103  rsa_context d_context; 
     104  unsigned int d_algorithm; 
    98105}; 
    99106 
     
    159166  return string((char*) signature, sizeof(signature)); 
    160167} 
     168 
     169std::string RSADNSPrivateKey::hash(const std::string& toHash) const 
     170{ 
     171  if(d_algorithm <= 7 ) {  // RSASHA1 
     172    unsigned char hash[20]; 
     173    sha1((unsigned char*)toHash.c_str(), toHash.length(), hash); 
     174    return string((char*)hash, sizeof(hash)); 
     175  }  
     176  else if(d_algorithm == 8) { // RSASHA256 
     177    unsigned char hash[32]; 
     178    sha2((unsigned char*)toHash.c_str(), toHash.length(), hash, 0); 
     179    return string((char*)hash, sizeof(hash)); 
     180  }  
     181  else if(d_algorithm == 10) { // RSASHA512 
     182    unsigned char hash[64]; 
     183    sha4((unsigned char*)toHash.c_str(), toHash.length(), hash, 0); 
     184    return string((char*)hash, sizeof(hash)); 
     185  } 
     186  throw runtime_error("PolarSSL hashing method can't hash algorithm "+lexical_cast<string>(d_algorithm)); 
     187} 
     188 
    161189 
    162190std::string RSADNSPrivateKey::convertToISC(unsigned int algorithm) const