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.

298 lines
6.8 KiB

  1. // ObjectInfoFile.cpp: implementation of the CObjectInfoFile class.
  2. //
  3. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  4. // 1999. This computer program includes Confidential, Proprietary
  5. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  6. // use, disclosure, and/or reproduction is prohibited unless authorized
  7. // in writing. All Rights Reserved.
  8. //////////////////////////////////////////////////////////////////////
  9. #include "cciCard.h"
  10. #include "SymbolTable.h"
  11. #include "ObjectInfoFile.h"
  12. using std::string;
  13. using namespace cci;
  14. using namespace iop;
  15. CObjectInfoFile::CObjectInfoFile(CSmartCard &rSmartCard,
  16. string const &Path,
  17. ObjectAccess oa)
  18. : m_oa(oa),
  19. m_Path(Path),
  20. m_SymbolTable(rSmartCard, Path, ObjMasterBlkSize),
  21. m_rSmartCard(rSmartCard)
  22. {
  23. }
  24. void CObjectInfoFile::Reset()
  25. {
  26. BYTE bMasterBlk[ObjMasterBlkSize];
  27. memset(bMasterBlk,0,ObjMasterBlkSize);
  28. m_rSmartCard.Select(m_Path.c_str());
  29. m_rSmartCard.WriteBinary(0, ObjMasterBlkSize, bMasterBlk);
  30. m_bDefaultContainer = 0;
  31. m_bFirstContainer = 0;
  32. m_bFirstCertificate = 0;
  33. m_bFirstPublicKey = 0;
  34. m_bFirstPrivateKey = 0;
  35. m_bFirstDataObject = 0;
  36. m_SymbolTable.Reset();
  37. }
  38. void CObjectInfoFile::UpdateCache()
  39. {
  40. BYTE bMasterBlk[ObjMasterBlkSize];
  41. m_rSmartCard.Select(m_Path.c_str());
  42. m_rSmartCard.ReadBinary(0, ObjMasterBlkSize, bMasterBlk);
  43. m_bDefaultContainer = bMasterBlk[ObjDefaultContainerLoc];
  44. m_bFirstContainer = bMasterBlk[ObjFirstContainerLoc];
  45. m_bFirstCertificate = bMasterBlk[ObjFirstCertificateLoc];
  46. m_bFirstPublicKey = bMasterBlk[ObjFirstPublicKeyLoc];
  47. m_bFirstPrivateKey = bMasterBlk[ObjFirstPrivateKeyLoc];
  48. m_bFirstDataObject = bMasterBlk[ObjFirstDataObjectLoc];
  49. }
  50. ObjectAccess
  51. CObjectInfoFile::AccessType() const
  52. {
  53. return m_oa;
  54. }
  55. void CObjectInfoFile::DefaultContainer(SymbolID bHandle)
  56. {
  57. m_rSmartCard.Select(m_Path.c_str());
  58. m_rSmartCard.WriteBinary(ObjDefaultContainerLoc, 1, &bHandle);
  59. m_bDefaultContainer = bHandle;
  60. }
  61. SymbolID CObjectInfoFile::AddSymbol(string aString)
  62. {
  63. return m_SymbolTable.Add(aString);
  64. }
  65. string CObjectInfoFile::FindSymbol(SymbolID sid)
  66. {
  67. return m_SymbolTable.Find(sid);
  68. }
  69. void CObjectInfoFile::RemoveSymbol(SymbolID sid)
  70. {
  71. m_SymbolTable.Remove(sid);
  72. }
  73. SymbolID CObjectInfoFile::FirstObject(ObjectType type) const
  74. {
  75. SymbolID sid;
  76. switch(type)
  77. {
  78. case otContainerObject:
  79. sid = m_bFirstContainer;
  80. break;
  81. case otCertificateObject:
  82. sid = m_bFirstCertificate;
  83. break;
  84. case otPublicKeyObject:
  85. sid = m_bFirstPublicKey;
  86. break;
  87. case otPrivateKeyObject:
  88. sid = m_bFirstPrivateKey;
  89. break;
  90. case otDataObjectObject:
  91. sid = m_bFirstDataObject;
  92. break;
  93. default:
  94. throw cci::Exception(ccBadObjectType);
  95. break;
  96. }
  97. return sid;
  98. }
  99. void CObjectInfoFile::FirstObject(ObjectType type, SymbolID bHandle)
  100. {
  101. unsigned short sLoc;
  102. SymbolID *pbCachedValue;
  103. switch(type) {
  104. case otContainerObject:
  105. sLoc = ObjFirstContainerLoc;
  106. pbCachedValue = &m_bFirstContainer;
  107. break;
  108. case otCertificateObject:
  109. sLoc = ObjFirstCertificateLoc;
  110. pbCachedValue = &m_bFirstCertificate;
  111. break;
  112. case otPublicKeyObject:
  113. sLoc = ObjFirstPublicKeyLoc;
  114. pbCachedValue = &m_bFirstPublicKey;
  115. break;
  116. case otPrivateKeyObject:
  117. sLoc = ObjFirstPrivateKeyLoc;
  118. pbCachedValue = &m_bFirstPrivateKey;
  119. break;
  120. case otDataObjectObject:
  121. sLoc = ObjFirstDataObjectLoc;
  122. pbCachedValue = &m_bFirstDataObject;
  123. break;
  124. default:
  125. throw cci::Exception(ccBadObjectType);
  126. }
  127. m_rSmartCard.Select(m_Path.c_str());
  128. m_rSmartCard.WriteBinary(sLoc, 1, &bHandle);
  129. *pbCachedValue = bHandle; // Update cache after successful write
  130. }
  131. SymbolID CObjectInfoFile::NextObject(SymbolID bHandle)
  132. {
  133. string ObjInfoRecord(m_SymbolTable.Find(bHandle));
  134. if (!ObjInfoRecord.size())
  135. throw cci::Exception(ccFormatError);
  136. return static_cast<SymbolID>(ObjInfoRecord[0]);
  137. }
  138. void CObjectInfoFile::NextObject(SymbolID bHandle, SymbolID bHandleNext)
  139. {
  140. string ObjInfoRecord(m_SymbolTable.Find(bHandle));
  141. if (!ObjInfoRecord.size())
  142. throw cci::Exception(ccFormatError);
  143. ObjInfoRecord[0] = bHandleNext;
  144. m_SymbolTable.Replace(bHandle,ObjInfoRecord);
  145. }
  146. SymbolID CObjectInfoFile::AddObject(ObjectType type, unsigned short size)
  147. {
  148. string strTemp;
  149. strTemp.resize(size+1);
  150. SymbolID bHandle;
  151. bHandle = m_SymbolTable.Add(strTemp, smExclusive);
  152. // Add to end of list
  153. SymbolID bLast = FirstObject(type);
  154. if(!bLast) // No objects in list, add to head of list
  155. {
  156. FirstObject(type,bHandle);
  157. }
  158. else
  159. {
  160. // Search for the last object
  161. SymbolID bLastNext = NextObject(bLast);
  162. while(bLastNext) {
  163. bLast = bLastNext;
  164. bLastNext = NextObject(bLast);
  165. }
  166. NextObject(bLast,bHandle);
  167. }
  168. return bHandle;
  169. }
  170. void CObjectInfoFile::RemoveObject(ObjectType type, SymbolID bHandle)
  171. {
  172. if (FirstObject(type) == bHandle) // Remove from head of list
  173. {
  174. FirstObject(type,NextObject(bHandle));
  175. }
  176. else
  177. {
  178. // Remove from middle/end of list, search for the previous object
  179. SymbolID bPrevNext, bPrevious = FirstObject(type);
  180. while(bPrevious)
  181. {
  182. bPrevNext = NextObject(bPrevious);
  183. if (bHandle == bPrevNext)
  184. break;
  185. bPrevious = bPrevNext;
  186. }
  187. if (!bPrevious)
  188. throw cci::Exception(ccFormatError); // Object not linked
  189. // through this list
  190. NextObject(bPrevious,NextObject(bHandle));
  191. }
  192. m_SymbolTable.Remove(bHandle);
  193. }
  194. void CObjectInfoFile::ReadObject(SymbolID bHandle, BYTE* bData)
  195. {
  196. string ObjInfoRecord(m_SymbolTable.Find(bHandle));
  197. string::size_type size = ObjInfoRecord.size();
  198. if (!size)
  199. throw cci::Exception(ccFormatError);
  200. ObjInfoRecord.copy(reinterpret_cast<char*>(bData) , size - 1,
  201. 1); // Skip the leading byte.
  202. }
  203. void CObjectInfoFile::WriteObject(SymbolID bHandle, BYTE* bData)
  204. {
  205. string ObjInfoRecord(m_SymbolTable.Find(bHandle));
  206. string::size_type size = ObjInfoRecord.size();
  207. if (!size)
  208. throw cci::Exception(ccFormatError);
  209. ObjInfoRecord.resize(1);
  210. ObjInfoRecord += string(reinterpret_cast<char *>(bData), size - 1);
  211. m_SymbolTable.Replace(bHandle,ObjInfoRecord);
  212. }
  213. unsigned short CObjectInfoFile::TableSize()
  214. {
  215. return m_SymbolTable.TableSize();
  216. }
  217. unsigned short CObjectInfoFile::FreeSpace()
  218. {
  219. return m_SymbolTable.FreeSpace();
  220. }