summaryrefslogtreecommitdiff
path: root/cpp/src/IceSSL/RSACertificateGen.cpp
blob: 46cfde37851a97cc6723b830a338e5f7fbc739c2 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/Config.h>
#include <IceSSL/RSACertificateGen.h>
#include <IceSSL/OpenSSLJanitors.h>
#include <IceSSL/RSAKeyPair.h>
#include <IceSSL/RSAPrivateKey.h>
#include <IceSSL/RSAPublicKey.h>
#include <IceSSL/Exception.h>
#include <IceSSL/OpenSSLUtils.h>
#include <openssl/err.h>
#include <openssl/ssl.h>

using std::string;
using std::back_inserter;

IceSSL::RSACertificateGenContext::RSACertificateGenContext() :
    _modulusLength(0),
    _secondsValid(0),
    _issuedAdjustment(0)
{
}

IceSSL::RSACertificateGenContext::~RSACertificateGenContext()
{
}

long
IceSSL::RSACertificateGenContext::minutesToSeconds(long minutes)
{
    return minutes * 60L;
}

long
IceSSL::RSACertificateGenContext::hoursToSeconds(long hours)
{
    return minutesToSeconds(hours * 60L);
}

long
IceSSL::RSACertificateGenContext::daysToSeconds(long days)
{
    return hoursToSeconds(days * 24L);
}

long
IceSSL::RSACertificateGenContext::weeksToSeconds(long weeks)
{
    return daysToSeconds(weeks * 7L);
}

long
IceSSL::RSACertificateGenContext::yearsToSeconds(long years)
{
    return weeksToSeconds(years * 365L);
}

void
IceSSL::RSACertificateGenContext::setCountry(const string& country)
{
    _country = country;
}

void
IceSSL::RSACertificateGenContext::setStateProvince(const string& stateProvince)
{
    _stateProvince = stateProvince;
}

void
IceSSL::RSACertificateGenContext::setLocality(const string& locality)
{
    _locality = locality;
}

void
IceSSL::RSACertificateGenContext::setOrganization(const string& organization)
{
    _organization = organization;
}

void
IceSSL::RSACertificateGenContext::setOrgainizationalUnit(const string& organizationalUnit)
{
    _organizationalUnit = organizationalUnit;
}

void
IceSSL::RSACertificateGenContext::setCommonName(const string& commonName)
{
    _commonName = commonName;
}

void
IceSSL::RSACertificateGenContext::setBitStrength(int bitStrength)
{
    _modulusLength = bitStrength;
}

void
IceSSL::RSACertificateGenContext::setSecondsValid(long secondsValid)
{
    _secondsValid = secondsValid;
}

void
IceSSL::RSACertificateGenContext::setIssuedAdjustment(long issuedAdjustment)
{
    _issuedAdjustment = issuedAdjustment;
}

unsigned char*
IceSSL::RSACertificateGenContext::getCountry() const
{
    unsigned char* country = reinterpret_cast<unsigned char *>(const_cast<char*>(_country.c_str()));

    assert(country != 0);

    return country;
}

unsigned char*
IceSSL::RSACertificateGenContext::getStateProvince() const
{
    unsigned char* stateProvince =  reinterpret_cast<unsigned char *>(const_cast<char*>(_stateProvince.c_str()));

    assert(stateProvince != 0);

    return stateProvince;
}

unsigned char*
IceSSL::RSACertificateGenContext::getLocality() const
{
    unsigned char* locality = reinterpret_cast<unsigned char *>(const_cast<char*>(_locality.c_str()));

    assert(locality != 0);

    return locality;
}

unsigned char*
IceSSL::RSACertificateGenContext::getOrganization() const
{
    unsigned char* organization = reinterpret_cast<unsigned char *>(const_cast<char*>(_organization.c_str()));

    assert(organization != 0);

    return organization;
}

unsigned char*
IceSSL::RSACertificateGenContext::getOrganizationalUnit() const
{
    unsigned char* orgUnit = reinterpret_cast<unsigned char *>(const_cast<char*>(_organizationalUnit.c_str()));

    assert(orgUnit != 0);

    return orgUnit;
}

