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.

271 lines
6.6 KiB

  1. /*++
  2. Copyright (C) 1996, 1997 Microsoft Corporation
  3. Module Name:
  4. nt5wrap.cpp
  5. Abstract:
  6. Client side CryptXXXData calls.
  7. Client funcs are preceeded by "CS" == Client Side
  8. Server functions are preceeded by "SS" == Server Side
  9. Author:
  10. Scott Field (sfield) 14-Aug-97
  11. Revisions:
  12. Todds 04-Sep-97 Ported to .dll
  13. Matt Thomlinson (mattt) 09-Oct-97 Moved to common area for link by crypt32
  14. philh 03-Dec-97 Added I_CertProtectFunction
  15. philh 29-Sep-98 Renamed I_CertProtectFunction to
  16. I_CertCltProtectFunction.
  17. I_CertProtectFunction was moved to
  18. ..\ispu\pki\certstor\protroot.cpp
  19. petesk 25-Jan-00 Moved to keysvc
  20. --*/
  21. #include <windows.h>
  22. #include <wincrypt.h>
  23. #include <cryptui.h>
  24. #include "unicode.h"
  25. #include "waitsvc.h"
  26. #include "certprot.h"
  27. // midl generated files
  28. #include "keyrpc.h"
  29. #include "lenroll.h"
  30. #include "keysvc.h"
  31. #include "keysvcc.h"
  32. #include "cerrpc.h"
  33. // fwds
  34. RPC_STATUS CertBindA(
  35. RPC_BINDING_HANDLE *phBind
  36. );
  37. RPC_STATUS CertUnbindA(
  38. RPC_BINDING_HANDLE *phBind
  39. );
  40. BOOL
  41. WINAPI
  42. I_CertCltProtectFunction(
  43. IN DWORD dwFuncId,
  44. IN DWORD dwFlags,
  45. IN OPTIONAL LPCWSTR pwszIn,
  46. IN OPTIONAL BYTE *pbIn,
  47. IN DWORD cbIn,
  48. OUT OPTIONAL BYTE **ppbOut,
  49. OUT OPTIONAL DWORD *pcbOut
  50. )
  51. {
  52. BOOL fResult;
  53. DWORD dwRetVal;
  54. RPC_BINDING_HANDLE h = NULL;
  55. RPC_STATUS RpcStatus;
  56. BYTE *pbSSOut = NULL;
  57. DWORD cbSSOut = 0;
  58. BYTE rgbIn[1];
  59. if (NULL == pwszIn)
  60. pwszIn = L"";
  61. if (NULL == pbIn) {
  62. pbIn = rgbIn;
  63. cbIn = 0;
  64. }
  65. if (!FIsWinNT5()) {
  66. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  67. goto ErrorReturn;
  68. }
  69. RpcStatus = CertBindA(&h);
  70. if (RPC_S_OK != RpcStatus) {
  71. SetLastError(RpcStatus);
  72. goto ErrorReturn;
  73. }
  74. __try {
  75. dwRetVal = SSCertProtectFunction(
  76. h,
  77. dwFuncId,
  78. dwFlags,
  79. pwszIn,
  80. pbIn,
  81. cbIn,
  82. &pbSSOut,
  83. &cbSSOut
  84. );
  85. } __except(EXCEPTION_EXECUTE_HANDLER) {
  86. dwRetVal = GetExceptionCode();
  87. }
  88. CertUnbindA(&h);
  89. if (ERROR_SUCCESS != dwRetVal) {
  90. if (RPC_S_UNKNOWN_IF == dwRetVal)
  91. dwRetVal = ERROR_CALL_NOT_IMPLEMENTED;
  92. SetLastError(dwRetVal);
  93. goto ErrorReturn;
  94. }
  95. fResult = TRUE;
  96. CommonReturn:
  97. if (ppbOut)
  98. *ppbOut = pbSSOut;
  99. else if (pbSSOut)
  100. midl_user_free(pbSSOut);
  101. if (pcbOut)
  102. *pcbOut = cbSSOut;
  103. return fResult;
  104. ErrorReturn:
  105. fResult = FALSE;
  106. goto CommonReturn;
  107. }
  108. static RPC_STATUS CertBindA(RPC_BINDING_HANDLE *phBind)
  109. {
  110. static BOOL fDone = FALSE;
  111. RPC_STATUS RpcStatus = RPC_S_OK;
  112. unsigned char * pszBinding = NULL;
  113. RPC_BINDING_HANDLE hBind = NULL;
  114. RPC_SECURITY_QOS RpcSecurityQOS;
  115. SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
  116. PSID pSID = NULL;
  117. WCHAR szName[64];
  118. DWORD cbName = 64;
  119. WCHAR szDomainName[256]; // max domain is 255
  120. DWORD cbDomainName = 256;
  121. SID_NAME_USE Use;
  122. //
  123. // wait for the service to be available before attempting bind
  124. //
  125. WaitForCryptService(L"CryptSvc", &fDone);
  126. RpcStatus = RpcStringBindingComposeA(
  127. NULL,
  128. (unsigned char*)KEYSVC_LOCAL_PROT_SEQ,
  129. NULL,
  130. (unsigned char*)KEYSVC_LOCAL_ENDPOINT,
  131. NULL,
  132. &pszBinding
  133. );
  134. if (RPC_S_OK != RpcStatus)
  135. goto ErrorReturn;
  136. RpcStatus = RpcBindingFromStringBindingA(pszBinding, &hBind);
  137. if (RPC_S_OK != RpcStatus)
  138. goto ErrorReturn;
  139. RpcStatus = RpcEpResolveBinding(
  140. hBind,
  141. ICertProtectFunctions_v1_0_c_ifspec
  142. );
  143. if (RPC_S_OK != RpcStatus)
  144. goto ErrorReturn;
  145. //
  146. // Set the autorization so that we will only call a Local Service process
  147. //
  148. memset(&RpcSecurityQOS, 0, sizeof(RpcSecurityQOS));
  149. RpcSecurityQOS.Version = RPC_C_SECURITY_QOS_VERSION;
  150. RpcSecurityQOS.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH;
  151. RpcSecurityQOS.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
  152. RpcSecurityQOS.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE;
  153. if (AllocateAndInitializeSid(&SIDAuth, 1,
  154. SECURITY_LOCAL_SYSTEM_RID,
  155. 0, 0, 0, 0, 0, 0, 0,
  156. &pSID) == 0)
  157. {
  158. RpcStatus = RPC_S_OUT_OF_MEMORY;
  159. goto ErrorReturn;
  160. }
  161. if (LookupAccountSidW(NULL,
  162. pSID,
  163. szName,
  164. &cbName,
  165. szDomainName,
  166. &cbDomainName,
  167. &Use) == 0)
  168. {
  169. RpcStatus = RPC_S_UNKNOWN_PRINCIPAL;
  170. goto ErrorReturn;
  171. }
  172. RpcStatus = RpcBindingSetAuthInfoExW(
  173. hBind,
  174. szName,
  175. RPC_C_AUTHN_LEVEL_PKT,
  176. RPC_C_AUTHN_WINNT,
  177. NULL,
  178. 0,
  179. &RpcSecurityQOS
  180. );
  181. if (RPC_S_OK != RpcStatus)
  182. goto ErrorReturn;
  183. CommonReturn:
  184. if (NULL != pszBinding) {
  185. RpcStringFreeA(&pszBinding);
  186. }
  187. if (NULL != pSID) {
  188. FreeSid(pSID);
  189. }
  190. *phBind = hBind;
  191. return RpcStatus;
  192. ErrorReturn:
  193. if (NULL != hBind)
  194. {
  195. RpcBindingFree(&hBind);
  196. hBind = NULL;
  197. }
  198. goto CommonReturn;
  199. }
  200. static RPC_STATUS CertUnbindA(RPC_BINDING_HANDLE *phBind)
  201. {
  202. RPC_STATUS RpcStatus;
  203. if (NULL != *phBind) {
  204. RpcStatus = RpcBindingFree(phBind);
  205. } else {
  206. RpcStatus = RPC_S_OK;
  207. }
  208. return RpcStatus;
  209. }