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.

262 lines
5.7 KiB

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