unsigned char*
IceSSL::RSACertificateGenContext::getCommonName() const
{
    unsigned char* commonName = reinterpret_cast<unsigned char *>(const_cast<char*>(_commonName.c_str()));

    assert(commonName != 0);

    return commonName;
}

int
IceSSL::RSACertificateGenContext::getModulusLength() const
{
    return _modulusLength;
}

long
IceSSL::RSACertificateGenContext::getSecondsValid() const
{
    return _secondsValid;
}

long
IceSSL::RSACertificateGenContext::getIssuedAdjustment() const
{
    return _issuedAdjustment;
}

IceSSL::RSACertificateGen::RSACertificateGen()
{
    ERR_load_crypto_strings();
}

IceSSL::RSACertificateGen::~RSACertificateGen()
{
}

IceSSL::RSAKeyPairPtr
IceSSL::RSACertificateGen::generate(const RSACertificateGenContext& context)
{
    // Generate an RSA key pair.
    RSAJanitor rsaJanitor(RSA_generate_key(context.getModulusLength(), RSA_F4, 0, 0));
    RSA* rsaKeyPair = rsaJanitor.get();

    assert(rsaKeyPair != 0);

    EVP_PKEYJanitor evpPkeyJanitor(EVP_PKEY_new());
    EVP_PKEY* pkey = evpPkeyJanitor.get();
    assert(pkey != 0);
    EVP_PKEY_assign_RSA(pkey, rsaKeyPair);

    // The RSA structure now belongs (temporarily) to the EVP_PKEY
    rsaJanitor.clear();

    // Create a signing request
    X509_REQJanitor x509ReqJanitor(X509_REQ_new());
    X509_REQ* signingRequest = x509ReqJanitor.get();
    assert(signingRequest != 0);

    X509Janitor x509Janitor(X509_new());
    X509* x509SelfSigned = x509Janitor.get();
    assert(x509SelfSigned != 0);

    // Set version to V3.
#ifdef NDEBUG // Avoid compiler warnings when compiling with optimization.
    X509_set_version(x509SelfSigned, 2);
#else
    assert(X509_set_version(x509SelfSigned, 2) != 0);
#endif

    ASN1_INTEGER_set(X509_get_serialNumber(x509SelfSigned), 0);

    // NOTE: This is wierd.  It looks like, for some reason, that the typedef of
    // X509_NAME gets lost in this code module.  I am using the straight struct
    // here because X509_NAME isn't here.

    // X509_NAME* subjectName = X509_REQ_get_subject_name(signingRequest);
    struct X509_name_st* subjectName = X509_REQ_get_subject_name(signingRequest);

    // Set valid time period.
    X509_gmtime_adj(X509_get_notBefore(x509SelfSigned), context.getIssuedAdjustment());
    X509_gmtime_adj(X509_get_notAfter(x509SelfSigned), context.getSecondsValid());

    // Set up subject/issuer Distinguished Name (DN).
    X509_NAME_add_entry_by_txt(subjectName, const_cast<char*>("C"), MBSTRING_ASC, context.getCountry(),
        -1, -1, 0);
    X509_NAME_add_entry_by_txt(subjectName, const_cast<char*>("ST"), MBSTRING_ASC, context.getStateProvince(),
        -1, -1, 0);
    X509_NAME_add_entry_by_txt(subjectName, const_cast<char*>("L"), MBSTRING_ASC, context.getLocality(),
        -1, -1, 0);
    X509_NAME_add_entry_by_txt(subjectName, const_cast<char*>("O"), MBSTRING_ASC, context.getOrganization(),
        -1, -1, 0);
    X509_NAME_add_entry_by_txt(subjectName, const_cast<char*>("OU"), MBSTRING_ASC, context.getOrganizationalUnit(),
        -1, -1, 0);
    X509_NAME_add_entry_by_txt(subjectName, const_cast<char*>("CN"), MBSTRING_ASC, context.getCommonName(),
        -1, -1, 0);

    // Self signed - set issuer and subject names identical
    X509_set_issuer_name(x509SelfSigned, subjectName);
    X509_set_subject_name(x509SelfSigned, subjectName);

    // Set the public key in the self signed certificate from the request.
    X509_set_pubkey(x509SelfSigned, pkey);

    // Sign the public key using an MD5 digest.
    if(!X509_sign(x509SelfSigned, pkey, EVP_md5()))
    {
        throw IceSSL::CertificateSigningException(__FILE__, __LINE__);
    }

    // Verify the Signature (paranoia).
    if(!X509_REQ_verify(signingRequest, pkey))
    {
        throw IceSSL::CertificateSignatureException(__FILE__, __LINE__);
    }

    // Nasty Hack: Getting the pkey to let go of our rsaKeyPair - we own that now.
    //             Checked this out, though, and there are no current issues (0.9.7a) with doing this.
    pkey->pkey.ptr = 0;

    RSAPrivateKeyPtr privKeyPtr = new RSAPrivateKey(rsaKeyPair);
    RSAPublicKeyPtr pubKeyPtr = new RSAPublicKey(x509SelfSigned);
    RSAKeyPair* keyPairPtr = new RSAKeyPair(privKeyPtr, pubKeyPtr);

    // Do not let the janitors clean up, we're keeping the keys for ourselves.
    rsaJanitor.clear();
    x509Janitor.clear();

    return keyPairPtr;
}

