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.

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