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.

347 lines
8.7 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. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  19. namespace
  20. {
  21. // Like std::mem_fun1_t but for constant member functions
  22. template<class R, class T, class A>
  23. class ConstMemFun1Type
  24. : public binary_function<T const *, A, R>
  25. {
  26. public:
  27. explicit ConstMemFun1Type(R (T::*Pm)(A) const)
  28. : m_Ptr(Pm)
  29. {}
  30. R operator()(T const *P, A Arg) const
  31. {
  32. return ((P->*m_Ptr)(Arg));
  33. }
  34. private:
  35. R (T::*m_Ptr)(A) const;
  36. };
  37. // Like std::mem_fun1 but for constant member functions
  38. template<class R, class T, class A> inline
  39. ConstMemFun1Type<R, T, A>
  40. ConstMemFun1(R (T::*Pm)(A) const)
  41. {
  42. return (ConstMemFun1Type<R, T, A>(Pm));
  43. }
  44. template<class R>
  45. ConstMemFun1Type<vector<R>, CAbstractCard const, ObjectAccess> const
  46. PriviledgedEnumFun(vector<R> (CAbstractCard::*pmf)(ObjectAccess) const)
  47. {
  48. return ConstMemFun1Type<vector<R>, CAbstractCard const, ObjectAccess>(pmf);
  49. }
  50. template<class T, class Op>
  51. vector<T>
  52. EnumAll(Op Enumerator)
  53. {
  54. vector<T> vResult = Enumerator(oaPublicAccess);
  55. vector<T> vPriv = Enumerator(oaPrivateAccess);
  56. vResult.reserve(vResult.size() + vPriv.size());
  57. vResult.insert(vResult.end(), vPriv.begin(), vPriv.end());
  58. return vResult;
  59. };
  60. } // namespace
  61. /////////////////////////// PUBLIC /////////////////////////////////
  62. // Types
  63. // C'tors/D'tors
  64. CAbstractCard::~CAbstractCard()
  65. {}
  66. // Operators
  67. bool
  68. CAbstractCard::operator==(CAbstractCard const &rhs) const
  69. {
  70. CTransactionWrap wrap(this);
  71. CTransactionWrap rhswrap(rhs); // will throw if the card is pulled.
  72. return m_strReaderName == rhs.m_strReaderName;
  73. }
  74. bool
  75. CAbstractCard::operator!=(CAbstractCard const &rhs) const
  76. {
  77. return !(*this == rhs);
  78. }
  79. // Operations
  80. void
  81. CAbstractCard::AuthenticateUser(string const &rstrBuffer)
  82. {
  83. m_apSmartCard->VerifyCHV(1,
  84. reinterpret_cast<BYTE const *>(rstrBuffer.data()));
  85. }
  86. void
  87. CAbstractCard::ChangePIN(string const &rstrOldPIN,
  88. string const &rstrNewPIN)
  89. {
  90. if ((rstrOldPIN.size() != 0x08) || (rstrNewPIN.size() != 0x08))
  91. throw Exception(ccBadPinLength);
  92. m_apSmartCard->ChangeCHV(1,
  93. reinterpret_cast<BYTE const *>(rstrOldPIN.data()),
  94. reinterpret_cast<BYTE const *>(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. CMarker
  202. CAbstractCard::Marker(CMarker::MarkerType const &Type) const
  203. {
  204. return m_apSmartCard->Marker(Type);
  205. }
  206. string
  207. CAbstractCard::ReaderName() const
  208. {
  209. return m_strReaderName;
  210. }
  211. CSmartCard &
  212. CAbstractCard::SmartCard() const
  213. {
  214. return *m_apSmartCard;
  215. }
  216. // Predicates
  217. // Card is connected and available (e.g. in the reader)
  218. bool
  219. CAbstractCard::IsAvailable() const
  220. {
  221. bool fIsAvailable = false;
  222. try
  223. {
  224. CTransactionWrap wrap(this);
  225. DWORD dwState;
  226. DWORD dwProtocol;
  227. m_apSmartCard->GetState(dwState, dwProtocol);
  228. fIsAvailable = (SCARD_SPECIFIC == dwState);
  229. }
  230. catch (...)
  231. {
  232. }
  233. return fIsAvailable;
  234. }
  235. // Static Variables
  236. /////////////////////////// PROTECTED /////////////////////////////////
  237. // C'tors/D'tors
  238. CAbstractCard::CAbstractCard(string const &rstrReaderName,
  239. auto_ptr<iop::CIOP> &rapiop,
  240. auto_ptr<CSmartCard> &rapSmartCard)
  241. : CCryptFactory(),
  242. slbRefCnt::RCObject(),
  243. m_strReaderName(rstrReaderName),
  244. m_apiop(rapiop),
  245. m_apSmartCard(rapSmartCard)
  246. {}
  247. // Operators
  248. // Operations
  249. void
  250. CAbstractCard::DoSetup()
  251. {}
  252. // Access
  253. // Predicates
  254. // Static Variables
  255. /////////////////////////// PRIVATE /////////////////////////////////
  256. // C'tors/D'tors
  257. // Operators
  258. // Operations
  259. void
  260. CAbstractCard::Setup()
  261. {
  262. CTransactionWrap wrap(this);
  263. DoSetup();
  264. }
  265. // Access
  266. // Predicates
  267. // Static Variables