~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to extra/yassl/src/cert_wrapper.cpp

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (c) 2005-2007 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
 
3
   Use is subject to license terms.
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; version 2 of the License.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; see the file COPYING. If not, write to the
 
16
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 
17
   MA  02110-1301  USA.
 
18
*/
 
19
 
 
20
 
 
21
/*  The certificate wrapper source implements certificate management functions
 
22
 *
 
23
 */
 
24
 
 
25
#include "runtime.hpp"
 
26
#include "cert_wrapper.hpp"
 
27
#include "yassl_int.hpp"
 
28
#include "error.hpp"
 
29
 
 
30
#if defined(USE_CML_LIB)
 
31
    #include "cmapi_cpp.h"
 
32
#else
 
33
    #include "asn.hpp"
 
34
    #include "file.hpp"
 
35
#endif // USE_CML_LIB
 
36
 
 
37
 
 
38
namespace yaSSL {
 
39
 
 
40
 
 
41
x509::x509(uint sz) : length_(sz), buffer_(NEW_YS opaque[sz]) 
 
42
{
 
43
}
 
44
 
 
45
 
 
46
x509::~x509() 
 
47
 
48
    ysArrayDelete(buffer_); 
 
49
}
 
50
 
 
51
 
 
52
x509::x509(const x509& that) : length_(that.length_),
 
53
                               buffer_(NEW_YS opaque[length_])
 
54
{
 
55
    memcpy(buffer_, that.buffer_, length_);
 
56
}
 
57
 
 
58
 
 
59
void x509::Swap(x509& that)
 
60
{
 
61
    STL::swap(length_, that.length_);
 
62
    STL::swap(buffer_, that.buffer_);
 
63
}
 
64
 
 
65
 
 
66
x509& x509::operator=(const x509& that)
 
67
{
 
68
    x509 temp(that);
 
69
    Swap(temp);
 
70
    return *this;
 
71
}
 
72
 
 
73
 
 
74
uint x509::get_length() const
 
75
 
76
    return length_; 
 
77
}
 
78
 
 
79
 
 
80
const opaque* x509::get_buffer() const
 
81
 
82
    return buffer_; 
 
83
}
 
84
 
 
85
 
 
86
opaque* x509::use_buffer()
 
87
 
88
    return buffer_; 
 
89
}
 
90
 
 
91
 
 
92
//CertManager
 
93
CertManager::CertManager()
 
94
    : peerX509_(0), verifyPeer_(false), verifyNone_(false), failNoCert_(false),
 
95
      sendVerify_(false), verifyCallback_(0)
 
96
{}
 
97
 
 
98
 
 
99
CertManager::~CertManager()
 
100
{
 
101
    ysDelete(peerX509_);
 
102
 
 
103
    STL::for_each(signers_.begin(), signers_.end(), del_ptr_zero()) ;
 
104
 
 
105
    STL::for_each(peerList_.begin(), peerList_.end(), del_ptr_zero()) ;
 
106
 
 
107
    STL::for_each(list_.begin(), list_.end(), del_ptr_zero()) ;
 
108
}
 
109
 
 
110
 
 
111
bool CertManager::verifyPeer() const
 
112
{
 
113
    return verifyPeer_;
 
114
}
 
115
 
 
116
 
 
117
bool CertManager::verifyNone() const
 
118
{
 
119
    return verifyNone_;
 
120
}
 
121
 
 
122
 
 
123
bool CertManager::failNoCert() const
 
124
{
 
125
    return failNoCert_;
 
126
}
 
127
 
 
128
 
 
129
bool CertManager::sendVerify() const
 
130
{
 
131
    return sendVerify_;
 
132
}
 
133
 
 
134
 
 
135
void CertManager::setVerifyPeer()
 
136
{
 
137
    verifyPeer_ = true;
 
138
}
 
139
 
 
140
 
 
141
void CertManager::setVerifyNone()
 
142
{
 
143
    verifyNone_ = true;
 
144
}
 
145
 
 
146
 
 
147
void CertManager::setFailNoCert()
 
148
{
 
149
    failNoCert_ = true;
 
150
}
 
151
 
 
152
 
 
153
void CertManager::setSendVerify()
 
