Source code of Windows XP (NT5)
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.

355 lines
15 KiB

  1. #ifndef __CERT_REQUESTER_CONTEXT_H__
  2. #define __CERT_REQUESTER_CONTEXT_H__ 1
  3. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  4. //
  5. // CLASS CertRequesterContext.
  6. //
  7. // The CertRequesterContext class is used to encapsulate any details of the certificate enrollment
  8. // implementation which are dependent upon the context in which the program is running.
  9. // Currently, there are two supported contexts:
  10. //
  11. // 1) LocalContext. This is used when the program runs under the current user's context,
  12. // on the local machine.
  13. //
  14. // 2) KeySvcContext. This is used when some other context must be specified through
  15. // keysvc.
  16. //
  17. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  18. class CertRequesterContext {
  19. public:
  20. //-------------------------------------------------------------------------
  21. // Builds a list of the CSPs supported in this context.
  22. // (Different machines may have different CSP lists).
  23. //
  24. // Requires:
  25. // * This CertRequesterContext was created with a valid CERT_WIZARD_INFO pointer.
  26. //
  27. // Modifies:
  28. // * The following fields of the internal CERT_WIZARD_INFO pointer:
  29. // dwCSPCount : contains the number of valid CSPs.
  30. // rgdwProviderType : contains a dwCSPCount-element array of the provider types available.
  31. // This field should be freed with WizardFree().
  32. // rgwszProvider : contains a dwCSPCount-element array of the provider names available.
  33. // This field, and each array element should be freed with WizardFree().
  34. //
  35. // Use GetWizardInfo() to retrieve a pointer to the updated CERT_WIZARD_INFO.
  36. //
  37. // Returns:
  38. // * S_OK if successful, failure code otherwise.
  39. //
  40. //--------------------------------------------------------------------------
  41. virtual HRESULT BuildCSPList() = 0;
  42. //---------------------------------------------------------------------------
  43. // Checks if this context has sufficient access to use the specified cert type
  44. // in enrollment.
  45. //
  46. // Requires:
  47. // * This CertRequesterContext was created with a valid CERT_WIZARD_INFO pointer.
  48. //
  49. // Modifies:
  50. // Nothing
  51. //
  52. // Returns:
  53. // * Returns TRUE if this context has the permission to access the specified cert type.
  54. // Returns FALSE if not, or if an error occurs.
  55. //
  56. //--------------------------------------------------------------------------
  57. virtual BOOL CheckAccessPermission(IN HCERTTYPE hCertType) = 0;
  58. //---------------------------------------------------------------------------
  59. // Checks if this context has sufficient access to use the specified CA in enrollment.
  60. //
  61. // Requires:
  62. // * This CertRequesterContext was created with a valid CERT_WIZARD_INFO pointer.
  63. //
  64. // Modifies:
  65. // Nothing
  66. //
  67. // Returns:
  68. // * Returns TRUE if this context has the permission to access the specified CA.
  69. // Returns FALSE if not, or if an error occurs.
  70. //
  71. //--------------------------------------------------------------------------
  72. virtual BOOL CheckCAPermission(IN HCAINFO hCAInfo) = 0;
  73. //-----------------------------------------------------------------------
  74. // Gets the name of the default CSP on the local machine, based on the provider
  75. // type specified by the internal CERT_WIZARD_INFO pointer.
  76. //
  77. // Requires:
  78. // * This CertRequesterContext was created with a valid CERT_WIZARD_INFO pointer.
  79. //
  80. // Modifies:
  81. // * The following fields of the internal CERT_WIZARD_INFO pointer:
  82. // pwszProvider : contains the name of the default provider, for the specified
  83. // provider type, on this machine. Use WizardFree to free the memory
  84. // associated with this field. HOWEVER, check the out parameter to
  85. // ensure that memory for this field was actually allocated.
  86. //
  87. // Use GetWizardInfo() to retrieve a pointer to the updated CERT_WIZARD_INFO.
  88. //
  89. // Returns:
  90. // S_OK if successful, an error code otherwise.
  91. // If memory was allocated to store the default CSP's name, returns TRUE into
  92. // the OUT parameter, FALSE otherwise.
  93. //
  94. // If the dwProviderType field of the interal wizard pointer is not set, OR
  95. // both the dwProviderType and pwszProvider fields are set, then the function
  96. // does not attempt to find a default CSP, and returns successfully without allocating
  97. // any memory.
  98. //
  99. //------------------------------------------------------------------------
  100. virtual HRESULT GetDefaultCSP(OUT BOOL *pfAllocateCSP) = 0;
  101. //----------------------------------------------------------------------
  102. //
  103. // Enrolls for a certificate, or renews a certificate, based on the
  104. // supplied parameters.
  105. //
  106. // Parameters:
  107. // * pdwStatus : Status of the request. One of the CRYPTUI_WIZ_CERT_REQUEST_STATUS_*
  108. // defines in cryptui.h.
  109. // * pResult : For certificate request creation, returns a pointer to an opaque
  110. // data blob which can be used as a parameter to SubmitRequest().
  111. // Otherwise, returns a PCCERT_CONTEXT representing the
  112. // enrolled/renewed certificate.
  113. //
  114. // Requires:
  115. // Modifies:
  116. // Returns:
  117. // S_OK if the operation completed without an error, returns a standard error
  118. // code otherwise. Note that a return value of S_OK does not
  119. // guarantee that a certificate was issued: check that the pdwStatus parameter.
  120. //
  121. //----------------------------------------------------------------------
  122. virtual HRESULT Enroll(OUT DWORD *pdwStatus,
  123. OUT HANDLE *pResult) = 0;
  124. //----------------------------------------------------------------------
  125. // Performs context-specific initialization. This should always be called after
  126. // the context is created.
  127. //
  128. // Requires:
  129. // * This CertRequesterContext was created with a valid CERT_WIZARD_INFO pointer.
  130. //
  131. // Modifies:
  132. // Implementation-specific (see implementation documentation).
  133. //
  134. // Returns:
  135. // S_OK if the initialization succeeded.
  136. //
  137. //----------------------------------------------------------------------
  138. virtual HRESULT Initialize() = 0;
  139. virtual HRESULT QueryRequestStatus(IN HANDLE hRequest, OUT CRYPTUI_WIZ_QUERY_CERT_REQUEST_INFO *pQueryInfo) = 0;
  140. virtual ~CertRequesterContext();
  141. static HRESULT MakeDefaultCertRequesterContext(OUT CertRequesterContext **ppRequesterContext);
  142. static HRESULT MakeCertRequesterContext(IN LPCWSTR pwszAccountName,
  143. IN LPCWSTR pwszMachineName,
  144. IN DWORD dwCertOpenStoreFlags,
  145. IN CERT_WIZARD_INFO *pCertWizardInfo,
  146. OUT CertRequesterContext **ppRequesterContext,
  147. OUT UINT *pIDSText);
  148. UINT GetErrorString() { return m_idsText; }
  149. CERT_WIZARD_INFO * GetWizardInfo() { return m_pCertWizardInfo; }
  150. protected:
  151. CertRequesterContext(CERT_WIZARD_INFO * pCertWizardInfo) : m_pCertWizardInfo(pCertWizardInfo) { }
  152. CERT_WIZARD_INFO * m_pCertWizardInfo;
  153. UINT m_idsText;
  154. private:
  155. CertRequesterContext();
  156. };
  157. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  158. //
  159. // LocalContext.
  160. //
  161. // This class provides an implementation of the CertRequestContext interface
  162. // which runs under the current user's context on the local machine.
  163. //
  164. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  165. class LocalContext : public CertRequesterContext {
  166. friend class CertRequesterContext;
  167. public:
  168. //----------------------------------------------------------------------
  169. // See CertRequesterContext::BuildCSPList().
  170. //----------------------------------------------------------------------
  171. virtual HRESULT BuildCSPList();
  172. //----------------------------------------------------------------------
  173. // See CertRequesterContext::CheckAccessPermission(HCERTTYPE).
  174. //----------------------------------------------------------------------
  175. virtual BOOL CheckAccessPermission(HCERTTYPE hCertType);
  176. //----------------------------------------------------------------------
  177. // See CertRequesterContext::BuildCSPList(BOOL *).
  178. //----------------------------------------------------------------------
  179. virtual HRESULT GetDefaultCSP(BOOL *pfAllocateCSP);
  180. //----------------------------------------------------------------------
  181. // See CertRequesterContext::BuildCSPList(HCAINFO).
  182. //----------------------------------------------------------------------
  183. virtual BOOL CheckCAPermission(HCAINFO hCAInfo);
  184. virtual HRESULT Enroll(OUT DWORD *pdwStatus,
  185. OUT HANDLE *pResult);
  186. virtual HRESULT QueryRequestStatus(IN HANDLE hRequest, OUT CRYPTUI_WIZ_QUERY_CERT_REQUEST_INFO *pQueryInfo);
  187. //----------------------------------------------------------------------
  188. // Always returns S_OK.
  189. //----------------------------------------------------------------------
  190. virtual HRESULT Initialize();
  191. private:
  192. HANDLE GetClientIdentity();
  193. LocalContext();
  194. LocalContext(CERT_WIZARD_INFO * pCertWizardInfo) : CertRequesterContext(pCertWizardInfo)
  195. {
  196. if (NULL != m_pCertWizardInfo) { m_pCertWizardInfo->fLocal = TRUE; }
  197. }
  198. };
  199. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  200. //
  201. // KeySvcContext.
  202. //
  203. // This class provides an implementation of the CertRequestContext interface
  204. // using key svc.
  205. //
  206. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  207. class KeySvcContext : public CertRequesterContext {
  208. friend class CertRequesterContext;
  209. public:
  210. //----------------------------------------------------------------------
  211. // See CertRequesterContext::BuildCSPList().
  212. //----------------------------------------------------------------------
  213. virtual HRESULT BuildCSPList();
  214. //----------------------------------------------------------------------
  215. // See CertRequesterContext::CheckAccessPermission(HCERTTYPE).
  216. //----------------------------------------------------------------------
  217. virtual BOOL CheckAccessPermission(HCERTTYPE hCertType);
  218. //----------------------------------------------------------------------
  219. // See CertRequesterContext::BuildCSPList(BOOL *).
  220. //----------------------------------------------------------------------
  221. virtual HRESULT GetDefaultCSP(BOOL *pfAllocateCSP);
  222. //----------------------------------------------------------------------
  223. // See CertRequesterContext::BuildCSPList(HCAINFO).
  224. //----------------------------------------------------------------------
  225. virtual BOOL CheckCAPermission(HCAINFO hCAInfo);
  226. virtual HRESULT Enroll(OUT DWORD *pdwStatus,
  227. OUT HANDLE *pResult) = 0;
  228. virtual HRESULT QueryRequestStatus(IN HANDLE hRequest, OUT CRYPTUI_WIZ_QUERY_CERT_REQUEST_INFO *pQueryInfo);
  229. //----------------------------------------------------------------------
  230. // Requires:
  231. // * This CertRequesterContext was created with a valid CERT_WIZARD_INFO pointer.
  232. // * This CertRequesterContext has been initialized with a call to Initialize().
  233. //
  234. // Modifies:
  235. // * The following fields of the internal CERT_WIZARD_INFO pointer:
  236. // awszAllowedCertTypes : contains an array of cert types which this context
  237. // has permission to enroll for. This field, and each
  238. // array element, must be freed with WizardFree().
  239. // awszValidCA : contains an array of CAs which this context has
  240. // permission to enroll from. This field, and each array
  241. // element, must be freed with WizardFree().
  242. // Returns:
  243. // S_OK if initialization succeeded, an error code otherwise.
  244. //
  245. //----------------------------------------------------------------------
  246. virtual HRESULT Initialize();
  247. virtual ~KeySvcContext()
  248. {
  249. // free the list of allowed CertTypes
  250. // These may be allocate by the KeySvcContext's Initialize() method.
  251. if(NULL != m_pCertWizardInfo->awszAllowedCertTypes) { WizardFree(m_pCertWizardInfo->awszAllowedCertTypes); }
  252. if(NULL != m_pCertWizardInfo->awszValidCA) { WizardFree(m_pCertWizardInfo->awszValidCA); }
  253. }
  254. protected:
  255. KeySvcContext(CERT_WIZARD_INFO * pCertWizardInfo) : CertRequesterContext(pCertWizardInfo)
  256. {
  257. if (NULL != m_pCertWizardInfo)
  258. {
  259. m_pCertWizardInfo->fLocal = FALSE;
  260. m_pCertWizardInfo->awszAllowedCertTypes = NULL;
  261. m_pCertWizardInfo->awszValidCA = NULL;
  262. }
  263. }
  264. HRESULT ToCertContext(IN CERT_BLOB *pPKCS7Blob,
  265. IN CERT_BLOB *pHashBlob,
  266. OUT DWORD *pdwStatus,
  267. OUT PCCERT_CONTEXT *ppCertContext);
  268. private:
  269. KeySvcContext();
  270. };
  271. class WhistlerMachineContext : public KeySvcContext {
  272. friend class CertRequesterContext;
  273. virtual HRESULT Enroll(OUT DWORD *pdwStatus,
  274. OUT HANDLE *ppCertContext);
  275. private:
  276. WhistlerMachineContext(CERT_WIZARD_INFO * pCertWizardInfo) : KeySvcContext(pCertWizardInfo)
  277. { }
  278. HRESULT CreateRequest(IN KEYSVCC_HANDLE hKeyService,
  279. IN LPSTR pszMachineName,
  280. IN LPWSTR pwszCALocation,
  281. IN LPWSTR pwszCAName,
  282. IN PCERT_REQUEST_PVK_NEW pKeyNew,
  283. IN CERT_BLOB *pCert,
  284. IN PCERT_REQUEST_PVK_NEW pRenewKey,
  285. IN LPWSTR pszHashAlg,
  286. IN PCERT_ENROLL_INFO pRequestInfo,
  287. OUT HANDLE *phRequest);
  288. HRESULT SubmitRequest(IN KEYSVCC_HANDLE hKeyService,
  289. IN LPSTR pszMachineName,
  290. IN LPWSTR pwszCALocation,
  291. IN LPWSTR pwszCAName,
  292. IN HANDLE hRequest,
  293. OUT PCCERT_CONTEXT *ppCertContext,
  294. OUT DWORD *pdwStatus);
  295. void FreeRequest(IN KEYSVCC_HANDLE hKeyService,
  296. IN LPSTR pszMachineName,
  297. IN HANDLE hRequest);
  298. };
  299. #endif // #ifndef __CERT_REQUESTER_CONTEXT_H__