00001
00009 #ifndef __TOOLKITUTILS_H__
00010 #define __TOOLKITUTILS_H__
00011
00012 #include "PKIFdll.h"
00013 #include "PKIFCryptoConstants.h"
00014 #include "PKIFEnums.h"
00015
00016 #include <assert.h>
00017 #include <string>
00018 #include <iosfwd>
00019 #include <bitset>
00020 #include <iomanip>
00021
00022
00023 class CPKIFOCSPChecker;
00024 class IPKIFCertRepository;
00025 class IPKIFCertRepositoryUpdate;
00026 class IPKIFCertSearch;
00027 class IPKIFCRLRepository;
00028 class IPKIFCRLRepositoryUpdate;
00029 class IPKIFTrustCache;
00030 class IPKIFTrustCacheUpdate;
00031 class IPKIFCryptoKeyManagement;
00032 class IPKIFCryptoKeyIDOperations;
00033 class IPKIFCryptoMisc;
00034 class IPKIFCryptoRawOperations;
00035 class IPKIFCryptoAlgSupport;
00036 class IPKIFDefaultPathManagement;
00037 class IPKIFPathBuild;
00038 class IPKIFPathValidate;
00039 class IPKIFRevocationStatus;
00040 class CPKIFCertificatePath;
00041
00042 FD_SMART_PTR(CPKIFCertificate);
00043 FD_SMART_PTR(CPKIFBuffer);
00044 FD_SMART_PTR(CPKIFOID);
00045 FD_SMART_PTR(CPKIFPolicyInformation);
00046 FD_SMART_PTR(CPKIFAlgorithmIdentifier);
00047 FD_SMART_PTR(CPKIFName);
00048 FD_SMART_PTR(CPKIFCertificateNodeEntry);
00049 FD_SMART_PTR(CPKIFPathValidationResults);
00050 FD_LIST_PTR(CPKIFGeneralName);
00051
00052 FD_MC_PTR();
00053 FD_SMART_PTR(CPKIFOCSPChecker);
00054 FD_LIST_PTR(CPKIFCRL);
00055
00056
00057
00058
00059 FD_NODE_LIST_PTR(CPKIFCertificateNodeEntry);
00060
00061
00062 CAC_API IPKIFMediatorPtr MakeDefaultMediator(bool isService = false);
00063 CAC_API IPKIFMediatorPtr MakeDefaultMediator(bool isService, CPKIFOCSPCheckerPtr& trustedResponder);
00064 CAC_API IPKIFMediatorPtr MakeDefaultMediator(bool isService, CPKIFOCSPCheckerPtr& trustedResponder, CPKIFOCSPCheckerPtr& aiaColleague);
00065 void CAC_API FreeDefaultMediator(IPKIFMediator* p);
00066
00067 CAC_API IPKIFMediatorPtr MakeDefaultMediatorNSS(const char *db);
00068 CAC_API IPKIFMediatorPtr MakeDefaultMediatorNSS(const char *db, CPKIFOCSPCheckerPtr& trustedResponder);
00069 CAC_API IPKIFMediatorPtr MakeDefaultMediatorNSS(const char *db, CPKIFOCSPCheckerPtr& trustedResponder, CPKIFOCSPCheckerPtr& aiaColleague);
00070 void CAC_API FreeDefaultMediatorNSS(IPKIFMediator* p);
00071
00072
00073 CAC_API IPKIFCertRepository* Get_IPKIFCertRepository(IPKIFMediatorPtr& m);
00074 CAC_API IPKIFCertRepositoryUpdate* Get_IPKIFCertRepositoryUpdate(IPKIFMediatorPtr& m);
00075 CAC_API IPKIFCertSearch* Get_IPKIFCertSearch(IPKIFMediatorPtr& m);
00076 CAC_API IPKIFCRLRepository* Get_IPKIFCRLRepository(IPKIFMediatorPtr& m);
00077 CAC_API IPKIFCRLRepositoryUpdate* Get_IPKIFCRLRepositoryUpdate(IPKIFMediatorPtr& m);
00078 CAC_API IPKIFTrustCache* Get_IPKIFTrustCache(IPKIFMediatorPtr& m);
00079 CAC_API IPKIFTrustCacheUpdate* Get_IPKIFTrustCacheUpdate(IPKIFMediatorPtr& m);
00080
00081
00082 CAC_API IPKIFCryptoKeyManagement* Get_IPKIFCryptoKeyManagement(IPKIFMediatorPtr& m);
00083 CAC_API IPKIFCryptoKeyIDOperations* Get_IPKIFCryptoKeyIDOperations(IPKIFMediatorPtr& m);
00084 CAC_API IPKIFCryptoMisc* Get_IPKIFCryptoMisc(IPKIFMediatorPtr& m);
00085 CAC_API IPKIFCryptoRawOperations* Get_IPKIFCryptoRawOperations(IPKIFMediatorPtr& m);
00086 CAC_API IPKIFCryptoAlgSupport* Get_IPKIFCryptoAlgSupport(IPKIFMediatorPtr& m);
00087
00088
00089
00090 CAC_API IPKIFDefaultPathManagement* Get_IPKIFDefaultPathManagement(IPKIFMediatorPtr& m);
00091
00092
00093
00094
00095
00096
00097 CAC_API void AddCacheColleague(IPKIFMediatorPtr& m, IPKIFColleaguePtr& ldap);
00098
00099
00100
00101 #ifndef DISABLE_LOGGING_MACROS
00102 #define LOG_STRING_DEBUG(errorMsg, subComponentID, errorCode, objectPointer)\
00103 LogString(errorMsg, subComponentID, errorCode, objectPointer, nsCLog::debug, __FILE__, __LINE__);
00104 #define LOG_STRING_INFO(errorMsg, subComponentID, errorCode, objectPointer)\
00105 LogString(errorMsg, subComponentID, errorCode, objectPointer, nsCLog::info, __FILE__, __LINE__);
00106 #define LOG_STRING_WARN(errorMsg, subComponentID, errorCode, objectPointer)\
00107 LogString(errorMsg, subComponentID, errorCode, objectPointer, nsCLog::warning, __FILE__, __LINE__);
00108 #define LOG_STRING_ERROR(errorMsg, subComponentID, errorCode, objectPointer)\
00109 LogString(errorMsg, subComponentID, errorCode, objectPointer, nsCLog::error, __FILE__, __LINE__);
00110 #define LOG_STRING_FATAL(errorMsg, subComponentID, errorCode, objectPointer)\
00111 LogString(errorMsg, subComponentID, errorCode, objectPointer, nsCLog::fatal, __FILE__, __LINE__);
00112 #else
00113 #define LOG_STRING_DEBUG(errorMsg, subComponentID, errorCode, objectPointer)
00114 #define LOG_STRING_INFO(errorMsg, subComponentID, errorCode, objectPointer)
00115 #define LOG_STRING_WARN(errorMsg, subComponentID, errorCode, objectPointer)
00116 #define LOG_STRING_ERROR(errorMsg, subComponentID, errorCode, objectPointer)
00117 #define LOG_STRING_FATAL(errorMsg, subComponentID, errorCode, objectPointer)
00118 #endif
00119
00120 #define RAISE_CACHE_EXCEPTION(__errorMsg__, __component__, __errorCode__, __object__)\
00121 {\
00122 LOG_STRING_ERROR(__errorMsg__, __component__, __errorCode__, __object__);\
00123 throw CPKIFCacheException(__component__, __errorCode__, __errorMsg__);\
00124 }
00125
00126 #define RAISE_CRYPTO_EXCEPTION(__errorMsg__, __component__, __errorCode__, __object__)\
00127 {\
00128 LOG_STRING_ERROR(__errorMsg__, __component__, __errorCode__, __object__);\
00129 throw CPKIFCryptoException(__component__, __errorCode__, __errorMsg__);\
00130 }
00131
00132 #define RAISE_PATH_EXCEPTION(__errorMsg__, __component__, __errorCode__, __object__)\
00133 {\
00134 LOG_STRING_ERROR(__errorMsg__, __component__, __errorCode__, __object__);\
00135 throw CPKIFPathException(__component__, __errorCode__, __errorMsg__);\
00136 }
00137
00138 #define RAISE_PKIF_EXCEPTION(__errorMsg__, __component__, __errorCode__, __object__)\
00139 {\
00140 LOG_STRING_ERROR(__errorMsg__, __component__, __errorCode__, __object__);\
00141 throw CPKIFException(__component__, __errorCode__, __errorMsg__);\
00142 }
00143
00144
00145
00146 #define NEW_NODE(cur) \
00147 { \
00148 cur = new DListNode;\
00149 cur->next = NULL;\
00150 cur->prev = NULL; \
00151 }
00152
00153 #define NEW_NEXT_AND_ADVANCE(cur) \
00154 { \
00155 cur->next = new DListNode; \
00156 cur->next->prev = cur; \
00157 cur->next->next = NULL; \
00158 cur = cur->next; \
00159 }
00160
00161 #define SET_HEAD_TAIL_INCREMENT(list, cur) \
00162 { \
00163 if(0 == list.count)\
00164 {\
00165 list.head = cur;\
00166 list.tail = cur;\
00167 }\
00168 else\
00169 list.tail = cur;\
00170 ++list.count;\
00171 }
00172
00173
00174
00175 void CAC_API GetCertsFromIssuerAltName(CPKIFCertificate& cert, CPKIFCertificateNodeList& certList, PKIInfoSource source = REMOTE, PathBuildingDirection pbd = PBD_FORWARD);
00177 enum PKIFServiceType {PKIF_TSA, PKIF_OCSP, PKIF_SCVP, PKIF_SCVP_VAL_POL, PKIF_NONE};
00178 BOOL CAC_API PostRequestURL(CPKIFBufferPtr& encTSR, CPKIFBufferPtr& resp,
00179 const char* pstrURL, PKIFServiceType pst);
00180
00181 bool CAC_API RetrieveCertGivenHTTPURL(
00183 const char* url,
00185 CPKIFCertificateNodeList& certNodeList);
00186 int CAC_API GetCRLfromLDAPURL(const char *url_in, CPKIFCRLList& crlList);
00187 int CAC_API GetCertfromLDAPURL(const char *url, CPKIFCertificateNodeList& certList, PathBuildingDirection pbd = PBD_FORWARD);
00188
00189
00190 BOOL CAC_API PostRequest(CPKIFBufferPtr& encTSR, CPKIFBufferPtr& resp,
00191 const char* pstrServer, int port, const char* pstrObject, PKIFServiceType pst);
00192
00193
00194 BOOL CAC_API GetRequest(const char* pstrURL, CPKIFBufferPtr& resp);
00195
00196
00197
00198 #define MAX_ERROR_CODE_STRING_SIZE 255
00199 CAC_API char* GetErrorCodeString(int errorCode, char* buf);
00200
00201
00202
00203
00204
00205
00206 extern CAC_API CPKIFOIDPtr g_md2WithRSAEncryption;
00207 extern CAC_API CPKIFOIDPtr g_md5WithRSAEncryption;
00208 extern CAC_API CPKIFOIDPtr g_sha1WithRSAEncryption;
00209 extern CAC_API CPKIFOIDPtr g_sha224WithRSAEncryption;
00210 extern CAC_API CPKIFOIDPtr g_sha256WithRSAEncryption;
00211 extern CAC_API CPKIFOIDPtr g_sha384WithRSAEncryption;
00212 extern CAC_API CPKIFOIDPtr g_sha512WithRSAEncryption;
00213 extern CAC_API CPKIFOIDPtr g_dsaWithSHA1;
00214 extern CAC_API CPKIFOIDPtr g_dsaWithSHA1Alternative;
00215 extern CAC_API CPKIFOIDPtr g_dsa;
00216 extern CAC_API CPKIFOIDPtr g_rsa;
00217 extern CAC_API CPKIFOIDPtr g_ecc;
00218 extern CAC_API CPKIFOIDPtr g_ecdsa_sha1;
00219 extern CAC_API CPKIFOIDPtr g_ecdsa_rec;
00220 extern CAC_API CPKIFOIDPtr g_ecdsa_params;
00221 extern CAC_API CPKIFOIDPtr g_ecdsa_sha224;
00222 extern CAC_API CPKIFOIDPtr g_ecdsa_sha256;
00223 extern CAC_API CPKIFOIDPtr g_ecdsa_sha384;
00224 extern CAC_API CPKIFOIDPtr g_ecdsa_sha512;
00225
00226
00227 extern CAC_API CPKIFOIDPtr g_ecdh_std_sha1kdf;
00228 extern CAC_API CPKIFOIDPtr g_ecdh_cofactor_sha1kdf;
00229 extern CAC_API CPKIFOIDPtr g_ecmqv_sha1kdf;
00230 extern CAC_API CPKIFOIDPtr g_ecdh_std_sha256kdf;
00231 extern CAC_API CPKIFOIDPtr g_ecdh_std_sha384kdf;
00232
00233 extern CAC_API CPKIFOIDPtr g_sha1;
00234 extern CAC_API CPKIFOIDPtr g_sha224;
00235 extern CAC_API CPKIFOIDPtr g_sha256;
00236 extern CAC_API CPKIFOIDPtr g_sha384;
00237 extern CAC_API CPKIFOIDPtr g_sha512;
00238 extern CAC_API CPKIFOIDPtr g_md5;
00239
00240 extern CAC_API CPKIFOIDPtr g_desCBC;
00241 extern CAC_API CPKIFOIDPtr g_tdesCBC;
00242 extern CAC_API CPKIFOIDPtr g_tdesECB;
00243 extern CAC_API CPKIFOIDPtr g_desECB;
00244
00245 extern CAC_API CPKIFOIDPtr g_aes128ECB;
00246 extern CAC_API CPKIFOIDPtr g_aes128CBC;
00247 extern CAC_API CPKIFOIDPtr g_aes192ECB;
00248 extern CAC_API CPKIFOIDPtr g_aes192CBC;
00249 extern CAC_API CPKIFOIDPtr g_aes256ECB;
00250 extern CAC_API CPKIFOIDPtr g_aes256CBC;
00251
00252 extern CAC_API CPKIFOIDPtr g_aes128Wrap;
00253 extern CAC_API CPKIFOIDPtr g_aes192Wrap;
00254 extern CAC_API CPKIFOIDPtr g_aes256Wrap;
00255
00256
00257 extern CAC_API CPKIFOIDPtr g_contentInfo;
00258 extern CAC_API CPKIFOIDPtr g_data;
00259 extern CAC_API CPKIFOIDPtr g_signedData;
00260 extern CAC_API CPKIFOIDPtr g_envelopedData;
00261 extern CAC_API CPKIFOIDPtr g_encryptedData;
00262
00263 extern CAC_API CPKIFOIDPtr g_contentCollection;
00264 extern CAC_API CPKIFOIDPtr g_contentWithAttributes;
00265 extern CAC_API CPKIFOIDPtr g_compressedData;
00266 extern CAC_API CPKIFOIDPtr g_authenticatedData;
00267 extern CAC_API CPKIFOIDPtr g_digestedData;
00268 extern CAC_API CPKIFOIDPtr g_authEnvelopedData;
00269
00270
00271 extern CAC_API CPKIFOIDPtr g_contentTypeAttribute;
00272 extern CAC_API CPKIFOIDPtr g_messageDigestAttribute;
00273 extern CAC_API CPKIFOIDPtr g_signingTimeAttribute;
00274 extern CAC_API CPKIFOIDPtr g_countersignatureAttribute;
00275 extern CAC_API CPKIFOIDPtr g_timestampAttribute;
00276 extern CAC_API CPKIFOIDPtr g_archiveTokenAttribute;
00277 extern CAC_API CPKIFOIDPtr g_binarySigningTimeAttribute;
00278 extern CAC_API CPKIFOIDPtr g_signingCertificateAttribute;
00279
00280
00281 extern CAC_API CPKIFOIDPtr g_timestampingEKU;
00282 extern CAC_API CPKIFOIDPtr g_ocspSigningEKU;
00283 extern CAC_API CPKIFOIDPtr g_anyEKU;
00284
00285 extern CAC_API CPKIFOIDPtr g_anyPolicyOID;
00286 extern CAC_API CPKIFPolicyInformationPtr g_anyPolicy;
00287
00288 extern CAC_API CPKIFOIDPtr g_aiaCAIssuer;
00289 extern CAC_API CPKIFOIDPtr g_aiaCARepository;
00290
00291
00292 extern CAC_API CPKIFOIDPtr g_secp224r1;
00293 extern CAC_API CPKIFOIDPtr g_secp256r1;
00294 extern CAC_API CPKIFOIDPtr g_secp384r1;
00295 extern CAC_API CPKIFOIDPtr g_secp521r1;
00296
00297
00298
00299
00300 extern CAC_API CPKIFAlgorithmIdentifierPtr g_md5AI;
00301 extern CAC_API CPKIFAlgorithmIdentifierPtr g_sha1AI;
00302 extern CAC_API CPKIFAlgorithmIdentifierPtr g_sha256AI;
00303 extern CAC_API CPKIFAlgorithmIdentifierPtr g_sha384AI;
00304 extern CAC_API CPKIFAlgorithmIdentifierPtr g_sha512AI;
00305
00306
00307 extern CAC_API unsigned char g_nullParams[];
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00364 enum AlgClass {DSA_CLASS, RSA_CLASS, ECDSA_CLASS};
00365 AlgClass CAC_API GetAlgClass(const CPKIFAlgorithmIdentifierPtr& alg);
00366 bool ModeRequiresIV(PKIFCRYPTO::SYMKEY_MODE mode);
00367
00368
00369 bool CAC_API GetCACHashAlg(const CPKIFOIDPtr& alg, PKIFCRYPTO::HASH_ALG* hashAlg);
00370 CPKIFAlgorithmIdentifierPtr CAC_API GetHashAlgAI(PKIFCRYPTO::HASH_ALG hashAlgorithm);
00371
00372
00373
00374 inline size_t BitsToBytes(size_t bitCount)
00375 {
00376 return ((bitCount+7)/(8));
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 CPKIFNamePtr CAC_API GetPKIFNameFromStr(unsigned char* name);
00388 CPKIFGeneralNamePtr CAC_API GetPKIFGenNameFromStr(unsigned char* name, int nameType);
00389 CPKIFBufferPtr CAC_API GetEncGenNameFromStr(unsigned char* name, int nameType);
00390 CPKIFBufferPtr CAC_API EncodeCPKIFNameAsGenName(const CPKIFNamePtr& name);
00391
00392
00393
00394 CPKIFBufferPtr CAC_API EncodeGeneralName(CPKIFGeneralNamePtr& gn);
00395
00396
00397
00398
00399
00400
00401
00402
00403 CPKIFStringPtr GetCertInfo(CPKIFCertificatePtr& targetCert);
00404 bool CertIsSelfIssued(const CPKIFCertificatePtr& cert);
00405
00406 void CollectNamesFromCRLDP(const CPKIFCertificatePtr& cert, CPKIFGeneralNameList& namesFromCert);
00407
00408
00409 bool keyUsageTest(BYTE keyUse[2], std::bitset<9>* ku);
00410
00411 void CAC_API FormatErrorMessage(std::string& result, const char* primaryMessage, unsigned int code, const char* file = NULL, int lineNum = 0);
00412 int CAC_API atob(char* byteval, char* ascval, unsigned int *alength);
00413 void CAC_API btoa(const char* byteval, char* ascval, unsigned int blength);
00414
00415
00416
00417 void RemoveParentRelationships(std::vector<IPKIFColleaguePtr>& modules, IPKIFMediator* mediator);
00418 void FreeAdditionalModules(std::vector<IPKIFColleague*>& modules, IPKIFMediator* mediator);
00419 void FreeAdditionalModules2(std::vector<IPKIFColleague*>& modules, IPKIFMediator* mediator, std::vector<void (*)( void * )>& vDeleteFuncs);
00420
00421
00422
00423
00424
00425 #define LOG_ASN_COMPONENTS 0x00000001
00426 #define LOG_CACHE_COMPONENTS 0x00000002
00427 #define LOG_CRYPTO_COMPONENTS 0x00000004
00428 #define LOG_MESSAGE_COMPONENTS 0x00000008
00429 #define LOG_OCSP_COMPONENTS 0x00000010
00430 #define LOG_PATH_COMPONENTS 0x00000020
00431 #define LOG_TAP_COMPONENTS 0x00000040
00432 #define LOG_TSP_COMPONENTS 0x00000080
00433 #define LOG_UTILS_COMPONENTS 0x00000100
00434 #define LOG_APP_COMPONENTS 0x00000200
00435 #define LOG_SUCCESSES 0x00000400 //log successful events (i.e. errorCode == 0)
00436 #define LOG_USE_EVENT_LOG 0x00000800 //write audited events to the system event log
00437 #define LOG_AUDITED_EVENTS 0x00001000 //write audited events to the log file
00438
00439
00440
00441 #define LOGINFO_CUR_TIME 0x00000001 //Bit 1
00442 #define LOGINFO_THREAD_ID 0x00000002 //Bit 2
00443 #define LOGINFO_OBJECT_POINTER 0x00000004 //Bit 3
00444 #define LOGINFO_SUBCOMPONENT_ID 0x00000008 //Bit 4
00445 #define LOGINFO_COMPONENT_CATEGORY 0x00000010 //Bit 5
00446 #define LOGINFO_SUBCOMPONENT_DESC 0x00000020 //Bit 6
00447 #define LOGINFO_ERROR_CODE 0x00000040 //Bit 7
00448 #define LOGINFO_ERROR_CODE_STRING 0x00000080 //Bit 8
00449 #define LOGINFO_ERROR_MESSAGE 0x00000100 //Bit 9
00450
00451 namespace nsCLog {
00453 enum eSeverity {
00454 debug = 100,
00455 info = 200,
00456 warning = 300,
00457 error = 400,
00458 fatal = 500
00459 };
00460 }
00461
00462 void GetLoggingIndicator(DWORD* value);
00463 bool GetLogFile(char* ppszFilename, unsigned long* pnLen);
00464
00465 enum PathOp {PO_BUILD, PO_VALIDATE};
00466 void CAC_API AuditPathEvent(PathOp op, bool succeeded, CPKIFCertificatePath& path);
00467
00468 void CAC_API LogString(const char* errorMessage, DWORD subcomponentID, DWORD errorCode = 0, const void* objectPointer = 0, nsCLog::eSeverity threshold = nsCLog::debug, const char* fileName = NULL, int lineNumber = NULL);
00469
00470 #ifndef _DISABLE_LOGGING_MACROS
00471 bool InitLogging();
00472 bool TerminateLogging();
00473 #endif
00474
00475 bool CAC_API InitPKIF();
00476
00477 bool InitAudit();
00478 #ifndef _NO_AUDITFACILITY
00479 void CAC_API AuditString(int eventType, int eventCat, int eventID, const char* str, DWORD subcomponentID, DWORD errorCode = 0, void* objectPointer = 0);
00480 #endif
00481 bool TerminateAudit();
00482
00483 #ifdef _NO_GETTICKCOUNT
00484 unsigned long GetTickCount(void);
00485 #endif //_NO_GETTICKCOUNT
00486
00487
00488 void CAC_API keyUsageChecker_AnyEE(const CPKIFCertificateNodeEntryPtr& certNode, CPKIFPathValidationResults& results, CertificateType type);
00489 void CAC_API EKUChecker_OcspSigning(const CPKIFCertificateNodeEntryPtr& certNode, CPKIFPathValidationResults& results, CertificateType type);
00490 void CAC_API EKUChecker_Timestamp(const CPKIFCertificateNodeEntryPtr& certNode, CPKIFPathValidationResults& results, CertificateType type);
00491
00492 std::string DecodeIPAndMask(const CPKIFBufferPtr& encodedIP);
00493 #endif
00494