00001
00011 #include "ToolkitUtils.h"
00012 #include "PKIFAlgorithm.h"
00013
00014
00015 #ifdef _WIN32
00016 #ifdef _DEBUG
00017 #include <crtdbg.h>
00018 #endif
00019 #include "PKIFCAPIRepository2.h"
00020 #include "PKIFCAPIUserRepository2.h"
00021 #include "PKIFCAPITrustStore2.h"
00022 #include "PKIFPathBuildAndValidate.h"
00023 #include "PKIFCAPIRaw.h"
00024 #include "PKIFCNGCAPIRaw.h"
00025 #include "PKIFCNGCAPI.h"
00026 #include "PKIFBCryptGuard.h"
00027 #include "CAPITrustRootCRLRepository2.h"
00028 #include "CAPICRLUpdate2.h"
00029 #include "CAPICertUpdate2.h"
00030 #include "PKIFCAPI2.h"
00031 #endif
00032
00033 #include "PKIFAiaOrSiaRetrieval.h"
00034 #include "PKIFMediators.h"
00035 #include "IPKIFTrustCacheUpdate.h"
00036 #include "IPKIFCryptoKeyManagement.h"
00037 #include "IPKIFCryptoKeyIDOperations.h"
00038 #include "IPKIFDefaultPathManagement.h"
00039 #include "PKIFCacheMediator2.h"
00040 #include "CPKIFPathProcessingMediator2.h"
00041 #include "CPKIFRevocationStatusMediator2.h"
00042 #include "CPKIFCryptoMediator2.h"
00043 #include "SimpleCertCache.h"
00044 #include "SimpleCRLCache.h"
00045 #include "PKIFCRLDPRetrieval.h"
00046 #include "PKIFX509CRLChecker.h"
00047 #include "PKIFCryptUtils.h"
00048 #include "HttpServerBlacklist.h"
00049
00050 #include "PKIFOCSPChecker.h"
00051
00052 #include "CRLDistributionPoints.h"
00053 #include "GeneralSubtree.h"
00054 #include "DistributionPointName.h"
00055 #include "CRLDistributionPoint.h"
00056
00057 #include "ASN1Helper.h"
00058 #include "PKIX1Implicit88.h"
00059 #include "PKIX1Explicit88.h"
00060
00061 #include "PKIFNameMemoryHelper.h"
00062 #include "PKIFErrors.h"
00063
00064 #include "Buffer.h"
00065 #include "OID.h"
00066 #include "GeneralName.h"
00067 #include "AlgorithmIdentifier.h"
00068 #include "Certificate.h"
00069 #include "ExtendedKeyUsage.h"
00070 #include "GottaMatch.h"
00071 #include "KeyUsage.h"
00072
00073 #include "IPKIFCryptoAlgSupport.h"
00074
00075 #include "PKIFNSSRepository.h"
00076 #include "PKIFNSSPasswordStorage.h"
00077 #include "PKIFNSSCertUpdate.h"
00078 #include "PKIFNSSCRLUpdate.h"
00079 #include "PKIFNSSTrustStore.h"
00080 #include "PKIFNSSRaw.h"
00081 #include "PKIFNSS.h"
00082
00083 #include "ASN1HelperLib.h"
00084 #include "ToolkitutilsPrivate.h"
00085 #include "LDAP_URL_Header.h"
00086 #include "CSingletonLDAPConnection.h"
00087
00088 #include "boost/cstdint.hpp"
00089 #include "boost/numeric/conversion/cast.hpp"
00090 #include "boost/asio/io_service.hpp"
00091 #include "boost/asio/ip/address.hpp"
00092 #include "boost/asio/ip/address_v4.hpp"
00093 #include "boost/asio/ip/address_v6.hpp"
00094
00095 using boost::numeric_cast;
00096 using boost::bad_numeric_cast;
00097 using boost::asio::ip::address_v4;
00098 using boost::asio::ip::address_v6;
00099 using boost::asio::ip::address;
00100 using boost::asio::io_service;
00101
00102 using namespace boost;
00103 using namespace boost::asio;
00104
00105
00106 #if defined(_WIN32) || defined(WIN32)
00107 #define STRCASECMP stricmp
00108 #else
00109 #define STRCASECMP strcasecmp
00110 #endif
00111
00112 #include <sstream>
00113 #include <cstdio>
00114
00115
00116
00117 #include "curl/curl.h"
00118
00119 #ifdef _NO_GETTICKCOUNT
00120 #include <sys/time.h>
00121 #endif //_NO_GETTICKCOUNT
00122
00123
00124 #ifndef _WIN32
00125
00126 #define CERT_DIGITAL_SIGNATURE_KEY_USAGE 0x80
00127 #define CERT_NON_REPUDIATION_KEY_USAGE 0x40
00128 #define CERT_KEY_ENCIPHERMENT_KEY_USAGE 0x20
00129 #define CERT_DATA_ENCIPHERMENT_KEY_USAGE 0x10
00130 #define CERT_KEY_AGREEMENT_KEY_USAGE 0x08
00131 #define CERT_KEY_CERT_SIGN_KEY_USAGE 0x04
00132 #define CERT_OFFLINE_CRL_SIGN_KEY_USAGE 0x02
00133 #define CERT_CRL_SIGN_KEY_USAGE 0x02
00134 #define CERT_ENCIPHER_ONLY_KEY_USAGE 0x01
00135
00136 #define CERT_DECIPHER_ONLY_KEY_USAGE 0x80
00137 #endif
00138
00139 using namespace std;
00140
00141
00142 using namespace PKIFLDAP;
00150 CAC_API IPKIFCertRepository* Get_IPKIFCertRepository(
00152 IPKIFMediatorPtr& m)
00153 {
00154 if(m) return m->GetMediator<IPKIFCertRepository>(); else return NULL;
00155 }
00163 CAC_API IPKIFCertRepositoryUpdate* Get_IPKIFCertRepositoryUpdate(
00165 IPKIFMediatorPtr& m)
00166 {
00167 if(m) return m->GetMediator<IPKIFCertRepositoryUpdate>(); else return NULL;
00168 }
00176 CAC_API IPKIFCertSearch* Get_IPKIFCertSearch(
00178 IPKIFMediatorPtr& m)
00179 {
00180 if(m) return m->GetMediator<IPKIFCertSearch>(); else return NULL;
00181 }
00189 CAC_API IPKIFCRLRepository* Get_IPKIFCRLRepository(
00191 IPKIFMediatorPtr& m)
00192 {
00193 if(m) return m->GetMediator<IPKIFCRLRepository>(); else return NULL;
00194 }
00202 CAC_API IPKIFCRLRepositoryUpdate* Get_IPKIFCRLRepositoryUpdate(
00204 IPKIFMediatorPtr& m)
00205 {
00206 if(m) return m->GetMediator<IPKIFCRLRepositoryUpdate>(); else return NULL;
00207 }
00215 CAC_API IPKIFTrustCache* Get_IPKIFTrustCache(
00217 IPKIFMediatorPtr& m)
00218 {
00219 if(m) return m->GetMediator<IPKIFTrustCache>(); else return NULL;
00220 }
00228 CAC_API IPKIFTrustCacheUpdate* Get_IPKIFTrustCacheUpdate(
00230 IPKIFMediatorPtr& m)
00231 {
00232 if(m) return m->GetMediator<IPKIFTrustCacheUpdate>(); else return NULL;
00233 }
00234
00235
00243 CAC_API IPKIFCryptoKeyManagement* Get_IPKIFCryptoKeyManagement(
00245 IPKIFMediatorPtr& m)
00246 {
00247 if(m) return m->GetMediator<IPKIFCryptoKeyManagement>(); else return NULL;
00248 }
00256 CAC_API IPKIFCryptoKeyIDOperations* Get_IPKIFCryptoKeyIDOperations(
00258 IPKIFMediatorPtr& m)
00259 {
00260 if(m) return m->GetMediator<IPKIFCryptoKeyIDOperations>(); else return NULL;
00261 }
00269 CAC_API IPKIFCryptoMisc* Get_IPKIFCryptoMisc(
00271 IPKIFMediatorPtr& m)
00272 {
00273 if(m) return m->GetMediator<IPKIFCryptoMisc>(); else return NULL;
00274 }
00282 CAC_API IPKIFCryptoRawOperations* Get_IPKIFCryptoRawOperations(
00284 IPKIFMediatorPtr& m)
00285 {
00286 if(m) return m->GetMediator<IPKIFCryptoRawOperations>(); else return NULL;
00287 }
00295 CAC_API IPKIFCryptoAlgSupport* Get_IPKIFCryptoAlgSupport(
00297 IPKIFMediatorPtr& m)
00298 {
00299 if(m) return m->GetMediator<IPKIFCryptoAlgSupport>(); else return NULL;
00300 }
00301
00302
00310 CAC_API IPKIFDefaultPathManagement* Get_IPKIFDefaultPathManagement(
00312 IPKIFMediatorPtr& m)
00313 {
00314 if(m) return m->GetMediator<IPKIFDefaultPathManagement>(); else return NULL;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00326
00327
00328
00329
00337 CAC_API void AddCacheColleague(
00339 IPKIFMediatorPtr& m,
00341 IPKIFColleaguePtr& c)
00342 {
00343 CPKIFCacheMediator2 * i = m->GetMediator<CPKIFCacheMediator2>();
00344 if(i)
00345 i->AddColleague(c);
00346 }
00347
00364 CAC_API IPKIFMediatorPtr MakeDefaultMediator(
00366 bool isService)
00367 {
00368 CPKIFOCSPCheckerPtr empty;
00369 return MakeDefaultMediator(isService, empty, empty);
00370 }
00371
00388 CAC_API IPKIFMediatorPtr MakeDefaultMediator(
00390 bool isService,
00392 CPKIFOCSPCheckerPtr& trustedResponder)
00393 {
00394 CPKIFOCSPCheckerPtr empty;
00395 return MakeDefaultMediator(isService, trustedResponder, empty);
00396 }
00397
00398
00433 CAC_API IPKIFMediatorPtr MakeDefaultMediator(
00435 bool isService,
00437 CPKIFOCSPCheckerPtr& trustedResponder,
00439 CPKIFOCSPCheckerPtr& aiaColleague)
00440 {
00441 LOG_STRING_DEBUG("MakeDefaultMediator", TOOLKIT_UTILS_SUBCOMPONENT, 0, NULL);
00442
00443
00444
00445
00446 CPKIFCacheMediator2Ptr cache;
00447 CPKIFCryptoMediator2Ptr crypto;
00448 CPKIFRevocationStatusMediator2Ptr revStatus;
00449 CPKIFPathProcessingMediator2Ptr path;
00450
00451 try
00452 {
00453
00454
00455
00456
00457 CPKIFCacheMediator2Ptr tmpCache(new CPKIFCacheMediator2(false));
00458 cache = tmpCache;
00459 cache->InitializeMediator(NULL);
00460
00461 #ifdef _WIN32
00462 if(!isService)
00463 {
00464 CPKIFCAPITrustStore2Ptr rootStoreCerts(new CPKIFCAPITrustStore2());
00465 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague, CPKIFCAPITrustStore2>(rootStoreCerts));
00466
00467 CPKIFCAPITrustRootCRLRepository2Ptr rootStoreCRLs(new CPKIFCAPITrustRootCRLRepository2);
00468 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPITrustRootCRLRepository2>(rootStoreCRLs));
00469 }
00470 else
00471 {
00472 CPKIFCAPITrustStore2Ptr rootStoreCerts(new CPKIFCAPITrustStore2(CERT_SYSTEM_STORE_LOCAL_MACHINE));
00473 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPITrustStore2>(rootStoreCerts));
00474
00475 CPKIFCAPITrustRootCRLRepository2Ptr rootStoreCRLs(new CPKIFCAPITrustRootCRLRepository2(CERT_SYSTEM_STORE_LOCAL_MACHINE));
00476 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPITrustRootCRLRepository2>(rootStoreCRLs));
00477 }
00478
00479 if(!isService)
00480 {
00481 CPKIFCAPIRepository2Ptr caStoreCerts(new CPKIFCAPIRepository2());
00482 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIRepository2>(caStoreCerts));
00483
00484 CPKIFCAPICRLUpdate2Ptr crlUpdate(new CPKIFCAPICRLUpdate2());
00485 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPICRLUpdate2>(crlUpdate));
00486
00487 CPKIFCAPICRLUpdate2Ptr crlUpdate2(new CPKIFCAPICRLUpdate2(CERT_SYSTEM_STORE_CURRENT_USER, g_defCACCAPITrustStore));
00488 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPICRLUpdate2>(crlUpdate2));
00489
00490 CPKIFCAPICertUpdate2Ptr certUpdate(new CPKIFCAPICertUpdate2());
00491 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPICertUpdate2>(certUpdate));
00492
00493
00494 CPKIFCAPIUserRepository2Ptr userRep(new CPKIFCAPIUserRepository2());
00495 try
00496 {
00497 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIUserRepository2>(userRep));
00498 }
00499 catch(CPKIFException&)
00500 {
00501
00502
00503
00504 }
00505
00506 CPKIFCAPIUserRepository2Ptr userRep2(new CPKIFCAPIUserRepository2(CERT_SYSTEM_STORE_CURRENT_USER, "My"));
00507 try
00508 {
00509 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIUserRepository2>(userRep2));
00510 }
00511 catch(CPKIFException&)
00512 {
00513
00514
00515
00516 }
00517
00518
00519 CPKIFCAPIUserRepository2Ptr userRepIntermediateCAs(new CPKIFCAPIUserRepository2(CERT_SYSTEM_STORE_CURRENT_USER, g_defCACCAPIStore));
00520 try
00521 {
00522 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIUserRepository2>(userRepIntermediateCAs));
00523 }
00524 catch(CPKIFException&)
00525 {
00526
00527
00528 }
00529 CPKIFCAPIUserRepository2Ptr userRepTAs(new CPKIFCAPIUserRepository2(CERT_SYSTEM_STORE_CURRENT_USER, g_defCACCAPITrustStore));
00530 try
00531 {
00532 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIUserRepository2>(userRepTAs));
00533 }
00534 catch(CPKIFException&)
00535 {
00536
00537
00538 }
00539 }
00540 else
00541 {
00542 CPKIFCAPIRepository2Ptr caStoreCerts(new CPKIFCAPIRepository2(CERT_SYSTEM_STORE_LOCAL_MACHINE));
00543 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIRepository2>(caStoreCerts));
00544
00545 CPKIFCAPICRLUpdate2Ptr crlUpdate(new CPKIFCAPICRLUpdate2(CERT_SYSTEM_STORE_LOCAL_MACHINE));
00546 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPICRLUpdate2>(crlUpdate));
00547
00548
00549 CPKIFCAPICRLUpdate2Ptr crlUpdate2(new CPKIFCAPICRLUpdate2(CERT_SYSTEM_STORE_LOCAL_MACHINE, g_defCACCAPITrustStore));
00550 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPICRLUpdate2>(crlUpdate2));
00551
00552 CPKIFCAPICertUpdate2Ptr certUpdate(new CPKIFCAPICertUpdate2(CERT_SYSTEM_STORE_LOCAL_MACHINE));
00553 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPICertUpdate2>(certUpdate));
00554
00555
00556 CPKIFCAPIUserRepository2Ptr userRep(new CPKIFCAPIUserRepository2(CERT_SYSTEM_STORE_LOCAL_MACHINE));
00557 try
00558 {
00559 cache->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIUserRepository2>(userRep));
00560 }
00561 catch(CPKIFException&)
00562 {
00563
00564
00565
00566 }
00567
00568
00569 }
00570
00571 #endif
00572
00573 SimpleCertCachePtr certCache(new SimpleCertCache);
00574 certCache->Initialize();
00575
00576
00577 IPKIFColleaguePtr cp = dynamic_pointer_cast<IPKIFColleague,SimpleCertCache>(certCache);
00578 cache->AddColleague(cp);
00579
00580 SimpleCRLCachePtr crlCache(new SimpleCRLCache);
00581 crlCache->Initialize();
00582 cp = dynamic_pointer_cast<IPKIFColleague,SimpleCRLCache>(crlCache);
00583 cache->AddColleague(cp);
00584
00585
00586 CPKIFCRLDPRetrievalPtr crldpRetrieval(new CPKIFCRLDPRetrieval);
00587 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFCRLDPRetrieval>(crldpRetrieval);
00588 cache->AddColleague(cp);
00589
00590 CPKIFAiaOrSiaRetrievalPtr siaAia(new CPKIFAiaOrSiaRetrieval);
00591 cp = dynamic_pointer_cast<IPKIFColleague, CPKIFAiaOrSiaRetrieval>(siaAia);
00592 cache->AddColleague(cp);
00593
00594
00595
00596
00597
00598
00599 if(!isService)
00600 {
00601
00602 CPKIFCryptoMediator2Ptr tmpCrypto(new CPKIFCryptoMediator2(true));
00603 crypto = tmpCrypto;
00604 crypto->InitializeMediator(NULL);
00605 }
00606 else
00607 {
00608
00609 CPKIFCryptoMediator2Ptr tmpCrypto(new CPKIFCryptoMediator2(false));
00610 crypto = tmpCrypto;
00611 crypto->InitializeMediator(NULL);
00612 #ifdef _WIN32
00613 CPKIFBCryptGuard cngGuard;
00614 if(!cngGuard.IsCNGAvailable())
00615 {
00616 CPKIFCAPI2Ptr newCAPI(new CPKIFCAPI2(NULL, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE));
00617 CPKIFCAPIRawPtr newCAPIRaw(new CPKIFCAPIRaw());
00618
00619 crypto->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPI2>(newCAPI));
00620 crypto->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCAPIRaw>(newCAPIRaw));
00621 } else {
00622 CPKIFCNGCAPIPtr newCAPI(new CPKIFCNGCAPI(NULL, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE));
00623 CPKIFCNGCAPIRawPtr newCAPIRaw(new CPKIFCNGCAPIRaw());
00624
00625 crypto->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCNGCAPI>(newCAPI));
00626 crypto->AddColleague(dynamic_pointer_cast<IPKIFColleague,CPKIFCNGCAPIRaw>(newCAPIRaw));
00627 }
00628 #endif
00629 }
00630
00631
00632
00633
00634
00635
00636 CPKIFRevocationStatusMediator2Ptr tmpRevStatus(new CPKIFRevocationStatusMediator2(false));
00637 revStatus = tmpRevStatus;
00638 revStatus->InitializeMediator(NULL);
00639
00640 if(trustedResponder)
00641 {
00642
00643
00644 IPKIFColleaguePtr trcp = dynamic_pointer_cast<IPKIFColleague,CPKIFOCSPChecker>(trustedResponder);
00645 revStatus->AddColleague(trcp);
00646 }
00647
00648 CPKIFOCSPCheckerPtr ocspChecker(new CPKIFOCSPChecker());
00649
00650
00651 IPKIFColleaguePtr ocp = dynamic_pointer_cast<IPKIFColleague,CPKIFOCSPChecker>(ocspChecker);
00652 revStatus->AddColleague(ocp);
00653
00654 if(aiaColleague != (CPKIFOCSPChecker*)NULL)
00655 {
00656 aiaColleague = ocspChecker;
00657 }
00658
00659 CPKIFX509CRLCheckerPtr crlChecker(new CPKIFX509CRLChecker());
00660
00661
00662 IPKIFColleaguePtr crlcp = dynamic_pointer_cast<IPKIFColleague,CPKIFX509CRLChecker>(crlChecker);
00663 revStatus->AddColleague(crlcp);
00664
00665
00666
00667
00668
00669
00670 CPKIFPathProcessingMediator2Ptr tmpPath(new CPKIFPathProcessingMediator2(true));
00671 path = tmpPath;
00672 path->InitializeMediator(NULL);
00673
00674
00675
00676
00677 IPKIFMediatorPtr mp = dynamic_pointer_cast<IPKIFMediator, CPKIFCacheMediator2>(cache);
00678 path->AddMediator(mp);
00679 mp = dynamic_pointer_cast<IPKIFMediator, CPKIFCryptoMediator2>(crypto);
00680 path->AddMediator(mp);
00681 mp = dynamic_pointer_cast<IPKIFMediator, CPKIFRevocationStatusMediator2>(revStatus);
00682 path->AddMediator(mp);
00683
00684 return dynamic_pointer_cast<IPKIFMediator, CPKIFPathProcessingMediator2>(path);
00685 }
00686 catch(...)
00687 {
00688 throw;
00689 }
00690 }
00700 void CAC_API FreeDefaultMediator(
00702 IPKIFMediator* p)
00703 {
00704 if(p) {
00705 delete p;
00706
00707 ShutdownCrypto();
00708 }
00709 }
00710
00722 CAC_API IPKIFMediatorPtr MakeDefaultMediatorNSS(const char *db)
00723 {
00724 CPKIFOCSPCheckerPtr empty;
00725 return MakeDefaultMediatorNSS(db, empty, empty);
00726 }
00727
00739 CAC_API IPKIFMediatorPtr MakeDefaultMediatorNSS(const char *db, CPKIFOCSPCheckerPtr& trustedResponder)
00740 {
00741 CPKIFOCSPCheckerPtr empty;
00742 return MakeDefaultMediatorNSS(db, trustedResponder, empty);
00743 }
00744
00745
00774 CAC_API IPKIFMediatorPtr MakeDefaultMediatorNSS(
00776 const char *dbdir,
00778 CPKIFOCSPCheckerPtr& trustedResponder,
00780 CPKIFOCSPCheckerPtr& aiaColleague)
00781 {
00782
00783 CPKIFCacheMediator2Ptr cache;
00784 CPKIFCryptoMediator2Ptr crypto;
00785 CPKIFRevocationStatusMediator2Ptr revStatus;
00786 CPKIFPathProcessingMediator2Ptr path;
00787 IPKIFColleaguePtr cp;
00788 try
00789 {
00790 CPKIFCacheMediator2Ptr tmpCache(new CPKIFCacheMediator2(false));
00791 cache = tmpCache;
00792 cache->InitializeMediator(NULL);
00793
00794
00795 SimpleCertCachePtr certCache(new SimpleCertCache);
00796 certCache->Initialize();
00797 cp = dynamic_pointer_cast<IPKIFColleague,SimpleCertCache>(certCache);
00798 cache->AddColleague(cp);
00799
00800 SimpleCRLCachePtr crlCache(new SimpleCRLCache);
00801 crlCache->Initialize();
00802 cp = dynamic_pointer_cast<IPKIFColleague,SimpleCRLCache>(crlCache);
00803 cache->AddColleague(cp);
00804
00805
00806 CPKIFNSSRepositoryPtr repo(new CPKIFNSSRepository(dbdir));
00807 repo->Initialize();
00808 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFNSSRepository>(repo);
00809 cache->AddColleague(cp);
00810
00811 CPKIFNSSCertUpdatePtr certup(new CPKIFNSSCertUpdate(dbdir));
00812 certup->Initialize();
00813 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFNSSCertUpdate>(certup);
00814 cache->AddColleague(cp);
00815
00816 CPKIFNSSCRLUpdatePtr crlup(new CPKIFNSSCRLUpdate(dbdir));
00817 crlup->Initialize();
00818 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFNSSCRLUpdate>(crlup);
00819 cache->AddColleague(cp);
00820
00821 CPKIFNSSTrustStorePtr trust(new CPKIFNSSTrustStore(dbdir));
00822 trust->Initialize();
00823 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFNSSTrustStore>(trust);
00824 cache->AddColleague(cp);
00825
00826 CPKIFCRLDPRetrievalPtr crldpRetrieval(new CPKIFCRLDPRetrieval);
00827 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFCRLDPRetrieval>(crldpRetrieval);
00828 cache->AddColleague(cp);
00829
00830 CPKIFAiaOrSiaRetrievalPtr siaAia(new CPKIFAiaOrSiaRetrieval);
00831 cp = dynamic_pointer_cast<IPKIFColleague, CPKIFAiaOrSiaRetrieval>(siaAia);
00832 cache->AddColleague(cp);
00833
00834
00835 CPKIFCryptoMediator2Ptr tmpCrypto(new CPKIFCryptoMediator2(false));
00836 crypto = tmpCrypto;
00837 crypto->InitializeMediator(NULL);
00838
00839
00840 CPKIFNSSPtr newNSS(new CPKIFNSS(dbdir));
00841 newNSS->Initialize();
00842 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFNSS>(newNSS);
00843 crypto->AddColleague(cp);
00844
00845 CPKIFNSSRawPtr newNSSRaw(new CPKIFNSSRaw());
00846 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFNSSRaw>(newNSSRaw);
00847 crypto->AddColleague(cp);
00848
00849
00850
00851 CPKIFRevocationStatusMediator2Ptr tmpRevStatus(new CPKIFRevocationStatusMediator2(false));
00852 revStatus = tmpRevStatus;
00853 revStatus->InitializeMediator(NULL);
00854
00855 if(trustedResponder)
00856 {
00857 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFOCSPChecker>(trustedResponder);
00858 revStatus->AddColleague(cp);
00859 }
00860
00861 CPKIFOCSPCheckerPtr ocspChecker(new CPKIFOCSPChecker());
00862 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFOCSPChecker>(ocspChecker);
00863 revStatus->AddColleague(cp);
00864
00865 if(aiaColleague != (CPKIFOCSPChecker*)NULL)
00866 {
00867 aiaColleague = ocspChecker;
00868 }
00869
00870 CPKIFX509CRLCheckerPtr crlChecker(new CPKIFX509CRLChecker());
00871 cp = dynamic_pointer_cast<IPKIFColleague,CPKIFX509CRLChecker>(crlChecker);
00872 revStatus->AddColleague(cp);
00873
00874 CPKIFPathProcessingMediator2Ptr tmpPath(new CPKIFPathProcessingMediator2(true));
00875 path = tmpPath;
00876 path->InitializeMediator(NULL);
00877 IPKIFMediatorPtr mp = dynamic_pointer_cast<IPKIFMediator, CPKIFCacheMediator2>(cache);
00878 path->AddMediator(mp);
00879 mp = dynamic_pointer_cast<IPKIFMediator, CPKIFCryptoMediator2>(crypto);
00880 path->AddMediator(mp);
00881 mp = dynamic_pointer_cast<IPKIFMediator, CPKIFRevocationStatusMediator2>(revStatus);
00882 path->AddMediator(mp);
00883
00884 return dynamic_pointer_cast<IPKIFMediator, CPKIFPathProcessingMediator2>(path);
00885 }
00886 catch(...)
00887 {
00888 throw;
00889 }
00890
00891 }
00901 void CAC_API FreeDefaultMediatorNSS(
00903 IPKIFMediator* p)
00904 {
00905 if(p) {
00906 delete p;
00907
00908 ShutdownCrypto();
00909 }
00910 }
00911
00912
00913
00914 char* g_tspHeaders = "Content-Type: application/timestamp-query";
00915 char* g_scvpHeaders = "Content-Type: application/scvp-cv-request";
00916 char* g_scvpValPolHeaders = "Content-Type: application/scvp-vp-request";
00917 char* g_ocspHeaders = "Content-Type: application/ocsp-request";
00925 size_t WriteCB(
00927 void * ptr,
00929 size_t size,
00931 size_t count,
00933 void * data)
00934 {
00935 size_t realsize = size * count;
00936 CPKIFBuffer * buf = (CPKIFBuffer *) data;
00937 unsigned int oldLen = buf->GetLength();
00938 unsigned char * oldData = NULL;
00939 if(oldLen > 0)
00940 {
00941 oldData = new unsigned char[oldLen];
00942 if(NULL == oldData) return 0;
00943 memcpy(oldData,buf->GetBuffer(),oldLen);
00944 }
00945
00946 size_t stOldLen = oldLen;
00947
00948 int buffsize = 0;
00949 try
00950 {
00951 buffsize = numeric_cast<int>(realsize + stOldLen);
00952 }
00953 catch(bad_numeric_cast &)
00954 {
00955 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Size is an impossibly long number.");
00956 }
00957
00958 unsigned char * newData = buf->AllocateBuffer(buffsize);
00959 if(NULL != oldData)
00960 {
00961 memcpy(newData,oldData,oldLen);
00962 delete[] oldData;
00963 }
00964 memcpy(newData+oldLen,ptr,realsize);
00965
00966 return realsize;
00967 }
00968
00980 BOOL CAC_API PostRequestURL(
00983 CPKIFBufferPtr& encTSR,
00985 CPKIFBufferPtr& resp,
00988 const char* pstrURL,
00990 PKIFServiceType pst)
00991 {
00992 #undef RETURN
00993 #define RETURN(rv) \
00994 { \
00995 if (NULL != headers) \
00996 { curl_slist_free_all(headers); } \
00997 if (NULL != hCurl) \
00998 { curl_easy_cleanup(hCurl); } \
00999 return rv; \
01000 }
01001 CURL * hCurl = NULL;
01002 CURLcode curlRes = CURLE_OK;
01003 unsigned char* buf = NULL;
01004 char * url = NULL;
01005 curl_slist * headers = NULL;
01006
01007
01008 if(!pstrURL || strlen(pstrURL) == 0)
01009 {
01010 std::ostringstream os;
01011 os << "PostRequestURL() called without a valid URL";
01012 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01013 RETURN(FALSE);
01014 }
01015
01016
01017 std::string pstrURLString = pstrURL;
01018 if(PKIFHTTP::IsHttpServerBlacklisted(pstrURLString))
01019 {
01020 std::ostringstream os;
01021 os << pstrURL << " has been blacklisted";
01022 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01023 RETURN(FALSE);
01024 }
01025
01026
01027
01028 resp.reset(new CPKIFBuffer());
01029
01030
01031 switch(pst)
01032 {
01033 case PKIF_TSA:
01034 headers = curl_slist_append(headers,g_tspHeaders);
01035 break;
01036 case PKIF_SCVP:
01037 headers = curl_slist_append(headers,g_scvpHeaders);
01038 break;
01039 case PKIF_SCVP_VAL_POL:
01040 headers = curl_slist_append(headers,g_scvpValPolHeaders);
01041 break;
01042 case PKIF_OCSP:
01043 headers = curl_slist_append(headers,g_ocspHeaders);
01044 break;
01045 case PKIF_NONE:
01046
01047 headers = curl_slist_append(headers,"Pragma: ");
01048 break;
01049 default:
01050 return FALSE;
01051 };
01052
01053
01054
01055
01056 hCurl = curl_easy_init();
01057
01058 if(!hCurl)
01059 {
01060 std::ostringstream os;
01061 os << "curl_easy_init() failed";
01062 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01063 RETURN(FALSE);
01064 }
01065
01066
01067
01068
01069
01070
01071
01072 curlRes = curl_easy_setopt(hCurl,CURLOPT_URL,pstrURL);
01073 if(curlRes != CURLE_OK) {
01074 std::ostringstream os;
01075 os << "curl_easy_setopt(CURLOPT_URL) failed: " << curl_easy_strerror(curlRes);
01076 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01077 RETURN(FALSE);
01078 }
01079
01080
01081 curlRes = curl_easy_setopt(hCurl,CURLOPT_SSL_VERIFYPEER,0L);
01082
01083 if(curlRes != CURLE_OK) {
01084 std::ostringstream os;
01085 os << "curl_easy_setopt(CURLOPT_SSL_VERIFYPEER) failed: " << curl_easy_strerror(curlRes);
01086 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01087 RETURN(FALSE);
01088 }
01089
01090 curlRes = curl_easy_setopt(hCurl,CURLOPT_FOLLOWLOCATION,1L);
01091
01092 if(curlRes != CURLE_OK) {
01093 std::ostringstream os;
01094 os << "curl_easy_setopt(CURLOPT_FOLLOWLOCATION) failed: " << curl_easy_strerror(curlRes);
01095 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01096 RETURN(FALSE);
01097 }
01098
01099
01100 curlRes = curl_easy_setopt(hCurl,CURLOPT_WRITEFUNCTION,WriteCB);
01101 if(curlRes != CURLE_OK) {
01102 std::ostringstream os;
01103 os << "curl_easy_setopt(CURLOPT_WRITEFUNCTION) failed: " << curl_easy_strerror(curlRes);
01104 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01105 RETURN(FALSE);
01106 }
01107
01108
01109
01110
01111
01112 curlRes = curl_easy_setopt(hCurl,CURLOPT_WRITEDATA,(void *)&(*resp));
01113 if(curlRes != CURLE_OK) {
01114 std::ostringstream os;
01115 os << "curl_easy_setopt(CURLOPT_WRITEDATA) failed: " << curl_easy_strerror(curlRes);
01116 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01117 RETURN(FALSE);
01118 }
01119
01120
01121
01122
01123 if(headers != NULL)
01124 {
01125 curlRes = curl_easy_setopt(hCurl,CURLOPT_HTTPHEADER,headers);
01126 if(curlRes != CURLE_OK) {
01127 std::ostringstream os;
01128 os << "curl_easy_setopt(CURLOPT_HTTPHEADER) failed: " << curl_easy_strerror(curlRes);
01129 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01130 RETURN(FALSE);
01131 }
01132 }
01133
01134
01135
01136 if(0 != encTSR->GetLength()) {
01137 curlRes = curl_easy_setopt(hCurl,CURLOPT_POSTFIELDS,encTSR->GetBuffer());
01138 if(curlRes != CURLE_OK) {
01139 std::ostringstream os;
01140 os << "curl_easy_setopt(CURLOPT_POSTFIELDS) failed: " << curl_easy_strerror(curlRes);
01141 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01142 RETURN(FALSE);
01143 }
01144
01145
01146
01147 curlRes = curl_easy_setopt(hCurl,CURLOPT_POSTFIELDSIZE,encTSR->GetLength());
01148 if(curlRes != CURLE_OK) {
01149 std::ostringstream os;
01150 os << "curl_easy_setopt(CURLOPT_POSTFIELDSIZE) failed: " << curl_easy_strerror(curlRes);
01151 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01152 RETURN(FALSE);
01153 }
01154 }
01155
01156
01157 curlRes = curl_easy_perform(hCurl);
01158 if(curlRes != CURLE_OK)
01159 {
01160 if(CURLE_COULDNT_CONNECT == curlRes)
01161 {
01162 PKIFHTTP::AddBlacklistedHttpServer(pstrURLString);
01163 }
01164
01165 std::ostringstream os;
01166 os << "curl_easy_setopt(CURLOPT_POSTFIELDSIZE) failed: " << curl_easy_strerror(curlRes);
01167 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01168 RETURN(FALSE);
01169 }
01170
01171 RETURN(TRUE);
01172 }
01183 BOOL CAC_API PostRequest(
01186 CPKIFBufferPtr& encTSR,
01188 CPKIFBufferPtr& resp,
01191 const char* pstrServer,
01194 int port,
01197 const char* pstrObject,
01199 PKIFServiceType pst)
01200 {
01201 #undef RETURN
01202 #define RETURN(rv) \
01203 { \
01204 if (NULL != url) \
01205 { free(url); url = NULL; } \
01206 return rv; \
01207 }
01208 char * url;
01209 std::ostringstream us;
01210
01211 us << "http://" << pstrServer << ":" << port << pstrObject;
01212 url = strdup(us.str().c_str());
01213 if(NULL == url)
01214 {
01215 std::ostringstream os;
01216 os << "Unable to allocate buffer for URL";
01217 LOG_STRING_ERROR(os.str().c_str(), TOOLKIT_UTILS, COMMON_URL_OPERATION_FAILED, NULL);
01218 RETURN(FALSE);
01219 }
01220 BOOL rv = PostRequestURL(encTSR,resp,url,pst);
01221 RETURN(rv);
01222 }
01223
01224
01232 BOOL CAC_API GetRequest(
01235 const char* pstrURL,
01237 CPKIFBufferPtr& resp)
01238 {
01239 CPKIFBufferPtr tmp(new CPKIFBuffer());
01240 return PostRequestURL(tmp, resp, pstrURL, PKIF_NONE);
01241 }
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01331 CPKIFAlgorithmIdentifierPtr CAC_API GetHashAlgAI(
01333 PKIFCRYPTO::HASH_ALG hashAlgorithm)
01334 {
01335 switch(hashAlgorithm)
01336 {
01337 case PKIFCRYPTO::MD5:
01338 return g_md5AI;
01339 case PKIFCRYPTO::SHA1:
01340 return g_sha1AI;
01341 case PKIFCRYPTO::SHA256:
01342 return g_sha256AI;
01343 case PKIFCRYPTO::SHA384:
01344 return g_sha384AI;
01345 case PKIFCRYPTO::SHA512:
01346 return g_sha512AI;
01347 default:
01348 CPKIFAlgorithmIdentifierPtr tmpAI;
01349 return tmpAI;
01350 }
01351 }
01370 AlgClass GetAlgClass(
01372 const CPKIFAlgorithmIdentifierPtr& alg)
01373 {
01374 CPKIFOIDPtr sigAlgOID = alg->oid();
01375 if(*g_md5WithRSAEncryption == *sigAlgOID || *g_rsa == *sigAlgOID || *g_sha1WithRSAEncryption == *sigAlgOID ||
01376 *g_md2WithRSAEncryption == *sigAlgOID || *g_sha224WithRSAEncryption == *sigAlgOID ||
01377 *g_sha256WithRSAEncryption == *sigAlgOID || *g_sha384WithRSAEncryption == *sigAlgOID ||
01378 *g_sha512WithRSAEncryption == *sigAlgOID)
01379 return RSA_CLASS;
01380 else if(*g_dsaWithSHA1 == *sigAlgOID || *g_dsa == *sigAlgOID)
01381 return DSA_CLASS;
01382 else if(*g_ecc == *sigAlgOID ||
01383 *g_ecdsa_sha1 == *sigAlgOID ||
01384 *g_ecdsa_sha224 == *sigAlgOID ||
01385 *g_ecdsa_sha256 == *sigAlgOID ||
01386 *g_ecdsa_sha384 == *sigAlgOID ||
01387 *g_ecdsa_sha512 == *sigAlgOID)
01388 return ECDSA_CLASS;
01389 else
01390 throw CPKIFException(TOOLKIT_UTILS_SUBCOMPONENT, COMMON_UNSUPPORTED_ALG, "Unable to determine algorithm class");
01391 }
01392
01393
01401 bool CAC_API GetCACHashAlg(
01403 const CPKIFOIDPtr& alg,
01405 PKIFCRYPTO::HASH_ALG* hashAlg)
01406 {
01407 CPKIFAlgorithm* a = CPKIFAlgorithm::GetAlg(alg);
01408 if(a && hashAlg)
01409 {
01410 *hashAlg = a->HashAlg();
01411 return true;
01412 }
01413 else
01414 return false;
01415 }
01416
01417
01418
01419
01420
01421 CPKIFBufferPtr CAC_API EncodeCPKIFNameAsGenName(
01423 const CPKIFNamePtr& name)
01424 {
01425 CACX509V3GeneralName* objname = new CACX509V3GeneralName;
01426 memset(objname, 0, sizeof(CACX509V3GeneralName));
01427
01428
01429
01430
01431
01432
01433
01434
01435 CPKIFBufferPtr encodedName = name->Encoded();
01436 CACASNWRAPPER_CREATE(CACX509V3Name, rdnPDU);
01437 objname->u.directoryName = rdnPDU.Decode(encodedName->GetBuffer(), encodedName->GetLength());
01438 objname->t = T_CACX509V3GeneralName_directoryName;
01439
01440 CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
01441 ASN1OpenType* data1 = objPDU.Encode(objname);
01442 CPKIFBufferPtr tmpBuf;
01443 if(data1 != NULL)
01444 {
01445 tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
01446 delete data1;
01447 }
01448
01449 if(objname)
01450 {
01451 if(objname->u.directoryName)
01452 objname->u.directoryName = NULL;
01453 delete objname;
01454 }
01455
01456 return tmpBuf;
01457 }
01458
01459
01470 CPKIFNamePtr CAC_API GetPKIFNameFromStr(
01472 unsigned char* dn)
01473 {
01474 PKIFNameMemoryHelper mhX509V3Name;
01475 mhX509V3Name.pCACX509V3Name = new CACX509V3Name;
01476 memset(mhX509V3Name.pCACX509V3Name, 0, sizeof(CACX509V3Name));
01477
01478 CACX509V3RelativeDistinguishedName* pCacTmpRDN = new CACX509V3RelativeDistinguishedName;
01479
01480 if(NULL == dn || 3 > strlen((char*)dn))
01481 {
01482 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL DN string or DN less than three characters passed to GetPKIFNameFromStr");
01483 }
01484
01485 BuildCACX509V3DistinguishedName(dn, pCacTmpRDN);
01486 mhX509V3Name.pCACX509V3Name->t = T_CACX509V3Name_rdnSequence;
01487 mhX509V3Name.pCACX509V3Name->u.rdnSequence = pCacTmpRDN;
01488
01489
01490 CACASNWRAPPER_CREATE(CACX509V3Name, objPDU2);
01491 ASN1OpenType* data1 = objPDU2.Encode((mhX509V3Name.pCACX509V3Name));
01492 CPKIFBufferPtr tmpBuf;
01493 if(data1 != NULL)
01494 {
01495 tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
01496 delete data1;
01497 }
01498
01499 CPKIFNamePtr pCacName(new CPKIFName(tmpBuf));
01500
01501
01502 return pCacName;
01503 }
01504
01505
01506
01507
01517 void BuildCACX509V3DistinguishedName(
01519 unsigned char* dn,
01521 CACX509V3DistinguishedName* cacx509v3dn)
01522 {
01523 if(NULL == cacx509v3dn)
01524 {
01525 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL CACX509V3DistinguishedName* passed to BuildCACX509V3DistinguishedName");
01526 }
01527 if(NULL == dn || 3 > strlen((char*)dn))
01528 {
01529 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL DN string or DN less than three characters passed to BuildCACX509V3DistinguishedName");
01530 }
01531
01532
01533 cacx509v3dn->count = 0;
01534 DListNode *rdnNode = NULL;
01535
01536 enum State
01537 {
01538 START,
01539 VALUE,
01540 ESCAPED,
01541 QUOTE_OPEN
01542 };
01543
01544 State eState = START;
01545 State eStatePrevious = START;
01546 size_t nStart = 0;
01547 size_t nIndex = 0;
01548
01549 size_t nLen = strlen((char*)dn);
01550 for( nIndex; nIndex < nLen; nIndex++ )
01551 {
01552 switch( eState )
01553 {
01554 case START:
01555 if(dn[nIndex] != ' ' && dn[nIndex] != ',')
01556 {
01557 nStart = nIndex;
01558 eState = VALUE;
01559 }
01560 break;
01561 case VALUE:
01562 if(dn[nIndex] == ',' )
01563 {
01564 unsigned char* rdn = new unsigned char[nIndex - nStart + 1];
01565 memcpy(rdn, &dn[nStart], nIndex - nStart);
01566 rdn[nIndex - nStart] = '\0';
01567
01568
01569 PKIFNameMemoryHelper mhCACX509V3RDN;
01570 mhCACX509V3RDN.pCACX509V3RDN = new CACX509V3RelativeDistinguishedName;
01571 memset(mhCACX509V3RDN.pCACX509V3RDN, 0, sizeof(CACX509V3RelativeDistinguishedName));
01572
01573 BuildCACX509V3RelativeDistinguishedName(rdn, mhCACX509V3RDN.pCACX509V3RDN);
01574 CACASNWRAPPER_CREATE(CACX509V3RelativeDistinguishedName, objPDU);
01575 ASN1OpenType* asn1ot = objPDU.Encode(mhCACX509V3RDN.pCACX509V3RDN);
01576
01577 if(NULL == rdnNode)
01578 {
01579 NEW_NODE(rdnNode)
01580 }
01581 else
01582 {
01583 NEW_NEXT_AND_ADVANCE(rdnNode)
01584 }
01585
01586 rdnNode->data = new ASN1OpenType;
01587 ((ASN1OpenType*)rdnNode->data)->data = new unsigned char[asn1ot->numocts];
01588 memcpy((void*)((ASN1OpenType*)rdnNode->data)->data, asn1ot->data, asn1ot->numocts);
01589 ((ASN1OpenType*)rdnNode->data)->numocts = asn1ot->numocts;
01590
01591
01592 SET_HEAD_TAIL_INCREMENT((*cacx509v3dn), rdnNode)
01593
01594 delete rdn;
01595 delete asn1ot;
01596 eState = START;
01597 }
01598 else if(dn[nIndex] == '\"')
01599 {
01600 eState = QUOTE_OPEN;
01601 }
01602 else if(dn[nIndex] == '\\')
01603 {
01604 eStatePrevious = VALUE;
01605 eState = ESCAPED;
01606 }
01607 break;
01608 case ESCAPED:
01609 eState = eStatePrevious;
01610 break;
01611 case QUOTE_OPEN:
01612 if(dn[nIndex] == '\"' )
01613 {
01614 eState = VALUE;
01615 }
01616 else if(dn[nIndex] == '\\')
01617 {
01618 eStatePrevious = QUOTE_OPEN;
01619 eState = ESCAPED;
01620 }
01621 break;
01622 }
01623 }
01624 if(eState == VALUE)
01625 {
01626 unsigned char* rdn = new unsigned char[nLen - nStart + 1];
01627 memcpy(rdn, &dn[nStart], nLen - nStart);
01628
01629
01630 unsigned char *tmp = rdn;
01631 int n = 0;
01632 while (tmp[n] == ' ') n++;
01633 tmp = &(tmp[n]);
01634
01635 int size = 0;
01636 try
01637 {
01638 size = numeric_cast<int>(nLen - nStart);
01639 }
01640 catch(bad_numeric_cast &)
01641 {
01642 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Size is an impossibly long number.");
01643 }
01644
01645 n = size - n - 1;
01646 while (tmp[n] == ' ') n--;
01647 tmp[n+1] = '\0';
01648
01649
01650 PKIFNameMemoryHelper mhCACX509V3RDN;
01651 mhCACX509V3RDN.pCACX509V3RDN = new CACX509V3RelativeDistinguishedName;
01652 memset(mhCACX509V3RDN.pCACX509V3RDN, 0, sizeof(CACX509V3RelativeDistinguishedName));
01653
01654 BuildCACX509V3RelativeDistinguishedName(tmp, mhCACX509V3RDN.pCACX509V3RDN);
01655 CACASNWRAPPER_CREATE(CACX509V3RelativeDistinguishedName, objPDU);
01656 ASN1OpenType* asn1ot = objPDU.Encode(mhCACX509V3RDN.pCACX509V3RDN);
01657
01658 if(NULL == rdnNode)
01659 {
01660 NEW_NODE(rdnNode)
01661 }
01662 else
01663 {
01664 NEW_NEXT_AND_ADVANCE(rdnNode)
01665 }
01666
01667 rdnNode->data = new ASN1OpenType;
01668 ((ASN1OpenType*)rdnNode->data)->data = new unsigned char[asn1ot->numocts];
01669 memcpy((void*)((ASN1OpenType*)rdnNode->data)->data, asn1ot->data, asn1ot->numocts);
01670 ((ASN1OpenType*)rdnNode->data)->numocts = asn1ot->numocts;
01671
01672
01673 SET_HEAD_TAIL_INCREMENT((*cacx509v3dn), rdnNode)
01674 delete rdn;
01675 delete asn1ot;
01676 }
01677
01678
01679 DListNode* currNode = cacx509v3dn->head;
01680 for(unsigned int ii = 0; ii < cacx509v3dn->count; ++ii)
01681 {
01682 DListNode* tmpNext = currNode->next;
01683 currNode->next = currNode->prev;
01684 currNode = currNode->prev = tmpNext;
01685 }
01686 DListNode* tail = cacx509v3dn->head;
01687 cacx509v3dn->head = cacx509v3dn->tail;
01688 cacx509v3dn->tail = tail;
01689 }
01690
01691
01692
01702 void BuildCACX509V3RelativeDistinguishedName(
01704 unsigned char* rdn,
01706 CACX509V3RelativeDistinguishedName* cacx509v3rdn)
01707 {
01708 if(NULL == cacx509v3rdn)
01709 {
01710 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL CACX509V3RelativeDistinguishedName* passed to BuildCACX509V3RelativeDistinguishedName");
01711 }
01712
01713 if(NULL == rdn || 3 > strlen((char*)rdn))
01714 {
01715 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL RDN string or RDN less than three characters passed to BuildCACX509V3RelativeDistinguishedName");
01716 }
01717
01718 cacx509v3rdn->count = 0;
01719 DListNode *typeAndValNode = NULL;
01720
01721 enum State
01722 {
01723 START,
01724 VALUE,
01725 ESCAPED,
01726 QUOTE_OPEN
01727 };
01728
01729 State eState = START;
01730 State eStatePrevious = START;
01731 size_t nStart = 0;
01732 size_t nIndex = 0;
01733
01734 size_t nLen = strlen((char*)rdn);
01735 for(nIndex; nIndex < nLen; nIndex++)
01736 {
01737 switch(eState)
01738 {
01739 case START:
01740 if(rdn[nIndex] != ' ' && rdn[nIndex] != '+')
01741 {
01742 nStart = nIndex;
01743 eState = VALUE;
01744 }
01745 break;
01746 case VALUE:
01747 if(rdn[nIndex] == '+')
01748 {
01749 char* attrTypeAndVal = new char[nIndex - nStart + 1];
01750 memcpy(attrTypeAndVal, &rdn[nStart], nIndex - nStart);
01751
01752
01753 char *tmp = attrTypeAndVal;
01754 int n = 0;
01755 while (tmp[n] == ' ') n++;
01756 tmp = &(tmp[n]);
01757
01758 int size = 0;
01759 try
01760 {
01761 size = numeric_cast<int>(nIndex - nStart);
01762 }
01763 catch(bad_numeric_cast &)
01764 {
01765 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Size is an impossibly long number.");
01766 }
01767
01768 n = size - n - 1;
01769 while (tmp[n] == ' ') n--;
01770 tmp[n+1] = '\0';
01771
01772 CACX509V3AttributeTypeAndValue* atadv = new CACX509V3AttributeTypeAndValue;
01773 BuildCACX509V3AttributeTypeAndValue(tmp, atadv);
01774 if(NULL == typeAndValNode)
01775 {
01776 NEW_NODE(typeAndValNode)
01777 }
01778 else
01779 {
01780 NEW_NEXT_AND_ADVANCE(typeAndValNode)
01781 }
01782
01783 typeAndValNode->data = atadv;
01784
01785
01786 SET_HEAD_TAIL_INCREMENT((*cacx509v3rdn), typeAndValNode)
01787
01788 delete attrTypeAndVal;
01789 eState = START;
01790 }
01791 else if(rdn[nIndex] == '\"')
01792 {
01793 eState = QUOTE_OPEN;
01794 }
01795 else if(rdn[nIndex] == '\\')
01796 {
01797 eStatePrevious = VALUE;
01798 eState = ESCAPED;
01799 }
01800 break;
01801
01802 case ESCAPED:
01803 eState = eStatePrevious;
01804 break;
01805
01806 case QUOTE_OPEN:
01807 if(rdn[nIndex] == '\"')
01808 {
01809 eState = VALUE;
01810 }
01811 else if(rdn[nIndex] == '\\' )
01812 {
01813 eStatePrevious = QUOTE_OPEN;
01814 eState = ESCAPED;
01815 }
01816 break;
01817 };
01818 }
01819 if(eState == VALUE)
01820 {
01821 char* attrTypeAndVal = new char[nLen - nStart + 1];
01822 memcpy(attrTypeAndVal, &rdn[nStart], nLen - nStart);
01823
01824
01825 char *tmp = attrTypeAndVal;
01826 int n = 0;
01827 while (tmp[n] == ' ') n++;
01828 tmp = &(tmp[n]);
01829
01830 int size = 0;
01831 try
01832 {
01833 size = numeric_cast<int>(nLen - nStart);
01834 }
01835 catch(bad_numeric_cast &)
01836 {
01837 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Size is an impossibly long number.");
01838 }
01839 n = size - n - 1;
01840 while (tmp[n] == ' ') n--;
01841 tmp[n+1] = '\0';
01842
01843 CACX509V3AttributeTypeAndValue* atdv;
01844 atdv = new CACX509V3AttributeTypeAndValue;
01845 BuildCACX509V3AttributeTypeAndValue(tmp, atdv);
01846 if(NULL == typeAndValNode)
01847 {
01848 NEW_NODE(typeAndValNode)
01849 }
01850 else
01851 {
01852 NEW_NEXT_AND_ADVANCE(typeAndValNode)
01853 }
01854
01855 typeAndValNode->data = atdv;
01856
01857
01858 SET_HEAD_TAIL_INCREMENT((*cacx509v3rdn), typeAndValNode)
01859
01860 delete attrTypeAndVal;
01861 }
01862 }
01863
01864
01865
01876 void BuildCACX509V3AttributeTypeAndValue(
01878 char* attrTypeAndValue,
01880 CACX509V3AttributeTypeAndValue* cacx509v3atadv)
01881 {
01882 if(NULL == cacx509v3atadv)
01883 {
01884 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL CACX509V3AttributeTypeAndValue* passed to BuildCACX509V3AttributeTypeAndValue");
01885 }
01886
01887 if(NULL == attrTypeAndValue || 3 > strlen(attrTypeAndValue))
01888 {
01889 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL attribute type and value string or atav string less than three characters passed to BuildCACX509V3AttributeTypeAndValue");
01890 }
01891
01892 size_t attrTypeAndValLen = strlen(attrTypeAndValue);
01893 char equalChar[] = "=";
01894 char* pEqual = strstr(attrTypeAndValue, (const char*)equalChar);
01895 if(NULL == pEqual)
01896 {
01897 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Attribute type and distinguished value string passed to BuildCACX509V3AttributeTypeAndValue is missing equal sign");
01898 }
01899
01900
01901 size_t tmpLen = pEqual - attrTypeAndValue;
01902 char* attrType = new char[tmpLen + 1];
01903 memcpy(attrType, attrTypeAndValue, tmpLen);
01904 while(attrType[tmpLen-1] == ' ' && &attrType[tmpLen-1] > attrType)
01905 --tmpLen;
01906 attrType[tmpLen] = '\0';
01907
01908
01909 tmpLen = &(attrTypeAndValue[attrTypeAndValLen - 1]) - pEqual;
01910 char* attrVal = new char[tmpLen + 1];
01911 memcpy(attrVal, pEqual + 1, tmpLen);
01912 attrVal[tmpLen] = '\0';
01913
01914
01915
01916 char oidBuf[MAXOID];
01917 if(0 == STRCASECMP(attrType, "cn"))
01918 strcpy((char*)oidBuf, "2.5.4.3");
01919 else if(0 == STRCASECMP(attrType, "c"))
01920 strcpy((char*)oidBuf, "2.5.4.6");
01921 else if(0 == STRCASECMP(attrType, "l") || 0 == STRCASECMP(attrType, "locality"))
01922 strcpy((char*)oidBuf, "2.5.4.7");
01923 else if(0 == STRCASECMP(attrType, "o") || 0 == STRCASECMP(attrType, "organization"))
01924 strcpy((char*)oidBuf, "2.5.4.10");
01925 else if(0 == STRCASECMP(attrType, "ou"))
01926 strcpy((char*)oidBuf, "2.5.4.11");
01927 else if(0 == STRCASECMP(attrType, "e") || 0 == STRCASECMP(attrType, "email"))
01928 strcpy((char*)oidBuf, "1.2.840.113549.1.9.1");
01929 else if(0 == STRCASECMP(attrType, "surname") || 0 == STRCASECMP(attrType, "SN"))
01930 strcpy((char*)oidBuf, "2.5.4.4");
01931 else if(0 == STRCASECMP(attrType, "serialNumber") || 0 == STRCASECMP(attrType, "Serial Number"))
01932 strcpy((char*)oidBuf, "2.5.4.5");
01933 else if(0 == STRCASECMP(attrType, "stateOrProvinceName") || 0 == STRCASECMP(attrType, "S") || 0 == STRCASECMP(attrType, "ST"))
01934 strcpy((char*)oidBuf, "2.5.4.8");
01935 else if(0 == STRCASECMP(attrType, "streetAddress") || 0 == STRCASECMP(attrType, "street"))
01936 strcpy((char*)oidBuf, "2.5.4.9");
01937 else if(0 == STRCASECMP(attrType, "title") || 0 == STRCASECMP(attrType, "t"))
01938 strcpy((char*)oidBuf, "2.5.4.12");
01939 else if(0 == STRCASECMP(attrType, "dnQualifier"))
01940 strcpy((char*)oidBuf, "2.5.4.46");
01941 else if(0 == STRCASECMP(attrType, "i") || 0 == STRCASECMP(attrType, "initials"))
01942 strcpy((char*)oidBuf, "2.5.4.43");
01943 else if(0 == STRCASECMP(attrType, "givenName") || 0 == STRCASECMP(attrType, "g"))
01944 strcpy((char*)oidBuf, "2.5.4.42");
01945 else if(0 == STRCASECMP(attrType, "generationQualifier"))
01946 strcpy((char*)oidBuf, "2.5.4.44");
01947 else if(0 == STRCASECMP(attrType, "pseudonym"))
01948 strcpy((char*)oidBuf, "2.5.4.65");
01949 else if(0 == STRCASECMP(attrType, "dc") || 0 == STRCASECMP(attrType, "domainComponent"))
01950 strcpy((char*)oidBuf, "0.9.2342.19200300.100.1.25");
01951 else
01952 {
01953
01954
01955 strcpy(oidBuf, attrType);
01956 }
01957 CPKIFOIDPtr oid(new CPKIFOID(CPKIFStringPtr(new std::string(oidBuf))));
01958
01959 try
01960 {
01961
01962 CPKIFStringPtr str(new std::string(oid->ToString()));
01963 ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
01964 CopyOID(&cacx509v3atadv->type, tmpOid);
01965
01966 if(NULL != tmpOid)
01967 delete tmpOid;
01968
01969 }
01970 catch(...)
01971 {
01972 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Attribute type and distinguished value string passed to BuildCACX509V3AttributeTypeAndValue contains an unexpected attribute type");
01973 }
01974
01975
01976
01977 ASN1OpenType* ds = NULL;
01978 if(0 == STRCASECMP(attrType, "dc") || 0 == STRCASECMP(attrType, "domainComponent"))
01979 {
01980 CACASNWRAPPER_CREATE(CACX509V3CACDirectoryString, objPDU);
01981 CACX509V3CACDirectoryString dirStr;
01982 dirStr.t = T_CACX509V3CACDirectoryString_ia5String;
01983 dirStr.u.ia5String = attrVal;
01984 ds = objPDU.Encode(&dirStr);
01985
01986 cacx509v3atadv->value.data = new unsigned char[ds->numocts + 1];
01987 memcpy((void*)(cacx509v3atadv->value.data), (void*)ds->data, ds->numocts);
01988 cacx509v3atadv->value.numocts = ds->numocts;
01989 }
01990 else if(0 == STRCASECMP(attrType, "e") || 0 == STRCASECMP(attrType, "email"))
01991 {
01992 CACASNWRAPPER_CREATE(CACX509V3CACDirectoryString, objPDU);
01993 CACX509V3CACDirectoryString dirStr;
01994 dirStr.t = T_CACX509V3CACDirectoryString_ia5String;
01995 dirStr.u.ia5String = attrVal;
01996 ds = objPDU.Encode(&dirStr);
01997
01998 cacx509v3atadv->value.data = new unsigned char[ds->numocts + 1];
01999 memcpy((void*)(cacx509v3atadv->value.data), (void*)ds->data, ds->numocts);
02000 cacx509v3atadv->value.numocts = ds->numocts;
02001 }
02002 else
02003 {
02004 CACASNWRAPPER_CREATE(CACX509V3CACDirectoryString, objPDU);
02005 CACX509V3CACDirectoryString dirStr;
02006 dirStr.t = T_CACX509V3CACDirectoryString_printableString;
02007 dirStr.u.printableString = attrVal;
02008 ds = objPDU.Encode(&dirStr);
02009
02010 cacx509v3atadv->value.data = new unsigned char[ds->numocts + 1];
02011 memcpy((void*)(cacx509v3atadv->value.data), (void*)ds->data, ds->numocts);
02012 cacx509v3atadv->value.numocts = ds->numocts;
02013 }
02014
02015 delete attrVal;
02016 delete attrType;
02017 delete ds;
02018 }
02019
02026 void split(const string & text, string & separators, vector<string> & words)
02027 {
02028 size_t n = text.length();
02029 size_t start, stop;
02030
02031 start = text.find_first_not_of(separators);
02032 while ((start >= 0) && (start < n)) {
02033 stop = text.find_first_of(separators, start);
02034 if ((stop < 0) || (stop > n)) stop = n;
02035 words.push_back(text.substr(start, stop - start));
02036 start = text.find_first_not_of(separators, stop+1);
02037 }
02038 }
02039
02072 CPKIFGeneralNamePtr GetPKIFGenNameFromStr(
02074 unsigned char* name,
02076 int nameType)
02077 {
02078
02079 if(name == (unsigned char*)NULL)
02080 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL pointer passed to GetPKIFGenNameFromStr");
02081
02082 PKIFNameMemoryHelper mhCACX509V3GN;
02083 mhCACX509V3GN.pCACX509V3GN = new CACX509V3GeneralName;
02084 memset(mhCACX509V3GN.pCACX509V3GN, 0, sizeof(CACX509V3GeneralName));
02085
02086 switch(nameType)
02087 {
02088 case T_CACX509V3GeneralName_otherName:
02089 mhCACX509V3GN.pCACX509V3GN->u.otherName = new ASN1OpenType;
02090 mhCACX509V3GN.pCACX509V3GN->u.otherName->data = new unsigned char[strlen((char*)name) + 1];
02091 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.otherName->data, (char*)name);
02092 try {
02093 mhCACX509V3GN.pCACX509V3GN->u.otherName->numocts = boost::numeric_cast<boost::uint32_t,size_t>(strlen((char*)name));
02094 }catch(boost::numeric::bad_numeric_cast & ){
02095 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid character string passed to GetPKIFGenNameFromStr");
02096 }
02097 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_otherName;
02098 break;
02099
02100 case T_CACX509V3GeneralName_rfc822Name:
02101 mhCACX509V3GN.pCACX509V3GN->u.rfc822Name = new char[strlen((char*)name) + 1];
02102 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.rfc822Name, (char*)name);
02103 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_rfc822Name;
02104 break;
02105
02106 case T_CACX509V3GeneralName_dNSName:
02107 mhCACX509V3GN.pCACX509V3GN->u.dNSName = new char[strlen((char*)name) + 1];
02108 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.dNSName, (char*)name);
02109 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_dNSName;
02110 break;
02111
02112 case T_CACX509V3GeneralName_x400Address:
02113 mhCACX509V3GN.pCACX509V3GN->u.x400Address = new ASN1OpenType;
02114 mhCACX509V3GN.pCACX509V3GN->u.x400Address->data = new unsigned char[strlen((char*)name) + 1];
02115 memcpy((void*)(mhCACX509V3GN.pCACX509V3GN->u.x400Address->data), (void*)name, strlen((char*)name));
02116 try {
02117 mhCACX509V3GN.pCACX509V3GN->u.x400Address->numocts = boost::numeric_cast<boost::uint32_t,size_t>(strlen((char*)name));
02118 }catch(boost::numeric::bad_numeric_cast &){
02119 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid character string passed to GetPKIFGenNameFromStr");
02120 }
02121 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_x400Address;
02122 break;
02123
02124 case T_CACX509V3GeneralName_directoryName:
02125 {
02126 mhCACX509V3GN.pCACX509V3GN->u.directoryName = new CACX509V3Name;
02127 CACX509V3RelativeDistinguishedName* pCacTmpRDN = new CACX509V3RelativeDistinguishedName;
02128 BuildCACX509V3DistinguishedName(name, pCacTmpRDN);
02129 mhCACX509V3GN.pCACX509V3GN->u.directoryName->t = T_CACX509V3Name_rdnSequence;
02130 mhCACX509V3GN.pCACX509V3GN->u.directoryName->u.rdnSequence = pCacTmpRDN;
02131 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_directoryName;
02132 }
02133 break;
02134
02135 case T_CACX509V3GeneralName_ediPartyName:
02136
02137 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_ediPartyName;
02138 break;
02139
02140 case T_CACX509V3GeneralName_uniformResourceIdentifier:
02141 mhCACX509V3GN.pCACX509V3GN->u.uniformResourceIdentifier = new char[strlen((char*)name) + 1];
02142 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.uniformResourceIdentifier, (char*)name);
02143 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_uniformResourceIdentifier;
02144 break;
02145
02146 case T_CACX509V3GeneralName_iPAddress:
02147 {
02148 string base = (char*)name;
02149 vector<string> components;
02150 vector<string>::iterator iter;
02151 string tmp = "/";
02152 split(string((char *)base.c_str()),tmp,components);
02153
02154 boost::asio::io_service io_service;
02155 ip::address addr;
02156 try {
02157 addr = ip::address::from_string(components[0].c_str());
02158 }catch(std::exception & ){
02159 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid IP address passed to GetPKIFGenNameFromStr");
02160 }
02161
02162 if(addr.is_v4())
02163 {
02164 ip::address_v4 v4Addr = addr.to_v4();
02165 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_iPAddress;
02166 mhCACX509V3GN.pCACX509V3GN->u.iPAddress = new ASN1DynOctStr;
02167
02168 ip::address_v4::bytes_type v4Addr_bytes_value = v4Addr.to_bytes();
02169
02170
02171 int v4_len = 4;
02172 int size = v4_len;
02173 if(components.size() > 1)
02174 {
02175 ip::address mask;
02176 try {
02177 mask = ip::address::from_string(components[1].c_str());
02178 }catch(std::exception & ){
02179 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid mask passed to GetPKIFGenNameFromStr");
02180 }
02181 ip::address_v4 v4Mask = mask.to_v4();
02182
02183 ip::address_v4::bytes_type mask_bytes_value = v4Mask.to_bytes();
02184
02185 size += v4_len;
02186 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02187 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02188 memcpy((void*)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data, v4Addr_bytes_value.c_array(),v4_len);
02189 memcpy((void*)(mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data + v4_len), mask_bytes_value.c_array(),v4_len);
02190 }
02191 else
02192 {
02193 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02194 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02195 memcpy((void*)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data, v4Addr_bytes_value.c_array(),v4_len);
02196 }
02197
02198 }
02199 else if(addr.is_v6())
02200 {
02201 ip::address_v6 v6Addr = addr.to_v6();
02202
02203 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_iPAddress;
02204 mhCACX509V3GN.pCACX509V3GN->u.iPAddress = new ASN1DynOctStr;
02205
02206 ip::address_v6::bytes_type v6Addr_bytes_value = v6Addr.to_bytes();
02207
02208 int v6_len = 16;
02209 int size = v6_len;
02210 if(components.size() > 1)
02211 {
02212 ip::address mask;
02213 try {
02214 mask = ip::address::from_string(components[1].c_str());
02215 }catch(std::exception & ){
02216 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid mask passed to GetPKIFGenNameFromStr");
02217 }
02218 ip::address_v6 v6Mask = mask.to_v6();
02219
02220 ip::address_v6::bytes_type mask_bytes_value = v6Mask.to_bytes();
02221
02222 size += v6_len;
02223 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02224 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02225 memcpy((void *)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data,v6Addr_bytes_value.c_array(),v6_len);
02226 memcpy((void *)(mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data + v6_len),mask_bytes_value.c_array(),v6_len);
02227
02228 }
02229 else
02230 {
02231 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02232 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02233 memcpy((void*)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data, v6Addr_bytes_value.c_array(), v6_len);
02234
02235 }
02236 }
02237
02238 }
02239 break;
02240
02241 case T_CACX509V3GeneralName_registeredID:
02242 {
02243 mhCACX509V3GN.pCACX509V3GN->u.registeredID = new ASN1OBJID;
02244 CPKIFOIDPtr oid(new CPKIFOID(CPKIFStringPtr(new std::string((char*)name))));
02245 try
02246 {
02247 CPKIFStringPtr str(new std::string(oid->ToString()));
02248 ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
02249 CopyOID(mhCACX509V3GN.pCACX509V3GN->u.registeredID, tmpOid);
02250 if(tmpOid)
02251 delete tmpOid;
02252
02253 }
02254 catch(...)
02255 {
02256 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Invalid OID passed to BuildCACX509V3AttributeTypeAndValue");
02257 }
02258 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_registeredID;
02259 }
02260 break;
02261 default:
02262 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Unexpected general name type passed to GetPKIFGenNameFromStr");
02263 }
02264
02265 CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
02266 ASN1OpenType* data1 = objPDU.Encode((CACX509V3GeneralName*)mhCACX509V3GN.pCACX509V3GN);
02267 CPKIFBufferPtr tmpBuf;
02268 if(data1 != NULL)
02269 {
02270 tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
02271 delete data1;
02272 }
02273
02274 CPKIFGeneralNamePtr pCacGenName(new CPKIFGeneralName(tmpBuf));
02275
02276 return pCacGenName;
02277 }
02311 CPKIFBufferPtr GetEncGenNameFromStr(
02313 unsigned char* name,
02315 int nameType)
02316 {
02317
02318 if(name == (unsigned char*)NULL)
02319 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL pointer passed to GetPKIFGenNameFromStr");
02320
02321 PKIFNameMemoryHelper mhCACX509V3GN;
02322 mhCACX509V3GN.pCACX509V3GN = new CACX509V3GeneralName;
02323 memset(mhCACX509V3GN.pCACX509V3GN, 0, sizeof(CACX509V3GeneralName));
02324
02325 switch(nameType)
02326 {
02327 case T_CACX509V3GeneralName_otherName:
02328 mhCACX509V3GN.pCACX509V3GN->u.otherName = new ASN1OpenType;
02329 mhCACX509V3GN.pCACX509V3GN->u.otherName->data = new unsigned char[strlen((char*)name) + 1];
02330 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.otherName->data, (char*)name);
02331 try {
02332 mhCACX509V3GN.pCACX509V3GN->u.otherName->numocts = boost::numeric_cast<boost::uint32_t,size_t>(strlen((char*)name));
02333 }catch(boost::numeric::bad_numeric_cast & ){
02334 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid character string passed to GetPKIFGenNameFromStr");
02335 }
02336 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_otherName;
02337 break;
02338
02339 case T_CACX509V3GeneralName_rfc822Name:
02340 mhCACX509V3GN.pCACX509V3GN->u.rfc822Name = new char[strlen((char*)name) + 1];
02341 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.rfc822Name, (char*)name);
02342 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_rfc822Name;
02343 break;
02344
02345 case T_CACX509V3GeneralName_dNSName:
02346 mhCACX509V3GN.pCACX509V3GN->u.dNSName = new char[strlen((char*)name) + 1];
02347 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.dNSName, (char*)name);
02348 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_dNSName;
02349 break;
02350
02351 case T_CACX509V3GeneralName_x400Address:
02352 mhCACX509V3GN.pCACX509V3GN->u.x400Address = new ASN1OpenType;
02353 mhCACX509V3GN.pCACX509V3GN->u.x400Address->data = new unsigned char[strlen((char*)name) + 1];
02354 memcpy((void*)(mhCACX509V3GN.pCACX509V3GN->u.x400Address->data), (void*)name, strlen((char*)name));
02355 try {
02356 mhCACX509V3GN.pCACX509V3GN->u.x400Address->numocts = boost::numeric_cast<boost::uint32_t,size_t>(strlen((char*)name));
02357 }catch(boost::numeric::bad_numeric_cast &){
02358 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid character string passed to GetPKIFGenNameFromStr");
02359 }
02360 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_x400Address;
02361 break;
02362
02363 case T_CACX509V3GeneralName_directoryName:
02364 {
02365 mhCACX509V3GN.pCACX509V3GN->u.directoryName = new CACX509V3Name;
02366 CACX509V3RelativeDistinguishedName* pCacTmpRDN = new CACX509V3RelativeDistinguishedName;
02367 BuildCACX509V3DistinguishedName(name, pCacTmpRDN);
02368 mhCACX509V3GN.pCACX509V3GN->u.directoryName->t = T_CACX509V3Name_rdnSequence;
02369 mhCACX509V3GN.pCACX509V3GN->u.directoryName->u.rdnSequence = pCacTmpRDN;
02370 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_directoryName;
02371 }
02372 break;
02373
02374 case T_CACX509V3GeneralName_ediPartyName:
02375
02376 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_ediPartyName;
02377 break;
02378
02379 case T_CACX509V3GeneralName_uniformResourceIdentifier:
02380 mhCACX509V3GN.pCACX509V3GN->u.uniformResourceIdentifier = new char[strlen((char*)name) + 1];
02381 strcpy((char*)mhCACX509V3GN.pCACX509V3GN->u.uniformResourceIdentifier, (char*)name);
02382 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_uniformResourceIdentifier;
02383 break;
02384
02385 case T_CACX509V3GeneralName_iPAddress:
02386 {
02387 string base = (char*)name;
02388 vector<string> components;
02389 vector<string>::iterator iter;
02390 string tmp = "/";
02391 split(string((char *)base.c_str()),tmp,components);
02392
02393 boost::asio::io_service io_service;
02394 ip::address addr;
02395 try {
02396 addr = ip::address::from_string(components[0].c_str());
02397 }catch(std::exception & ){
02398 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid IP address passed to GetPKIFGenNameFromStr");
02399 }
02400
02401 if(addr.is_v4())
02402 {
02403 ip::address_v4 v4Addr = addr.to_v4();
02404 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_iPAddress;
02405 mhCACX509V3GN.pCACX509V3GN->u.iPAddress = new ASN1DynOctStr;
02406
02407 ip::address_v4::bytes_type v4Addr_bytes_value = v4Addr.to_bytes();
02408
02409
02410 int v4_len = 4;
02411 int size = v4_len;
02412 if(components.size() > 1)
02413 {
02414 ip::address mask;
02415 try {
02416 mask = ip::address::from_string(components[1].c_str());
02417 }catch(std::exception & ){
02418 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid mask passed to GetPKIFGenNameFromStr");
02419 }
02420 ip::address_v4 v4Mask = mask.to_v4();
02421
02422 ip::address_v4::bytes_type mask_bytes_value = v4Mask.to_bytes();
02423
02424 size += v4_len;
02425 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02426 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02427 memcpy((void*)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data, v4Addr_bytes_value.c_array(),v4_len);
02428 memcpy((void*)(mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data + v4_len), mask_bytes_value.c_array(),v4_len);
02429 }
02430 else
02431 {
02432 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02433 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02434 memcpy((void*)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data, v4Addr_bytes_value.c_array(),v4_len);
02435 }
02436
02437 }
02438 else if(addr.is_v6())
02439 {
02440 ip::address_v6 v6Addr = addr.to_v6();
02441
02442 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_iPAddress;
02443 mhCACX509V3GN.pCACX509V3GN->u.iPAddress = new ASN1DynOctStr;
02444
02445 ip::address_v6::bytes_type v6Addr_bytes_value = v6Addr.to_bytes();
02446
02447 int v6_len = 16;
02448 int size = v6_len;
02449 if(components.size() > 1)
02450 {
02451 ip::address mask;
02452 try {
02453 mask = ip::address::from_string(components[1].c_str());
02454 }catch(std::exception & ){
02455 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "invalid mask passed to GetPKIFGenNameFromStr");
02456 }
02457 ip::address_v6 v6Mask = mask.to_v6();
02458
02459 ip::address_v6::bytes_type mask_bytes_value = v6Mask.to_bytes();
02460
02461 size += v6_len;
02462 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02463 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02464 memcpy((void *)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data,v6Addr_bytes_value.c_array(),v6_len);
02465 memcpy((void *)(mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data + v6_len),mask_bytes_value.c_array(),v6_len);
02466
02467 }
02468 else
02469 {
02470 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->numocts = size;
02471 mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data = new unsigned char[size];
02472 memcpy((void*)mhCACX509V3GN.pCACX509V3GN->u.iPAddress->data, v6Addr_bytes_value.c_array(), v6_len);
02473
02474 }
02475 }
02476
02477 }
02478 break;
02479
02480 case T_CACX509V3GeneralName_registeredID:
02481 {
02482 mhCACX509V3GN.pCACX509V3GN->u.registeredID = new ASN1OBJID;
02483 CPKIFOIDPtr oid(new CPKIFOID(CPKIFStringPtr(new std::string((char*)name))));
02484 try
02485 {
02486 CPKIFStringPtr str(new std::string(oid->ToString()));
02487 ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
02488 CopyOID(mhCACX509V3GN.pCACX509V3GN->u.registeredID, tmpOid);
02489 if(tmpOid)
02490 delete tmpOid;
02491
02492 }
02493 catch(...)
02494 {
02495 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Invalid OID passed to BuildCACX509V3AttributeTypeAndValue");
02496 }
02497 mhCACX509V3GN.pCACX509V3GN->t = T_CACX509V3GeneralName_registeredID;
02498 }
02499 break;
02500 default:
02501 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "Unexpected general name type passed to GetPKIFGenNameFromStr");
02502 }
02503
02504 CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
02505 ASN1OpenType* data1 = objPDU.Encode((CACX509V3GeneralName*)mhCACX509V3GN.pCACX509V3GN);
02506 CPKIFBufferPtr tmpBuf;
02507 if(data1 != NULL)
02508 {
02509 tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
02510 delete data1;
02511 }
02512
02513 return tmpBuf;
02514 }
02515
02516
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531
02532
02533
02534
02535
02536
02537
02538
02539
02540
02541
02542
02543
02544
02545
02546
02547
02548
02549
02550
02551
02562 CPKIFBufferPtr CAC_API EncodeGeneralName(
02564 CPKIFGeneralNamePtr& gn)
02565 {
02566
02567 if(gn == (CPKIFGeneralName*)NULL || CPKIFGeneralName::NOTSET == gn->GetType())
02568 throw CPKIFException(TOOLKIT_UTILS, COMMON_INVALID_INPUT, "NULL pointer passed to EncodeGeneralName");
02569
02570
02571 PKIFNameMemoryHelper mhCACX509V3GN;
02572 mhCACX509V3GN.pCACX509V3GN = new CACX509V3GeneralName;
02573 memset(mhCACX509V3GN.pCACX509V3GN, 0, sizeof(CACX509V3GeneralName));
02574
02575
02576 CopyGeneralName(*mhCACX509V3GN.pCACX509V3GN, gn);
02577
02578 CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
02579
02580 ASN1OpenType* data1 = objPDU.Encode(mhCACX509V3GN.pCACX509V3GN);
02581
02582 if (data1 != NULL)
02583 {
02584 CPKIFBufferPtr tmp(new CPKIFBuffer(data1->data, data1->numocts));
02585 delete data1;
02586 return tmp;
02587 }
02588 else
02589 {
02590 CPKIFBufferPtr tmp(new CPKIFBuffer());
02591 delete data1;
02592 return tmp;
02593 }
02594 }
02595
02603 void FreeName(
02605 CACX509V3Name** dest)
02606 {
02607 if(!dest || ! *dest)
02608 return;
02609
02610 DListNode* cur = (*dest)->u.rdnSequence->head, *tmpNext = NULL;
02611 ASN1OpenType* curAT = NULL;
02612 while(cur)
02613 {
02614 curAT = (ASN1OpenType*)cur->data;
02615
02616 if(curAT)
02617 {
02618 if(curAT->data)
02619 {
02620 delete curAT->data;
02621 }
02622
02623 delete curAT;
02624 }
02625
02626 tmpNext = cur->next;
02627 delete cur;
02628 cur = tmpNext;
02629 }
02630
02631 delete (*dest)->u.rdnSequence;
02632 delete *dest; *dest = NULL;
02633 }
02641 void FreeGeneralName(
02643 CACX509V3GeneralName& objName)
02644 {
02645 switch(objName.t)
02646 {
02647 case 9:
02648 case 8:
02649 case 7:
02650 case 6:
02651 case 4:
02652 case 3:
02653 case 1:
02654
02655 break;
02656 case 5:
02657 FreeName(&objName.u.directoryName);
02658 break;
02659 case 2:
02660 if(objName.u.rfc822Name)
02661 {
02662 delete[] objName.u.rfc822Name; objName.u.rfc822Name = NULL;
02663 }
02664 break;
02665 }
02666 }
02667
02668
02676 CPKIFStringPtr GetCertInfo(
02679 CPKIFCertificatePtr& targetCert)
02680 {
02681 CPKIFStringPtr result(new std::string());
02682
02683 if(targetCert != (CPKIFCertificate*)NULL)
02684 {
02685 result->append("Issuer DN: ");
02686 result->append(targetCert->Issuer()->ToString());
02687 result->append(". Serial number: ");
02688 result->append(targetCert->SerialNumber());
02689 result->append(". Subject DN: ");
02690 result->append(targetCert->Subject()->ToString());
02691 result->append(".");
02692 }
02693 return result;
02694 }
02695
02696
02704 bool CertIsSelfIssued(
02706 const CPKIFCertificatePtr& cert)
02707 {
02708 return *cert->Subject() == *cert->Issuer();
02709 }
02717 void CollectNamesFromCRLDP(
02719 const CPKIFCertificatePtr& cert,
02721 CPKIFGeneralNameList& namesFromCert)
02722 {
02723 CPKIFCRLDistributionPointsPtr crldp = cert->GetExtension<CPKIFCRLDistributionPoints>();
02724 if(crldp == (CPKIFCRLDistributionPoints*)NULL)
02725 return;
02726
02727 CPKIFCRLDistributionPointListPtr crlDPs = crldp->DPs();
02728 CPKIFCRLDistributionPointList::iterator pos;
02729 CPKIFCRLDistributionPointList::iterator end = crlDPs->end();
02730 bool issuerAdded = false;
02731 for(pos = crlDPs->begin(); pos != end; ++pos)
02732 {
02733 CPKIFGeneralNameList crlIssuer;
02734 (*pos)->CRLIssuer(crlIssuer);
02735
02736
02737 CPKIFDistributionPointNamePtr dpName = (*pos)->DistributionPoint();
02738 if(dpName != (CPKIFDistributionPointName*)NULL)
02739 {
02740 CPKIFGeneralNameList dpNames;
02741 dpName->FullName(dpNames);
02742 CPKIFGeneralNameList::iterator dpNamesPos;
02743 CPKIFGeneralNameList::iterator dpNamesEnd = dpNames.end();
02744 for(dpNamesPos = dpNames.begin(); dpNamesPos != dpNamesEnd; ++dpNamesPos)
02745 {
02746 if(CPKIFGeneralName::URI == (*dpNamesPos)->GetType())
02747 {
02748 const char* uri = (*dpNamesPos)->uri();
02749 PKIFLDAP::LDAPURLDesc *ldapInfo = NULL;
02750 PKIFLDAP::ldap_url_parse(uri, &ldapInfo);
02751 if(NULL != ldapInfo && (0 == ldapInfo->lud_host && 0 != ldapInfo->lud_dn))
02752 {
02753 CPKIFNamePtr dn = GetPKIFNameFromStr((unsigned char*)ldapInfo->lud_dn);
02754 PKIFLDAP::ldap_free_urldesc( ldapInfo );
02755 ldapInfo = NULL;
02756
02757 CPKIFGeneralNamePtr gnp(new CPKIFGeneralName(dn));
02758 namesFromCert.push_back(gnp);
02759 }
02760 }
02761 else
02762 {
02763 namesFromCert.push_back(*dpNamesPos);
02764 }
02765 }
02766
02767 if(dpName->NameRelativeToIssuerPresent())
02768 {
02769 CPKIFNamePtr iss = cert->Issuer();
02770 CPKIFNamePtr relName = dpName->GetRelativeNameAsFullName(iss);
02771 CPKIFGeneralNamePtr gnp(new CPKIFGeneralName(relName));
02772 namesFromCert.push_back(gnp);
02773
02774 if(!crlIssuer.empty())
02775 {
02776 CPKIFGeneralNameList::iterator issuerPos;
02777 CPKIFGeneralNameList::iterator issuerEnd = crlIssuer.end();
02778 for(issuerPos = crlIssuer.begin(); issuerPos != issuerEnd; ++issuerPos)
02779 {
02780 if(CPKIFGeneralName::DIRECTORYNAME == (*issuerPos)->GetType())
02781 {
02782 CPKIFNamePtr dn = (*issuerPos)->directoryName();
02783 CPKIFNamePtr relName = dpName->GetRelativeNameAsFullName(dn);
02784 CPKIFGeneralNamePtr gnp(new CPKIFGeneralName(relName));
02785 namesFromCert.push_back(gnp);
02786 }
02787 }
02788 }
02789 }
02790 }
02791
02792
02793 CPKIFGeneralNameList::iterator issuerPos;
02794 CPKIFGeneralNameList::iterator issuerEnd = crlIssuer.end();
02795 for(issuerPos = crlIssuer.begin(); issuerPos != issuerEnd; ++issuerPos)
02796 {
02797 namesFromCert.push_back(*issuerPos);
02798 }
02799 }
02800 }
02801
02802
02803 static const unsigned char binVal[256] =
02804 {
02805 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02806 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02807 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02808 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02809 0xF0,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02810 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02811 0xF0,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02812 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02813 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02814 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02815 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02816 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02817 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02818 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02819 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,
02820 0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0
02821 };
02822
02823
02824 #define ACCEPTABLE(a) ( binVal[a] != 0xF0 )
02825
02826
02827
02828
02837 int CAC_API atob(
02839 char* byteval,
02841 char* ascval,
02843 unsigned int *alength)
02844 {
02845
02846
02847 if( NULL == ascval || NULL == byteval || NULL == alength)
02848 return -1;
02849
02850 size_t alen = strlen(ascval);
02851
02852 unsigned int outlen = 0;
02853
02854 if( 0 == alen )
02855 {
02856 return -2;
02857 }
02858
02859
02860 if( 0 != (alen % 2) )
02861 {
02862
02863 if( ACCEPTABLE( ascval[0] ) )
02864 {
02865 *byteval++ = binVal[(int)ascval[0]];
02866 ascval++;
02867 alen--;
02868 outlen++;
02869 }
02870 else
02871 {
02872 return -3;
02873 }
02874 }
02875
02876
02877 for(unsigned int XX = 0; XX < alen; XX += 2 )
02878 {
02879 if( ACCEPTABLE(ascval[XX]) && ACCEPTABLE(ascval[XX+1]) )
02880 {
02881 *byteval++ = ( binVal[ascval[XX]] << 4 ) + ( binVal[ascval[XX+1]] );
02882 outlen++;
02883 }
02884 else
02885 {
02886 return -4;
02887 }
02888 }
02889
02890 *alength = outlen;
02891 return 0;
02892 }
02893
02894 static const char *hex_list = "0123456789abcdef";
02903 void CAC_API btoa(
02905 const char* byteval,
02907 char* ascval,
02909 unsigned int blength)
02910 {
02911 if( 0 == blength || NULL == byteval || NULL == ascval )
02912 return;
02913
02914 for( unsigned int XX = 0; XX < blength; XX++ )
02915 {
02916 *ascval++ = hex_list[(((unsigned char)(byteval[XX])) >> 4)];
02917 *ascval++ = hex_list[(byteval[XX] & 15)];
02918 }
02919
02920 *ascval = '\0';
02921 }
02929 void FormatErrorMessage(
02931 std::string& result,
02933 const char* primaryMessage,
02935 unsigned int code,
02937 const char* file,
02939 int lineNum)
02940 {
02941 result = primaryMessage;
02942 result += ": ";
02943
02944 char asciiHexCode[25];
02945 sprintf(asciiHexCode, "0x%x", code);
02946 result += asciiHexCode;
02947 result += ". ";
02948
02949 if(NULL != file)
02950 {
02951 result += file;
02952 result += " line #";
02953 sprintf (asciiHexCode, "%u", lineNum);
02954 result += asciiHexCode;
02955
02956 }
02957 }
02958
02959
02960
02979 bool keyUsageTest(BYTE keyUse[2], bitset<9>* ku)
02980 {
02981 if(NULL == ku)
02982 return true;
02983
02984 bool matchesAtLeastOne = false;
02985
02986 if((*ku)[0] && (CERT_DIGITAL_SIGNATURE_KEY_USAGE == (keyUse[0] & CERT_DIGITAL_SIGNATURE_KEY_USAGE)))
02987 matchesAtLeastOne = true;
02988 if((*ku)[1] && (CERT_NON_REPUDIATION_KEY_USAGE == (keyUse[0] & CERT_NON_REPUDIATION_KEY_USAGE)))
02989 matchesAtLeastOne = true;
02990 if((*ku)[2] && (CERT_KEY_ENCIPHERMENT_KEY_USAGE == (keyUse[0] & CERT_KEY_ENCIPHERMENT_KEY_USAGE)))
02991 matchesAtLeastOne = true;
02992 if((*ku)[3] && (CERT_DATA_ENCIPHERMENT_KEY_USAGE == (keyUse[0] & CERT_DATA_ENCIPHERMENT_KEY_USAGE)))
02993 matchesAtLeastOne = true;
02994 if((*ku)[4] && (CERT_KEY_AGREEMENT_KEY_USAGE == (keyUse[0] & CERT_KEY_AGREEMENT_KEY_USAGE)))
02995 matchesAtLeastOne = true;
02996 if((*ku)[5] && (CERT_KEY_CERT_SIGN_KEY_USAGE == (keyUse[0] & CERT_KEY_CERT_SIGN_KEY_USAGE)))
02997 matchesAtLeastOne = true;
02998 if((*ku)[6] && (CERT_OFFLINE_CRL_SIGN_KEY_USAGE != (keyUse[0] & CERT_OFFLINE_CRL_SIGN_KEY_USAGE)))
02999 matchesAtLeastOne = true;
03000 if((*ku)[7] && (CERT_ENCIPHER_ONLY_KEY_USAGE == (keyUse[0] & CERT_ENCIPHER_ONLY_KEY_USAGE)))
03001 matchesAtLeastOne = true;
03002 if((*ku)[8] && (CERT_DECIPHER_ONLY_KEY_USAGE == (keyUse[1] & CERT_DECIPHER_ONLY_KEY_USAGE)))
03003 matchesAtLeastOne = true;
03004
03005 return matchesAtLeastOne;
03006 }
03007
03008
03016 void RemoveParentRelationships(
03018 std::vector<IPKIFColleaguePtr>& modules,
03020 IPKIFMediator* mediator)
03021 {
03022 vector<IPKIFColleaguePtr>::iterator pos;
03023 vector<IPKIFColleaguePtr>::iterator end = modules.end();
03024 for(pos = modules.begin(); pos != end; ++pos)
03025 {
03026 try
03027 {
03028 (*pos)->RemoveParent(mediator);
03029 }
03030 catch(...)
03031 {
03032
03033 }
03034 }
03035 }
03036
03037
03045 void FreeAdditionalModules(
03047 std::vector<IPKIFColleague*>& modules,
03049 IPKIFMediator* mediator)
03050 {
03051 vector<IPKIFColleague*>::iterator pos;
03052 vector<IPKIFColleague*>::iterator end = modules.end();
03053 for(pos = modules.begin(); pos != end; ++pos)
03054 {
03055 try
03056 {
03057 (*pos)->RemoveParent(mediator);
03058 delete (*pos);
03059 }
03060 catch(...)
03061 {
03062
03063 }
03064 }
03065
03066 modules.clear();
03067 }
03068
03069
03077 void FreeAdditionalModules2(
03079 std::vector<IPKIFColleague*>& modules,
03081 IPKIFMediator* mediator,
03083 std::vector<void (*)( void * )>& vDeleteFuncs)
03084 {
03085 vector<IPKIFColleague*>::iterator pos;
03086 vector<IPKIFColleague*>::iterator end = modules.end();
03087 std::vector<void (*)( void * )>::iterator dPos;
03088 std::vector<void (*)( void * )>::iterator dEnd = vDeleteFuncs.end();
03089 for(pos = modules.begin(), dPos = vDeleteFuncs.begin(); pos != end; ++pos, ++dPos)
03090 {
03091 try
03092 {
03093 (*pos)->RemoveParent(mediator);
03094 if(*dPos)
03095 (*dPos)(*pos);
03096 else
03097 delete *pos;
03098 }
03099 catch(...)
03100 {
03101
03102 }
03103 }
03104
03105 vDeleteFuncs.clear();
03106 modules.clear();
03107 }
03117 CAC_API char* GetErrorCodeString(
03119 int errorCode,
03122 char* buf)
03123 {
03124 switch(errorCode)
03125 {
03126 case 0:
03127 strcpy(buf, "Success");
03128 break;
03129
03130
03131 case ASN1_DECODE_ERROR:
03132 strcpy(buf, "ASN1_DECODE_ERROR");
03133 break;
03134 case ASN1_ENCODE_ERROR:
03135 strcpy(buf, "ASN1_ENCODE_ERROR");
03136 break;
03137 case ASN1_UNRECOGNIZED_TIME_FORMAT:
03138 strcpy(buf, "ASN1_UNRECOGNIZED_TIME_FORMAT");
03139 break;
03140 case ASN1_INVALID_PERIOD:
03141 strcpy(buf, "ASN1_INVALID_PERIOD");
03142 break;
03143 case ASN1_INVALID_TIME:
03144 strcpy(buf, "ASN1_INVALID_TIME");
03145 break;
03146
03147
03148 case CACHE_TRUST_STORE_OPEN_FAILED:
03149 strcpy(buf, "CACHE_TRUST_STORE_OPEN_FAILED");
03150 break;
03151 case CACHE_LDAP_CONNECT_AND_BIND_FAILED:
03152 strcpy(buf, "CACHE_LDAP_CONNECT_AND_BIND_FAILED");
03153 break;
03154 case CACHE_LDAP_ERROR:
03155 strcpy(buf, "CACHE_LDAP_ERROR");
03156 break;
03157 case CACHE_PARSE_ERROR:
03158 strcpy(buf, "CACHE_PARSE_ERROR");
03159 break;
03160 case CACHE_CERT_STORE_OPEN_FAILED:
03161 strcpy(buf, "CACHE_CERT_STORE_OPEN_FAILED");
03162 break;
03163 case CACHE_UPDATE_FAILED:
03164 strcpy(buf, "CACHE_UPDATE_FAILED");
03165 break;
03166
03167
03168 case CRYPTO_ALG_NOT_SUPPORTED:
03169 strcpy(buf, "CRYPTO_ALG_NOT_SUPPORTED");
03170 break;
03171 case CRYPTO_UNRECOGNIZED_CREDENTIAL:
03172 strcpy(buf, "CRYPTO_UNRECOGNIZED_CREDENTIAL");
03173 break;
03174 case CRYPTO_DEFAULT_KEY_TYPE_UNKNOWN:
03175 strcpy(buf, "CRYPTO_DEFAULT_KEY_TYPE_UNKNOWN");
03176 break;
03177 case CRYPTO_MODE_NOT_SUPPORTED:
03178 strcpy(buf, "CRYPTO_MODE_NOT_SUPPORTED");
03179 break;
03180 case CRYPTO_MISSING_IV:
03181 strcpy(buf, "CRYPTO_MISSING_IV");
03182 break;
03183 case CRYPTO_ENCRYPT_FAILED:
03184 strcpy(buf, "CRYPTO_ENCRYPT_FAILED");
03185 break;
03186 case CRYPTO_DECRYPT_FAILED:
03187 strcpy(buf, "CRYPTO_DECRYPT_FAILED");
03188 break;
03189 case CRYPTO_KEY_USAGE_NOT_COMPATIBLE:
03190 strcpy(buf, "CRYPTO_KEY_USAGE_NOT_COMPATIBLE");
03191 break;
03192 case CRYPTO_SIGN_FAILED:
03193 strcpy(buf, "CRYPTO_SIGN_FAILED");
03194 break;
03195 case CRYPTO_VERIFY_FAILED:
03196 strcpy(buf, "CRYPTO_VERIFY_FAILED");
03197 break;
03198
03199
03200 case PKIFCAPI_ACQUIRE_CONTEXT_FAILED:
03201 strcpy(buf, "PKIFCAPI_ACQUIRE_CONTEXT_FAILED");
03202 break;
03203 case PKIFCAPI_GEN_RANDOM_FAILED:
03204 strcpy(buf, "PKIFCAPI_GEN_RANDOM_FAILED");
03205 break;
03206 case PKIFCAPI_INCORRECT_HASH_CONTEXT:
03207 strcpy(buf, "PKIFCAPI_INCORRECT_HASH_CONTEXT");
03208 break;
03209 case PKIFCAPI_EMPTY_HASH_CONTEXT:
03210 strcpy(buf, "PKIFCAPI_EMPTY_HASH_CONTEXT");
03211 break;
03212 case PKIFCAPI_KEY_MATERIAL_NOT_SUPPORTED:
03213 strcpy(buf, "PKIFCAPI_KEY_MATERIAL_NOT_SUPPORTED");
03214 break;
03215 case PKIFCAPI_FAILED_TO_OPEN_CERT_STORE:
03216 strcpy(buf, "PKIFCAPI_FAILED_TO_OPEN_CERT_STORE");
03217 break;
03218 case PKIFCAPI_GET_NAME_FAILED:
03219 strcpy(buf, "PKIFCAPI_GET_NAME_FAILED");
03220 break;
03221 case PKIFCAPI_CREATE_HASH_FAILED:
03222 strcpy(buf, "PKIFCAPI_CREATE_HASH_FAILED");
03223 break;
03224 case PKIFCAPI_MISC_HASH_CALL_FAILED:
03225 strcpy(buf, "PKIFCAPI_MISC_HASH_CALL_FAILED");
03226 break;
03227 case PKIFCAPI_SIGN_FAILED:
03228 strcpy(buf, "PKIFCAPI_SIGN_FAILED");
03229 break;
03230 case PKIFCAPI_DECRYPT_FAILED:
03231 strcpy(buf, "PKIFCAPI_DECRYPT_FAILED");
03232 break;
03233 case PKIFCAPI_INVALID_HASH_STATE:
03234 strcpy(buf, "PKIFCAPI_INVALID_HASH_STATE");
03235 break;
03236 case PKIFCAPI_CREATE_CERT_FAILED:
03237 strcpy(buf, "PKIFCAPI_CREATE_CERT_FAILED");
03238 break;
03239 case PKIFCAPI_KEY_IMPORT_FAILED:
03240 strcpy(buf, "PKIFCAPI_KEY_IMPORT_FAILED");
03241 break;
03242 case PKIFCAPI_VERIFY_FAILED:
03243 strcpy(buf, "PKIFCAPI_VERIFY_FAILED");
03244 break;
03245 case PKIFCAPI_SESSION_KEY_ENCRYPT_FAILED:
03246 strcpy(buf, "PKIFCAPI_SESSION_KEY_ENCRYPT_FAILED");
03247 break;
03248 case PKIFCAPI_SET_MODE_FAILED:
03249 strcpy(buf, "PKIFCAPI_SET_MODE_FAILED");
03250 break;
03251 case PKIFCAPI_SET_IV_FAILED:
03252 strcpy(buf, "PKIFCAPI_SET_IV_FAILED");
03253 break;
03254
03255
03256 case PKIFCAPI_GET_USER_KEY_FAILED:
03257 strcpy(buf, "PKIFCAPI_GET_USER_KEY_FAILED");
03258 break;
03259 case PKIFCAPI_SET_PASSWORD_FAILED:
03260 strcpy(buf, "PKIFCAPI_SET_PASSWORD_FAILED");
03261 break;
03262 case PKIFCAPI_KEY_PROV_INFO_FAILED:
03263 strcpy(buf, "PKIFCAPI_KEY_PROV_INFO_FAILED");
03264 break;
03265 case PKIFCAPI_NO_KEY_ASSOCIATED:
03266 strcpy(buf, "PKIFCAPI_NO_KEY_ASSOCIATED");
03267 break;
03268
03269 #if BUILD_CMS_STUFF // moving CMS into separate DLL...
03270
03271 case MSG_DECODE_FAILED:
03272 strcpy(buf, "MSG_DECODE_FAILED");
03273 break;
03274 case MSG_INCOMPLETE:
03275 strcpy(buf, "MSG_INCOMPLETE");
03276 break;
03277 case MSG_ENCODE_FAILED:
03278 strcpy(buf, "MSG_ENCODE_FAILED");
03279 break;
03280 case MSG_SYMKEY_DECRYPT_FAILED:
03281 strcpy(buf, "MSG_SYMKEY_DECRYPT_FAILED");
03282 break;
03283 case MSG_MISSING_PARAMS:
03284 strcpy(buf, "MSG_MISSING_PARAMS");
03285 break;
03286 case MSG_INVALID_INDEX:
03287 strcpy(buf, "MSG_INVALID_INDEX");
03288 break;
03289 case MSG_INVALID_CREDENTIAL:
03290 strcpy(buf, "MSG_INVALID_CREDENTIAL");
03291 break;
03292 case MSG_INVALID_RECIP:
03293 strcpy(buf, "MSG_INVALID_RECIP");
03294 break;
03295 case MSG_SIGNATURE_GENERATION_ERROR:
03296 strcpy(buf, "MSG_SIGNATURE_GENERATION_ERROR");
03297 break;
03298 case MSG_INVALID_STATE:
03299 strcpy(buf, "MSG_INVALID_STATE");
03300 break;
03301 case MSG_NO_CERTIFICATE:
03302 strcpy(buf, "MSG_NO_CERTIFICATE");
03303 break;
03304 #endif //CMS_MESSAGE_STUFF
03305
03306
03307 case OCSP_MISSING_REQUIRED_FIELD:
03308 strcpy(buf, "OCSP_MISSING_REQUIRED_FIELD");
03309 break;
03310 case OCSP_UNRECOGNIZED_CHOICE:
03311 strcpy(buf, "OCSP_UNRECOGNIZED_CHOICE");
03312 break;
03313 case OCSP_FAILED_TO_OBTAIN_RESPONSE:
03314 strcpy(buf, "OCSP_FAILED_TO_OBTAIN_RESPONSE");
03315 break;
03316 case OCSP_RECEIVED_NON_SUCCESSFUL_RESPONSE:
03317 strcpy(buf, "OCSP_RECEIVED_NON_SUCCESSFUL_RESPONSE");
03318 break;
03319 case OCSP_NO_RESPONSE_BODY:
03320 strcpy(buf, "OCSP_NO_RESPONSE_BODY");
03321 break;
03322 case OCSP_UNSUPPORTED_RESPONSE_TYPE:
03323 strcpy(buf, "OCSP_UNSUPPORTED_RESPONSE_TYPE");
03324 break;
03325 case OCSP_RESPONSE_SIG_VERIFICATION_FAILED:
03326 strcpy(buf, "OCSP_RESPONSE_SIG_VERIFICATION_FAILED");
03327 break;
03328 case OCSP_SIGNER_MISMATCH:
03329 strcpy(buf, "OCSP_SIGNER_MISMATCH");
03330 break;
03331 case OCSP_RESPONDER_IS_TARGET:
03332 strcpy(buf, "OCSP_RESPONDER_IS_TARGET");
03333 break;
03334 case OCSP_RESPONDER_NOT_AUTHORIZED:
03335 strcpy(buf, "OCSP_RESPONDER_NOT_AUTHORIZED");
03336 break;
03337 case OCSP_TIME_CHECK_FAILED:
03338 strcpy(buf, "OCSP_TIME_CHECK_FAILED");
03339 break;
03340 case OCSP_UNKNOWN_CERT_STATUS:
03341 strcpy(buf, "OCSP_UNKNOWN_CERT_STATUS");
03342 break;
03343 case OCSP_PATH_BUILDING_FAILED:
03344 strcpy(buf, "OCSP_PATH_BUILDING_FAILED");
03345 break;
03346 case OCSP_PATH_VALIDATION_FAILED:
03347 strcpy(buf, "OCSP_PATH_VALIDATION_FAILED");
03348 break;
03349 case OCSP_AIA_URL_PARSE_FAILED:
03350 strcpy(buf, "OCSP_AIA_URL_PARSE_FAILED");
03351 break;
03352 case OCSP_NONCE_MISMATCH:
03353 strcpy(buf, "OCSP_NONCE_MISMATCH");
03354 break;
03355 case OCSP_NONCE_MISSING:
03356 strcpy(buf, "OCSP_NONCE_MISSING");
03357 break;
03358 case OCSP_PATH_STATUS_NOT_DETERMINED:
03359 strcpy(buf, "OCSP_PATH_STATUS_NOT_DETERMINED");
03360 break;
03361 case OCSP_NEXT_UPDATE_PASSED:
03362 strcpy(buf, "OCSP_NEXT_UPDATE_PASSED");
03363 break;
03364 case OCSP_STATUS_NOT_SUFFICIENTLY_RECENT:
03365 strcpy(buf, "OCSP_STATUS_NOT_SUFFICIENTLY_RECENT");
03366 break;
03367 case OCSP_UNPROCESSED_CRITICAL_EXTENSION:
03368 strcpy(buf, "OCSP_UNPROCESSED_CRITICAL_EXTENSION");
03369 break;
03370 case OCSP_TARGET_RESPONSE_NOT_INCLUDED:
03371 strcpy(buf, "OCSP_TARGET_RESPONSE_NOT_INCLUDED");
03372 break;
03373
03374
03375 case PATH_LENGTH_VIOLATION:
03376 strcpy(buf, "PATH_LENGTH_VIOLATION");
03377 break;
03378 case PATH_NAME_CHAINING_VIOLATION:
03379 strcpy(buf, "PATH_NAME_CHAINING_VIOLATION");
03380 break;
03381 case PATH_BASIC_CONSTRAINTS_VIOLATION:
03382 strcpy(buf, "PATH_BASIC_CONSTRAINTS_VIOLATION");
03383 break;
03384 case PATH_VALIDITY_PERIOD_VIOLATION_NOT_YET_VALID:
03385 strcpy(buf, "PATH_VALIDITY_PERIOD_VIOLATION");
03386 break;
03387 case PATH_VALIDITY_PERIOD_VIOLATION_EXPIRED:
03388 strcpy(buf, "PATH_VALIDITY_PERIOD_VIOLATION");
03389 break;
03390 case PATH_KEY_USAGE_VIOLATION:
03391 strcpy(buf, "PATH_KEY_USAGE_VIOLATION");
03392 break;
03393 case PATH_SIGNATURE_VERIFICATION_FAILED:
03394 strcpy(buf, "PATH_SIGNATURE_VERIFICATION_FAILED");
03395 break;
03396 case PATH_CERT_REVOKED:
03397 strcpy(buf, "PATH_CERT_REVOKED");
03398 break;
03399 case PATH_UNPROCESSED_CRITICAL_EXTENSION:
03400 strcpy(buf, "PATH_UNPROCESSED_CRITICAL_EXTENSION");
03401 break;
03402 case PATH_TRUST_ROOT_NOT_SET:
03403 strcpy(buf, "PATH_TRUST_ROOT_NOT_SET");
03404 break;
03405 case PATH_TRUST_ROOT_NO_CERT:
03406 strcpy(buf, "PATH_TRUST_ROOT_NO_CERT");
03407 break;
03408 case PATH_NAME_CONSTRAINTS_VIOLATION:
03409 strcpy(buf, "PATH_NAME_CONSTRAINTS_VIOLATION");
03410 break;
03411 case PATH_APP_DEFINED_CHECK_FAILED:
03412 strcpy(buf, "PATH_APP_DEFINED_CHECK_FAILED");
03413 break;
03414 case PATH_ALG_CHAINING_FAILURE:
03415 strcpy(buf, "PATH_ALG_CHAINING_FAILURE");
03416 break;
03417 case PATH_NULL_USER_POLICY_SET:
03418 strcpy(buf, "PATH_NULL_USER_POLICY_SET");
03419 break;
03420 case PATH_CERT_REVOCATION_STATUS_NOT_DETERMINED:
03421 strcpy(buf, "PATH_CERT_REVOCATION_STATUS_NOT_DETERMINED");
03422 break;
03423 case PATH_NULL_AUTH_POLICY_SET:
03424 strcpy(buf, "PATH_NULL_AUTH_POLICY_SET");
03425 break;
03426
03427 #if BUILD_TAPTSP_STUFF // moving TAP/TSP into separate DLL
03428
03429 case TAP_MISSING_REQUIRED_FIELD:
03430 strcpy(buf, "TAP_MISSING_REQUIRED_FIELD");
03431 break;
03432 case TAP_UNRECOGNIZED_CHOICE:
03433 strcpy(buf, "TAP_UNRECOGNIZED_CHOICE");
03434 break;
03435
03436
03437 case TSP_MISSING_REQUIRED_FIELD:
03438 strcpy(buf, "TSP_MISSING_REQUIRED_FIELD");
03439 break;
03440 case TSP_MISSING_REQUIRED_EKU:
03441 strcpy(buf, "TSP_MISSING_REQUIRED_EKU");
03442 break;
03443 case TSP_HASH_MISMATCH:
03444 strcpy(buf, "TSP_HASH_MISMATCH");
03445 break;
03446 case TSP_NOT_WITHIN_SKEW:
03447 strcpy(buf, "TSP_NOT_WITHIN_SKEW");
03448 break;
03449 case TSP_NOT_WITHIN_VALIDITY_PERIOD:
03450 strcpy(buf, "TSP_NOT_WITHIN_VALIDITY_PERIOD");
03451 break;
03452 case TSP_REVOCATION_DATE_PRECEDES_TIME:
03453 strcpy(buf, "TSP_REVOCATION_DATE_PRECEDES_TIME");
03454 break;
03455 case TSP_VERIFICATION_FAILED:
03456 strcpy(buf, "TSP_VERIFICATION_FAILED");
03457 break;
03458 #endif //BUILD_TAPTSP_STUFF
03459
03460
03461 case COMMON_NOT_IMPLEMENTED:
03462 strcpy(buf, "COMMON_NOT_IMPLEMENTED");
03463 break;
03464 case COMMON_ALREADY_INITIALIZED:
03465 strcpy(buf, "COMMON_ALREADY_INITIALIZED");
03466 break;
03467 case COMMON_NOT_INITIALIZED:
03468 strcpy(buf, "COMMON_NOT_INITIALIZED");
03469 break;
03470 case COMMON_OPERATION_NOT_HANDLED:
03471 strcpy(buf, "COMMON_OPERATION_NOT_HANDLED");
03472 break;
03473 case COMMON_INVALID_INPUT:
03474 strcpy(buf, "COMMON_INVALID_INPUT");
03475 break;
03476 case COMMON_MEMORY_ALLOC_FAILURE:
03477 strcpy(buf, "COMMON_MEMORY_ALLOC_FAILURE");
03478 break;
03479 case COMMON_UNKNOWN_ERROR:
03480 strcpy(buf, "COMMON_UNKNOWN_ERROR");
03481 break;
03482 case COMMON_MEDIATOR_MISSING:
03483 strcpy(buf, "COMMON_MEDIATOR_MISSING");
03484 break;
03485 case COMMON_UNSUPPORTED_ALG:
03486 strcpy(buf, "COMMON_UNSUPPORTED_ALG");
03487 break;
03488 case COMMON_TERMINATION_ERROR:
03489 strcpy(buf, "COMMON_TERMINATION_ERROR");
03490 break;
03491 case COMMON_OPERATION_NOT_SUCCESSFUL:
03492 strcpy(buf, "COMMON_OPERATION_NOT_SUCCESSFUL");
03493 break;
03494 case COMMON_INITIALIZATION_FAILED:
03495 strcpy(buf, "COMMON_INITIALIZATION_FAILED");
03496 break;
03497 case COMMON_URL_TOO_BIG:
03498 strcpy(buf, "COMMON_URL_TOO_BIG");
03499 break;
03500 case COMMON_URL_OPERATION_FAILED:
03501 strcpy(buf, "COMMON_URL_OPERATION_FAILED");
03502 break;
03503 case COMMON_UNSUPPORTED_VERSION:
03504 strcpy(buf, "COMMON_UNSUPPORTED_VERSION");
03505 break;
03506 case COMMON_UNSUPPORTED_CHOICE:
03507 strcpy(buf, "COMMON_UNSUPPORTED_CHOICE");
03508 break;
03509
03510 default:
03511 strcpy(buf, "Unrecognized error code");
03512 break;
03513 }
03514
03515 return buf;
03516 }
03517
03520
03521
03522
03523
03524
03525
03526
03527
03528
03529
03530
03531
03532
03533
03534
03535
03536
03537 #ifdef _NO_GETTICKCOUNT
03538
03546 unsigned long GetTickCount(void)
03547 {
03548 unsigned long rv;
03549 struct timeval now;
03550 gettimeofday(&now,NULL);
03551 rv = now.tv_sec * 1000;
03552 rv += now.tv_usec / 1000;
03553 return rv;
03554 }
03555 #endif //_NO_GETTICKCOUNT
03556
03557
03558
03559
03560
03561
03562
03563
03564
03565
03566
03567
03568
03569
03570
03571
03572
03573
03574
03575
03585 void CAC_API keyUsageChecker_AnyEE(
03587 const CPKIFCertificateNodeEntryPtr& certNode,
03589 CPKIFPathValidationResults& results,
03591 CertificateType type)
03592 {
03593 if(EE == type)
03594 {
03595 CPKIFCertificatePtr curCert = certNode->GetCert();
03596 CPKIFKeyUsagePtr keyUsage = curCert->GetExtension<CPKIFKeyUsage>();
03597 if(keyUsage != (CPKIFKeyUsage*)NULL)
03598 {
03599 CPKIFX509ExtensionPtr keyUsage2 = keyUsage;
03600 certNode->MarkExtensionAsProcessed(keyUsage2);
03601 }
03602 }
03603 }
03604
03605
03615 void CAC_API EKUChecker_Timestamp(
03617 const CPKIFCertificateNodeEntryPtr& certNode,
03619 CPKIFPathValidationResults& results,
03621 CertificateType type)
03622 {
03623 LOG_STRING_DEBUG("EKUChecker_Timestamp", TOOLKIT_TSP_TIMESTAMP_VERIFIER, 0, NULL);
03624
03625 if(EE == type)
03626 {
03627 CPKIFCertificatePtr curCert = certNode->GetCert();
03628
03629
03630
03631
03632
03633 CPKIFExtendedKeyUsagePtr eku = curCert->GetExtension<CPKIFExtendedKeyUsage>();
03634 if(eku != (CPKIFExtendedKeyUsage*)NULL)
03635 {
03636 vector<CPKIFOIDPtr> keyPurposeIDs;
03637 eku->KeyPurposeIDs(keyPurposeIDs);
03638
03639 GottaMatch<CPKIFOIDPtr> gm;
03640 gm.SetRHS(g_timestampingEKU);
03641
03642 vector<CPKIFOIDPtr>::iterator end = keyPurposeIDs.end();
03643 if(end != find_if(keyPurposeIDs.begin(), keyPurposeIDs.end(), gm))
03644 {
03645 CPKIFX509ExtensionPtr eku2 = eku;
03646 certNode->MarkExtensionAsProcessed(eku2);
03647 }
03648 else
03649 {
03650 gm.SetRHS(g_anyEKU);
03651 if(end != find_if(keyPurposeIDs.begin(), keyPurposeIDs.end(), gm))
03652 {
03653 CPKIFX509ExtensionPtr eku2 = eku;
03654 certNode->MarkExtensionAsProcessed(eku2);
03655 }
03656 }
03657 }
03658 }
03659 }
03660
03661
03671 void CAC_API EKUChecker_OcspSigning(
03673 const CPKIFCertificateNodeEntryPtr& certNode,
03675 CPKIFPathValidationResults& results,
03677 CertificateType type)
03678 {
03679 LOG_STRING_DEBUG("EKUChecker_Timestamp", TOOLKIT_TSP_TIMESTAMP_VERIFIER, 0, NULL);
03680
03681 if(EE == type)
03682 {
03683 CPKIFCertificatePtr curCert = certNode->GetCert();
03684
03685
03686
03687
03688
03689 CPKIFExtendedKeyUsagePtr eku = curCert->GetExtension<CPKIFExtendedKeyUsage>();
03690 if(eku != (CPKIFExtendedKeyUsage*)NULL)
03691 {
03692 vector<CPKIFOIDPtr> keyPurposeIDs;
03693 eku->KeyPurposeIDs(keyPurposeIDs);
03694
03695 GottaMatch<CPKIFOIDPtr> gm;
03696 gm.SetRHS(g_ocspSigningEKU);
03697
03698 vector<CPKIFOIDPtr>::iterator end = keyPurposeIDs.end();
03699 if(end != find_if(keyPurposeIDs.begin(), keyPurposeIDs.end(), gm))
03700 {
03701 CPKIFX509ExtensionPtr eku2 = eku;
03702 certNode->MarkExtensionAsProcessed(eku2);
03703 }
03704 else
03705 {
03706 gm.SetRHS(g_anyEKU);
03707 if(end != find_if(keyPurposeIDs.begin(), keyPurposeIDs.end(), gm))
03708 {
03709 CPKIFX509ExtensionPtr eku2 = eku;
03710 certNode->MarkExtensionAsProcessed(eku2);
03711 }
03712 }
03713 }
03714 }
03715 }
03716
03724 std::string DecodeIPAndMask(
03726 const CPKIFBufferPtr& encodedIP)
03727 {
03728 string rv = "";
03729 if(encodedIP->GetLength() == 32)
03730 {
03731 ip::address_v6::bytes_type bytesAddr;
03732 memcpy(bytesAddr.c_array(), encodedIP->GetBuffer(),encodedIP->GetLength()/2);
03733 ip::address_v6 addr(bytesAddr);
03734 rv = addr.to_string();
03735
03736 rv+="/";
03737
03738 ip::address_v6::bytes_type bytesMask;
03739 memcpy(bytesMask.c_array(), encodedIP->GetBuffer()+encodedIP->GetLength()/2,encodedIP->GetLength()/2);
03740 ip::address_v6 mask(bytesMask);
03741 string sMask = mask.to_string();
03742
03743 rv +=sMask;
03744
03745 }
03746 else if(encodedIP->GetLength() == 8)
03747 {
03748 ip::address_v4::bytes_type bytesAddr;
03749 memcpy(bytesAddr.c_array(), encodedIP->GetBuffer(),encodedIP->GetLength()/2);
03750 ip::address_v4 addr(bytesAddr);
03751 rv = addr.to_string();
03752
03753 rv+="/";
03754
03755 ip::address_v4::bytes_type bytesMask;
03756 memcpy(bytesMask.c_array(), encodedIP->GetBuffer()+encodedIP->GetLength()/2,encodedIP->GetLength()/2);
03757 ip::address_v4 mask(bytesMask);
03758 string sMask = mask.to_string();
03759
03760 rv +=sMask;
03761 }
03762 return rv;
03763 }