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.

341 lines
8.8 KiB

  1. // ACard.cpp: implementation of the CAbstractCard 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 <functional>
  11. #include "ACard.h"
  12. #include "V1Card.h"
  13. #include "V2Card.h"
  14. #include "TransactionWrap.h"
  15. using namespace std;
  16. using namespace iop;
  17. using namespace cci;
  18. using namespace scu;
  19. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  20. namespace
  21. {
  22. // Like std::mem_fun1_t but for constant member functions
  23. template<class R, class T, class A>
  24. class ConstMemFun1Type
  25. : public binary_function<T const *, A, R>
  26. {
  27. public:
  28. explicit ConstMemFun1Type(R (T::*Pm)(A) const)
  29. : m_Ptr(Pm)
  30. {}
  31. R operator()(T const *P, A Arg) const
  32. {
  33. return ((P->*m_Ptr)(Arg));
  34. }
  35. private:
  36. R (T::*m_Ptr)(A) const;
  37. };
  38. // Like std::mem_fun1 but for constant member functions
  39. template<class R, class T, class A> inline
  40. ConstMemFun1Type<R, T, A>
  41. ConstMemFun1(R (T::*Pm)(A) const)
  42. {
  43. return (ConstMemFun1Type<R, T, A>(Pm));
  44. }
  45. template<class R>
  46. ConstMemFun1Type<vector<R>, CAbstractCard const, ObjectAccess> const
  47. PriviledgedEnumFun(vector<R> (CAbstractCard::*pmf)(ObjectAccess) const)
  48. {
  49. return ConstMemFun1Type<vector<R>, CAbstractCard const, ObjectAccess>(pmf);
  50. }
  51. template<class T, class Op>
  52. vector<T>
  53. EnumAll(Op Enumerator)
  54. {
  55. vector<T> vResult = Enumerator(oaPublicAccess);
  56. vector<T> vPriv = Enumerator(oaPrivateAccess);
  57. vResult.reserve(vResult.size() + vPriv.size());
  58. vResult.insert(vResult.end(), vPriv.begin(), vPriv.end());
  59. return vResult;
  60. };
  61. } // namespace
  62. /////////////////////////// PUBLIC /////////////////////////////////
  63. // Types
  64. // C'tors/D'tors
  65. CAbstractCard::~CAbstractCard()
  66. {}
  67. // Operators
  68. bool
  69. CAbstractCard::operator==(CAbstractCard const &rhs) const
  70. {
  71. CTransactionWrap wrap(this);
  72. CTransactionWrap rhswrap(rhs); // will throw if the card is pulled.
  73. return m_strReaderName == rhs.m_strReaderName;
  74. }
  75. bool
  76. CAbstractCard::operator!=(CAbstractCard const &rhs) const
  77. {
  78. return !(*this == rhs);
  79. }
  80. // Operations
  81. void
  82. CAbstractCard::AuthenticateUser(SecureArray<BYTE> const &rstrBuffer)
  83. {
  84. m_apSmartCard->VerifyCHV(1, rstrBuffer.data());
  85. }
  86. void
  87. CAbstractCard::ChangePIN(SecureArray<BYTE> const &rstrOldPIN,
  88. SecureArray<BYTE> const &rstrNewPIN)
  89. {
  90. if ((rstrOldPIN.size() != 0x08) || (rstrNewPIN.size() != 0x08))
  91. throw Exception(ccBadPinLength);
  92. m_apSmartCard->ChangeCHV(1,
  93. rstrOldPIN.data(),
  94. rstrNewPIN.data());
  95. }
  96. void
  97. CAbstractCard::Logout()
  98. {
  99. m_apSmartCard->LogoutAll();
  100. }
  101. CAbstractCard *
  102. CAbstractCard::Make(string const &rstrReader)
  103. {
  104. auto_ptr<CIOP> apiop(new CIOP());
  105. auto_ptr<CSmartCard>
  106. apSmartCard(apiop->Connect(rstrReader.c_str()));
  107. typedef auto_ptr<CAbstractCard>(*PCardMakerFun)(string const &rstrReader,
  108. auto_ptr<CIOP> &rapiop,
  109. auto_ptr<CSmartCard> &rapSmartCard);
  110. static PCardMakerFun aCardMakers[] =
  111. {
  112. CV2Card::DoMake,
  113. CV1Card::DoMake,
  114. 0
  115. };
  116. auto_ptr<CAbstractCard> apCard;
  117. for (PCardMakerFun *ppcmf = aCardMakers;
  118. (*ppcmf && !apCard.get()); ++ppcmf)
  119. apCard = auto_ptr<CAbstractCard>((*ppcmf)(rstrReader,
  120. apiop,
  121. apSmartCard));
  122. if (!apCard.get())
  123. throw Exception(ccNotPersonalized);
  124. apCard->Setup();
  125. return apCard.release(); // caller given ownership
  126. }
  127. void
  128. CAbstractCard::SetUserPIN(string const &rstrPin)
  129. {
  130. if (rstrPin.size() != 8)
  131. throw Exception(ccBadPinLength);
  132. m_apSmartCard->ChangeCHV(1, reinterpret_cast<BYTE const *>(rstrPin.data()));
  133. }
  134. void
  135. CAbstractCard::VerifyKey(string const &rstrKey,
  136. BYTE bKeyNum)
  137. {
  138. m_apSmartCard->VerifyKey(bKeyNum,
  139. static_cast<WORD>(rstrKey.size()),
  140. reinterpret_cast<BYTE const *>(rstrKey.data()));
  141. }
  142. void
  143. CAbstractCard::VerifyTransportKey(string const &rstrKey)
  144. {
  145. m_apSmartCard->VerifyTransportKey(reinterpret_cast<BYTE const *>(rstrKey.data()));
  146. }
  147. void
  148. CAbstractCard::GenRandom(DWORD dwNumBytes, BYTE *bpRand)
  149. {
  150. m_apSmartCard->GetChallenge(dwNumBytes, bpRand);
  151. }
  152. // Access
  153. SCardType
  154. CAbstractCard::CardType()
  155. {
  156. char const *szCardName = m_apSmartCard->getCardName();
  157. // TO DO: Kludge Alert--a better type interface is needed in the
  158. // IOP to determine the card type rather than keying on the name.
  159. // For now, the Cryptoflex8K type is interpreted to mean it's a
  160. // Cryptoflex card and Access16K means it's an Access card. Thus
  161. // any "Cryptoflex" will map to Cryptoflex8K (including 4K) and
  162. // any "Cyberflex" will map to Access16K. The folded mapping was
  163. // to minimize impact to PKCS with the impending release.
  164. //
  165. // All of this should be revisited with the IOP.
  166. SCardType sct = UnknownCard;
  167. if (strstr(szCardName, "Cryptoflex"))
  168. sct = Cryptoflex8K;
  169. if (strstr(szCardName, "Cyberflex"))
  170. sct = Access16K;
  171. return sct;
  172. }
  173. vector<CCertificate>
  174. CAbstractCard::EnumCertificates() const
  175. {
  176. CTransactionWrap wrap(this);
  177. return
  178. EnumAll<CCertificate>(bind1st(PriviledgedEnumFun<CCertificate>(EnumCertificates), this));
  179. }
  180. vector<CDataObject>
  181. CAbstractCard::EnumDataObjects() const
  182. {
  183. CTransactionWrap wrap(this);
  184. return
  185. EnumAll<CDataObject>(bind1st(PriviledgedEnumFun<CDataObject>(EnumDataObjects), this));
  186. }
  187. vector <CPrivateKey>
  188. CAbstractCard::EnumPrivateKeys() const
  189. {
  190. CTransactionWrap wrap(this);
  191. return
  192. EnumAll<CPrivateKey>(bind1st(PriviledgedEnumFun<CPrivateKey>(EnumPrivateKeys), this));
  193. }
  194. vector<CPublicKey>
  195. CAbstractCard::EnumPublicKeys() const
  196. {
  197. CTransactionWrap wrap(this);
  198. return
  199. EnumAll<CPublicKey>(bind1st(PriviledgedEnumFun<CPublicKey>(EnumPublicKeys), this));
  200. }
  201. string
  202. CAbstractCard::ReaderName() const
  203. {
  204. return m_strReaderName;
  205. }
  206. CSmartCard &
  207. CAbstractCard::SmartCard() const
  208. {
  209. return *m_apSmartCard;
  210. }
  211. // Predicates
  212. // Card is connected and available (e.g. in the reader)
  213. bool
  214. CAbstractCard::IsAvailable() const
  215. {
  216. bool fIsAvailable = false;
  217. try
  218. {
  219. CTransactionWrap wrap(this);
  220. DWORD dwState;
  221. DWORD dwProtocol;
  222. m_apSmartCard->GetState(dwState, dwProtocol);
  223. fIsAvailable = (SCARD_SPECIFIC == dwState);
  224. }
  225. catch (...)
  226. {
  227. }
  228. return fIsAvailable;
  229. }
  230. // Static Variables
  231. /////////////////////////// PROTECTED /////////////////////////////////
  232. // C'tors/D'tors
  233. CAbstractCard::CAbstractCard(string const &rstrReaderName,
  234. auto_ptr<iop::CIOP> &rapiop,
  235. auto_ptr<CSmartCard> &rapSmartCard)
  236. : CCryptFactory(),
  237. slbRefCnt::RCObject(),
  238. m_strReaderName(rstrReaderName),
  239. m_apiop(rapiop),
  240. m_apSmartCard(rapSmartCard)
  241. {}
  242. // Operators
  243. // Operations
  244. void
  245. CAbstractCard::DoSetup()
  246. {}
  247. // Access
  248. // Predicates
  249. // Static Variables
  250. /////////////////////////// PRIVATE /////////////////////////////////
  251. // C'tors/D'tors
  252. // Operators
  253. // Operations
  254. void
  255. CAbstractCard::Setup()
  256. {
  257. CTransactionWrap wrap(this);
  258. DoSetup();
  259. }
  260. // Access
  261. // Predicates
  262. // Static Variables