1
#ifdef HAVE_LIBPOLARSSLSSL
1
2
#include <polarssl/rsa.h>
2
3
#include <polarssl/base64.h>
3
4
#include <polarssl/sha1.h>
4
5
#include <polarssl/sha2.h>
5
6
#include <polarssl/sha4.h>
6
7
#include <polarssl/havege.h>
9
#include "ext/polarssl-1.1.2/include/polarssl/rsa.h"
10
#include "ext/polarssl-1.1.2/include/polarssl/base64.h"
11
#include "ext/polarssl-1.1.2/include/polarssl/sha1.h"
12
#include "ext/polarssl-1.1.2/include/polarssl/sha2.h"
13
#include "ext/polarssl-1.1.2/include/polarssl/sha4.h"
14
#include "ext/polarssl-1.1.2/include/polarssl/havege.h"
7
16
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
8
17
#include <boost/foreach.hpp>
9
18
#include "dnssecinfra.hh"
10
19
using namespace boost::assign;
12
#define PDNSSEC_MI(x) mpi_init(&d_context.x, 0)
21
#define PDNSSEC_MI(x) mpi_init(&d_context.x)
13
22
#define PDNSSEC_MC(x) PDNSSEC_MI(x); mpi_copy(&d_context.x, const_cast<mpi*>(&orig.d_context.x))
14
#define PDNSSEC_MF(x) mpi_free(&d_context.x, 0)
23
#define PDNSSEC_MF(x) mpi_free(&d_context.x)
16
25
class RSADNSCryptoKeyEngine : public DNSCryptoKeyEngine
40
49
RSADNSCryptoKeyEngine(const RSADNSCryptoKeyEngine& orig) : DNSCryptoKeyEngine(orig.d_algorithm)
42
d_context.ver = orig.d_context.ver;
51
// this part is a little bit scary.. we make a 'deep copy' of an RSA state, and polarssl isn't helping us so we delve into thr struct
52
d_context.ver = orig.d_context.ver;
43
53
d_context.len = orig.d_context.len;
45
55
d_context.padding = orig.d_context.padding;
46
56
d_context.hash_id = orig.d_context.hash_id;
47
d_context.f_rng = orig.d_context.f_rng;
48
d_context.p_rng = orig.d_context.p_rng;
51
59
PDNSSEC_MC(E); PDNSSEC_MC(D); PDNSSEC_MC(P); PDNSSEC_MC(Q); PDNSSEC_MC(DP); PDNSSEC_MC(DQ); PDNSSEC_MC(QP); PDNSSEC_MC(RN); PDNSSEC_MC(RP); PDNSSEC_MC(RQ);
108
116
havege_init( &hs );
110
rsa_init(&d_context, RSA_PKCS_V15, 0, havege_rand, &hs ); // FIXME this leaks memory
111
int ret=rsa_gen_key(&d_context, bits, 65537);
118
rsa_init(&d_context, RSA_PKCS_V15, 0); // FIXME this leaks memory (it does?)
119
int ret=rsa_gen_key(&d_context, havege_random, (void*)&hs, bits, 65537);
113
121
throw runtime_error("Key generation failed");
142
150
hashKind = SIG_RSA_SHA512;
144
int ret=rsa_pkcs1_sign(const_cast<rsa_context*>(&d_context), RSA_PRIVATE,
152
int ret=rsa_pkcs1_sign(const_cast<rsa_context*>(&d_context), NULL, NULL, RSA_PRIVATE,
147
155
(const unsigned char*) hash.c_str(), signature);
234
242
typedef map<string, mpi*> places_t;
237
rsa_init(&d_context, RSA_PKCS_V15, 0, NULL, NULL );
245
rsa_init(&d_context, RSA_PKCS_V15, 0);
239
247
places["Modulus"]=&d_context.N;
240
248
places["PublicExponent"]=&d_context.E;
265
273
cerr<<"Got "<<integers.size()<<" integers"<<endl;
266
274
map<int, mpi*> places;
268
rsa_init(&d_context, RSA_PKCS_V15, 0, NULL, NULL );
276
rsa_init(&d_context, RSA_PKCS_V15, 0);
270
278
places[1]=&d_context.N;
271
279
places[2]=&d_context.E;
306
314
void RSADNSCryptoKeyEngine::fromPublicKeyString(const std::string& rawString)
308
rsa_init(&d_context, RSA_PKCS_V15, 0, NULL, NULL );
316
rsa_init(&d_context, RSA_PKCS_V15, 0);
309
317
string exponent, modulus;
310
318
const unsigned char* raw = (const unsigned char*)rawString.c_str();