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.

523 lines
12 KiB

  1. // V2PriKey.cpp: implementation of the CV2PriKey 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 "cciCard.h"
  12. #include "TransactionWrap.h"
  13. #include "V2PriKey.h"
  14. #include "PriKeyInfoRecord.h"
  15. using namespace std;
  16. using namespace cci;
  17. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  18. /////////////////////////// PUBLIC /////////////////////////////////
  19. // Types
  20. // C'tors/D'tors
  21. CV2PrivateKey::CV2PrivateKey(CV2Card const &rv2card,
  22. ObjectAccess oa)
  23. : CAbstractPrivateKey(rv2card, oa),
  24. m_sidHandle(0),
  25. m_apcir()
  26. {
  27. Setup(rv2card);
  28. // write new private key object into info file
  29. m_apcir->Write();
  30. }
  31. CV2PrivateKey::CV2PrivateKey(CV2Card const &rv2card,
  32. SymbolID sidHandle,
  33. ObjectAccess oa)
  34. : CAbstractPrivateKey(rv2card, oa),
  35. m_sidHandle(0),
  36. m_apcir()
  37. {
  38. Setup(rv2card, sidHandle);
  39. }
  40. CV2PrivateKey::CV2PrivateKey(CV2Card const &rv2card,
  41. BYTE bKeyType,
  42. BYTE bKeyNumber,
  43. ObjectAccess oa)
  44. : CAbstractPrivateKey(rv2card, oa),
  45. m_sidHandle(0),
  46. m_apcir()
  47. {
  48. Setup(rv2card);
  49. m_apcir->m_bKeyType = bKeyType;
  50. m_apcir->m_bKeyNum = bKeyNumber;
  51. // write new private key object into info file
  52. m_apcir->Write();
  53. }
  54. CV2PrivateKey::~CV2PrivateKey()
  55. {}
  56. // Operators
  57. // Operations
  58. void
  59. CV2PrivateKey::CredentialID(string const &rstrID)
  60. {
  61. m_apcir->Symbol(&m_apcir->m_bCredentialID, rstrID);
  62. }
  63. void
  64. CV2PrivateKey::Decrypt(bool flag)
  65. {
  66. m_apcir->Flag(PrivDecryptFlag, flag);
  67. }
  68. void
  69. CV2PrivateKey::Derive(bool flag)
  70. {
  71. m_apcir->Flag(PrivDeriveFlag, flag);
  72. }
  73. void
  74. CV2PrivateKey::EndDate(Date const &rEndDate)
  75. {
  76. CTransactionWrap wrap(m_hcard);
  77. m_apcir->Read();
  78. m_apcir->m_dtEnd = rEndDate;
  79. m_apcir->Write();
  80. }
  81. void
  82. CV2PrivateKey::Exportable(bool flag)
  83. {
  84. m_apcir->Flag(PrivExportableFlag, flag);
  85. }
  86. void
  87. CV2PrivateKey::ID(string const &rstrID)
  88. {
  89. m_apcir->Symbol(&m_apcir->m_bID, rstrID);
  90. }
  91. string
  92. CV2PrivateKey::InternalAuth(string const &rstrOld)
  93. {
  94. CTransactionWrap wrap(m_hcard);
  95. m_apcir->Read();
  96. string strRetVal;
  97. if (rstrOld.size() > 0x80)
  98. throw Exception(ccBadLength);
  99. if(m_apcir->m_bKeyType == CardKeyTypeNone)
  100. throw Exception(ccKeyNotFound);
  101. BYTE bKeyNum = m_apcir->m_bKeyNum;
  102. BYTE bData[128];
  103. m_hcard->SmartCard().Select("/3f00/3f11/3f03");
  104. // TODO: Handling of keys with length != 1024 is incomplete
  105. m_hcard->SmartCard().InternalAuth(ktRSA1024, bKeyNum,
  106. static_cast<BYTE>(rstrOld.length()),
  107. reinterpret_cast<BYTE const *>(rstrOld.data()),
  108. bData);
  109. strRetVal = string(reinterpret_cast<char *>(bData), rstrOld.length());
  110. m_hcard->SmartCard().Select("/3f00/3f11");
  111. return strRetVal;
  112. }
  113. void
  114. CV2PrivateKey::Label(string const &rstrLabel)
  115. {
  116. m_apcir->Symbol(&m_apcir->m_bLabel, rstrLabel);
  117. }
  118. void
  119. CV2PrivateKey::Local(bool flag)
  120. {
  121. m_apcir->Flag(PrivLocalFlag, flag);
  122. }
  123. CV2PrivateKey *
  124. CV2PrivateKey::Make(CV2Card const &rv2card,
  125. SymbolID sidHandle,
  126. ObjectAccess oa)
  127. {
  128. return new CV2PrivateKey(rv2card, sidHandle, oa);
  129. }
  130. void
  131. CV2PrivateKey::Modifiable(bool flag)
  132. {
  133. m_apcir->Flag(PrivModifiableFlag, flag);
  134. }
  135. void
  136. CV2PrivateKey::Modulus(string const &rstrModulus)
  137. {
  138. m_apcir->Symbol(&m_apcir->m_bModulus, rstrModulus);
  139. }
  140. void
  141. CV2PrivateKey::NeverExportable(bool flag)
  142. {
  143. m_apcir->Flag(PrivNeverExportableFlag, flag);
  144. }
  145. void
  146. CV2PrivateKey::NeverRead(bool flag)
  147. {
  148. m_apcir->Flag(PrivNeverReadFlag, flag);
  149. }
  150. void
  151. CV2PrivateKey::PublicExponent(string const &rstrExponent)
  152. {
  153. m_apcir->Symbol(&m_apcir->m_bPublExponent, rstrExponent);
  154. }
  155. void
  156. CV2PrivateKey::Read(bool flag)
  157. {
  158. m_apcir->Flag(PrivReadFlag, flag);
  159. }
  160. void
  161. CV2PrivateKey::Sign(bool flag)
  162. {
  163. m_apcir->Flag(PrivSignFlag, flag);
  164. }
  165. void
  166. CV2PrivateKey::SignRecover(bool flag)
  167. {
  168. m_apcir->Flag(PrivSignRecoverFlag, flag);
  169. }
  170. void
  171. CV2PrivateKey::StartDate(Date &rdtStart)
  172. {
  173. CTransactionWrap wrap(m_hcard);
  174. m_apcir->Read();
  175. m_apcir->m_dtStart = rdtStart;
  176. m_apcir->Write();
  177. }
  178. void
  179. CV2PrivateKey::Subject(string const &rstrSubject)
  180. {
  181. m_apcir->Symbol(&m_apcir->m_bSubject, rstrSubject);
  182. }
  183. void
  184. CV2PrivateKey::Unwrap(bool flag)
  185. {
  186. m_apcir->Flag(PrivUnwrapFlag, flag);
  187. }
  188. // Access
  189. string
  190. CV2PrivateKey::CredentialID()
  191. {
  192. return m_apcir->Symbol(&m_apcir->m_bCredentialID);
  193. }
  194. bool
  195. CV2PrivateKey::Decrypt()
  196. {
  197. return m_apcir->Flag(PrivDecryptFlag);
  198. }
  199. bool
  200. CV2PrivateKey::Derive()
  201. {
  202. return m_apcir->Flag(PrivDeriveFlag);
  203. }
  204. Date
  205. CV2PrivateKey::EndDate()
  206. {
  207. CTransactionWrap wrap(m_hcard);
  208. m_apcir->Read();
  209. return m_apcir->m_dtEnd;
  210. }
  211. bool
  212. CV2PrivateKey::Exportable()
  213. {
  214. return m_apcir->Flag(PrivExportableFlag);
  215. }
  216. SymbolID
  217. CV2PrivateKey::Handle() const
  218. {
  219. return m_sidHandle;
  220. }
  221. string
  222. CV2PrivateKey::ID()
  223. {
  224. return m_apcir->Symbol(&m_apcir->m_bID);
  225. }
  226. string
  227. CV2PrivateKey::Label()
  228. {
  229. return m_apcir->Symbol(&m_apcir->m_bLabel);
  230. }
  231. bool
  232. CV2PrivateKey::Local()
  233. {
  234. return m_apcir->Flag(PrivLocalFlag);
  235. }
  236. bool
  237. CV2PrivateKey::Modifiable()
  238. {
  239. return m_apcir->Flag(PrivModifiableFlag);
  240. }
  241. string
  242. CV2PrivateKey::Modulus()
  243. {
  244. return m_apcir->Symbol(&m_apcir->m_bModulus);
  245. }
  246. bool
  247. CV2PrivateKey::NeverExportable()
  248. {
  249. return m_apcir->Flag(PrivNeverExportableFlag);
  250. }
  251. bool
  252. CV2PrivateKey::NeverRead()
  253. {
  254. return m_apcir->Flag(PrivNeverReadFlag);
  255. }
  256. bool
  257. CV2PrivateKey::Private()
  258. {
  259. return m_apcir->Private();
  260. }
  261. string
  262. CV2PrivateKey::PublicExponent()
  263. {
  264. return m_apcir->Symbol(&m_apcir->m_bPublExponent);
  265. }
  266. bool
  267. CV2PrivateKey::Read()
  268. {
  269. return m_apcir->Flag(PrivReadFlag);
  270. }
  271. bool
  272. CV2PrivateKey::Sign()
  273. {
  274. return m_apcir->Flag(PrivSignFlag);
  275. }
  276. bool
  277. CV2PrivateKey::SignRecover()
  278. {
  279. return m_apcir->Flag(PrivSignRecoverFlag);
  280. }
  281. Date
  282. CV2PrivateKey::StartDate()
  283. {
  284. CTransactionWrap wrap(m_hcard);
  285. m_apcir->Read();
  286. return m_apcir->m_dtStart;
  287. }
  288. string
  289. CV2PrivateKey::Subject()
  290. {
  291. return m_apcir->Symbol(&m_apcir->m_bSubject);
  292. }
  293. bool
  294. CV2PrivateKey::Unwrap()
  295. {
  296. return m_apcir->Flag(PrivUnwrapFlag);
  297. }
  298. // Predicates
  299. // Static Variables
  300. /////////////////////////// PROTECTED /////////////////////////////////
  301. // C'tors/D'tors
  302. // Operators
  303. bool
  304. CV2PrivateKey::DoEquals(CAbstractPrivateKey const &rhs) const
  305. {
  306. CV2PrivateKey const &rv2rhs =
  307. scu::DownCast<CV2PrivateKey const &, CAbstractPrivateKey const &>(rhs);
  308. return (rv2rhs.m_sidHandle == m_sidHandle);
  309. }
  310. // Operations
  311. void
  312. CV2PrivateKey::DoDelete()
  313. {
  314. m_apcir->Read();
  315. CV2Card &rv2card = scu::DownCast<CV2Card &, CAbstractCard &>(*m_hcard);
  316. CObjectInfoFile &roif = rv2card.ObjectInfoFile(m_oa);
  317. // Remove allocation of entry in private key file
  318. if (m_apcir->m_bKeyType!=CardKeyTypeNone)
  319. {
  320. CCardInfo &rci = rv2card.CardInfo();
  321. rci.FreePrivateKey(m_apcir->m_bKeyType, m_apcir->m_bKeyNum);
  322. }
  323. // Release symbols
  324. if (m_apcir->m_bLabel)
  325. roif.RemoveSymbol(m_apcir->m_bLabel);
  326. if (m_apcir->m_bID)
  327. roif.RemoveSymbol(m_apcir->m_bID);
  328. if (m_apcir->m_bCredentialID)
  329. roif.RemoveSymbol(m_apcir->m_bCredentialID);
  330. if (m_apcir->m_bSubject)
  331. roif.RemoveSymbol(m_apcir->m_bSubject);
  332. if (m_apcir->m_bModulus)
  333. roif.RemoveSymbol(m_apcir->m_bModulus);
  334. if (m_apcir->m_bPublExponent)
  335. roif.RemoveSymbol(m_apcir->m_bPublExponent);
  336. // Delete info record
  337. roif.RemoveObject(otPrivateKeyObject, m_sidHandle);
  338. }
  339. void
  340. CV2PrivateKey::DoWriteKey(CPrivateKeyBlob const &rblob)
  341. {
  342. m_apcir->Read();
  343. BYTE bKeyType;
  344. KeyType kt;
  345. switch(rblob.bPLen)
  346. {
  347. case 0x20:
  348. bKeyType = CardKeyTypeRSA512;
  349. kt = ktRSA512;
  350. break;
  351. case 0x30:
  352. bKeyType = CardKeyTypeRSA768;
  353. kt = ktRSA768;
  354. break;
  355. case 0x40:
  356. bKeyType = CardKeyTypeRSA1024;
  357. kt = ktRSA1024;
  358. break;
  359. default:
  360. throw Exception(ccBadKeySpec);
  361. }
  362. // Allocated a slot in the key file, unless a correct one is
  363. // already allocated?
  364. CV2Card &rv2card = scu::DownCast<CV2Card &, CAbstractCard &>(*m_hcard);
  365. CCardInfo &rci = rv2card.CardInfo();
  366. BYTE bKeyNum;
  367. if (CardKeyTypeNone != m_apcir->m_bKeyType)
  368. {
  369. if (m_apcir->m_bKeyType != bKeyType)
  370. {
  371. rci.FreePrivateKey(m_apcir->m_bKeyType, m_apcir->m_bKeyNum);
  372. m_apcir->m_bKeyType = CardKeyTypeNone;
  373. m_apcir->Write();
  374. }
  375. else
  376. bKeyNum = m_apcir->m_bKeyNum;
  377. }
  378. if (CardKeyTypeNone == m_apcir->m_bKeyType)
  379. bKeyNum = rci.AllocatePrivateKey(bKeyType);
  380. // Store private key blob
  381. rv2card.SmartCard().Select(rv2card.PrivateKeyPath(kt).c_str());
  382. rv2card.SmartCard().WritePrivateKey(rblob, bKeyNum);
  383. m_apcir->m_bKeyType = bKeyType;
  384. m_apcir->m_bKeyNum = bKeyNum;
  385. rv2card.SmartCard().Select(rv2card.RootPath().c_str());
  386. m_apcir->Write();
  387. }
  388. // Access
  389. // Predicates
  390. // Static Variables
  391. /////////////////////////// PRIVATE /////////////////////////////////
  392. // C'tors/D'tors
  393. // Operators
  394. // Operations
  395. void
  396. CV2PrivateKey::Setup(CV2Card const &rv2card)
  397. {
  398. Setup(rv2card,
  399. rv2card.ObjectInfoFile(m_oa).AddObject(otPrivateKeyObject,
  400. PrivInfoRecordSize));
  401. }
  402. void
  403. CV2PrivateKey::Setup(CV2Card const &rv2card,
  404. SymbolID sidHandle)
  405. {
  406. m_sidHandle = sidHandle;
  407. m_apcir =
  408. auto_ptr<CPriKeyInfoRecord>(new CPriKeyInfoRecord(rv2card,
  409. m_sidHandle,
  410. m_oa));
  411. }
  412. // Access
  413. // Predicates
  414. // Static Variables