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.

281 lines
9.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: ctxpvdr.cpp
  7. //
  8. // Contents: Context Providers for Remote Object Retrieval
  9. //
  10. // History: 23-Jul-97 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <global.hxx>
  14. //+---------------------------------------------------------------------------
  15. // Function: CreateObjectContext
  16. //
  17. // Synopsis: create single context or store containing multiple contexts
  18. //----------------------------------------------------------------------------
  19. BOOL WINAPI CreateObjectContext (
  20. IN DWORD dwRetrievalFlags,
  21. IN PCRYPT_BLOB_ARRAY pObject,
  22. IN DWORD dwExpectedContentTypeFlags,
  23. IN BOOL fQuerySingleContext,
  24. OUT LPVOID* ppvContext
  25. )
  26. {
  27. BOOL fResult = TRUE;
  28. HCERTSTORE hStore;
  29. DWORD cCount;
  30. int iQueryResult;
  31. DWORD dwQueryErr = 0;
  32. if ( !( dwRetrievalFlags & CRYPT_RETRIEVE_MULTIPLE_OBJECTS ) )
  33. {
  34. assert( pObject->cBlob > 0 );
  35. return( CryptQueryObject(
  36. CERT_QUERY_OBJECT_BLOB,
  37. (const void *)&(pObject->rgBlob[0]),
  38. fQuerySingleContext ?
  39. (dwExpectedContentTypeFlags &
  40. ( CERT_QUERY_CONTENT_FLAG_CERT |
  41. CERT_QUERY_CONTENT_FLAG_CTL |
  42. CERT_QUERY_CONTENT_FLAG_CRL ))
  43. : dwExpectedContentTypeFlags,
  44. CERT_QUERY_FORMAT_FLAG_ALL,
  45. 0,
  46. NULL,
  47. NULL,
  48. NULL,
  49. fQuerySingleContext ? NULL : (HCERTSTORE *) ppvContext,
  50. NULL,
  51. fQuerySingleContext ? (const void **) ppvContext : NULL
  52. ) );
  53. }
  54. if ( ( hStore = CertOpenStore(
  55. CERT_STORE_PROV_MEMORY,
  56. 0,
  57. NULL,
  58. 0,
  59. NULL
  60. ) ) == NULL )
  61. {
  62. return( FALSE );
  63. }
  64. // 0 => no CryptQueryObject()
  65. // 1 => 1 successful CryptQueryObject()
  66. // -1 => all CryptQueryObject()'s failed
  67. iQueryResult = 0;
  68. for ( cCount = 0;
  69. ( fResult == TRUE ) && ( cCount < pObject->cBlob );
  70. cCount++ )
  71. {
  72. PCERT_BLOB pBlob = &pObject->rgBlob[cCount];
  73. HCERTSTORE hChildStore;
  74. // Skip empty blobs. I have seen empty LDAP attributes containing
  75. // a single byte set to 0.
  76. if (0 == pBlob->cbData ||
  77. (1 == pBlob->cbData && 0 == pBlob->pbData[0]))
  78. {
  79. continue;
  80. }
  81. if (CryptQueryObject(
  82. CERT_QUERY_OBJECT_BLOB,
  83. (LPVOID) pBlob,
  84. dwExpectedContentTypeFlags,
  85. CERT_QUERY_FORMAT_FLAG_ALL,
  86. 0,
  87. NULL,
  88. NULL,
  89. NULL,
  90. &hChildStore,
  91. NULL,
  92. NULL
  93. ))
  94. {
  95. if (fQuerySingleContext)
  96. {
  97. if (0 == (dwExpectedContentTypeFlags &
  98. CERT_QUERY_CONTENT_FLAG_CERT))
  99. {
  100. PCCERT_CONTEXT pDeleteCert;
  101. while (pDeleteCert = CertEnumCertificatesInStore(
  102. hChildStore, NULL))
  103. {
  104. CertDeleteCertificateFromStore(pDeleteCert);
  105. }
  106. }
  107. if (0 == (dwExpectedContentTypeFlags &
  108. CERT_QUERY_CONTENT_FLAG_CRL))
  109. {
  110. PCCRL_CONTEXT pDeleteCrl;
  111. while (pDeleteCrl = CertEnumCRLsInStore(
  112. hChildStore, NULL))
  113. {
  114. CertDeleteCRLFromStore(pDeleteCrl);
  115. }
  116. }
  117. }
  118. fResult = I_CertUpdateStore( hStore, hChildStore, 0, NULL );
  119. CertCloseStore( hChildStore, 0 );
  120. iQueryResult = 1;
  121. }
  122. else if (iQueryResult == 0)
  123. {
  124. iQueryResult = -1;
  125. dwQueryErr = GetLastError();
  126. }
  127. }
  128. if ( fResult == TRUE && iQueryResult < 0)
  129. {
  130. fResult = FALSE;
  131. SetLastError(dwQueryErr);
  132. }
  133. if ( fResult == TRUE )
  134. {
  135. *ppvContext = (LPVOID)hStore;
  136. }
  137. else
  138. {
  139. CertCloseStore( hStore, 0 );
  140. }
  141. return( fResult );
  142. }
  143. //+---------------------------------------------------------------------------
  144. //
  145. // Function: CertificateCreateObjectContext
  146. //
  147. // Synopsis: creates a certificate context from encoded certificate bits
  148. //
  149. //----------------------------------------------------------------------------
  150. BOOL WINAPI CertificateCreateObjectContext (
  151. IN LPCSTR pszObjectOid,
  152. IN DWORD dwRetrievalFlags,
  153. IN PCRYPT_BLOB_ARRAY pObject,
  154. OUT LPVOID* ppvContext
  155. )
  156. {
  157. return CreateObjectContext (
  158. dwRetrievalFlags,
  159. pObject,
  160. CERT_QUERY_CONTENT_FLAG_CERT |
  161. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
  162. CERT_QUERY_CONTENT_FLAG_CERT_PAIR,
  163. TRUE, // fQuerySingleContext
  164. ppvContext
  165. );
  166. }
  167. //+---------------------------------------------------------------------------
  168. //
  169. // Function: CTLCreateObjectContext
  170. //
  171. // Synopsis: creates a CTL context from encoded CTL bits
  172. //
  173. //----------------------------------------------------------------------------
  174. BOOL WINAPI CTLCreateObjectContext (
  175. IN LPCSTR pszObjectOid,
  176. IN DWORD dwRetrievalFlags,
  177. IN PCRYPT_BLOB_ARRAY pObject,
  178. OUT LPVOID* ppvContext
  179. )
  180. {
  181. return CreateObjectContext (
  182. dwRetrievalFlags,
  183. pObject,
  184. CERT_QUERY_CONTENT_FLAG_CTL,
  185. TRUE, // fQuerySingleContext
  186. ppvContext
  187. );
  188. }
  189. //+---------------------------------------------------------------------------
  190. //
  191. // Function: CRLCreateObjectContext
  192. //
  193. // Synopsis: creates a CRL context from encoded CRL bits
  194. //
  195. //----------------------------------------------------------------------------
  196. BOOL WINAPI CRLCreateObjectContext (
  197. IN LPCSTR pszObjectOid,
  198. IN DWORD dwRetrievalFlags,
  199. IN PCRYPT_BLOB_ARRAY pObject,
  200. OUT LPVOID* ppvContext
  201. )
  202. {
  203. return CreateObjectContext (
  204. dwRetrievalFlags,
  205. pObject,
  206. CERT_QUERY_CONTENT_FLAG_CRL |
  207. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
  208. TRUE, // fQuerySingleContext
  209. ppvContext
  210. );
  211. }
  212. //+---------------------------------------------------------------------------
  213. //
  214. // Function: Pkcs7CreateObjectContext
  215. //
  216. // Synopsis: creates a certificate store context from a PKCS7 message
  217. //
  218. //----------------------------------------------------------------------------
  219. BOOL WINAPI Pkcs7CreateObjectContext (
  220. IN LPCSTR pszObjectOid,
  221. IN DWORD dwRetrievalFlags,
  222. IN PCRYPT_BLOB_ARRAY pObject,
  223. OUT LPVOID* ppvContext
  224. )
  225. {
  226. return CreateObjectContext (
  227. dwRetrievalFlags,
  228. pObject,
  229. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED,
  230. FALSE, // fQuerySingleContext
  231. ppvContext
  232. );
  233. }
  234. //+---------------------------------------------------------------------------
  235. //
  236. // Function: Capi2CreateObjectContext
  237. //
  238. // Synopsis: create a store of CAPI objects
  239. //
  240. //----------------------------------------------------------------------------
  241. BOOL WINAPI Capi2CreateObjectContext (
  242. IN LPCSTR pszObjectOid,
  243. IN DWORD dwRetrievalFlags,
  244. IN PCRYPT_BLOB_ARRAY pObject,
  245. OUT LPVOID* ppvContext
  246. )
  247. {
  248. return CreateObjectContext (
  249. dwRetrievalFlags,
  250. pObject,
  251. CERT_QUERY_CONTENT_FLAG_CERT |
  252. CERT_QUERY_CONTENT_FLAG_CTL |
  253. CERT_QUERY_CONTENT_FLAG_CRL |
  254. CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE |
  255. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT |
  256. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL |
  257. CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL |
  258. CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
  259. CERT_QUERY_CONTENT_FLAG_CERT_PAIR,
  260. FALSE, // fQuerySingleContext
  261. ppvContext
  262. );
  263. }