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.

417 lines
8.7 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. // Security: guard against buffer overrun
  82. if (rstrExp.length() > (sizeof m_apKeyBlob->bExponent / sizeof
  83. *m_apKeyBlob->bExponent))
  84. throw cci::Exception(ccBadLength);
  85. CopyMemory(m_apKeyBlob->bExponent,
  86. reinterpret_cast<BYTE const *>(rstrExp.data()),
  87. rstrExp.length());
  88. if (ksNone != m_ks)
  89. Store();
  90. }
  91. void
  92. CV1PublicKey::Label(string const &rstrLabel)
  93. {
  94. throw Exception(ccNotImplemented);
  95. }
  96. void
  97. CV1PublicKey::Local(bool flag)
  98. {
  99. throw Exception(ccNotImplemented);
  100. }
  101. CV1PublicKey *
  102. CV1PublicKey::Make(CV1Card const &rv1card,
  103. KeySpec ks)
  104. {
  105. CTransactionWrap wrap(rv1card);
  106. return new CV1PublicKey(rv1card, ks);
  107. }
  108. void
  109. CV1PublicKey::Modifiable(bool flag)
  110. {
  111. throw Exception(ccNotImplemented);
  112. }
  113. void
  114. CV1PublicKey::Modulus(string const &rstrMod)
  115. {
  116. CTransactionWrap wrap(m_hcard);
  117. if (!m_apKeyBlob.get()) // preserve the exponent, if previously cached
  118. {
  119. m_apKeyBlob =
  120. auto_ptr<CPublicKeyBlob>(new CPublicKeyBlob);
  121. Clear(*m_apKeyBlob.get());
  122. }
  123. CopyMemory(m_apKeyBlob->bModulus,
  124. reinterpret_cast<BYTE const *>(rstrMod.data()),
  125. rstrMod.length());
  126. m_apKeyBlob->bModulusLength = static_cast<BYTE>(rstrMod.length());
  127. if (ksNone != m_ks)
  128. Store();
  129. }
  130. void
  131. CV1PublicKey::StartDate(Date const &rdtStart)
  132. {
  133. throw Exception(ccNotImplemented);
  134. }
  135. void
  136. CV1PublicKey::Subject(string const &rstrSubject)
  137. {
  138. throw Exception(ccNotImplemented);
  139. }
  140. void
  141. CV1PublicKey::Verify(bool flag)
  142. {
  143. throw Exception(ccNotImplemented);
  144. }
  145. void
  146. CV1PublicKey::VerifyRecover(bool flag)
  147. {
  148. throw Exception(ccNotImplemented);
  149. }
  150. void
  151. CV1PublicKey::Wrap(bool flag)
  152. {
  153. throw Exception(ccNotImplemented);
  154. }
  155. // Access
  156. bool
  157. CV1PublicKey::CKInvisible()
  158. {
  159. return false;
  160. }
  161. string
  162. CV1PublicKey::CredentialID()
  163. {
  164. throw Exception(ccNotImplemented);
  165. return string();
  166. }
  167. bool
  168. CV1PublicKey::Derive()
  169. {
  170. return true;
  171. }
  172. bool
  173. CV1PublicKey::Encrypt()
  174. {
  175. return true;
  176. }
  177. Date
  178. CV1PublicKey::EndDate()
  179. {
  180. throw Exception(ccNotImplemented);
  181. return Date();
  182. }
  183. string
  184. CV1PublicKey::Exponent()
  185. {
  186. CTransactionWrap wrap(m_hcard);
  187. if (!m_apKeyBlob.get())
  188. Load();
  189. return string(reinterpret_cast<char *>(m_apKeyBlob->bExponent),
  190. sizeof m_apKeyBlob->bExponent);
  191. }
  192. string
  193. CV1PublicKey::ID()
  194. {
  195. throw Exception(ccNotImplemented);
  196. return string();
  197. }
  198. string
  199. CV1PublicKey::Label()
  200. {
  201. throw Exception(ccNotImplemented);
  202. }
  203. bool
  204. CV1PublicKey::Local()
  205. {
  206. return false;
  207. }
  208. bool
  209. CV1PublicKey::Modifiable()
  210. {
  211. return true;
  212. }
  213. string
  214. CV1PublicKey::Modulus()
  215. {
  216. CTransactionWrap wrap(m_hcard);
  217. if (!m_apKeyBlob.get())
  218. Load();
  219. return string(reinterpret_cast<char *>(m_apKeyBlob.get()->bModulus),
  220. m_apKeyBlob.get()->bModulusLength);
  221. }
  222. bool
  223. CV1PublicKey::Private()
  224. {
  225. return false;
  226. }
  227. Date
  228. CV1PublicKey::StartDate()
  229. {
  230. throw Exception(ccNotImplemented);
  231. return Date();
  232. }
  233. string
  234. CV1PublicKey::Subject()
  235. {
  236. throw Exception(ccNotImplemented);
  237. return string();
  238. }
  239. bool
  240. CV1PublicKey::Verify()
  241. {
  242. return true;
  243. }
  244. bool
  245. CV1PublicKey::VerifyRecover()
  246. {
  247. throw true;
  248. }
  249. bool
  250. CV1PublicKey::Wrap()
  251. {
  252. return true;
  253. }
  254. // Predicates
  255. // Static Variables
  256. /////////////////////////// PROTECTED /////////////////////////////////
  257. // C'tors/D'tors
  258. // Operators
  259. // Operations
  260. void
  261. CV1PublicKey::DoDelete()
  262. {
  263. if (ksNone != m_ks)
  264. {
  265. CContainer hcntr(m_hcard->DefaultContainer());
  266. if (!hcntr)
  267. throw Exception(ccInvalidParameter);
  268. CV1Container &rv1cntr =
  269. scu::DownCast<CV1Container &, CAbstractContainer &>(*hcntr);
  270. CPublicKeyBlob KeyBlob;
  271. Clear(KeyBlob);
  272. rv1cntr.Record().Write(m_ks, KeyBlob);
  273. }
  274. else
  275. throw Exception(ccInvalidParameter);
  276. }
  277. // Access
  278. // Predicates
  279. bool
  280. CV1PublicKey::DoEquals(CAbstractPublicKey const &rhs) const
  281. {
  282. CV1PublicKey const &rv1rhs =
  283. scu::DownCast<CV1PublicKey const &, CAbstractPublicKey const &>(rhs);
  284. return rv1rhs.m_ks == m_ks;
  285. }
  286. // Static Variables
  287. /////////////////////////// PRIVATE /////////////////////////////////
  288. // C'tors/D'tors
  289. // Operators
  290. // Operations
  291. void
  292. CV1PublicKey::Load()
  293. {
  294. if (ksNone != m_ks)
  295. {
  296. CV1Card &rv1card =
  297. scu::DownCast<CV1Card &, CAbstractCard &>(*m_hcard);
  298. CV1ContainerRecord CntrRec(rv1card,
  299. CV1ContainerRecord::DefaultName(),
  300. CV1ContainerRecord::cmNever);
  301. m_apKeyBlob =
  302. auto_ptr<CPublicKeyBlob>(new CPublicKeyBlob);
  303. CntrRec.Read(m_ks, *m_apKeyBlob.get());
  304. }
  305. else
  306. throw Exception(ccInvalidParameter);
  307. }
  308. void
  309. CV1PublicKey::Store()
  310. {
  311. if (ksNone != m_ks)
  312. {
  313. if (m_apKeyBlob.get())
  314. {
  315. CV1Card &rv1card =
  316. scu::DownCast<CV1Card &, CAbstractCard &>(*m_hcard);
  317. CV1ContainerRecord CntrRec(rv1card,
  318. CV1ContainerRecord::DefaultName(),
  319. CV1ContainerRecord::cmNever);
  320. CntrRec.Write(m_ks, *m_apKeyBlob.get());
  321. }
  322. }
  323. else
  324. throw Exception(ccInvalidParameter);
  325. }
  326. // Access
  327. // Predicates
  328. // Static Variables