PKIFXSECCrypto.cpp
Go to the documentation of this file.00001
00010 #include "PKIFXSECCrypto.h"
00011
00012 #include <xsec/framework/XSECError.hpp>
00013 #include <xsec/enc/XSCrypt/XSCryptCryptoBase64.hpp>
00014
00015 #include "PKIFXSECCryptoHash.h"
00016 #include "PKIFXSECCryptoKeyHMAC.h"
00017 #include "PKIFXSECCryptoKeyDSA.h"
00018 #include "PKIFXSECCryptoKeyRSA.h"
00019 #include "PKIFXSECCryptoX509.h"
00020 #include "PKIFXSECCryptoSymmetricKey.h"
00021
00023 struct PKIFXSECCryptoImpl
00024 {
00025 PKIFXSECCryptoImpl();
00026
00027 IPKIFMediatorPtr m_mediator;
00028
00029 static bool s_bInitialized;
00030 static XMLCh * s_provName;
00031 };
00032
00033 bool PKIFXSECCryptoImpl::s_bInitialized = false;
00034 XMLCh * PKIFXSECCryptoImpl::s_provName = 0;
00042 PKIFXSECCryptoImpl::PKIFXSECCryptoImpl()
00043 {
00044 if(!s_bInitialized) {
00045 s_bInitialized = true;
00046 s_provName = XMLString::transcode("PKIF XML Security Crypto provider");
00047 }
00048 }
00050
00059 PKIFXSECCrypto::PKIFXSECCrypto()
00060 :m_impl(new PKIFXSECCryptoImpl)
00061 {
00062
00063
00064 }
00065
00075 PKIFXSECCrypto::PKIFXSECCrypto(
00077 IPKIFMediatorPtr & med
00078 )
00079 :m_impl(new PKIFXSECCryptoImpl)
00080 {
00081 m_impl->m_mediator = med;
00082 }
00083
00091 PKIFXSECCrypto::~PKIFXSECCrypto()
00092 {
00093 if(m_impl) {
00094 m_impl = 0;
00095 delete m_impl;
00096 }
00097 }
00098
00106 XSECCryptoHash * PKIFXSECCrypto::hashSHA1() const
00107 {
00108 return hashSHA(160);
00109 }
00110
00119 XSECCryptoHash * PKIFXSECCrypto::hashSHA(
00121 int length) const
00122 {
00123 PKIFXSECCryptoHash * rv = 0;
00124 if(!m_impl->m_mediator) return rv;
00125
00126 try {
00127 switch(length) {
00128 case 160:
00129 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA1));
00130 break;
00131 case 224:
00132 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA224));
00133 break;
00134 case 256:
00135 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA256));
00136 break;
00137 case 384:
00138 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA384));
00139 break;
00140 case 512:
00141 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA512));
00142 break;
00143 }
00144 } catch(XSECException &) {
00145
00146 rv = 0;
00147 }
00148 return rv;
00149 }
00150
00158 XSECCryptoHash * PKIFXSECCrypto::hashHMACSHA1() const
00159 {
00160 return hashHMACSHA(160);
00161 }
00162
00171 XSECCryptoHash * PKIFXSECCrypto::hashHMACSHA(
00173 int length) const
00174 {
00175
00176 PKIFXSECCryptoHash * rv = 0;
00177 if(!m_impl->m_mediator) return rv;
00178 try {
00179 switch(length) {
00180 case 160:
00181 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA1));
00182 break;
00183 case 224:
00184 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA224));
00185 break;
00186 case 256:
00187 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA256));
00188 break;
00189 case 384:
00190 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA384));
00191 break;
00192 case 512:
00193 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_SHA512));
00194 break;
00195 }
00196 } catch(XSECException &) {
00197
00198 rv = 0;
00199 }
00200 return rv;
00201 }
00202
00210 XSECCryptoHash * PKIFXSECCrypto::hashMD5() const
00211 {
00212 #if defined(PKIFXML_FIPSONLY)
00213 return 0;
00214 #endif //defined(PKIFXML_FIPSONLY)
00215 PKIFXSECCryptoHash * rv = 0;
00216 if(!m_impl->m_mediator) return rv;
00217 try {
00218 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_MD5));
00219 }catch(XSECException &) {
00220
00221 rv = 0;
00222 }
00223 return rv;
00224 }
00225
00233 XSECCryptoHash * PKIFXSECCrypto::hashHMACMD5() const
00234 {
00235 #if defined(PKIFXML_FIPSONLY)
00236 return 0;
00237 #endif //defined(PKIFXML_FIPSONLY)
00238 PKIFXSECCryptoHash * rv = 0;
00239 try {
00240 XSECnew(rv,PKIFXSECCryptoHash(m_impl->m_mediator,XSECCryptoHash::HASH_MD5));
00241 }catch(XSECException &) {
00242
00243 rv = 0;
00244 }
00245 return rv;
00246 }
00247
00255 XSECCryptoKeyHMAC * PKIFXSECCrypto::keyHMAC() const
00256 {
00257 XSECCryptoKeyHMAC * rv = 0;
00258 try {
00259 XSECnew(rv,PKIFXSECCryptoKeyHMAC());
00260 }catch(XSECException &) {
00261
00262 rv = 0;
00263 }
00264 return rv;
00265 }
00266
00276 XSECCryptoBase64 * PKIFXSECCrypto::base64() const
00277 {
00278 XSCryptCryptoBase64 * rv;
00279 XSECnew(rv,XSCryptCryptoBase64());
00280 return rv;
00281 }
00282
00291 XSECCryptoKeyDSA * PKIFXSECCrypto::keyDSA() const
00292 {
00293 PKIFXSECCryptoKeyDSA * rv;
00294 try {
00295 XSECnew(rv,PKIFXSECCryptoKeyDSA());
00296 }catch(XSECException &) {
00297
00298 rv = 0;
00299 }
00300 rv->SetMediator(m_impl->m_mediator);
00301 return rv;
00302 }
00303
00311 XSECCryptoKeyRSA * PKIFXSECCrypto::keyRSA() const
00312 {
00313 PKIFXSECCryptoKeyRSA * rv;
00314 try {
00315 XSECnew(rv,PKIFXSECCryptoKeyRSA());
00316 }catch(XSECException &) {
00317
00318 rv = 0;
00319 }
00320 rv->SetMediator(m_impl->m_mediator);
00321 return rv;
00322 }
00323
00331 XSECCryptoX509 * PKIFXSECCrypto::X509() const
00332 {
00333 PKIFXSECCryptoX509 * rv;
00334 try {
00335 XSECnew(rv,PKIFXSECCryptoX509());
00336 }catch(XSECException &) {
00337
00338 rv = 0;
00339 }
00340 rv->SetMediator(m_impl->m_mediator);
00341 return rv;
00342 }
00343
00355 bool PKIFXSECCrypto::algorithmSupported(XSECCryptoSymmetricKey::SymmetricKeyType alg) const
00356 {
00357 if( alg == XSECCryptoSymmetricKey::KEY_3DES_192 ||
00358 alg == XSECCryptoSymmetricKey::KEY_AES_128 ||
00359 alg == XSECCryptoSymmetricKey::KEY_AES_192 ||
00360 alg == XSECCryptoSymmetricKey::KEY_AES_256 )
00361 {
00362 return true;
00363 }
00364 return false;
00365 }
00366
00378 bool PKIFXSECCrypto::algorithmSupported(XSECCryptoHash::HashType alg) const
00379 {
00380 if( alg == XSECCryptoHash::HASH_SHA1 ||
00381 alg == XSECCryptoHash::HASH_MD5 ||
00382 alg == XSECCryptoHash::HASH_SHA224 ||
00383 alg == XSECCryptoHash::HASH_SHA256 ||
00384 alg == XSECCryptoHash::HASH_SHA384 ||
00385 alg == XSECCryptoHash::HASH_SHA512 )
00386 {
00387 return true;
00388 }
00389 return false;
00390 }
00391
00401 XSECCryptoSymmetricKey * PKIFXSECCrypto::keySymmetric(
00403 XSECCryptoSymmetricKey::SymmetricKeyType alg) const
00404 {
00405 PKIFXSECCryptoSymmetricKey * rv = 0;
00406 IPKIFCryptoRawOperations * raw = m_impl->m_mediator->GetMediator<IPKIFCryptoRawOperations>();
00407 if(!raw) return 0;
00408 try {
00409 XSECnew(rv,PKIFXSECCryptoSymmetricKey(raw));
00410 }catch(XSECException &) {
00411
00412 rv = 0;
00413 }
00414 rv->setSymmetricType(alg);
00415 return rv;
00416 }
00417
00426 unsigned int PKIFXSECCrypto::getRandom(
00428 unsigned char * buffer,
00430 unsigned int numOctets) const
00431 {
00432 IPKIFCryptoMisc * misc = m_impl->m_mediator->GetMediator<IPKIFCryptoMisc>();
00433 if(!misc) return 0;
00434 try{
00435 misc->GenRandom(buffer,numOctets);
00436 } catch(CPKIFException &) {
00437 return 0;
00438 }
00439 return numOctets;
00440 }
00441
00442
00450 const XMLCh * PKIFXSECCrypto::getProviderName() const
00451 {
00452 return m_impl->s_provName;
00453 }
00454
00462 const XMLCh * pkifProvName()
00463 {
00464 static PKIFXSECCrypto xsc;
00465 return xsc.getProviderName();
00466 }
00467