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.

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