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.

265 lines
6.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1999
  6. //
  7. // File: Certhlp.cpp
  8. //
  9. // Contents: Certificate store management tools helper functions
  10. //
  11. //
  12. // History: July 21st xiaohs created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "certmgr.h"
  16. //+-------------------------------------------------------------------------
  17. // GetSignAlgids
  18. //--------------------------------------------------------------------------
  19. void GetSignAlgids(
  20. IN LPCSTR pszOID,
  21. OUT ALG_ID *paiHash,
  22. OUT ALG_ID *paiPubKey
  23. )
  24. {
  25. PCCRYPT_OID_INFO pInfo;
  26. *paiHash = 0;
  27. *paiPubKey = 0;
  28. if (pInfo = CryptFindOIDInfo(
  29. CRYPT_OID_INFO_OID_KEY,
  30. (void *) pszOID,
  31. CRYPT_SIGN_ALG_OID_GROUP_ID
  32. )) {
  33. DWORD cExtra = pInfo->ExtraInfo.cbData / sizeof(DWORD);
  34. DWORD *pdwExtra = (DWORD *) pInfo->ExtraInfo.pbData;
  35. *paiHash = pInfo->Algid;
  36. if (1 <= cExtra)
  37. *paiPubKey = pdwExtra[0];
  38. }
  39. }
  40. //+-------------------------------------------------------------------------
  41. // GetAlgid
  42. //--------------------------------------------------------------------------
  43. ALG_ID GetAlgid(LPCSTR pszOID, DWORD dwGroupId)
  44. {
  45. PCCRYPT_OID_INFO pInfo;
  46. if (pInfo = CryptFindOIDInfo(
  47. CRYPT_OID_INFO_OID_KEY,
  48. (void *) pszOID,
  49. dwGroupId
  50. ))
  51. return pInfo->Algid;
  52. return 0;
  53. }
  54. //+-------------------------------------------------------------------------
  55. //+-------------------------------------------------------------------------
  56. // Allocates and returns the specified cryptographic message parameter.
  57. //--------------------------------------------------------------------------
  58. void *AllocAndGetMsgParam(
  59. IN HCRYPTMSG hMsg,
  60. IN DWORD dwParamType,
  61. IN DWORD dwIndex,
  62. OUT DWORD *pcbData
  63. )
  64. {
  65. void *pvData;
  66. DWORD cbData;
  67. if (!CryptMsgGetParam(
  68. hMsg,
  69. dwParamType,
  70. dwIndex,
  71. NULL, // pvData
  72. &cbData) || 0 == cbData)
  73. goto ErrorReturn;
  74. if (NULL == (pvData = ToolUtlAlloc(cbData)))
  75. goto ErrorReturn;
  76. if (!CryptMsgGetParam(
  77. hMsg,
  78. dwParamType,
  79. dwIndex,
  80. pvData,
  81. &cbData)) {
  82. ToolUtlFree(pvData);
  83. goto ErrorReturn;
  84. }
  85. CommonReturn:
  86. *pcbData = cbData;
  87. return pvData;
  88. ErrorReturn:
  89. pvData = NULL;
  90. cbData = 0;
  91. goto CommonReturn;
  92. }
  93. ////////////////////////////////////////////////////////
  94. //
  95. // Convert STR to WSTR
  96. //
  97. HRESULT SZtoWSZ(LPSTR szStr,LPWSTR *pwsz)
  98. {
  99. DWORD dwSize=0;
  100. DWORD dwError=0;
  101. assert(pwsz);
  102. *pwsz=NULL;
  103. //return NULL
  104. if(!szStr)
  105. return S_OK;
  106. dwSize=MultiByteToWideChar(0, 0,szStr, -1,NULL,0);
  107. if(dwSize==0)
  108. {
  109. dwError=GetLastError();
  110. return HRESULT_FROM_WIN32(dwError);
  111. }
  112. //allocate memory
  113. *pwsz=(LPWSTR)ToolUtlAlloc(dwSize * sizeof(WCHAR));
  114. if(*pwsz==NULL)
  115. return E_OUTOFMEMORY;
  116. if(MultiByteToWideChar(0, 0,szStr, -1,
  117. *pwsz,dwSize))
  118. {
  119. return S_OK;
  120. }
  121. else
  122. {
  123. ToolUtlFree(*pwsz);
  124. dwError=GetLastError();
  125. return HRESULT_FROM_WIN32(dwError);
  126. }
  127. }
  128. //+-------------------------------------------------------------------------
  129. // Decode the object and allocate memory
  130. //--------------------------------------------------------------------------
  131. void *TestNoCopyDecodeObject(
  132. IN LPCSTR lpszStructType,
  133. IN const BYTE *pbEncoded,
  134. IN DWORD cbEncoded,
  135. OUT DWORD *pcbInfo
  136. )
  137. {
  138. BOOL fResult;
  139. DWORD cbInfo;
  140. void *pvInfo;
  141. if (pcbInfo)
  142. *pcbInfo = 0;
  143. // Set to bogus value. pvInfo == NULL, should cause it to be ignored.
  144. cbInfo = 0x12345678;
  145. fResult = CryptDecodeObject(
  146. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
  147. lpszStructType,
  148. pbEncoded,
  149. cbEncoded,
  150. CRYPT_DECODE_NOCOPY_FLAG,
  151. NULL, // pvInfo
  152. &cbInfo
  153. );
  154. if (!fResult || cbInfo == 0)
  155. return NULL;
  156. if (NULL == (pvInfo = ToolUtlAlloc(cbInfo)))
  157. return NULL;
  158. if (!CryptDecodeObject(
  159. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
  160. lpszStructType,
  161. pbEncoded,
  162. cbEncoded,
  163. CRYPT_DECODE_NOCOPY_FLAG,
  164. pvInfo,
  165. &cbInfo
  166. ))
  167. {
  168. ToolUtlFree(pvInfo);
  169. return NULL;
  170. }
  171. if (pcbInfo)
  172. *pcbInfo = cbInfo;
  173. return pvInfo;
  174. }
  175. //+-------------------------------------------------------------------------
  176. // Returns TRUE if the CTL is still time valid.
  177. //
  178. // A CTL without a NextUpdate is considered time valid.
  179. //--------------------------------------------------------------------------
  180. BOOL IsTimeValidCtl(
  181. IN PCCTL_CONTEXT pCtl
  182. )
  183. {
  184. PCTL_INFO pCtlInfo = pCtl->pCtlInfo;
  185. SYSTEMTIME SystemTime;
  186. FILETIME CurrentTime;
  187. // Get current time to be used to determine if CTLs are time valid
  188. GetSystemTime(&SystemTime);
  189. SystemTimeToFileTime(&SystemTime, &CurrentTime);
  190. // Note, NextUpdate is optional. When not present, its set to 0
  191. if ((0 == pCtlInfo->NextUpdate.dwLowDateTime &&
  192. 0 == pCtlInfo->NextUpdate.dwHighDateTime) ||
  193. CompareFileTime(&pCtlInfo->NextUpdate, &CurrentTime) >= 0)
  194. return TRUE;
  195. else
  196. return FALSE;
  197. }
  198. //+-------------------------------------------------------------------------
  199. // Display serial number
  200. //
  201. //--------------------------------------------------------------------------
  202. void DisplaySerialNumber(
  203. PCRYPT_INTEGER_BLOB pSerialNumber
  204. )
  205. {
  206. DWORD cb;
  207. BYTE *pb;
  208. for (cb = pSerialNumber->cbData,
  209. pb = pSerialNumber->pbData + (cb - 1); cb > 0; cb--, pb--) {
  210. printf(" %02X", *pb);
  211. }
  212. }
  213. //+-------------------------------------------------------------------------
  214. // Reverses a buffer of bytes in place
  215. //--------------------------------------------------------------------------
  216. void
  217. ReverseBytes(
  218. IN OUT PBYTE pbIn,
  219. IN DWORD cbIn
  220. )
  221. {
  222. // reverse in place
  223. PBYTE pbLo;
  224. PBYTE pbHi;
  225. BYTE bTmp;
  226. for (pbLo = pbIn, pbHi = pbIn + cbIn - 1; pbLo < pbHi; pbHi--, pbLo++) {
  227. bTmp = *pbHi;
  228. *pbHi = *pbLo;
  229. *pbLo = bTmp;
  230. }
  231. }