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.

234 lines
6.8 KiB

  1. // V1KeyPair.cpp: implementation of the CV1KeyPair 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 <scuCast.h>
  11. #include "slbCci.h"
  12. #include "cciCard.h"
  13. #include "TransactionWrap.h"
  14. #include "V1Cert.h"
  15. #include "V1Cont.h"
  16. #include "V1ContRec.h"
  17. #include "V1KeyPair.h"
  18. #include "V1PriKey.h"
  19. #include "V1PubKey.h"
  20. using namespace std;
  21. using namespace cci;
  22. /////////////////////////// LOCAL/HELPER /////////////////////////////////
  23. namespace
  24. {
  25. // Functor to call the binary T::Make
  26. template<class T>
  27. class Maker
  28. {
  29. public:
  30. Maker(CCard const &rhcard)
  31. : m_rv1card(scu::DownCast<CV1Card const &,
  32. CAbstractCard const &>(*rhcard))
  33. {}
  34. auto_ptr<T>
  35. operator()(KeySpec ks) const
  36. {
  37. return auto_ptr<T>(T::Make(m_rv1card, ks));
  38. }
  39. private:
  40. CV1Card const &m_rv1card;
  41. };
  42. // Update the cache handle. If the the handle has not been cached
  43. // and the key pair exists, then make the key pair.
  44. template<class R, class T>
  45. void
  46. UpdateCache(CArchivedValue<R> &ravhCache,
  47. CV1Container &rv1cntr,
  48. KeySpec ks,
  49. Maker<T> const &rMaker)
  50. {
  51. if (!ravhCache.IsCached())
  52. {
  53. auto_ptr<T> apObject(rv1cntr.Record().KeyExists(ks)
  54. ? rMaker(ks)
  55. : auto_ptr<T>(0));
  56. R Handle(apObject.get());
  57. apObject.release(); // transfer ownership
  58. ravhCache.Value(Handle);
  59. }
  60. }
  61. } // namespace
  62. /////////////////////////// PUBLIC /////////////////////////////////
  63. // Types
  64. // C'tors/D'tors
  65. CV1KeyPair::CV1KeyPair(CV1Card const &rv1card,
  66. CContainer const &rhcont,
  67. KeySpec ks)
  68. : CAbstractKeyPair(rv1card, rhcont, ks),
  69. m_avhcert(),
  70. m_avhprikey(),
  71. m_avhpubkey(),
  72. m_cntrCert(rv1card, CV1ContainerRecord::CertName(), false)
  73. {}
  74. CV1KeyPair::~CV1KeyPair() throw()
  75. {}
  76. // Operators
  77. // Operations
  78. void
  79. CV1KeyPair::Certificate(CCertificate const &rcert)
  80. {
  81. CTransactionWrap wrap(m_hcard);
  82. if (rcert)
  83. {
  84. CV1Certificate &rv1cert =
  85. scu::DownCast<CV1Certificate &, CAbstractCertificate &>(*rcert);
  86. rv1cert.AssociateWith(m_ks);
  87. }
  88. // else to preserve the CCI semantics, the certificate is
  89. // actually deleted using CAbstractCertificate::Delete;
  90. // otherwise calling rcert->Delete here would be "infinitely"
  91. // recursive. Unfortuantely, this means the Certificate
  92. // could "reappear" if CAbstractCertificate::Delete was never
  93. // called.
  94. m_avhcert.Value(rcert);
  95. }
  96. void
  97. CV1KeyPair::PrivateKey(CPrivateKey const &rprikey)
  98. {
  99. CTransactionWrap wrap(m_hcard);
  100. if (rprikey)
  101. {
  102. CV1PrivateKey &rv1prikey =
  103. scu::DownCast<CV1PrivateKey &, CAbstractPrivateKey &>(*rprikey);
  104. rv1prikey.AssociateWith(m_ks);
  105. }
  106. // else to preserve the CCI semantics, the key is
  107. // actually deleted using CAbstractPrivateKey::Delete;
  108. // otherwise calling rprikey->Delete here would be "infinitely"
  109. // recursive. Unfortuantely, this means the Certificate
  110. // could "reappear" if CAbstractPrivateKey::Delete was never
  111. // called.
  112. m_avhprikey.Value(rprikey);
  113. }
  114. void
  115. CV1KeyPair::PublicKey(CPublicKey const &rpubkey)
  116. {
  117. CTransactionWrap wrap(m_hcard);
  118. if (rpubkey)
  119. {
  120. CV1PublicKey &rv1pubkey =
  121. scu::DownCast<CV1PublicKey &, CAbstractPublicKey &>(*rpubkey);
  122. rv1pubkey.AssociateWith(m_ks);
  123. }
  124. // else to preserve the CCI semantics, the key is
  125. // actually deleted using CAbstractPublicKey::Delete;
  126. // otherwise calling rpubkey->Delete here would be "infinitely"
  127. // recursive. Unfortuantely, this means the Certificate
  128. // could "reappear" if CAbstractPublicKey::Delete was never
  129. // called.
  130. m_avhpubkey.Value(rpubkey);
  131. }
  132. // Access
  133. CCertificate
  134. CV1KeyPair::Certificate() const
  135. {
  136. CTransactionWrap wrap(m_hcard);
  137. UpdateCache(m_avhcert, m_cntrCert, m_ks,
  138. Maker<CV1Certificate>(m_hcard));
  139. return m_avhcert.Value();
  140. }
  141. CPrivateKey
  142. CV1KeyPair::PrivateKey() const
  143. {
  144. CTransactionWrap wrap(m_hcard);
  145. CV1Container &rv1cntr =
  146. scu::DownCast<CV1Container &, CAbstractContainer &>(*m_hcont);
  147. UpdateCache(m_avhprikey, rv1cntr, m_ks,
  148. Maker<CV1PrivateKey>(m_hcard));
  149. return m_avhprikey.Value();
  150. }
  151. CPublicKey
  152. CV1KeyPair::PublicKey() const
  153. {
  154. CTransactionWrap wrap(m_hcard);
  155. CV1Container &rv1cntr =
  156. scu::DownCast<CV1Container &, CAbstractContainer &>(*m_hcont);
  157. UpdateCache(m_avhpubkey, rv1cntr, m_ks,
  158. Maker<CV1PublicKey>(m_hcard));
  159. return m_avhpubkey.Value();
  160. }
  161. // Predicates
  162. bool
  163. CV1KeyPair::DoEquals(CAbstractKeyPair const &rhs) const
  164. {
  165. // Only one key pair can exists so must be the same.
  166. return true;
  167. }
  168. // Static Variables
  169. /////////////////////////// PROTECTED /////////////////////////////////
  170. // C'tors/D'tors
  171. // Operators
  172. // Operations
  173. // Access
  174. // Predicates
  175. // Static Variables
  176. /////////////////////////// PRIVATE /////////////////////////////////
  177. // C'tors/D'tors
  178. // Operators
  179. // Operations
  180. // Access
  181. // Predicates
  182. // Static Variables