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.

735 lines
20 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <ntsecapi.h>
  6. #include <wincrypt.h>
  7. #include <userenv.h>
  8. #include <lmcons.h>
  9. #include <certca.h>
  10. #include "keysvc.h"
  11. #include "cryptui.h"
  12. #include "lenroll.h"
  13. #include "keysvcc.h"
  14. DWORD BindLocalKeyService(handle_t *hProxy);
  15. // key service stub functions
  16. ULONG s_KeyrOpenKeyService(
  17. /* [in] */ handle_t hRPCBinding,
  18. /* [in] */ KEYSVC_TYPE OwnerType,
  19. /* [in] */ PKEYSVC_UNICODE_STRING pOwnerName,
  20. /* [in] */ ULONG ulDesiredAccess,
  21. /* [in] */ PKEYSVC_BLOB pAuthentication,
  22. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  23. /* [out] */ KEYSVC_HANDLE *phKeySvc)
  24. {
  25. RPC_BINDING_HANDLE hProxy = NULL;
  26. RPC_STATUS RpcStatus = RPC_S_OK;
  27. RpcStatus = RpcImpersonateClient(hRPCBinding);
  28. if (RPC_S_OK != RpcStatus)
  29. {
  30. return RpcStatus;
  31. }
  32. RpcStatus = BindLocalKeyService(&hProxy);
  33. if(RPC_S_OK != RpcStatus)
  34. {
  35. goto error;
  36. }
  37. __try
  38. {
  39. RpcStatus = s_KeyrOpenKeyService(
  40. hProxy,
  41. OwnerType,
  42. pOwnerName,
  43. ulDesiredAccess,
  44. pAuthentication,
  45. ppReserved,
  46. phKeySvc);
  47. }
  48. __except ( EXCEPTION_EXECUTE_HANDLER )
  49. {
  50. RpcStatus = _exception_code();
  51. }
  52. error:
  53. if(hProxy)
  54. {
  55. RpcBindingFree(&hProxy);
  56. }
  57. RpcRevertToSelf();
  58. return RpcStatus;
  59. }
  60. ULONG s_KeyrEnumerateProviders(
  61. /* [in] */ handle_t hRPCBinding,
  62. /* [in] */ KEYSVC_HANDLE hKeySvc,
  63. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  64. /* [in, out] */ ULONG *pcProviderCount,
  65. /* [in, out][size_is(,*pcProviderCount)] */
  66. PKEYSVC_PROVIDER_INFO *ppProviders)
  67. {
  68. RPC_BINDING_HANDLE hProxy = NULL;
  69. RPC_STATUS RpcStatus = RPC_S_OK;
  70. RpcStatus = RpcImpersonateClient(hRPCBinding);
  71. if (RPC_S_OK != RpcStatus)
  72. {
  73. return RpcStatus;
  74. }
  75. RpcStatus = BindLocalKeyService(&hProxy);
  76. if(RPC_S_OK != RpcStatus)
  77. {
  78. goto error;
  79. }
  80. __try
  81. {
  82. RpcStatus = s_KeyrEnumerateProviders(
  83. hProxy,
  84. hKeySvc,
  85. ppReserved,
  86. pcProviderCount,
  87. ppProviders);
  88. }
  89. __except ( EXCEPTION_EXECUTE_HANDLER )
  90. {
  91. RpcStatus = _exception_code();
  92. }
  93. error:
  94. if(hProxy)
  95. {
  96. RpcBindingFree(&hProxy);
  97. }
  98. RpcRevertToSelf();
  99. return RpcStatus;
  100. }
  101. ULONG s_KeyrEnumerateProviderTypes(
  102. /* [in] */ handle_t hRPCBinding,
  103. /* [in] */ KEYSVC_HANDLE hKeySvc,
  104. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  105. /* [in, out] */ ULONG *pcProviderCount,
  106. /* [in, out][size_is(,*pcProviderCount)] */
  107. PKEYSVC_PROVIDER_INFO *ppProviders)
  108. {
  109. return ERROR_CALL_NOT_IMPLEMENTED;
  110. }
  111. ULONG s_KeyrEnumerateProvContainers(
  112. /* [in] */ handle_t hRPCBinding,
  113. /* [in] */ KEYSVC_HANDLE hKeySvc,
  114. /* [in] */ KEYSVC_PROVIDER_INFO Provider,
  115. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  116. /* [in, out] */ ULONG *pcContainerCount,
  117. /* [in, out][size_is(,*pcContainerCount)] */
  118. PKEYSVC_UNICODE_STRING *ppContainers)
  119. {
  120. return ERROR_CALL_NOT_IMPLEMENTED;
  121. }
  122. ULONG s_KeyrCloseKeyService(
  123. /* [in] */ handle_t hRPCBinding,
  124. /* [in] */ KEYSVC_HANDLE hKeySvc,
  125. /* [in, out] */ PKEYSVC_BLOB *ppReserved)
  126. {
  127. RPC_BINDING_HANDLE hProxy = NULL;
  128. RPC_STATUS RpcStatus = RPC_S_OK;
  129. RpcStatus = RpcImpersonateClient(hRPCBinding);
  130. if (RPC_S_OK != RpcStatus)
  131. {
  132. return RpcStatus;
  133. }
  134. RpcStatus = BindLocalKeyService(&hProxy);
  135. if(RPC_S_OK != RpcStatus)
  136. {
  137. goto error;
  138. }
  139. __try
  140. {
  141. RpcStatus = s_KeyrCloseKeyService(
  142. hProxy,
  143. hKeySvc,
  144. ppReserved);
  145. }
  146. __except ( EXCEPTION_EXECUTE_HANDLER )
  147. {
  148. RpcStatus = _exception_code();
  149. }
  150. error:
  151. if(hProxy)
  152. {
  153. RpcBindingFree(&hProxy);
  154. }
  155. RpcRevertToSelf();
  156. return RpcStatus;
  157. }
  158. ULONG s_KeyrGetDefaultProvider(
  159. /* [in] */ handle_t hRPCBinding,
  160. /* [in] */ KEYSVC_HANDLE hKeySvc,
  161. /* [in] */ ULONG ulProvType,
  162. /* [in] */ ULONG ulFlags,
  163. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  164. /* [out] */ ULONG *pulDefType,
  165. /* [out] */ PKEYSVC_PROVIDER_INFO *ppProvider)
  166. {
  167. RPC_BINDING_HANDLE hProxy = NULL;
  168. RPC_STATUS RpcStatus = RPC_S_OK;
  169. RpcStatus = RpcImpersonateClient(hRPCBinding);
  170. if (RPC_S_OK != RpcStatus)
  171. {
  172. return RpcStatus;
  173. }
  174. RpcStatus = BindLocalKeyService(&hProxy);
  175. if(RPC_S_OK != RpcStatus)
  176. {
  177. goto error;
  178. }
  179. __try
  180. {
  181. RpcStatus = s_KeyrGetDefaultProvider(
  182. hProxy,
  183. hKeySvc,
  184. ulProvType,
  185. ulFlags,
  186. ppReserved,
  187. pulDefType,
  188. ppProvider);
  189. }
  190. __except ( EXCEPTION_EXECUTE_HANDLER )
  191. {
  192. RpcStatus = _exception_code();
  193. }
  194. error:
  195. if(hProxy)
  196. {
  197. RpcBindingFree(&hProxy);
  198. }
  199. RpcRevertToSelf();
  200. return RpcStatus;
  201. }
  202. ULONG s_KeyrSetDefaultProvider(
  203. /* [in] */ handle_t hRPCBinding,
  204. /* [in] */ KEYSVC_HANDLE hKeySvc,
  205. /* [in] */ ULONG ulFlags,
  206. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  207. /* [in] */ KEYSVC_PROVIDER_INFO Provider)
  208. {
  209. return ERROR_CALL_NOT_IMPLEMENTED;
  210. }
  211. ULONG s_KeyrEnroll(
  212. /* [in] */ handle_t hRPCBinding,
  213. /* [in] */ BOOL fKeyService,
  214. /* [in] */ ULONG ulPurpose,
  215. /* [in] */ PKEYSVC_UNICODE_STRING pAcctName,
  216. /* [in] */ PKEYSVC_UNICODE_STRING pCALocation,
  217. /* [in] */ PKEYSVC_UNICODE_STRING pCAName,
  218. /* [in] */ BOOL fNewKey,
  219. /* [in] */ PKEYSVC_CERT_REQUEST_PVK_NEW pKeyNew,
  220. /* [in] */ PKEYSVC_BLOB __RPC_FAR pCert,
  221. /* [in] */ PKEYSVC_CERT_REQUEST_PVK_NEW pRenewKey,
  222. /* [in] */ PKEYSVC_UNICODE_STRING pHashAlg,
  223. /* [in] */ PKEYSVC_UNICODE_STRING pDesStore,
  224. /* [in] */ ULONG ulStoreFlags,
  225. /* [in] */ PKEYSVC_CERT_ENROLL_INFO pRequestInfo,
  226. /* [in] */ ULONG ulFlags,
  227. /* [out][in] */ PKEYSVC_BLOB __RPC_FAR *ppReserved,
  228. /* [out] */ PKEYSVC_BLOB __RPC_FAR *ppPKCS7Blob,
  229. /* [out] */ PKEYSVC_BLOB __RPC_FAR *ppHashBlob,
  230. /* [out] */ ULONG __RPC_FAR *pulStatus)
  231. {
  232. RPC_BINDING_HANDLE hProxy = NULL;
  233. RPC_STATUS RpcStatus = RPC_S_OK;
  234. RpcStatus = RpcImpersonateClient(hRPCBinding);
  235. if (RPC_S_OK != RpcStatus)
  236. {
  237. return RpcStatus;
  238. }
  239. RpcStatus = BindLocalKeyService(&hProxy);
  240. if(RPC_S_OK != RpcStatus)
  241. {
  242. goto error;
  243. }
  244. __try
  245. {
  246. RpcStatus = s_KeyrEnroll(
  247. hProxy,
  248. fKeyService,
  249. ulPurpose,
  250. pAcctName,
  251. pCALocation,
  252. pCAName,
  253. fNewKey,
  254. pKeyNew,
  255. pCert,
  256. pRenewKey,
  257. pHashAlg,
  258. pDesStore,
  259. ulStoreFlags,
  260. pRequestInfo,
  261. ulFlags,
  262. ppReserved,
  263. ppPKCS7Blob,
  264. ppHashBlob,
  265. pulStatus);
  266. }
  267. __except ( EXCEPTION_EXECUTE_HANDLER )
  268. {
  269. RpcStatus = _exception_code();
  270. }
  271. error:
  272. if(hProxy)
  273. {
  274. RpcBindingFree(&hProxy);
  275. }
  276. RpcRevertToSelf();
  277. return RpcStatus;
  278. }
  279. ULONG s_KeyrExportCert(
  280. /* [in] */ handle_t hRPCBinding,
  281. /* [in] */ KEYSVC_HANDLE hKeySvc,
  282. /* [in] */ PKEYSVC_UNICODE_STRING pPassword,
  283. /* [in] */ PKEYSVC_UNICODE_STRING pCertStore,
  284. /* [in] */ ULONG cHashCount,
  285. /* [size_is][in] */
  286. KEYSVC_CERT_HASH *pHashes,
  287. /* [in] */ ULONG ulFlags,
  288. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  289. /* [out] */ PKEYSVC_BLOB *ppPFXBlob)
  290. {
  291. return ERROR_CALL_NOT_IMPLEMENTED;
  292. }
  293. ULONG s_KeyrImportCert(
  294. /* [in] */ handle_t hRPCBinding,
  295. /* [in] */ KEYSVC_HANDLE hKeySvc,
  296. /* [in] */ PKEYSVC_UNICODE_STRING pPassword,
  297. /* [in] */ KEYSVC_UNICODE_STRING *pCertStore,
  298. /* [in] */ PKEYSVC_BLOB pPFXBlob,
  299. /* [in] */ ULONG ulFlags,
  300. /* [in, out] */ PKEYSVC_BLOB *ppReserved)
  301. {
  302. return ERROR_CALL_NOT_IMPLEMENTED;
  303. }
  304. ULONG s_KeyrEnumerateAvailableCertTypes(
  305. /* [in] */ handle_t hRPCBinding,
  306. /* [in] */ KEYSVC_HANDLE hKeySvc,
  307. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  308. /* [out][in] */ ULONG *pcCertTypeCount,
  309. /* [in, out][size_is(,*pcCertTypeCount)] */
  310. PKEYSVC_UNICODE_STRING *ppCertTypes)
  311. {
  312. RPC_BINDING_HANDLE hProxy = NULL;
  313. RPC_STATUS RpcStatus = RPC_S_OK;
  314. RpcStatus = RpcImpersonateClient(hRPCBinding);
  315. if (RPC_S_OK != RpcStatus)
  316. {
  317. return RpcStatus;
  318. }
  319. RpcStatus = BindLocalKeyService(&hProxy);
  320. if(RPC_S_OK != RpcStatus)
  321. {
  322. goto error;
  323. }
  324. __try
  325. {
  326. RpcStatus = s_KeyrEnumerateAvailableCertTypes(
  327. hProxy,
  328. hKeySvc,
  329. ppReserved,
  330. pcCertTypeCount,
  331. ppCertTypes);
  332. }
  333. __except ( EXCEPTION_EXECUTE_HANDLER )
  334. {
  335. RpcStatus = _exception_code();
  336. }
  337. error:
  338. if(hProxy)
  339. {
  340. RpcBindingFree(&hProxy);
  341. }
  342. RpcRevertToSelf();
  343. return RpcStatus;
  344. }
  345. ULONG s_KeyrEnumerateCAs(
  346. /* [in] */ handle_t hRPCBinding,
  347. /* [in] */ KEYSVC_HANDLE hKeySvc,
  348. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  349. /* [in] */ ULONG ulFlags,
  350. /* [out][in] */ ULONG *pcCACount,
  351. /* [in, out][size_is(,*pcCACount)] */
  352. PKEYSVC_UNICODE_STRING *ppCAs)
  353. {
  354. RPC_BINDING_HANDLE hProxy = NULL;
  355. RPC_STATUS RpcStatus = RPC_S_OK;
  356. RpcStatus = RpcImpersonateClient(hRPCBinding);
  357. if (RPC_S_OK != RpcStatus)
  358. {
  359. return RpcStatus;
  360. }
  361. RpcStatus = BindLocalKeyService(&hProxy);
  362. if(RPC_S_OK != RpcStatus)
  363. {
  364. goto error;
  365. }
  366. __try
  367. {
  368. RpcStatus = s_KeyrEnumerateCAs(
  369. hProxy,
  370. hKeySvc,
  371. ppReserved,
  372. ulFlags,
  373. pcCACount,
  374. ppCAs);
  375. }
  376. __except ( EXCEPTION_EXECUTE_HANDLER )
  377. {
  378. RpcStatus = _exception_code();
  379. }
  380. error:
  381. if(hProxy)
  382. {
  383. RpcBindingFree(&hProxy);
  384. }
  385. RpcRevertToSelf();
  386. return RpcStatus;
  387. }
  388. DWORD BindLocalKeyService(handle_t *hProxy)
  389. {
  390. WCHAR *pStringBinding = NULL;
  391. *hProxy = NULL;
  392. RPC_STATUS RpcStatus = RPC_S_OK;
  393. RpcStatus = RpcStringBindingComposeW(
  394. NULL,
  395. KEYSVC_LOCAL_PROT_SEQ,
  396. NULL,
  397. KEYSVC_LOCAL_ENDPOINT,
  398. NULL,
  399. &pStringBinding);
  400. if (RPC_S_OK != RpcStatus)
  401. {
  402. goto error;
  403. }
  404. RpcStatus = RpcBindingFromStringBindingW(
  405. pStringBinding,
  406. hProxy);
  407. if (RPC_S_OK != RpcStatus)
  408. {
  409. goto error;
  410. }
  411. RpcStatus = RpcEpResolveBinding(
  412. *hProxy,
  413. IKeySvc_v1_0_c_ifspec);
  414. if (RPC_S_OK != RpcStatus)
  415. {
  416. if(*hProxy)
  417. {
  418. RpcBindingFree(hProxy);
  419. *hProxy = NULL;
  420. }
  421. goto error;
  422. }
  423. error:
  424. if (NULL != pStringBinding)
  425. {
  426. RpcStringFreeW(&pStringBinding);
  427. }
  428. return RpcStatus;
  429. }
  430. ULONG s_KeyrEnroll_V2
  431. (/* [in] */ handle_t hRPCBinding,
  432. /* [in] */ BOOL fKeyService,
  433. /* [in] */ ULONG ulPurpose,
  434. /* [in] */ ULONG ulFlags,
  435. /* [in] */ PKEYSVC_UNICODE_STRING pAcctName,
  436. /* [in] */ PKEYSVC_UNICODE_STRING pCALocation,
  437. /* [in] */ PKEYSVC_UNICODE_STRING pCAName,
  438. /* [in] */ BOOL fNewKey,
  439. /* [in] */ PKEYSVC_CERT_REQUEST_PVK_NEW_V2 pKeyNew,
  440. /* [in] */ PKEYSVC_BLOB __RPC_FAR pCert,
  441. /* [in] */ PKEYSVC_CERT_REQUEST_PVK_NEW_V2 pRenewKey,
  442. /* [in] */ PKEYSVC_UNICODE_STRING pHashAlg,
  443. /* [in] */ PKEYSVC_UNICODE_STRING pDesStore,
  444. /* [in] */ ULONG ulStoreFlags,
  445. /* [in] */ PKEYSVC_CERT_ENROLL_INFO pRequestInfo,
  446. /* [in] */ ULONG ulReservedFlags,
  447. /* [out][in] */ PKEYSVC_BLOB __RPC_FAR *ppReserved,
  448. /* [out][in] */ PKEYSVC_BLOB __RPC_FAR *ppRequest,
  449. /* [out] */ PKEYSVC_BLOB __RPC_FAR *ppPKCS7Blob,
  450. /* [out] */ PKEYSVC_BLOB __RPC_FAR *ppHashBlob,
  451. /* [out] */ ULONG __RPC_FAR *pulStatus)
  452. {
  453. RPC_BINDING_HANDLE hProxy = NULL;
  454. RPC_STATUS RpcStatus = RPC_S_OK;
  455. RpcStatus = RpcImpersonateClient(hRPCBinding);
  456. if (RPC_S_OK != RpcStatus)
  457. {
  458. return RpcStatus;
  459. }
  460. RpcStatus = BindLocalKeyService(&hProxy);
  461. if(RPC_S_OK != RpcStatus)
  462. {
  463. goto error;
  464. }
  465. __try
  466. {
  467. RpcStatus = s_KeyrEnroll_V2(
  468. hProxy,
  469. fKeyService,
  470. ulPurpose,
  471. ulFlags,
  472. pAcctName,
  473. pCALocation,
  474. pCAName,
  475. fNewKey,
  476. pKeyNew,
  477. pCert,
  478. pRenewKey,
  479. pHashAlg,
  480. pDesStore,
  481. ulStoreFlags,
  482. pRequestInfo,
  483. ulReservedFlags,
  484. ppReserved,
  485. ppRequest,
  486. ppPKCS7Blob,
  487. ppHashBlob,
  488. pulStatus);
  489. }
  490. __except ( EXCEPTION_EXECUTE_HANDLER )
  491. {
  492. RpcStatus = _exception_code();
  493. }
  494. error:
  495. if(hProxy)
  496. {
  497. RpcBindingFree(&hProxy);
  498. }
  499. RpcRevertToSelf();
  500. return RpcStatus;
  501. }
  502. ULONG s_KeyrQueryRequestStatus
  503. (/* [in] */ handle_t hRPCBinding,
  504. /* [in] */ unsigned __int64 u64Request,
  505. /* [out, ref] */ KEYSVC_QUERY_CERT_REQUEST_INFO *pQueryInfo)
  506. {
  507. RPC_BINDING_HANDLE hProxy = NULL;
  508. RPC_STATUS RpcStatus = RPC_S_OK;
  509. RpcStatus = RpcImpersonateClient(hRPCBinding);
  510. if (RPC_S_OK != RpcStatus)
  511. {
  512. return RpcStatus;
  513. }
  514. RpcStatus = BindLocalKeyService(&hProxy);
  515. if(RPC_S_OK != RpcStatus)
  516. {
  517. goto error;
  518. }
  519. __try
  520. {
  521. RpcStatus = s_KeyrQueryRequestStatus(
  522. hProxy,
  523. u64Request,
  524. pQueryInfo);
  525. }
  526. __except ( EXCEPTION_EXECUTE_HANDLER )
  527. {
  528. RpcStatus = _exception_code();
  529. }
  530. error:
  531. if(hProxy)
  532. {
  533. RpcBindingFree(&hProxy);
  534. }
  535. RpcRevertToSelf();
  536. return RpcStatus;
  537. }
  538. ULONG s_RKeyrPFXInstall
  539. (/* [in] */ handle_t hRPCBinding,
  540. /* [in] */ PKEYSVC_BLOB pPFX,
  541. /* [in] */ PKEYSVC_UNICODE_STRING pPassword,
  542. /* [in] */ ULONG ulFlags)
  543. {
  544. RPC_BINDING_HANDLE hProxy = NULL;
  545. RPC_STATUS RpcStatus = RPC_S_OK;
  546. RpcStatus = RpcImpersonateClient(hRPCBinding);
  547. if (RPC_S_OK != RpcStatus)
  548. {
  549. return RpcStatus;
  550. }
  551. RpcStatus = BindLocalKeyService(&hProxy);
  552. if(RPC_S_OK != RpcStatus)
  553. {
  554. goto error;
  555. }
  556. __try
  557. {
  558. RpcStatus = s_RKeyrPFXInstall(
  559. hProxy,
  560. pPFX,
  561. pPassword,
  562. ulFlags);
  563. }
  564. __except ( EXCEPTION_EXECUTE_HANDLER )
  565. {
  566. RpcStatus = _exception_code();
  567. }
  568. error:
  569. if(hProxy)
  570. {
  571. RpcBindingFree(&hProxy);
  572. }
  573. RpcRevertToSelf();
  574. return RpcStatus;
  575. }
  576. ULONG s_RKeyrOpenKeyService(
  577. /* [in] */ handle_t hRPCBinding,
  578. /* [in] */ KEYSVC_TYPE OwnerType,
  579. /* [in] */ PKEYSVC_UNICODE_STRING pOwnerName,
  580. /* [in] */ ULONG ulDesiredAccess,
  581. /* [in] */ PKEYSVC_BLOB pAuthentication,
  582. /* [in, out] */ PKEYSVC_BLOB *ppReserved,
  583. /* [out] */ KEYSVC_HANDLE *phKeySvc)
  584. {
  585. return s_KeyrOpenKeyService
  586. (hRPCBinding,
  587. OwnerType,
  588. pOwnerName,
  589. ulDesiredAccess,
  590. pAuthentication,
  591. ppReserved,
  592. phKeySvc);
  593. }
  594. ULONG s_RKeyrCloseKeyService(
  595. /* [in] */ handle_t hRPCBinding,
  596. /* [in] */ KEYSVC_HANDLE hKeySvc,
  597. /* [in, out] */ PKEYSVC_BLOB *ppReserved)
  598. {
  599. return s_KeyrCloseKeyService
  600. (hRPCBinding,
  601. hKeySvc,
  602. ppReserved);
  603. }