Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
5.1 KiB

  1. #ifdef INET_DEBUG
  2. #define CERT_CONTEXT_ARRAY_ALLOC_UNIT 2 // made small for testing
  3. #else
  4. #define CERT_CONTEXT_ARRAY_ALLOC_UNIT 10
  5. #endif
  6. #define ClearCreds(CredHandle) \
  7. CredHandle.dwLower = CredHandle.dwUpper = 0
  8. #define IsCredClear(CredHandle) \
  9. (( CredHandle.dwLower == 0 && CredHandle.dwUpper == 0 ) ? TRUE : FALSE )
  10. class CERT_CONTEXT_ARRAY
  11. {
  12. private:
  13. //
  14. // number of cert chains in array
  15. //
  16. DWORD _cCertContexts;
  17. //
  18. // number of slots allocated in array
  19. //
  20. DWORD _cAlloced;
  21. //
  22. // array of Cert Context pointers
  23. //
  24. PCCERT_CONTEXT* _ppCertContexts;
  25. //
  26. // Index of Cert Chain, selected to be used by user.
  27. //
  28. INT _iSelected;
  29. //
  30. // Not Equal to ERROR_SUCCESS upon error at intialization.
  31. //
  32. DWORD _error;
  33. // Critical section to guard the Cred Handle
  34. CRITICAL_SECTION _cs ;
  35. // Cred Handle created for the selected cert context which we should re-use
  36. // to prevent multiple prompts to the user.
  37. CredHandle _hCreds;
  38. public:
  39. CERT_CONTEXT_ARRAY();
  40. ~CERT_CONTEXT_ARRAY();
  41. void Reset (void);
  42. DWORD
  43. AddCertContext(
  44. PCCERT_CONTEXT pCertContext
  45. )
  46. {
  47. DWORD error = ERROR_SUCCESS;
  48. INET_ASSERT(pCertContext);
  49. //
  50. // If the Array is already full, Realloc
  51. //
  52. if ( _cAlloced <= _cCertContexts )
  53. {
  54. INET_ASSERT(_cAlloced == _cCertContexts);
  55. PCCERT_CONTEXT* pTemp = _ppCertContexts;
  56. #pragma prefast(suppress:308, "noise")
  57. _ppCertContexts = (PCCERT_CONTEXT *)
  58. REALLOCATE_MEMORY(_ppCertContexts,
  59. (sizeof(PCERT_CONTEXT)*
  60. (CERT_CONTEXT_ARRAY_ALLOC_UNIT+_cAlloced)),
  61. LMEM_MOVEABLE
  62. );
  63. _cAlloced += CERT_CONTEXT_ARRAY_ALLOC_UNIT;
  64. if ( _ppCertContexts == NULL )
  65. {
  66. delete pTemp;
  67. error = GetLastError();
  68. goto quit;
  69. }
  70. }
  71. //
  72. // Store new Pointer into array
  73. //
  74. PCCERT_CONTEXT pNewCertContext;
  75. pNewCertContext= CertDuplicateCertificateContext(pCertContext);
  76. if (pNewCertContext == NULL)
  77. {
  78. error = GetLastError();
  79. goto quit;
  80. }
  81. _ppCertContexts[_cCertContexts] = pNewCertContext;
  82. _cCertContexts++;
  83. quit:
  84. return error;
  85. }
  86. VOID
  87. SelectCertContext(
  88. INT index
  89. )
  90. {
  91. INET_ASSERT((index >= 0 && index < (INT) _cCertContexts) || index == -1);
  92. _iSelected = index;
  93. }
  94. PCCERT_CONTEXT
  95. GetCertContext(
  96. DWORD dwIndex
  97. )
  98. {
  99. INET_ASSERT(dwIndex < _cCertContexts);
  100. return _ppCertContexts[dwIndex];
  101. }
  102. PCCERT_CONTEXT
  103. GetSelectedCertContext(
  104. VOID
  105. )
  106. {
  107. INET_ASSERT(_iSelected >= 0 || _iSelected == -1);
  108. if ( _iSelected == -1 )
  109. return NULL;
  110. return GetCertContext((DWORD) _iSelected);
  111. }
  112. DWORD
  113. GetError(
  114. VOID
  115. )
  116. {
  117. return _error;
  118. }
  119. DWORD
  120. GetArraySize(
  121. VOID
  122. )
  123. {
  124. return _cCertContexts;
  125. }
  126. VOID
  127. LockCredHandle( )
  128. {
  129. EnterCriticalSection(&_cs);
  130. }
  131. VOID
  132. UnlockCredHandle( )
  133. {
  134. LeaveCriticalSection(&_cs);
  135. }
  136. CredHandle
  137. GetCredHandle( )
  138. {
  139. return _hCreds;
  140. }
  141. VOID
  142. SetCredHandle(CredHandle hCreds )
  143. {
  144. _hCreds = hCreds;
  145. }
  146. };
  147. typedef HRESULT
  148. (WINAPI * WIN_VERIFY_TRUST_FN)
  149. (
  150. IN OPTIONAL HWND hwnd,
  151. IN GUID *pgActionID,
  152. IN WINTRUST_DATA *pWinTrustData
  153. );
  154. typedef CRYPT_PROVIDER_DATA * (WINAPI * WT_HELPER_PROV_DATA_FROM_STATE_DATA_FN)
  155. (
  156. IN HANDLE hStateData
  157. );
  158. #define WIN_VERIFY_TRUST_NAME TEXT("WinVerifyTrust")
  159. #define WT_HELPER_PROV_DATA_FROM_STATE_DATA_NAME TEXT("WTHelperProvDataFromStateData")
  160. #define ADVAPI_DLLNAME TEXT("advapi32.dll")
  161. #define WINTRUST_DLLNAME TEXT("wintrust.dll")
  162. #define SOFTPUB_DLLNAME TEXT("softpub.dll")
  163. #define SP_REG_KEY_SCHANNEL_BASE TEXT("System\\CurrentControlSet\\Control\\SecurityProviders\\SCHANNEL")
  164. #define SP_REG_WINTRUST TEXT("Wintrust")
  165. #define CLIENT_AUTH_TYPE L"ClientAuth"
  166. #define CHAIN_BUFFER_SIZE 32768
  167. #define ISSUER_SIZE_FIELD_SIZE 2
  168. DWORD
  169. CliAuthSelectCredential(
  170. IN PCtxtHandle phContext,
  171. IN LPTSTR pszPackageName,
  172. IN CERT_CONTEXT_ARRAY *pCertContextArray,
  173. OUT PCredHandle phCredential);
  174. DWORD
  175. CliAuthAcquireCertContexts(
  176. IN PCtxtHandle phContext,
  177. IN LPTSTR pszPackageName,
  178. OUT CERT_CONTEXT_ARRAY **ppCertContextArray
  179. );