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.

245 lines
5.2 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. CCritSec _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. _ppCertContexts = (PCCERT_CONTEXT *)
  56. REALLOCATE_MEMORY(_ppCertContexts,
  57. (sizeof(PCERT_CONTEXT)*
  58. (CERT_CONTEXT_ARRAY_ALLOC_UNIT+_cAlloced)),
  59. LMEM_MOVEABLE
  60. );
  61. _cAlloced += CERT_CONTEXT_ARRAY_ALLOC_UNIT;
  62. if ( _ppCertContexts == NULL )
  63. {
  64. error = GetLastError();
  65. goto quit;
  66. }
  67. }
  68. //
  69. // Store new Pointer into array
  70. //
  71. PCCERT_CONTEXT pNewCertContext;
  72. WRAP_REVERT_USER(CertDuplicateCertificateContext,
  73. (pCertContext),
  74. pNewCertContext);
  75. if (pNewCertContext == NULL)
  76. {
  77. error = GetLastError();
  78. goto quit;
  79. }
  80. _ppCertContexts[_cCertContexts] = pNewCertContext;
  81. _cCertContexts++;
  82. quit:
  83. return error;
  84. }
  85. VOID
  86. SelectCertContext(
  87. INT index
  88. )
  89. {
  90. INET_ASSERT((index >= 0 && index < (INT) _cCertContexts) || index == -1);
  91. _iSelected = index;
  92. }
  93. PCCERT_CONTEXT
  94. GetCertContext(
  95. DWORD dwIndex
  96. )
  97. {
  98. INET_ASSERT(dwIndex < _cCertContexts);
  99. return _ppCertContexts[dwIndex];
  100. }
  101. PCCERT_CONTEXT
  102. GetSelectedCertContext(
  103. VOID
  104. )
  105. {
  106. INET_ASSERT(_iSelected >= 0 || _iSelected == -1);
  107. if ( _iSelected == -1 )
  108. return NULL;
  109. return GetCertContext((DWORD) _iSelected);
  110. }
  111. DWORD
  112. GetError(
  113. VOID
  114. )
  115. {
  116. return _error;
  117. }
  118. DWORD
  119. GetArraySize(
  120. VOID
  121. )
  122. {
  123. return _cCertContexts;
  124. }
  125. BOOL
  126. LockCredHandle( )
  127. {
  128. if (_cs.IsInitialized())
  129. return _cs.Lock();
  130. else
  131. // try initializing again
  132. return (_cs.Init() && _cs.Lock());
  133. }
  134. VOID
  135. UnlockCredHandle( )
  136. {
  137. _cs.Unlock();
  138. }
  139. CredHandle
  140. GetCredHandle( )
  141. {
  142. return _hCreds;
  143. }
  144. VOID
  145. SetCredHandle(CredHandle hCreds )
  146. {
  147. _hCreds = hCreds;
  148. }
  149. };
  150. typedef HRESULT
  151. (WINAPI * WIN_VERIFY_TRUST_FN)
  152. (
  153. IN OPTIONAL HWND hwnd,
  154. IN GUID *pgActionID,
  155. IN WINTRUST_DATA *pWinTrustData
  156. );
  157. typedef CRYPT_PROVIDER_DATA * (WINAPI * WT_HELPER_PROV_DATA_FROM_STATE_DATA_FN)
  158. (
  159. IN HANDLE hStateData
  160. );
  161. #define WIN_VERIFY_TRUST_NAME TEXT("WinVerifyTrust")
  162. #define WT_HELPER_PROV_DATA_FROM_STATE_DATA_NAME TEXT("WTHelperProvDataFromStateData")
  163. #define ADVAPI_DLLNAME TEXT("advapi32.dll")
  164. #define WINTRUST_DLLNAME TEXT("wintrust.dll")
  165. #define SOFTPUB_DLLNAME TEXT("softpub.dll")
  166. #define SP_REG_KEY_SCHANNEL_BASE TEXT("System\\CurrentControlSet\\Control\\SecurityProviders\\SCHANNEL")
  167. #define SP_REG_WINTRUST TEXT("Wintrust")
  168. #define CLIENT_AUTH_TYPE L"ClientAuth"
  169. #define CHAIN_BUFFER_SIZE 32768
  170. #define ISSUER_SIZE_FIELD_SIZE 2
  171. DWORD
  172. CliAuthSelectCredential(
  173. IN PCtxtHandle phContext,
  174. IN LPTSTR pszPackageName,
  175. IN CERT_CONTEXT_ARRAY *pCertContextArray,
  176. OUT PCredHandle phCredential,
  177. IN LPDWORD pdwStatus);
  178. DWORD
  179. CliAuthAcquireCertContexts(
  180. IN PCtxtHandle phContext,
  181. IN LPTSTR pszPackageName,
  182. OUT CERT_CONTEXT_ARRAY **ppCertContextArray,
  183. IN LPDWORD pdwStatus
  184. );