154
{
 
155
    sendVerify_ = true;
 
156
}
 
157
 
 
158
 
 
159
void CertManager::setVerifyCallback(VerifyCallback vc)
 
160
{
 
161
    verifyCallback_ = vc;
 
162
}
 
163
 
 
164
 
 
165
void CertManager::AddPeerCert(x509* x)
 
166
 
167
    peerList_.push_back(x);  // take ownership
 
168
}
 
169
 
 
170
 
 
171
void CertManager::CopySelfCert(const x509* x)
 
172
{
 
173
    if (x)
 
174
        list_.push_back(NEW_YS x509(*x));
 
175
}
 
176
 
 
177
 
 
178
// add to signers
 
179
int CertManager::CopyCaCert(const x509* x)
 
180
{
 
181
    TaoCrypt::Source source(x->get_buffer(), x->get_length());
 
182
    TaoCrypt::CertDecoder cert(source, true, &signers_, verifyNone_,
 
183
                               TaoCrypt::CertDecoder::CA);
 
184
 
 
185
    if (!cert.GetError().What()) {
 
186
        const TaoCrypt::PublicKey& key = cert.GetPublicKey();
 
187
        signers_.push_back(NEW_YS TaoCrypt::Signer(key.GetKey(), key.size(),
 
188
                                        cert.GetCommonName(), cert.GetHash()));
 
189
    }
 
190
    // just don't add, not an error return cert.GetError().What();
 
191
    return 0;
 
192
}
 
193
 
 
194
 
 
195
const x509* CertManager::get_cert() const
 
196
 
197
    return list_.front();
 
198
}
 
199
 
 
200
 
 
201
const opaque* CertManager::get_peerKey() const
 
202
 
203
    return peerPublicKey_.get_buffer();
 
204
}
 
205
 
 
206
 
 
207
X509* CertManager::get_peerX509() const
 
208
{
 
209
    return peerX509_;
 
210
}
 
211
 
 
212
 
 
213
SignatureAlgorithm CertManager::get_peerKeyType() const
 
214
{
 
215
    return peerKeyType_;
 
216
}
 
217
 
 
218
 
 
219
SignatureAlgorithm CertManager::get_keyType() const
 
220
{
 
221
    return keyType_;
 
222
}
 
223
 
 
224
 
 
225
uint CertManager::get_peerKeyLength() const
 
226
 
227
    return peerPublicKey_.get_size();
 
228
}
 
229
 
 
230
 
 
231
const opaque* CertManager::get_privateKey() const
 
232
 
233
    return privateKey_.get_buffer();
 
234
}
 
235
 
 
236
 
 
237
uint CertManager::get_privateKeyLength() const
 
238
 
239
    return privateKey_.get_size();
 
240
}
 
241
 
 
242
 
 
243
// Validate the peer's certificate list, from root to peer (last to first)
 
244
int CertManager::Validate()
 
