|
|
1.1 ! root 1: /* ! 2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved. ! 3: ! 4: Governed by the TrueCrypt License 2.6 the full text of which is contained ! 5: in the file License.txt included in TrueCrypt binary and source code ! 6: distribution packages. ! 7: */ ! 8: ! 9: #ifndef TC_HEADER_Common_SecurityToken ! 10: #define TC_HEADER_Common_SecurityToken ! 11: ! 12: #include "Platform/PlatformBase.h" ! 13: #if defined (TC_WINDOWS) && !defined (TC_PROTOTYPE) ! 14: # include "Exception.h" ! 15: #else ! 16: # include "Platform/Exception.h" ! 17: #endif ! 18: ! 19: #ifndef NULL_PTR ! 20: # define NULL_PTR 0 ! 21: #endif ! 22: #define CK_PTR * ! 23: #define CK_CALLBACK_FUNCTION(RET_TYPE, NAME) RET_TYPE (* NAME) ! 24: ! 25: #ifdef TC_WINDOWS ! 26: ! 27: # include <windows.h> ! 28: ! 29: # define CK_DEFINE_FUNCTION(RET_TYPE, NAME) RET_TYPE __declspec(dllexport) NAME ! 30: # define CK_DECLARE_FUNCTION(RET_TYPE, NAME) RET_TYPE __declspec(dllimport) NAME ! 31: # define CK_DECLARE_FUNCTION_POINTER(RET_TYPE, NAME) RET_TYPE __declspec(dllimport) (* NAME) ! 32: ! 33: # pragma pack(push, cryptoki, 1) ! 34: # include <pkcs11.h> ! 35: # pragma pack(pop, cryptoki) ! 36: ! 37: #else // !TC_WINDOWS ! 38: ! 39: # define CK_DEFINE_FUNCTION(RET_TYPE, NAME) RET_TYPE NAME ! 40: # define CK_DECLARE_FUNCTION(RET_TYPE, NAME) RET_TYPE NAME ! 41: # define CK_DECLARE_FUNCTION_POINTER(RET_TYPE, NAME) RET_TYPE (* NAME) ! 42: ! 43: # include <pkcs11.h> ! 44: ! 45: #endif // !TC_WINDOWS ! 46: ! 47: ! 48: #define TC_SECURITY_TOKEN_KEYFILE_URL_PREFIX L"token://" ! 49: #define TC_SECURITY_TOKEN_KEYFILE_URL_SLOT L"slot" ! 50: #define TC_SECURITY_TOKEN_KEYFILE_URL_FILE L"file" ! 51: ! 52: namespace TrueCrypt ! 53: { ! 54: struct SecurityTokenInfo ! 55: { ! 56: CK_SLOT_ID SlotId; ! 57: CK_FLAGS Flags; ! 58: wstring Label; ! 59: string LabelUtf8; ! 60: }; ! 61: ! 62: struct SecurityTokenKeyfilePath ! 63: { ! 64: SecurityTokenKeyfilePath () { } ! 65: SecurityTokenKeyfilePath (const wstring &path) : Path (path) { } ! 66: operator wstring () const { return Path; } ! 67: wstring Path; ! 68: }; ! 69: ! 70: struct SecurityTokenKeyfile ! 71: { ! 72: SecurityTokenKeyfile () { } ! 73: SecurityTokenKeyfile (const SecurityTokenKeyfilePath &path); ! 74: ! 75: operator SecurityTokenKeyfilePath () const; ! 76: ! 77: CK_OBJECT_HANDLE Handle; ! 78: wstring Id; ! 79: string IdUtf8; ! 80: CK_SLOT_ID SlotId; ! 81: SecurityTokenInfo Token; ! 82: }; ! 83: ! 84: struct Pkcs11Exception : public Exception ! 85: { ! 86: Pkcs11Exception (CK_RV errorCode = -1) ! 87: : ErrorCode (errorCode), ! 88: SubjectErrorCodeValid (false) ! 89: { ! 90: } ! 91: ! 92: Pkcs11Exception (CK_RV errorCode, uint64 subjectErrorCode) ! 93: : ErrorCode (errorCode), ! 94: SubjectErrorCodeValid (true), ! 95: SubjectErrorCode (subjectErrorCode) ! 96: { ! 97: } ! 98: ! 99: #ifdef TC_HEADER_Platform_Exception ! 100: virtual ~Pkcs11Exception () throw () { } ! 101: TC_SERIALIZABLE_EXCEPTION (Pkcs11Exception); ! 102: #else ! 103: void Show (HWND parent) const; ! 104: #endif ! 105: operator string () const; ! 106: CK_RV GetErrorCode () const { return ErrorCode; } ! 107: ! 108: protected: ! 109: CK_RV ErrorCode; ! 110: bool SubjectErrorCodeValid; ! 111: uint64 SubjectErrorCode; ! 112: }; ! 113: ! 114: ! 115: #ifdef TC_HEADER_Platform_Exception ! 116: ! 117: #define TC_EXCEPTION(NAME) TC_EXCEPTION_DECL(NAME,Exception) ! 118: ! 119: #undef TC_EXCEPTION_SET ! 120: #define TC_EXCEPTION_SET \ ! 121: TC_EXCEPTION_NODECL (Pkcs11Exception); \ ! 122: TC_EXCEPTION (InvalidSecurityTokenKeyfilePath); \ ! 123: TC_EXCEPTION (SecurityTokenLibraryNotInitialized); \ ! 124: TC_EXCEPTION (SecurityTokenKeyfileAlreadyExists); \ ! 125: TC_EXCEPTION (SecurityTokenKeyfileNotFound); ! 126: ! 127: TC_EXCEPTION_SET; ! 128: ! 129: #undef TC_EXCEPTION ! 130: ! 131: #else // !TC_HEADER_Platform_Exception ! 132: ! 133: struct SecurityTokenLibraryNotInitialized : public Exception ! 134: { ! 135: void Show (HWND parent) const { Error (SecurityTokenLibraryPath[0] == 0 ? "NO_PKCS11_MODULE_SPECIFIED" : "PKCS11_MODULE_INIT_FAILED"); } ! 136: }; ! 137: ! 138: struct InvalidSecurityTokenKeyfilePath : public Exception ! 139: { ! 140: void Show (HWND parent) const { Error ("INVALID_TOKEN_KEYFILE_PATH"); } ! 141: }; ! 142: ! 143: struct SecurityTokenKeyfileAlreadyExists : public Exception ! 144: { ! 145: void Show (HWND parent) const { Error ("TOKEN_KEYFILE_ALREADY_EXISTS"); } ! 146: }; ! 147: ! 148: struct SecurityTokenKeyfileNotFound : public Exception ! 149: { ! 150: void Show (HWND parent) const { Error ("TOKEN_KEYFILE_NOT_FOUND"); } ! 151: }; ! 152: ! 153: #endif // !TC_HEADER_Platform_Exception ! 154: ! 155: ! 156: struct Pkcs11Session ! 157: { ! 158: Pkcs11Session () : UserLoggedIn (false) { } ! 159: ! 160: CK_SESSION_HANDLE Handle; ! 161: bool UserLoggedIn; ! 162: }; ! 163: ! 164: struct GetPinFunctor ! 165: { ! 166: virtual ~GetPinFunctor () { } ! 167: virtual void operator() (string &str) = 0; ! 168: }; ! 169: ! 170: struct SendExceptionFunctor ! 171: { ! 172: virtual ~SendExceptionFunctor () { } ! 173: virtual void operator() (const Exception &e) = 0; ! 174: }; ! 175: ! 176: class SecurityToken ! 177: { ! 178: public: ! 179: static void CloseAllSessions () throw (); ! 180: static void CloseLibrary (); ! 181: static void CreateKeyfile (CK_SLOT_ID slotId, vector <byte> &keyfileData, const string &name); ! 182: static void DeleteKeyfile (const SecurityTokenKeyfile &keyfile); ! 183: static vector <SecurityTokenKeyfile> GetAvailableKeyfiles (CK_SLOT_ID *slotIdFilter = nullptr, const wstring keyfileIdFilter = wstring()); ! 184: static void GetKeyfileData (const SecurityTokenKeyfile &keyfile, vector <byte> &keyfileData); ! 185: static list <SecurityTokenInfo> GetAvailableTokens (); ! 186: static SecurityTokenInfo GetTokenInfo (CK_SLOT_ID slotId); ! 187: static void InitLibrary (const string &pkcs11LibraryPath, auto_ptr <GetPinFunctor> pinCallback, auto_ptr <SendExceptionFunctor> warningCallback); ! 188: static bool IsInitialized () { return Initialized; } ! 189: static bool IsKeyfilePathValid (const wstring &securityTokenKeyfilePath); ! 190: ! 191: static const size_t MaxPasswordLength = 128; ! 192: ! 193: protected: ! 194: static void CloseSession (CK_SLOT_ID slotId); ! 195: static vector <CK_OBJECT_HANDLE> GetObjects (CK_SLOT_ID slotId, CK_ATTRIBUTE_TYPE objectClass); ! 196: static void GetObjectAttribute (CK_SLOT_ID slotId, CK_OBJECT_HANDLE tokenObject, CK_ATTRIBUTE_TYPE attributeType, vector <byte> &attributeValue); ! 197: static list <CK_SLOT_ID> GetTokenSlots (); ! 198: static void Login (CK_SLOT_ID slotId, const string &pin); ! 199: static void LoginUserIfRequired (CK_SLOT_ID slotId); ! 200: static void OpenSession (CK_SLOT_ID slotId); ! 201: static void CheckLibraryStatus (); ! 202: ! 203: static bool Initialized; ! 204: static auto_ptr <GetPinFunctor> PinCallback; ! 205: static CK_FUNCTION_LIST_PTR Pkcs11Functions; ! 206: #ifdef TC_WINDOWS ! 207: static HMODULE Pkcs11LibraryHandle; ! 208: #else ! 209: static void *Pkcs11LibraryHandle; ! 210: #endif ! 211: static map <CK_SLOT_ID, Pkcs11Session> Sessions; ! 212: static auto_ptr <SendExceptionFunctor> WarningCallback; ! 213: }; ! 214: } ! 215: ! 216: #endif // TC_HEADER_Common_SecurityToken
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.