IceSSL::RSAKeyPairPtr
IceSSL::RSACertificateGen::loadKeyPair(const std::string& keyFile, const std::string& certFile)
{
    //
    // Read in the X509 Certificate Structure
    //
    BIOJanitor certBIO(BIO_new_file(certFile.c_str(), "r"));
    if(certBIO.get() == 0)
    {
        IceSSL::CertificateLoadException certLoadEx(__FILE__, __LINE__);

        certLoadEx.message = "unable to load certificate from '";
        certLoadEx.message += certFile;
        certLoadEx.message += "'\n";
        certLoadEx.message += sslGetErrors();

        throw certLoadEx;
    }

    X509Janitor x509Janitor(PEM_read_bio_X509(certBIO.get(), 0, 0, 0));

    if(x509Janitor.get() == 0)
    {
        IceSSL::CertificateLoadException certLoadEx(__FILE__, __LINE__);

        certLoadEx.message = "unable to load certificate from '";
        certLoadEx.message += certFile;
        certLoadEx.message += "'\n";
        certLoadEx.message += sslGetErrors();

        throw certLoadEx;
    }

    //
    // Read in the RSA Private Key Structure
    //
    BIOJanitor keyBIO(BIO_new_file(keyFile.c_str(), "r"));
    if(keyBIO.get() == 0)
    {
        IceSSL::PrivateKeyLoadException pklEx(__FILE__, __LINE__);

        pklEx.message = "unable to load private key from '";
        pklEx.message += keyFile;
        pklEx.message += "'\n";
        pklEx.message += sslGetErrors();

        throw pklEx;
    }

    RSAJanitor rsaJanitor(PEM_read_bio_RSAPrivateKey(keyBIO.get(), 0, 0, 0));

    if(rsaJanitor.get() == 0)
    {
        IceSSL::PrivateKeyLoadException pklEx(__FILE__, __LINE__);

        pklEx.message = "unable to load private key from '";
        pklEx.message += keyFile;
        pklEx.message += "'\n";
        pklEx.message += sslGetErrors();

        throw pklEx;
    }

    //
    // Construct our RSAKeyPair
    //
    RSAPrivateKeyPtr privKeyPtr = new RSAPrivateKey(rsaJanitor.get());
    RSAPublicKeyPtr pubKeyPtr = new RSAPublicKey(x509Janitor.get());
    RSAKeyPairPtr keyPairPtr = new RSAKeyPair(privKeyPtr, pubKeyPtr);

    // Do not let the janitors clean up, we're keeping these keys.
    rsaJanitor.clear();
    x509Janitor.clear();

    return keyPairPtr;
}