24 #include "runtime.hpp"
25 #include "cert_wrapper.hpp"
26 #include "yassl_int.hpp"
29 #if defined(USE_CML_LIB)
30 #include "cmapi_cpp.h"
40 x509::x509(uint sz) : length_(sz), buffer_(NEW_YS opaque[sz])
47 ysArrayDelete(buffer_);
51 x509::x509(
const x509& that) : length_(that.length_),
52 buffer_(NEW_YS opaque[length_])
54 memcpy(buffer_, that.buffer_, length_);
58 void x509::Swap(x509& that)
60 STL::swap(length_, that.length_);
61 STL::swap(buffer_, that.buffer_);
65 x509& x509::operator=(
const x509& that)
73 uint x509::get_length()
const
79 const opaque* x509::get_buffer()
const
85 opaque* x509::use_buffer()
92 CertManager::CertManager()
93 : peerX509_(0), selfX509_(0), verifyPeer_(false), verifyNone_(false), failNoCert_(false),
94 sendVerify_(false), verifyCallback_(0)
98 CertManager::~CertManager()
103 STL::for_each(signers_.begin(), signers_.end(), del_ptr_zero()) ;
105 STL::for_each(peerList_.begin(), peerList_.end(), del_ptr_zero()) ;
107 STL::for_each(list_.begin(), list_.end(), del_ptr_zero()) ;
111 bool CertManager::verifyPeer()
const
117 bool CertManager::verifyNone()
const
123 bool CertManager::failNoCert()
const
129 bool CertManager::sendVerify()
const
135 void CertManager::setVerifyPeer()
141 void CertManager::setVerifyNone()
147 void CertManager::setFailNoCert()
153 void CertManager::setSendVerify()
159 void CertManager::setVerifyCallback(VerifyCallback vc)
161 verifyCallback_ = vc;
165 void CertManager::AddPeerCert(x509* x)
167 peerList_.push_back(x);
171 void CertManager::CopySelfCert(
const x509* x)
174 list_.push_back(NEW_YS x509(*x));
179 int CertManager::CopyCaCert(
const x509* x)
183 TaoCrypt::CertDecoder::CA);
185 if (!cert.GetError().What()) {
188 cert.GetCommonName(), cert.GetHash()));
195 const x509* CertManager::get_cert()
const
197 return list_.front();
201 const opaque* CertManager::get_peerKey()
const
203 return peerPublicKey_.get_buffer();
207 X509* CertManager::get_peerX509()
const
213 X509* CertManager::get_selfX509()
const
219 SignatureAlgorithm CertManager::get_peerKeyType()
const
225 SignatureAlgorithm CertManager::get_keyType()
const
231 uint CertManager::get_peerKeyLength()
const
233 return peerPublicKey_.get_size();
237 const opaque* CertManager::get_privateKey()
const
239 return privateKey_.get_buffer();
243 uint CertManager::get_privateKeyLength()
const
245 return privateKey_.get_size();
250 int CertManager::Validate()
252 CertList::reverse_iterator last = peerList_.rbegin();
253 size_t count = peerList_.size();
255 while ( count > 1 ) {
259 if (
int err = cert.GetError().What())
264 cert.GetCommonName(), cert.GetHash()));
274 int err = cert.GetError().What();
275 if ( err && err != TaoCrypt::SIG_OTHER_E)
278 uint sz = cert.GetPublicKey().size();
279 peerPublicKey_.allocate(sz);
280 peerPublicKey_.assign(cert.GetPublicKey().GetKey(), sz);
282 if (cert.GetKeyType() == TaoCrypt::RSAk)
283 peerKeyType_ = rsa_sa_algo;
285 peerKeyType_ = dsa_sa_algo;
287 size_t iSz = strlen(cert.GetIssuer()) + 1;
288 size_t sSz = strlen(cert.GetCommonName()) + 1;
290 beforeDate.data= (
unsigned char *) cert.GetBeforeDate();
291 beforeDate.type= cert.GetBeforeDateType();
292 beforeDate.length= strlen((
char *) beforeDate.data) + 1;
293 afterDate.data= (
unsigned char *) cert.GetAfterDate();
294 afterDate.type= cert.GetAfterDateType();
295 afterDate.length= strlen((
char *) afterDate.data) + 1;
296 peerX509_ = NEW_YS X509(cert.GetIssuer(), iSz, cert.GetCommonName(),
297 sSz, &beforeDate, &afterDate);
299 if (err == TaoCrypt::SIG_OTHER_E && verifyCallback_) {
302 store.error_depth =
static_cast<int>(count) - 1;
303 store.current_cert = peerX509_;
305 int ok = verifyCallback_(0, &store);
309 if (err == TaoCrypt::SIG_OTHER_E)
return err;
316 int CertManager::SetPrivateKey(
const x509& key)
318 privateKey_.allocate(key.get_length());
319 privateKey_.assign(key.get_buffer(), key.get_length());
322 if (x509* cert = list_.front()) {
326 if (
int err = cd.GetError().What())
328 if (cd.GetKeyType() == TaoCrypt::RSAk)
329 keyType_ = rsa_sa_algo;
331 keyType_ = dsa_sa_algo;
333 size_t iSz = strlen(cd.GetIssuer()) + 1;
334 size_t sSz = strlen(cd.GetCommonName()) + 1;
336 beforeDate.data= (
unsigned char *) cd.GetBeforeDate();
337 beforeDate.type= cd.GetBeforeDateType();
338 beforeDate.length= strlen((
char *) beforeDate.data) + 1;
339 afterDate.data= (
unsigned char *) cd.GetAfterDate();
340 afterDate.type= cd.GetAfterDateType();
341 afterDate.length= strlen((
char *) afterDate.data) + 1;
342 selfX509_ = NEW_YS X509(cd.GetIssuer(), iSz, cd.GetCommonName(),
343 sSz, &beforeDate, &afterDate);
350 void CertManager::setPeerX509(X509* x)
354 X509_NAME* issuer = x->GetIssuer();
355 X509_NAME* subject = x->GetSubject();
359 peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
360 subject->GetName(), subject->GetLength(), before, after);
364 #if defined(USE_CML_LIB)
367 void CertManager::SetPeerKey()
370 x509* main = peerList_.front();
373 cert.num = main->get_length();
374 cert.data = main->set_buffer();
376 CML::Certificate cm(cert);
377 const CML::ASN::Cert& raw = cm.base();
378 CTIL::CSM_Buffer key = raw.pubKeyInfo.key;
381 opaque* key_buffer =
reinterpret_cast<opaque*
>(key.Get(sz));
382 peerPublicKey_.allocate(sz);
383 peerPublicKey_.assign(key_buffer, sz);
387 #endif // USE_CML_LIB