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.

275 lines
7.7 KiB

  1. // CardInfo.cpp: implementation of the CCardInfo 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 "NoWarning.h"
  10. #include "CardInfo.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. using namespace std;
  15. using namespace scu;
  16. using namespace cci;
  17. using namespace iop;
  18. CCardInfo::CCardInfo(CSmartCard &rCard)
  19. : m_rSmartCard(rCard),
  20. m_PersoDate(),
  21. m_Format(),
  22. m_sLabelSize(0),
  23. m_bUsagePolicy(0),
  24. m_bNum512Pri(0),
  25. m_bNum768Pri(0),
  26. m_bNum1024Pri(0),
  27. m_aabRSA512Keys(0),
  28. m_aabRSA768Keys(0),
  29. m_aabRSA1024Keys(0),
  30. m_sRSA512MaskOffset(0),
  31. m_sRSA512MaskSize(0),
  32. m_sRSA768MaskOffset(0),
  33. m_sRSA768MaskSize(0),
  34. m_sRSA1024MaskOffset(0),
  35. m_sRSA1024MaskSize(0)
  36. {}
  37. CCardInfo::~CCardInfo()
  38. {}
  39. void CCardInfo::Reset()
  40. {
  41. m_rSmartCard.Select(CardInfoFilePath);
  42. if(m_sRSA512MaskSize)
  43. {
  44. memset(m_aabRSA512Keys.Get(),0,m_sRSA512MaskSize);
  45. m_rSmartCard.WriteBinary(m_sRSA512MaskOffset,
  46. m_sRSA512MaskSize, m_aabRSA512Keys.Get());
  47. }
  48. if(m_sRSA768MaskSize)
  49. {
  50. memset(m_aabRSA768Keys.Get(),0,m_sRSA768MaskSize);
  51. m_rSmartCard.WriteBinary(m_sRSA768MaskOffset,
  52. m_sRSA768MaskSize,
  53. m_aabRSA768Keys.Get());
  54. }
  55. if(m_sRSA1024MaskSize)
  56. {
  57. memset(m_aabRSA1024Keys.Get(),0,m_sRSA1024MaskSize);
  58. m_rSmartCard.WriteBinary(m_sRSA1024MaskOffset,
  59. m_sRSA1024MaskSize,
  60. m_aabRSA1024Keys.Get());
  61. }
  62. Label("");
  63. }
  64. void CCardInfo::UpdateCache()
  65. {
  66. BYTE bMasterBlk[CardMasterBlkSize];
  67. m_rSmartCard.Select(CardInfoFilePath);
  68. m_rSmartCard.ReadBinary(0, CardMasterBlkSize, bMasterBlk);
  69. // Get the personalization date out and convert it to a date struct
  70. // Card format is yyyyyyyy yyyyyymm mmdddddd
  71. DateArrayToDateStruct(&bMasterBlk[CardDateLoc], &m_PersoDate);
  72. // Get Format and convert to Format struct
  73. m_Format.bMajor = bMasterBlk[CardVersionMajorLoc];
  74. m_Format.bMinor = bMasterBlk[CardVersionMinorLoc];
  75. // Check that the version is supported.
  76. if(m_Format.bMajor!=2 && m_Format.bMinor!=0) throw Exception(ccFormatNotSupported);
  77. // Various parameters
  78. m_bUsagePolicy = bMasterBlk[CardUsagePolicyLoc];
  79. m_sLabelSize = bMasterBlk[CardLabelSizeLoc+1]*256 + bMasterBlk[CardLabelSizeLoc];
  80. // Private key file parameters
  81. m_bNum512Pri = bMasterBlk[CardNumRSA512PrivLoc];
  82. m_bNum768Pri = bMasterBlk[CardNumRSA768PrivLoc];
  83. m_bNum1024Pri = bMasterBlk[CardNumRSA1024PrivLoc];
  84. // Calculate mask sizes and offsets
  85. m_sRSA512MaskSize = (m_bNum512Pri + 7) / 8;
  86. m_sRSA768MaskSize = (m_bNum768Pri + 7) / 8;
  87. m_sRSA1024MaskSize = (m_bNum1024Pri + 7) / 8;
  88. m_sRSA512MaskOffset = CardMasterBlkSize;
  89. m_sRSA768MaskOffset = m_sRSA512MaskOffset + m_sRSA512MaskSize;
  90. m_sRSA1024MaskOffset = m_sRSA768MaskOffset + m_sRSA768MaskSize;
  91. // Read and allocate the masks themselves
  92. m_aabRSA512Keys = AutoArrayPtr<BYTE>(new BYTE[m_sRSA512MaskSize]);
  93. m_aabRSA768Keys = AutoArrayPtr<BYTE>(new BYTE[m_sRSA768MaskSize]);
  94. m_aabRSA1024Keys = AutoArrayPtr<BYTE>(new BYTE[m_sRSA1024MaskSize]);
  95. AutoArrayPtr<BYTE> aabUsageBlk(new BYTE[UsageBlockSize()]);
  96. m_rSmartCard.ReadBinary(CardMasterBlkSize, UsageBlockSize(),
  97. aabUsageBlk.Get());
  98. // Lay out the usage masks
  99. memcpy(m_aabRSA512Keys.Get(),
  100. &aabUsageBlk[m_sRSA512MaskOffset-CardMasterBlkSize],
  101. m_sRSA512MaskSize);
  102. memcpy(m_aabRSA768Keys.Get(),
  103. &aabUsageBlk[m_sRSA768MaskOffset-CardMasterBlkSize],
  104. m_sRSA768MaskSize);
  105. memcpy(m_aabRSA1024Keys.Get(),
  106. &aabUsageBlk[m_sRSA1024MaskOffset-CardMasterBlkSize],
  107. m_sRSA1024MaskSize);
  108. }
  109. BYTE CCardInfo::AllocatePrivateKey(BYTE bKeyType)
  110. {
  111. BYTE *pbMask;
  112. BYTE bNumKeys;
  113. unsigned short sMaskSize;
  114. unsigned short sMaskOffset;
  115. switch(bKeyType)
  116. {
  117. case CardKeyTypeRSA512:
  118. pbMask = m_aabRSA512Keys.Get();
  119. bNumKeys = m_bNum512Pri;
  120. sMaskSize = m_sRSA512MaskSize;
  121. sMaskOffset = m_sRSA512MaskOffset;
  122. break;
  123. case CardKeyTypeRSA768:
  124. pbMask = m_aabRSA768Keys.Get();
  125. bNumKeys = m_bNum768Pri;
  126. sMaskSize = m_sRSA768MaskSize;
  127. sMaskOffset = m_sRSA768MaskOffset;
  128. break;
  129. case CardKeyTypeRSA1024:
  130. pbMask = m_aabRSA1024Keys.Get();
  131. bNumKeys = m_bNum1024Pri;
  132. sMaskSize = m_sRSA1024MaskSize;
  133. sMaskOffset = m_sRSA1024MaskOffset;
  134. break;
  135. default:
  136. throw Exception(ccBadKeySpec);
  137. }
  138. bool fFound = false;
  139. BYTE i = 0;
  140. while ((i < bNumKeys) && !fFound)
  141. {
  142. if (!BitSet(pbMask, i))
  143. {
  144. SetBit(pbMask, i);
  145. m_rSmartCard.Select(CardInfoFilePath);
  146. m_rSmartCard.WriteBinary(sMaskOffset, sMaskSize, pbMask);
  147. fFound = true;
  148. }
  149. else
  150. i++;
  151. }
  152. if (!fFound)
  153. throw Exception(ccOutOfPrivateKeySlots);
  154. return i;
  155. }
  156. void CCardInfo::FreePrivateKey(BYTE bKeyType, BYTE bKeyNum)
  157. {
  158. BYTE *pbMask;
  159. BYTE bNumKeys;
  160. unsigned short sMaskSize;
  161. unsigned short sMaskOffset;
  162. switch(bKeyType)
  163. {
  164. case CardKeyTypeRSA512:
  165. pbMask = m_aabRSA512Keys.Get();
  166. bNumKeys = m_bNum512Pri;
  167. sMaskSize = m_sRSA512MaskSize;
  168. sMaskOffset = m_sRSA512MaskOffset;
  169. break;
  170. case CardKeyTypeRSA768:
  171. pbMask = m_aabRSA768Keys.Get();
  172. bNumKeys = m_bNum768Pri;
  173. sMaskSize = m_sRSA768MaskSize;
  174. sMaskOffset = m_sRSA768MaskOffset;
  175. break;
  176. case CardKeyTypeRSA1024:
  177. pbMask = m_aabRSA1024Keys.Get();
  178. bNumKeys = m_bNum1024Pri;
  179. sMaskSize = m_sRSA1024MaskSize;
  180. sMaskOffset = m_sRSA1024MaskOffset;
  181. break;
  182. default:
  183. throw Exception(ccBadKeySpec);
  184. }
  185. if(bKeyNum>=bNumKeys) throw Exception(ccKeyNotFound);
  186. if(!BitSet(pbMask,bKeyNum)) throw Exception(ccKeyNotFound);
  187. ResetBit(pbMask,bKeyNum);
  188. m_rSmartCard.Select(CardInfoFilePath);
  189. m_rSmartCard.WriteBinary(sMaskOffset, sMaskSize, pbMask);
  190. }
  191. void CCardInfo::Label(string const &rLabel)
  192. {
  193. if(rLabel.size()>=m_sLabelSize) throw Exception(ccStringTooLong);
  194. unsigned short sLabelOffset = CardMasterBlkSize + UsageBlockSize() ;
  195. m_rSmartCard.Select(CardInfoFilePath);
  196. m_rSmartCard.WriteBinary(sLabelOffset, rLabel.size()+1,
  197. reinterpret_cast<BYTE const *>(rLabel.c_str()));
  198. }
  199. string CCardInfo::Label()
  200. {
  201. string RetVal;
  202. AutoArrayPtr<char> aabBlk(new char[m_sLabelSize]);
  203. unsigned short sLabelOffset = CardMasterBlkSize + UsageBlockSize() ;
  204. m_rSmartCard.Select(CardInfoFilePath);
  205. m_rSmartCard.ReadBinary(sLabelOffset, m_sLabelSize,
  206. reinterpret_cast<BYTE *>(aabBlk.Get()));
  207. aabBlk[m_sLabelSize-1] = '\0'; // Ensure termination
  208. RetVal.assign(aabBlk.Get());
  209. return RetVal;
  210. }