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.

253 lines
4.8 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: certdec.cpp
  7. //
  8. // Contents: Cert Server main & debug support
  9. //
  10. // History: 25-Jul-96 vich created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include <pch.cpp>
  14. #pragma hdrstop
  15. #include <tchar.h>
  16. #if DBG_CERTSRV
  17. # ifdef UNICODE
  18. # define szFMTTSTR "ws"
  19. # else
  20. # define szFMTTSTR "hs"
  21. # endif
  22. #endif
  23. // Read and decode uuencoded file into allocated memory.
  24. HRESULT
  25. DecodeFileW(
  26. IN TCHAR const *pszfn,
  27. OUT BYTE **ppbOut,
  28. OUT DWORD *pcbOut,
  29. IN DWORD Flags)
  30. {
  31. HANDLE hFile;
  32. HRESULT hr;
  33. CHAR *pchFile = NULL;
  34. BYTE *pbOut = NULL;
  35. DWORD cchFile;
  36. DWORD cbRead;
  37. DWORD cbOut;
  38. hFile = CreateFile(
  39. pszfn,
  40. GENERIC_READ,
  41. FILE_SHARE_READ,
  42. NULL,
  43. OPEN_EXISTING,
  44. 0,
  45. NULL);
  46. if (INVALID_HANDLE_VALUE == hFile)
  47. {
  48. hr = myHLastError();
  49. _JumpError(hr, error, "CreateFile");
  50. }
  51. cchFile = GetFileSize(hFile, NULL);
  52. if ((DWORD) -1 == cchFile)
  53. {
  54. hr = myHLastError();
  55. _JumpError(hr, error, "GetFileSize");
  56. }
  57. pchFile = (CHAR *) LocalAlloc(LMEM_FIXED, cchFile);
  58. if (NULL == pchFile)
  59. {
  60. hr = E_OUTOFMEMORY;
  61. _JumpError(hr, error, "LocalAlloc");
  62. }
  63. if (!ReadFile(hFile, pchFile, cchFile, &cbRead, NULL))
  64. {
  65. hr = myHLastError();
  66. _JumpError(hr, error, "ReadFile");
  67. }
  68. CSASSERT(cbRead <= cchFile);
  69. if (cbRead != cchFile)
  70. {
  71. hr = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
  72. DBGPRINT((
  73. DBG_SS_ERROR,
  74. "ReadFile read %u bytes, requested %u\n",
  75. cbRead,
  76. cchFile));
  77. _JumpError(hr, error, "ReadFile(cbRead)");
  78. }
  79. if (CRYPT_STRING_BINARY == Flags)
  80. {
  81. pbOut = (BYTE *) pchFile;
  82. cbOut = cchFile;
  83. pchFile = NULL;
  84. }
  85. else
  86. {
  87. // Decode file contents.
  88. hr = myCryptStringToBinaryA(
  89. pchFile,
  90. cchFile,
  91. Flags,
  92. &pbOut,
  93. &cbOut,
  94. NULL,
  95. NULL);
  96. _JumpIfError2(
  97. hr,
  98. error,
  99. "myCryptStringToBinaryA",
  100. HRESULT_FROM_WIN32(ERROR_INVALID_DATA));
  101. }
  102. *pcbOut = cbOut;
  103. *ppbOut = pbOut;
  104. pbOut = NULL;
  105. hr = S_OK;
  106. error:
  107. if (INVALID_HANDLE_VALUE != hFile)
  108. {
  109. CloseHandle(hFile);
  110. }
  111. if (NULL != pchFile)
  112. {
  113. LocalFree(pchFile);
  114. }
  115. if (S_OK != hr)
  116. {
  117. #if DBG_CERTSRV
  118. if (HRESULT_FROM_WIN32(ERROR_INVALID_DATA) != hr)
  119. {
  120. WCHAR awchr[cwcHRESULTSTRING];
  121. DBGPRINT((
  122. DBG_SS_ERROR,
  123. "DecodeFileW(%" szFMTTSTR "): error = %ws\n",
  124. pszfn,
  125. myHResultToString(awchr, hr)));
  126. }
  127. #endif
  128. if (NULL != pbOut)
  129. {
  130. LocalFree(pbOut);
  131. }
  132. }
  133. return(hr);
  134. }
  135. HRESULT
  136. EncodeToFileW(
  137. IN TCHAR const *pszfn,
  138. IN BYTE const *pbIn,
  139. IN DWORD cbIn,
  140. IN DWORD Flags)
  141. {
  142. HANDLE hFile = INVALID_HANDLE_VALUE;
  143. HRESULT hr;
  144. DWORD cbWritten;
  145. DWORD cchFile;
  146. CHAR *pchFile = NULL;
  147. BOOL fForceOverWrite;
  148. fForceOverWrite = 0 != (DECF_FORCEOVERWRITE & Flags);
  149. Flags &= ~DECF_FORCEOVERWRITE;
  150. if (CRYPT_STRING_BINARY == Flags)
  151. {
  152. pchFile = (CHAR *) pbIn;
  153. cchFile = cbIn;
  154. }
  155. else
  156. {
  157. hr = myCryptBinaryToStringA(pbIn, cbIn, Flags, &pchFile);
  158. _JumpIfError(hr, error, "myCryptBinaryToStringA");
  159. cchFile = strlen(pchFile);
  160. }
  161. // Write encoded certificate to file
  162. hFile = CreateFile(
  163. pszfn,
  164. GENERIC_WRITE,
  165. 0,
  166. NULL,
  167. CREATE_NEW,
  168. 0,
  169. NULL);
  170. if (INVALID_HANDLE_VALUE == hFile)
  171. {
  172. hr = myHLastError();
  173. if (fForceOverWrite && HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) == hr)
  174. {
  175. hFile = CreateFile(
  176. pszfn,
  177. GENERIC_WRITE,
  178. 0,
  179. NULL,
  180. CREATE_ALWAYS,
  181. 0,
  182. NULL);
  183. }
  184. if (INVALID_HANDLE_VALUE == hFile)
  185. {
  186. hr = myHLastError();
  187. _JumpError(hr, error, "CreateFile");
  188. }
  189. }
  190. if (!WriteFile(hFile, pchFile, cchFile, &cbWritten, NULL))
  191. {
  192. hr = myHLastError();
  193. _JumpError(hr, error, "WriteFile");
  194. }
  195. if (cbWritten != cchFile)
  196. {
  197. hr = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
  198. DBGPRINT((
  199. DBG_SS_ERROR,
  200. "WriteFile wrote %u bytes, requested %u\n",
  201. cbWritten,
  202. cchFile));
  203. _JumpError(hr, error, "WriteFile(cbWritten)");
  204. }
  205. hr = S_OK;
  206. error:
  207. if (INVALID_HANDLE_VALUE != hFile)
  208. {
  209. CloseHandle(hFile);
  210. }
  211. if (CRYPT_STRING_BINARY != Flags && NULL != pchFile)
  212. {
  213. LocalFree(pchFile);
  214. }
  215. #if DBG_CERTSRV
  216. if (S_OK != hr)
  217. {
  218. WCHAR awchr[cwcHRESULTSTRING];
  219. DBGPRINT((
  220. DBG_SS_ERROR,
  221. "EncodeToFileW(%" szFMTTSTR "): error = %ws\n",
  222. pszfn,
  223. myHResultToString(awchr, hr)));
  224. }
  225. #endif
  226. return(hr);
  227. }