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.

216 lines
3.7 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: crypt.cpp
  7. //
  8. // Contents: Cert Server wrapper routines
  9. //
  10. // History: 17-Oct-96 vich created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include <pch.cpp>
  14. #pragma hdrstop
  15. HRESULT
  16. myCryptStringToBinaryA(
  17. IN LPCSTR pszString,
  18. IN DWORD cchString,
  19. IN DWORD dwFlags,
  20. OUT BYTE **ppbBinary,
  21. OUT DWORD *pcbBinary,
  22. OUT DWORD *pdwSkip, // OPTIONAL
  23. OUT DWORD *pdwFlags) // OPTIONAL
  24. {
  25. HRESULT hr;
  26. BYTE *pb = NULL;
  27. DWORD cb;
  28. while (TRUE)
  29. {
  30. if (!CryptStringToBinaryA(
  31. pszString,
  32. cchString,
  33. dwFlags,
  34. pb,
  35. &cb,
  36. pdwSkip,
  37. pdwFlags))
  38. {
  39. hr = myHLastError();
  40. _JumpError2(
  41. hr,
  42. error,
  43. "CryptStringToBinaryA",
  44. HRESULT_FROM_WIN32(ERROR_INVALID_DATA));
  45. }
  46. if (NULL != pb)
  47. {
  48. break; // done
  49. }
  50. pb = (BYTE *) LocalAlloc(LMEM_FIXED, cb);
  51. if (NULL == pb)
  52. {
  53. hr = E_OUTOFMEMORY;
  54. _JumpError(hr, error, "LocalAlloc");
  55. }
  56. }
  57. *pcbBinary = cb;
  58. *ppbBinary = pb;
  59. pb = NULL;
  60. hr = S_OK;
  61. error:
  62. if (NULL != pb)
  63. {
  64. LocalFree(pb);
  65. }
  66. return(hr);
  67. }
  68. HRESULT
  69. myCryptStringToBinary(
  70. IN LPCWSTR pwszString,
  71. IN DWORD cwcString,
  72. IN DWORD dwFlags,
  73. OUT BYTE **ppbBinary,
  74. OUT DWORD *pcbBinary,
  75. OUT DWORD *pdwSkip, // OPTIONAL
  76. OUT DWORD *pdwFlags) // OPTIONAL
  77. {
  78. HRESULT hr;
  79. BYTE *pb = NULL;
  80. DWORD cb;
  81. while (TRUE)
  82. {
  83. if (!CryptStringToBinaryW(
  84. pwszString,
  85. cwcString,
  86. dwFlags,
  87. pb,
  88. &cb,
  89. pdwSkip,
  90. pdwFlags))
  91. {
  92. hr = myHLastError();
  93. _JumpError(hr, error, "CryptStringToBinaryW");
  94. }
  95. if (NULL != pb)
  96. {
  97. break; // done
  98. }
  99. pb = (BYTE *) LocalAlloc(LMEM_FIXED, cb);
  100. if (NULL == pb)
  101. {
  102. hr = E_OUTOFMEMORY;
  103. _JumpError(hr, error, "LocalAlloc");
  104. }
  105. }
  106. *pcbBinary = cb;
  107. *ppbBinary = pb;
  108. pb = NULL;
  109. hr = S_OK;
  110. error:
  111. if (NULL != pb)
  112. {
  113. LocalFree(pb);
  114. }
  115. return(hr);
  116. }
  117. HRESULT
  118. myCryptBinaryToStringA(
  119. IN CONST BYTE *pbBinary,
  120. IN DWORD cbBinary,
  121. IN DWORD dwFlags,
  122. OUT LPSTR *ppszString)
  123. {
  124. HRESULT hr;
  125. char *psz = NULL;
  126. DWORD cch;
  127. while (TRUE)
  128. {
  129. if (!CryptBinaryToStringA(pbBinary, cbBinary, dwFlags, psz, &cch))
  130. {
  131. hr = myHLastError();
  132. _JumpError(hr, error, "CryptBinaryToStringA");
  133. }
  134. if (NULL != psz)
  135. {
  136. break; // done
  137. }
  138. psz = (char *) LocalAlloc(LMEM_FIXED, cch * sizeof(char));
  139. if (NULL == psz)
  140. {
  141. hr = E_OUTOFMEMORY;
  142. _JumpError(hr, error, "LocalAlloc");
  143. }
  144. }
  145. *ppszString = psz;
  146. psz = NULL;
  147. hr = S_OK;
  148. error:
  149. if (NULL != psz)
  150. {
  151. LocalFree(psz);
  152. }
  153. return(hr);
  154. }
  155. HRESULT
  156. myCryptBinaryToString(
  157. IN CONST BYTE *pbBinary,
  158. IN DWORD cbBinary,
  159. IN DWORD dwFlags,
  160. OUT LPWSTR *ppwszString)
  161. {
  162. HRESULT hr;
  163. WCHAR *pwsz = NULL;
  164. DWORD cwc;
  165. DWORD cwc0;
  166. while (TRUE)
  167. {
  168. if (!CryptBinaryToStringW(pbBinary, cbBinary, dwFlags, pwsz, &cwc))
  169. {
  170. hr = myHLastError();
  171. _JumpError(hr, error, "CryptBinaryToStringW");
  172. }
  173. if (NULL != pwsz)
  174. {
  175. break; // done
  176. }
  177. cwc0 = cwc;
  178. pwsz = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  179. if (NULL == pwsz)
  180. {
  181. hr = E_OUTOFMEMORY;
  182. _JumpError(hr, error, "LocalAlloc");
  183. }
  184. }
  185. CSASSERT(cwc0 == cwc + 1);
  186. CSASSERT(L'\0' == pwsz[cwc]);
  187. *ppwszString = pwsz;
  188. pwsz = NULL;
  189. hr = S_OK;
  190. error:
  191. if (NULL != pwsz)
  192. {
  193. LocalFree(pwsz);
  194. }
  195. return(hr);
  196. }