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.

514 lines
15 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: init.cpp
  7. //
  8. // Contents: Initialization for Remote Object Retrieval
  9. //
  10. // History: 23-Jul-97 kirtd Created
  11. // 01-Jan-02 philh Changed to internally use UNICODE Urls
  12. //
  13. //----------------------------------------------------------------------------
  14. #include <global.hxx>
  15. //
  16. // Remote Object Retrieval Function Set definitions
  17. //
  18. HCRYPTOIDFUNCSET hSchemeRetrieveFuncSet;
  19. HCRYPTOIDFUNCSET hContextCreateFuncSet;
  20. HCRYPTOIDFUNCSET hGetObjectUrlFuncSet;
  21. HCRYPTOIDFUNCSET hGetTimeValidObjectFuncSet;
  22. HCRYPTOIDFUNCSET hFlushTimeValidObjectFuncSet;
  23. static const CRYPT_OID_FUNC_ENTRY SchemeRetrieveFuncTable[] = {
  24. LDAP_SCHEME, LdapRetrieveEncodedObject,
  25. HTTP_SCHEME, InetRetrieveEncodedObject,
  26. FILE_SCHEME, FileRetrieveEncodedObject
  27. };
  28. static const CRYPT_OID_FUNC_ENTRY ContextCreateFuncTable[] = {
  29. CONTEXT_OID_CERTIFICATE, CertificateCreateObjectContext,
  30. CONTEXT_OID_CTL, CTLCreateObjectContext,
  31. CONTEXT_OID_CRL, CRLCreateObjectContext,
  32. CONTEXT_OID_PKCS7, Pkcs7CreateObjectContext,
  33. CONTEXT_OID_CAPI2_ANY, Capi2CreateObjectContext
  34. };
  35. static const CRYPT_OID_FUNC_ENTRY GetObjectUrlFuncTable[] = {
  36. URL_OID_CERTIFICATE_ISSUER, CertificateIssuerGetObjectUrl,
  37. URL_OID_CERTIFICATE_CRL_DIST_POINT, CertificateCrlDistPointGetObjectUrl,
  38. URL_OID_CTL_ISSUER, CtlIssuerGetObjectUrl,
  39. URL_OID_CTL_NEXT_UPDATE, CtlNextUpdateGetObjectUrl,
  40. URL_OID_CRL_ISSUER, CrlIssuerGetObjectUrl,
  41. URL_OID_CERTIFICATE_FRESHEST_CRL, CertificateFreshestCrlGetObjectUrl,
  42. URL_OID_CRL_FRESHEST_CRL, CrlFreshestCrlGetObjectUrl,
  43. URL_OID_CROSS_CERT_DIST_POINT, CertificateCrossCertDistPointGetObjectUrl
  44. };
  45. static const CRYPT_OID_FUNC_ENTRY GetTimeValidObjectFuncTable[] = {
  46. TIME_VALID_OID_GET_CTL, CtlGetTimeValidObject,
  47. TIME_VALID_OID_GET_CRL, CrlGetTimeValidObject,
  48. TIME_VALID_OID_GET_CRL_FROM_CERT, CrlFromCertGetTimeValidObject,
  49. TIME_VALID_OID_GET_FRESHEST_CRL_FROM_CERT, FreshestCrlFromCertGetTimeValidObject,
  50. TIME_VALID_OID_GET_FRESHEST_CRL_FROM_CRL, FreshestCrlFromCrlGetTimeValidObject
  51. };
  52. static const CRYPT_OID_FUNC_ENTRY FlushTimeValidObjectFuncTable[] = {
  53. TIME_VALID_OID_FLUSH_CTL, CtlFlushTimeValidObject,
  54. TIME_VALID_OID_FLUSH_CRL, CrlFlushTimeValidObject,
  55. TIME_VALID_OID_FLUSH_CRL_FROM_CERT, CrlFromCertFlushTimeValidObject,
  56. TIME_VALID_OID_FLUSH_FRESHEST_CRL_FROM_CERT, FreshestCrlFromCertFlushTimeValidObject,
  57. TIME_VALID_OID_FLUSH_FRESHEST_CRL_FROM_CRL, FreshestCrlFromCrlFlushTimeValidObject
  58. };
  59. #define SCHEME_RETRIEVE_FUNC_COUNT (sizeof(SchemeRetrieveFuncTable)/ \
  60. sizeof(SchemeRetrieveFuncTable[0]))
  61. #define CONTEXT_CREATE_FUNC_COUNT (sizeof(ContextCreateFuncTable)/ \
  62. sizeof(ContextCreateFuncTable[0]))
  63. #define GET_OBJECT_URL_FUNC_COUNT (sizeof(GetObjectUrlFuncTable)/ \
  64. sizeof(GetObjectUrlFuncTable[0]))
  65. #define GET_TIME_VALID_OBJECT_FUNC_COUNT (sizeof(GetTimeValidObjectFuncTable)/ \
  66. sizeof(GetTimeValidObjectFuncTable[0]))
  67. #define FLUSH_TIME_VALID_OBJECT_FUNC_COUNT (sizeof(FlushTimeValidObjectFuncTable)/ \
  68. sizeof(FlushTimeValidObjectFuncTable[0]))
  69. HCRYPTTLS hCryptNetCancelTls;
  70. CTVOAgent* g_pProcessTVOAgent = NULL;
  71. CRITICAL_SECTION MSCtlDefaultStoresCriticalSection;
  72. extern void MSCtlCloseDefaultStores ();
  73. static
  74. VOID
  75. WINAPI
  76. CancelRetrievalFree(
  77. IN LPVOID pv
  78. )
  79. {
  80. if (pv)
  81. free(pv);
  82. }
  83. //+---------------------------------------------------------------------------
  84. //
  85. // Function: CryptInstallCancelRetrieval
  86. //
  87. // Synopsis: Install the call back function to cancel object retrieval
  88. // by HTTP, HTTPS, GOPHER, and FTP protocols.
  89. //
  90. //----------------------------------------------------------------------------
  91. BOOL
  92. WINAPI
  93. CryptInstallCancelRetrieval(
  94. IN PFN_CRYPT_CANCEL_RETRIEVAL pfnCancel,
  95. IN const void *pvArg,
  96. IN DWORD dwFlags,
  97. IN void *pvReserved
  98. )
  99. {
  100. PCRYPTNET_CANCEL_BLOCK pCancelBlock=NULL;
  101. if(NULL == pfnCancel)
  102. {
  103. SetLastError((DWORD) E_INVALIDARG);
  104. return FALSE;
  105. }
  106. pCancelBlock = (PCRYPTNET_CANCEL_BLOCK)malloc(sizeof(CRYPTNET_CANCEL_BLOCK));
  107. if(NULL == pCancelBlock)
  108. {
  109. SetLastError((DWORD) E_OUTOFMEMORY);
  110. return FALSE;
  111. }
  112. pCancelBlock->pfnCancel=pfnCancel;
  113. pCancelBlock->pvArg=(void *)pvArg;
  114. //uninstall the previous one
  115. if(!CryptUninstallCancelRetrieval(0, NULL))
  116. {
  117. free(pCancelBlock);
  118. return FALSE;
  119. }
  120. if(!I_CryptSetTls(hCryptNetCancelTls, pCancelBlock))
  121. {
  122. free(pCancelBlock);
  123. return FALSE;
  124. }
  125. return TRUE;
  126. }
  127. //+---------------------------------------------------------------------------
  128. //
  129. // Function: CryptUninstallCancelRetrieval
  130. //
  131. // Synopsis: Uninstall the call back function to cancel object retrieval
  132. // by HTTP, HTTPS, GOPHER, and FTP protocols.
  133. //
  134. //----------------------------------------------------------------------------
  135. BOOL
  136. WINAPI
  137. CryptUninstallCancelRetrieval(
  138. IN DWORD dwFlags,
  139. IN void *pvReserved
  140. )
  141. {
  142. PCRYPTNET_CANCEL_BLOCK pCancelBlock=NULL;
  143. //we just free the memory if there is one
  144. pCancelBlock = (PCRYPTNET_CANCEL_BLOCK)I_CryptGetTls(hCryptNetCancelTls);
  145. if(pCancelBlock)
  146. {
  147. free(pCancelBlock);
  148. I_CryptSetTls(hCryptNetCancelTls, NULL);
  149. }
  150. return TRUE;
  151. }
  152. //+---------------------------------------------------------------------------
  153. //
  154. // Function: RPORDllMain
  155. //
  156. // Synopsis: DLL Main like initialization of Remote PKI object retrieval
  157. //
  158. //----------------------------------------------------------------------------
  159. BOOL WINAPI RPORDllMain (
  160. HMODULE hModule,
  161. ULONG ulReason,
  162. LPVOID pvReserved
  163. )
  164. {
  165. switch ( ulReason )
  166. {
  167. case DLL_PROCESS_ATTACH:
  168. hSchemeRetrieveFuncSet = CryptInitOIDFunctionSet(
  169. SCHEME_OID_RETRIEVE_ENCODED_OBJECTW_FUNC,
  170. 0
  171. );
  172. hContextCreateFuncSet = CryptInitOIDFunctionSet(
  173. CONTEXT_OID_CREATE_OBJECT_CONTEXT_FUNC,
  174. 0
  175. );
  176. hGetObjectUrlFuncSet = CryptInitOIDFunctionSet(
  177. URL_OID_GET_OBJECT_URL_FUNC,
  178. 0
  179. );
  180. hGetTimeValidObjectFuncSet = CryptInitOIDFunctionSet(
  181. TIME_VALID_OID_GET_OBJECT_FUNC,
  182. 0
  183. );
  184. hFlushTimeValidObjectFuncSet = CryptInitOIDFunctionSet(
  185. TIME_VALID_OID_FLUSH_OBJECT_FUNC,
  186. 0
  187. );
  188. if ( ( hSchemeRetrieveFuncSet == NULL ) ||
  189. ( hContextCreateFuncSet == NULL ) ||
  190. ( hGetObjectUrlFuncSet == NULL ) ||
  191. ( hGetTimeValidObjectFuncSet == NULL ) ||
  192. ( hFlushTimeValidObjectFuncSet == NULL ) )
  193. {
  194. return( FALSE );
  195. }
  196. if ( CryptInstallOIDFunctionAddress(
  197. hModule,
  198. X509_ASN_ENCODING,
  199. SCHEME_OID_RETRIEVE_ENCODED_OBJECTW_FUNC,
  200. SCHEME_RETRIEVE_FUNC_COUNT,
  201. SchemeRetrieveFuncTable,
  202. 0
  203. ) == FALSE )
  204. {
  205. return( FALSE );
  206. }
  207. if ( CryptInstallOIDFunctionAddress(
  208. hModule,
  209. X509_ASN_ENCODING,
  210. CONTEXT_OID_CREATE_OBJECT_CONTEXT_FUNC,
  211. CONTEXT_CREATE_FUNC_COUNT,
  212. ContextCreateFuncTable,
  213. 0
  214. ) == FALSE )
  215. {
  216. return( FALSE );
  217. }
  218. if ( CryptInstallOIDFunctionAddress(
  219. hModule,
  220. X509_ASN_ENCODING,
  221. URL_OID_GET_OBJECT_URL_FUNC,
  222. GET_OBJECT_URL_FUNC_COUNT,
  223. GetObjectUrlFuncTable,
  224. 0
  225. ) == FALSE )
  226. {
  227. return( FALSE );
  228. }
  229. if ( CryptInstallOIDFunctionAddress(
  230. hModule,
  231. X509_ASN_ENCODING,
  232. TIME_VALID_OID_GET_OBJECT_FUNC,
  233. GET_TIME_VALID_OBJECT_FUNC_COUNT,
  234. GetTimeValidObjectFuncTable,
  235. 0
  236. ) == FALSE )
  237. {
  238. return( FALSE );
  239. }
  240. if ( CryptInstallOIDFunctionAddress(
  241. hModule,
  242. X509_ASN_ENCODING,
  243. TIME_VALID_OID_FLUSH_OBJECT_FUNC,
  244. FLUSH_TIME_VALID_OBJECT_FUNC_COUNT,
  245. FlushTimeValidObjectFuncTable,
  246. 0
  247. ) == FALSE )
  248. {
  249. return( FALSE );
  250. }
  251. hCryptNetCancelTls = I_CryptAllocTls();
  252. if (hCryptNetCancelTls == NULL )
  253. return( FALSE );
  254. InitializeOfflineUrlCache();
  255. InitializeCryptRetrieveObjectByUrl(hModule);
  256. if ( CreateProcessTVOAgent( &g_pProcessTVOAgent ) == FALSE )
  257. {
  258. return( FALSE );
  259. }
  260. if ( !Pki_InitializeCriticalSection(
  261. &MSCtlDefaultStoresCriticalSection ) )
  262. {
  263. return( FALSE );
  264. }
  265. return( TRUE );
  266. break;
  267. case DLL_THREAD_ATTACH:
  268. break;
  269. case DLL_PROCESS_DETACH:
  270. I_CryptFreeTls( hCryptNetCancelTls, CancelRetrievalFree );
  271. delete g_pProcessTVOAgent;
  272. DeleteCryptRetrieveObjectByUrl();
  273. DeleteOfflineUrlCache();
  274. MSCtlCloseDefaultStores();
  275. DeleteCriticalSection( &MSCtlDefaultStoresCriticalSection );
  276. break;
  277. case DLL_THREAD_DETACH:
  278. CancelRetrievalFree(I_CryptDetachTls(hCryptNetCancelTls));
  279. break;
  280. }
  281. return( TRUE );
  282. }
  283. //+---------------------------------------------------------------------------
  284. //
  285. // Function: RPORDllRegisterServer
  286. //
  287. // Synopsis: DllRegisterServer like registration of RPOR functions
  288. //
  289. //----------------------------------------------------------------------------
  290. STDAPI RPORDllRegisterServer (HMODULE hModule)
  291. {
  292. CHAR pszDll[MAX_PATH+1];
  293. WCHAR pwszDll[MAX_PATH+1];
  294. LPSTR pszDllRel = NULL;
  295. if ( GetModuleFileNameA( hModule, pszDll, MAX_PATH ) == 0 )
  296. {
  297. return( HRESULT_FROM_WIN32( GetLastError() ) );
  298. }
  299. pszDll[MAX_PATH] = '\0';
  300. pszDllRel = strrchr( pszDll, '\\' );
  301. assert( pszDllRel != NULL );
  302. pszDllRel += 1;
  303. if ( MultiByteToWideChar(
  304. CP_ACP,
  305. 0,
  306. pszDllRel,
  307. -1,
  308. pwszDll,
  309. MAX_PATH+1
  310. ) == 0 )
  311. {
  312. return( HRESULT_FROM_WIN32( GetLastError() ) );
  313. }
  314. if ( ( CryptRegisterDefaultOIDFunction(
  315. X509_ASN_ENCODING,
  316. CRYPT_OID_VERIFY_REVOCATION_FUNC,
  317. CRYPT_REGISTER_FIRST_INDEX,
  318. pwszDll
  319. ) == FALSE ) && ( GetLastError() != ERROR_FILE_EXISTS ) )
  320. {
  321. return( HRESULT_FROM_WIN32( GetLastError() ) );
  322. }
  323. if ( ( CryptRegisterDefaultOIDFunction(
  324. X509_ASN_ENCODING,
  325. CRYPT_OID_VERIFY_CTL_USAGE_FUNC,
  326. CRYPT_REGISTER_FIRST_INDEX,
  327. pwszDll
  328. ) == FALSE ) && ( GetLastError() != ERROR_FILE_EXISTS ) )
  329. {
  330. return( HRESULT_FROM_WIN32( GetLastError() ) );
  331. }
  332. if ( ( CryptRegisterOIDFunction(
  333. 0,
  334. CRYPT_OID_OPEN_STORE_PROV_FUNC,
  335. sz_CERT_STORE_PROV_LDAP,
  336. pwszDll,
  337. LDAP_OPEN_STORE_PROV_FUNC
  338. ) == FALSE ) && ( GetLastError() != ERROR_FILE_EXISTS ) )
  339. {
  340. return( GetLastError() );
  341. }
  342. if ( ( CryptRegisterOIDFunction(
  343. 0,
  344. CRYPT_OID_OPEN_STORE_PROV_FUNC,
  345. CERT_STORE_PROV_LDAP,
  346. pwszDll,
  347. LDAP_OPEN_STORE_PROV_FUNC
  348. ) == FALSE ) && ( GetLastError() != ERROR_FILE_EXISTS ) )
  349. {
  350. return( GetLastError() );
  351. }
  352. return( S_OK );
  353. }
  354. //+---------------------------------------------------------------------------
  355. //
  356. // Function: RPORDllUnregisterServer
  357. //
  358. // Synopsis: DllUnregisterServer like registration of RPOR functions
  359. //
  360. //----------------------------------------------------------------------------
  361. STDAPI RPORDllUnregisterServer (HMODULE hModule)
  362. {
  363. CHAR pszDll[MAX_PATH+1];
  364. WCHAR pwszDll[MAX_PATH+1];
  365. LPSTR pszDllRel = NULL;
  366. if ( GetModuleFileNameA( hModule, pszDll, MAX_PATH ) == 0 )
  367. {
  368. return( HRESULT_FROM_WIN32( GetLastError() ) );
  369. }
  370. pszDll[MAX_PATH] = '\0';
  371. pszDllRel = strrchr( pszDll, '\\' );
  372. assert( pszDllRel != NULL );
  373. pszDllRel += 1;
  374. if ( MultiByteToWideChar(
  375. CP_ACP,
  376. 0,
  377. pszDllRel,
  378. -1,
  379. pwszDll,
  380. MAX_PATH+1
  381. ) == 0 )
  382. {
  383. return( HRESULT_FROM_WIN32( GetLastError() ) );
  384. }
  385. if ( ( CryptUnregisterDefaultOIDFunction(
  386. X509_ASN_ENCODING,
  387. CRYPT_OID_VERIFY_REVOCATION_FUNC,
  388. pwszDll
  389. ) == FALSE ) && ( GetLastError() != ERROR_FILE_NOT_FOUND ) )
  390. {
  391. return( HRESULT_FROM_WIN32( GetLastError() ) );
  392. }
  393. if ( ( CryptUnregisterDefaultOIDFunction(
  394. X509_ASN_ENCODING,
  395. CRYPT_OID_VERIFY_CTL_USAGE_FUNC,
  396. pwszDll
  397. ) == FALSE ) && ( GetLastError() != ERROR_FILE_NOT_FOUND ) )
  398. {
  399. return( HRESULT_FROM_WIN32( GetLastError() ) );
  400. }
  401. if ( ( CryptUnregisterOIDFunction(
  402. 0,
  403. CRYPT_OID_OPEN_STORE_PROV_FUNC,
  404. sz_CERT_STORE_PROV_LDAP
  405. ) == FALSE ) && ( GetLastError() != ERROR_FILE_NOT_FOUND ) )
  406. {
  407. return( GetLastError() );
  408. }
  409. if ( ( CryptUnregisterOIDFunction(
  410. 0,
  411. CRYPT_OID_OPEN_STORE_PROV_FUNC,
  412. CERT_STORE_PROV_LDAP
  413. ) == FALSE ) && ( GetLastError() != ERROR_FILE_NOT_FOUND ) )
  414. {
  415. return( GetLastError() );
  416. }
  417. return( S_OK );
  418. }
  419. //+---------------------------------------------------------------------------
  420. //
  421. // Function: RPORDllRegUnregServer
  422. //
  423. // Synopsis: reg unreg server entry point for RPOR
  424. //
  425. //----------------------------------------------------------------------------
  426. HRESULT WINAPI RPORDllRegUnregServer (HMODULE hModule, BOOL fRegUnreg)
  427. {
  428. if ( fRegUnreg == TRUE )
  429. {
  430. return( RPORDllRegisterServer( hModule ) );
  431. }
  432. return( RPORDllUnregisterServer( hModule ) );
  433. }