245
{
 
246
    CertList::reverse_iterator last = peerList_.rbegin();
 
247
    size_t count = peerList_.size();
 
248
 
 
249
    while ( count > 1 ) {
 
250
        TaoCrypt::Source source((*last)->get_buffer(), (*last)->get_length());
 
251
        TaoCrypt::CertDecoder cert(source, true, &signers_, verifyNone_);
 
252
 
 
253
        if (int err = cert.GetError().What())
 
254
            return err;
 
255
 
 
256
        const TaoCrypt::PublicKey& key = cert.GetPublicKey();
 
257
        signers_.push_back(NEW_YS TaoCrypt::Signer(key.GetKey(), key.size(),
 
258
                                        cert.GetCommonName(), cert.GetHash()));
 
259
        ++last;
 
260
        --count;
 
261
    }
 
262
 
 
263
    if (count) {
 
264
        // peer's is at the front
 
265
        TaoCrypt::Source source((*last)->get_buffer(), (*last)->get_length());
 
266
        TaoCrypt::CertDecoder cert(source, true, &signers_, verifyNone_);
 
267
 
 
268
        int err = cert.GetError().What();
 
269
        if ( err )
 
270
            return err;
 
271
 
 
272
        uint sz = cert.GetPublicKey().size();
 
273
        peerPublicKey_.allocate(sz);
 
274
        peerPublicKey_.assign(cert.GetPublicKey().GetKey(), sz);
 
275
 
 
276
        if (cert.GetKeyType() == TaoCrypt::RSAk)
 
277
            peerKeyType_ = rsa_sa_algo;
 
278
        else
 
279
            peerKeyType_ = dsa_sa_algo;
 
280
 
 
281
        size_t iSz = strlen(cert.GetIssuer()) + 1;
 
282
        size_t sSz = strlen(cert.GetCommonName()) + 1;
 
283
        int bSz = (int)strlen(cert.GetBeforeDate()) + 1;
 
284
        int aSz = (int)strlen(cert.GetAfterDate()) + 1;
 
285
        peerX509_ = NEW_YS X509(cert.GetIssuer(), iSz, cert.GetCommonName(),
 
286
                                sSz, cert.GetBeforeDate(), bSz,
 
287
                                cert.GetAfterDate(), aSz);
 
288
 
 
289
        if (err == TaoCrypt::SIG_OTHER_E && verifyCallback_) {
 
290
            X509_STORE_CTX store;
 
291
            store.error = err;
 
292
            store.error_depth = static_cast<int>(count) - 1;
 
293
            store.current_cert = peerX509_;
 
294
 
 
295
            int ok = verifyCallback_(0, &store);
 
296
            if (ok) return 0;
 
297
        }
 
298
 
 
299
        if (err == TaoCrypt::SIG_OTHER_E) return err;
 
300
    }
 
301
    return 0;
 
302
}
 
303
 
 
304
 
 
305
// Set the private key
 
306
int CertManager::SetPrivateKey(const x509& key)
 
307
{
 
308
    privateKey_.allocate(key.get_length());
 
309
    privateKey_.assign(key.get_buffer(), key.get_length());
 
310
 
 
311
    // set key type
 
312
    if (x509* cert = list_.front()) {
 
313
        TaoCrypt::Source source(cert->get_buffer(), cert->get_length());
 
314
        TaoCrypt::CertDecoder cd(source, false);
 
315
        cd.DecodeToKey();
 
316
        if (int err = cd.GetError().What())
 
317
            return err;
 
318
        if (cd.GetKeyType() == TaoCrypt::RSAk)
 
319
            keyType_ = rsa_sa_algo;
 
320
        else
 
321
            keyType_ = dsa_sa_algo;
 
322
    }
 
323
    return 0;
 
324
}
 
325
 
 
326
 
 
327
// Store OpenSSL type peer's cert
 
328
void CertManager::setPeerX509(X509* x)
 
329
{
 
330
    assert(peerX509_ == 0);
 
331
    if (x == 0) return;
 
332
 
 
333
    X509_NAME* issuer   = x->GetIssuer();
 
334
    X509_NAME* subject  = x->GetSubject();
 
335
    ASN1_STRING* before = x->GetBefore();
 
336
    ASN1_STRING* after  = x->GetAfter();
 
337
 
 
338
    peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
 
339
        subject->GetName(), subject->GetLength(), (const char*) before->data,
 
340
        before->length, (const char*) after->data, after->length);
 
341
}
 
342
 
 
343
 
 
344
#if defined(USE_CML_LIB)
 
345
 
 
346
// Get the peer's certificate, extract and save public key
 
347
void CertManager::SetPeerKey()
 
348
{
 
349
    // first cert is the peer's
 
350
    x509* main = peerList_.front();
 
351
 
 
352
    Bytes_struct cert;
 
353
    cert.num  = main->get_length();
 
354
    cert.data = main->set_buffer();
 
355
 
 
356
    CML::Certificate cm(cert);
 
357
    const CML::ASN::Cert& raw = cm.base();
 
358
    CTIL::CSM_Buffer key = raw.pubKeyInfo.key;
 
359
 
 
360
    uint sz;
 
361
    opaque* key_buffer = reinterpret_cast<opaque*>(key.Get(sz));
 
362
    peerPublicKey_.allocate(sz);
 
363
    peerPublicKey_.assign(key_buffer, sz);
 
364
}
 
365
 
 
366
 
 
367
#endif // USE_CML_LIB
 
368
 
 
369
 
 
370
 
 
371
} // namespace