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.

413 lines
8.1 KiB

  1. // V1PubKey.cpp: implementation of the CV1PubKey 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 <scuCast.h>
  10. #include <iopPubBlob.h>
  11. #include "cciExc.h"
  12. #include "cciCard.h"
  13. #include "TransactionWrap.h"
  14. #include "V1Cont.h"
  15. #include "V1ContRec.h"
  16. #include "V1PubKey.h"
  17. using namespace std;
  18. using namespace cci;
  19. using namespace iop;
  20. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  21. /////////////////////////// PUBLIC /////////////////////////////////
  22. // Types
  23. // C'tors/D'tors
  24. CV1PublicKey::CV1PublicKey(CV1Card const &rv1card,
  25. KeySpec ks)
  26. : CAbstractPublicKey(rv1card, oaPublicAccess),
  27. m_ks(ks),
  28. m_apKeyBlob()
  29. {}
  30. CV1PublicKey::~CV1PublicKey()
  31. {}
  32. // Operators
  33. // Operations
  34. void
  35. CV1PublicKey::AssociateWith(KeySpec ks)
  36. {
  37. CTransactionWrap wrap(m_hcard);
  38. m_ks = ks;
  39. Store();
  40. }
  41. void
  42. CV1PublicKey::CKInvisible(bool flag)
  43. {
  44. throw Exception(ccNotImplemented);
  45. }
  46. void
  47. CV1PublicKey::CredentialID(string const &rstrID)
  48. {
  49. throw Exception(ccNotImplemented);
  50. }
  51. void
  52. CV1PublicKey::Derive(bool flag)
  53. {
  54. throw Exception(ccNotImplemented);
  55. }
  56. void
  57. CV1PublicKey::ID(string const &rstrID)
  58. {
  59. throw Exception(ccNotImplemented);
  60. }
  61. void
  62. CV1PublicKey::EndDate(Date const &rdtEnd)
  63. {
  64. throw Exception(ccNotImplemented);
  65. }
  66. void
  67. CV1PublicKey::Encrypt(bool flag)
  68. {
  69. throw Exception(ccNotImplemented);
  70. }
  71. void
  72. CV1PublicKey::Exponent(string const &rstrExp)
  73. {
  74. CTransactionWrap wrap(m_hcard);
  75. if (!m_apKeyBlob.get()) // preserve the modulus, if previously cached
  76. {
  77. m_apKeyBlob =
  78. auto_ptr<CPublicKeyBlob>(new CPublicKeyBlob);
  79. Clear(*m_apKeyBlob.get());
  80. }
  81. CopyMemory(m_apKeyBlob->bExponent,
  82. reinterpret_cast<BYTE const *>(rstrExp.data()),
  83. rstrExp.length());
  84. if (ksNone != m_ks)
  85. Store();
  86. }
  87. void
  88. CV1PublicKey::Label(string const &rstrLabel)
  89. {
  90. throw Exception(ccNotImplemented);
  91. }
  92. void
  93. CV1PublicKey::Local(bool flag)
  94. {
  95. throw Exception(ccNotImplemented);
  96. }
  97. CV1PublicKey *
  98. CV1PublicKey::Make(CV1Card const &rv1card,
  99. KeySpec ks)
  100. {
  101. CTransactionWrap wrap(rv1card);
  102. return new CV1PublicKey(rv1card, ks);
  103. }
  104. void
  105. CV1PublicKey::Modifiable(bool flag)
  106. {
  107. throw Exception(ccNotImplemented);
  108. }
  109. void
  110. CV1PublicKey::Modulus(string const &rstrMod)
  111. {
  112. CTransactionWrap wrap(m_hcard);
  113. if (!m_apKeyBlob.get()) // preserve the exponent, if previously cached
  114. {
  115. m_apKeyBlob =
  116. auto_ptr<CPublicKeyBlob>(new CPublicKeyBlob);
  117. Clear(*m_apKeyBlob.get());
  118. }
  119. CopyMemory(m_apKeyBlob->bModulus,
  120. reinterpret_cast<BYTE const *>(rstrMod.data()),
  121. rstrMod.length());
  122. m_apKeyBlob->bModulusLength = static_cast<BYTE>(rstrMod.length());
  123. if (ksNone != m_ks)
  124. Store();
  125. }
  126. void
  127. CV1PublicKey::StartDate(Date const &rdtStart)
  128. {
  129. throw Exception(ccNotImplemented);
  130. }
  131. void
  132. CV1PublicKey::Subject(string const &rstrSubject)
  133. {
  134. throw Exception(ccNotImplemented);
  135. }
  136. void
  137. CV1PublicKey::Verify(bool flag)
  138. {
  139. throw Exception(ccNotImplemented);
  140. }
  141. void
  142. CV1PublicKey::VerifyRecover(bool flag)
  143. {
  144. throw Exception(ccNotImplemented);
  145. }
  146. void
  147. CV1PublicKey::Wrap(bool flag)
  148. {
  149. throw Exception(ccNotImplemented);
  150. }
  151. // Access
  152. bool
  153. CV1PublicKey::CKInvisible()
  154. {
  155. return false;
  156. }
  157. string
  158. CV1PublicKey::CredentialID()
  159. {
  160. throw Exception(ccNotImplemented);
  161. return string();
  162. }
  163. bool
  164. CV1PublicKey::Derive()
  165. {
  166. return true;
  167. }
  168. bool
  169. CV1PublicKey::Encrypt()
  170. {
  171. return true;
  172. }
  173. Date
  174. CV1PublicKey::EndDate()
  175. {
  176. throw Exception(ccNotImplemented);
  177. return Date();
  178. }
  179. string
  180. CV1PublicKey::Exponent()
  181. {
  182. CTransactionWrap wrap(m_hcard);
  183. if (!m_apKeyBlob.get())
  184. Load();
  185. return string(reinterpret_cast<char *>(m_apKeyBlob->bExponent),
  186. sizeof m_apKeyBlob->bExponent);
  187. }
  188. string
  189. CV1PublicKey::ID()
  190. {
  191. throw Exception(ccNotImplemented);
  192. return string();
  193. }
  194. string
  195. CV1PublicKey::Label()
  196. {
  197. throw Exception(ccNotImplemented);
  198. }
  199. bool
  200. CV1PublicKey::Local()
  201. {
  202. return false;
  203. }
  204. bool
  205. CV1PublicKey::Modifiable()
  206. {
  207. return true;
  208. }
  209. string
  210. CV1PublicKey::Modulus()
  211. {
  212. CTransactionWrap wrap(m_hcard);
  213. if (!m_apKeyBlob.get())
  214. Load();
  215. return string(reinterpret_cast<char *>(m_apKeyBlob.get()->bModulus),
  216. m_apKeyBlob.get()->bModulusLength);
  217. }
  218. bool
  219. CV1PublicKey::Private()
  220. {
  221. return false;
  222. }
  223. Date
  224. CV1PublicKey::StartDate()
  225. {
  226. throw Exception(ccNotImplemented);
  227. return Date();
  228. }
  229. string
  230. CV1PublicKey::Subject()
  231. {
  232. throw Exception(ccNotImplemented);
  233. return string();
  234. }
  235. bool
  236. CV1PublicKey::Verify()
  237. {
  238. return true;
  239. }
  240. bool
  241. CV1PublicKey::VerifyRecover()
  242. {
  243. throw true;
  244. }
  245. bool
  246. CV1PublicKey::Wrap()
  247. {
  248. return true;
  249. }
  250. // Predicates
  251. // Static Variables
  252. /////////////////////////// PROTECTED /////////////////////////////////
  253. // C'tors/D'tors
  254. // Operators
  255. // Operations
  256. void
  257. CV1PublicKey::DoDelete()
  258. {
  259. if (ksNone != m_ks)
  260. {
  261. CContainer hcntr(m_hcard->DefaultContainer());
  262. if (!hcntr)
  263. throw Exception(ccInvalidParameter);
  264. CV1Container &rv1cntr =
  265. scu::DownCast<CV1Container &, CAbstractContainer &>(*hcntr);
  266. CPublicKeyBlob KeyBlob;
  267. Clear(KeyBlob);
  268. rv1cntr.Record().Write(m_ks, KeyBlob);
  269. }
  270. else
  271. throw Exception(ccInvalidParameter);
  272. }
  273. // Access
  274. // Predicates
  275. bool
  276. CV1PublicKey::DoEquals(CAbstractPublicKey const &rhs) const
  277. {
  278. CV1PublicKey const &rv1rhs =
  279. scu::DownCast<CV1PublicKey const &, CAbstractPublicKey const &>(rhs);
  280. return rv1rhs.m_ks == m_ks;
  281. }
  282. // Static Variables
  283. /////////////////////////// PRIVATE /////////////////////////////////
  284. // C'tors/D'tors
  285. // Operators
  286. // Operations
  287. void
  288. CV1PublicKey::Load()
  289. {
  290. if (ksNone != m_ks)
  291. {
  292. CV1Card &rv1card =
  293. scu::DownCast<CV1Card &, CAbstractCard &>(*m_hcard);
  294. CV1ContainerRecord CntrRec(rv1card,
  295. CV1ContainerRecord::DefaultName(),
  296. CV1ContainerRecord::cmNever);
  297. m_apKeyBlob =
  298. auto_ptr<CPublicKeyBlob>(new CPublicKeyBlob);
  299. CntrRec.Read(m_ks, *m_apKeyBlob.get());
  300. }
  301. else
  302. throw Exception(ccInvalidParameter);
  303. }
  304. void
  305. CV1PublicKey::Store()
  306. {
  307. if (ksNone != m_ks)
  308. {
  309. if (m_apKeyBlob.get())
  310. {
  311. CV1Card &rv1card =
  312. scu::DownCast<CV1Card &, CAbstractCard &>(*m_hcard);
  313. CV1ContainerRecord CntrRec(rv1card,
  314. CV1ContainerRecord::DefaultName(),
  315. CV1ContainerRecord::cmNever);
  316. CntrRec.Write(m_ks, *m_apKeyBlob.get());
  317. }
  318. }
  319. else
  320. throw Exception(ccInvalidParameter);
  321. }
  322. // Access
  323. // Predicates
  324. // Static Variables