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.

350 lines
9.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: NameVal.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. // implements the Certificate Trust List & persistent storage
  11. //
  12. // Functions: CatalogEncodeNameValue
  13. // CatalogDecodeNameValue
  14. // CatalogCertExt2CryptAttr
  15. // CatalogCryptAttr2CertExt
  16. //
  17. // *** local functions ***
  18. // EncodeUserOID
  19. // DecodeUserOID
  20. //
  21. // History: 16-May-1997 pberkman created
  22. //
  23. //--------------------------------------------------------------------------
  24. #include "global.hxx"
  25. #include "mscat32.h"
  26. BOOL EncodeUserOID(CRYPTCATSTORE *pCatStore, CAT_NAMEVALUE *pNameValue);
  27. BOOL DecodeUserOID(CRYPTCATSTORE *pCatStore, CAT_NAMEVALUE *pNV, BYTE **ppbUserOIDDecode,
  28. DWORD *pcbUserOIDDecode);
  29. void CatalogCertExt2CryptAttr(CERT_EXTENSION *pCertExt, CRYPT_ATTRIBUTE *pCryptAttr)
  30. {
  31. memset(pCryptAttr, 0x00, sizeof(CRYPT_ATTRIBUTE));
  32. pCryptAttr->pszObjId = pCertExt->pszObjId;
  33. pCryptAttr->cValue = 1;
  34. pCryptAttr->rgValue = &pCertExt->Value;
  35. }
  36. void CatalogCryptAttr2CertExt(CRYPT_ATTRIBUTE *pCryptAttr, CERT_EXTENSION *pCertExt)
  37. {
  38. memset(pCertExt, 0x00, sizeof(CERT_EXTENSION));
  39. pCertExt->pszObjId = pCryptAttr->pszObjId;
  40. pCertExt->fCritical = FALSE;
  41. if ((pCryptAttr->cValue) && (pCryptAttr->rgValue))
  42. {
  43. memcpy(&pCertExt->Value, &pCryptAttr->rgValue[0], sizeof(CRYPT_ATTR_BLOB));
  44. }
  45. }
  46. BOOL CatalogEncodeNameValue(CRYPTCATSTORE *pCatStore, CRYPTCATATTRIBUTE *pAttr,
  47. PCRYPT_ATTRIBUTE pCryptAttr)
  48. {
  49. CAT_NAMEVALUE sNV;
  50. memset(&sNV, 0x00, sizeof(CAT_NAMEVALUE));
  51. sNV.pwszTag = pAttr->pwszReferenceTag;
  52. sNV.fdwFlags = pAttr->dwAttrTypeAndAction;
  53. sNV.Value.cbData = pAttr->cbValue;
  54. if (!(sNV.Value.pbData = (BYTE *)CatalogNew(sNV.Value.cbData)))
  55. {
  56. return(FALSE);
  57. }
  58. memcpy(sNV.Value.pbData, pAttr->pbValue, sNV.Value.cbData);
  59. if (pAttr->dwAttrTypeAndAction & CRYPTCAT_ATTR_NAMEOBJID)
  60. {
  61. if (!(EncodeUserOID(pCatStore, &sNV)))
  62. {
  63. delete sNV.Value.pbData;
  64. return(FALSE);
  65. }
  66. }
  67. pCryptAttr->pszObjId = CAT_NAMEVALUE_OBJID;
  68. pCryptAttr->rgValue->cbData = 0;
  69. CryptEncodeObject(pCatStore->dwEncodingType,
  70. pCryptAttr->pszObjId,
  71. &sNV,
  72. NULL,
  73. &pCryptAttr->rgValue->cbData);
  74. if (pCryptAttr->rgValue->cbData > 0)
  75. {
  76. if (!(pCryptAttr->rgValue->pbData = (BYTE *)CatalogNew(pCryptAttr->rgValue->cbData)))
  77. {
  78. delete sNV.Value.pbData;
  79. return(FALSE);
  80. }
  81. if (!(CryptEncodeObject(pCatStore->dwEncodingType,
  82. pCryptAttr->pszObjId,
  83. &sNV,
  84. pCryptAttr->rgValue->pbData,
  85. &pCryptAttr->rgValue->cbData)))
  86. {
  87. delete sNV.Value.pbData;
  88. DELETE_OBJECT(pCryptAttr->rgValue->pbData);
  89. pCryptAttr->rgValue->cbData = 0;
  90. return(FALSE);
  91. }
  92. delete sNV.Value.pbData;
  93. return(TRUE);
  94. }
  95. delete sNV.Value.pbData;
  96. return(FALSE);
  97. }
  98. BOOL CatalogDecodeNameValue(CRYPTCATSTORE *pCatStore, PCRYPT_ATTRIBUTE pCryptAttr,
  99. CRYPTCATATTRIBUTE *pCatAttr)
  100. {
  101. CAT_NAMEVALUE *pNV;
  102. DWORD cbDecoded;
  103. cbDecoded = 0;
  104. CryptDecodeObject( pCatStore->dwEncodingType,
  105. CAT_NAMEVALUE_STRUCT,
  106. pCryptAttr->rgValue->pbData,
  107. pCryptAttr->rgValue->cbData,
  108. 0,
  109. NULL,
  110. &cbDecoded);
  111. if (cbDecoded > 0)
  112. {
  113. if (!(pNV = (CAT_NAMEVALUE *)CatalogNew(cbDecoded)))
  114. {
  115. return(FALSE);
  116. }
  117. if (!(CryptDecodeObject( pCatStore->dwEncodingType,
  118. CAT_NAMEVALUE_STRUCT,
  119. pCryptAttr->rgValue->pbData,
  120. pCryptAttr->rgValue->cbData,
  121. 0,
  122. pNV,
  123. &cbDecoded)))
  124. {
  125. delete pNV;
  126. return(FALSE);
  127. }
  128. if (!(pCatAttr->pwszReferenceTag = (LPWSTR)CatalogNew((wcslen(pNV->pwszTag) + 1) * sizeof(WCHAR))))
  129. {
  130. delete pNV;
  131. return(FALSE);
  132. }
  133. wcscpy(pCatAttr->pwszReferenceTag, pNV->pwszTag);
  134. pCatAttr->dwAttrTypeAndAction = pNV->fdwFlags;
  135. if (pCatAttr->dwAttrTypeAndAction & CRYPTCAT_ATTR_NAMEOBJID)
  136. {
  137. DWORD cbUserOIDDecode;
  138. BYTE *pbUserOIDDecode;
  139. if (!(DecodeUserOID(pCatStore, pNV, &pbUserOIDDecode, &cbUserOIDDecode)))
  140. {
  141. delete pNV;
  142. return(FALSE);
  143. }
  144. delete pNV;
  145. pCatAttr->pbValue = pbUserOIDDecode;
  146. pCatAttr->cbValue = cbUserOIDDecode;
  147. return(TRUE);
  148. }
  149. if (!(pCatAttr->pbValue = (BYTE *)CatalogNew(pNV->Value.cbData)))
  150. {
  151. delete pNV;
  152. return(FALSE);
  153. }
  154. memcpy(pCatAttr->pbValue, pNV->Value.pbData, pNV->Value.cbData);
  155. pCatAttr->cbValue = pNV->Value.cbData;
  156. delete pNV;
  157. return(TRUE);
  158. }
  159. return(FALSE);
  160. }
  161. BOOL EncodeUserOID(CRYPTCATSTORE *pCatStore, CAT_NAMEVALUE *pNameValue)
  162. {
  163. DWORD cbEncoded;
  164. BYTE *pbEncoded;
  165. DWORD cbConv;
  166. LPSTR pszObjId;
  167. pbEncoded = NULL;
  168. cbConv = WideCharToMultiByte(0, 0,
  169. pNameValue->pwszTag, wcslen(pNameValue->pwszTag) + 1,
  170. NULL, 0, NULL, NULL);
  171. if (cbConv < 1)
  172. {
  173. return(FALSE);
  174. }
  175. if (!(pszObjId = (LPSTR)CatalogNew(cbConv + 1)))
  176. {
  177. return(FALSE);
  178. }
  179. WideCharToMultiByte(0, 0,
  180. pNameValue->pwszTag, wcslen(pNameValue->pwszTag) + 1,
  181. pszObjId, cbConv, NULL, NULL);
  182. pszObjId[cbConv] = NULL;
  183. cbEncoded = 0;
  184. CryptEncodeObject(pCatStore->dwEncodingType,
  185. pszObjId,
  186. pNameValue->Value.pbData,
  187. NULL,
  188. &cbEncoded);
  189. if (cbEncoded > 0)
  190. {
  191. if (!(pbEncoded = (BYTE *)CatalogNew(cbEncoded)))
  192. {
  193. delete pszObjId;
  194. return(FALSE);
  195. }
  196. if (!(CryptEncodeObject(pCatStore->dwEncodingType,
  197. pszObjId,
  198. pNameValue->Value.pbData,
  199. pbEncoded,
  200. &cbEncoded)))
  201. {
  202. delete pszObjId;
  203. delete pbEncoded;
  204. return(FALSE);
  205. }
  206. }
  207. delete pszObjId;
  208. DELETE_OBJECT(pNameValue->Value.pbData);
  209. pNameValue->Value.pbData = pbEncoded;
  210. pNameValue->Value.cbData = cbEncoded;
  211. return(TRUE);
  212. }
  213. BOOL DecodeUserOID(CRYPTCATSTORE *pCatStore, CAT_NAMEVALUE *pNV, BYTE **ppbUserOIDDecode,
  214. DWORD *pcbUserOIDDecode)
  215. {
  216. *ppbUserOIDDecode = NULL;
  217. *pcbUserOIDDecode = 0;
  218. DWORD cbConv;
  219. LPSTR pszObjId;
  220. cbConv = WideCharToMultiByte(0, 0,
  221. pNV->pwszTag, wcslen(pNV->pwszTag) + 1,
  222. NULL, 0, NULL, NULL);
  223. if (cbConv < 1)
  224. {
  225. return(FALSE);
  226. }
  227. if (!(pszObjId = (LPSTR)CatalogNew(cbConv + 1)))
  228. {
  229. return(FALSE);
  230. }
  231. WideCharToMultiByte(0, 0,
  232. pNV->pwszTag, wcslen(pNV->pwszTag) + 1,
  233. pszObjId, cbConv, NULL, NULL);
  234. pszObjId[cbConv] = NULL;
  235. CryptDecodeObject(pCatStore->dwEncodingType,
  236. pszObjId,
  237. pNV->Value.pbData,
  238. pNV->Value.cbData,
  239. 0,
  240. NULL,
  241. pcbUserOIDDecode);
  242. if (*pcbUserOIDDecode > 0)
  243. {
  244. if (!(*ppbUserOIDDecode = (BYTE *)CatalogNew(*pcbUserOIDDecode)))
  245. {
  246. delete pszObjId;
  247. return(FALSE);
  248. }
  249. if (!(CryptDecodeObject(pCatStore->dwEncodingType,
  250. pszObjId,
  251. pNV->Value.pbData,
  252. pNV->Value.cbData,
  253. 0,
  254. *ppbUserOIDDecode,
  255. pcbUserOIDDecode)))
  256. {
  257. delete pszObjId;
  258. DELETE_OBJECT(*ppbUserOIDDecode);
  259. *pcbUserOIDDecode = 0;
  260. return(FALSE);
  261. }
  262. return(TRUE);
  263. }
  264. delete pszObjId;
  265. return(FALSE);
  266. }