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.

182 lines
4.7 KiB

  1. // Pkcs11Attr.cpp -- Implementation of PKCS #11 Attributes class for
  2. // interoperability with Netscape and Entrust using the SLB PKCS#11
  3. // package.
  4. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  5. // 1999. This computer program includes Confidential, Proprietary
  6. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  7. // use, disclosure, and/or reproduction is prohibited unless authorized
  8. // in writing. All Rights Reserved.
  9. #include "NoWarning.h"
  10. #include "ForceLib.h"
  11. // Don't allow the min & max methods in <limits> to be superceded by
  12. // the min/max macros in <windef.h>
  13. #define NOMINMAX
  14. #include <limits>
  15. #include <functional>
  16. #include <numeric>
  17. #include <iterator>
  18. #include <algorithm>
  19. #include <sstream>
  20. #include <malloc.h> // for _alloca
  21. #include <cciCard.h>
  22. #include <cciCont.h>
  23. #include "Pkcs11Attr.h"
  24. #include "AuxHash.h"
  25. using namespace std;
  26. using namespace pki;
  27. /////////////////////////// HELPER /////////////////////////////////
  28. namespace
  29. {
  30. class JoinWith
  31. : public binary_function<string, string, string>
  32. {
  33. public:
  34. explicit
  35. JoinWith(second_argument_type const &rGlue)
  36. : m_Glue(rGlue)
  37. {}
  38. result_type
  39. operator()(string const &rFirst,
  40. string const &rSecond) const
  41. {
  42. return rFirst + m_Glue + rSecond;
  43. }
  44. private:
  45. second_argument_type const m_Glue;
  46. };
  47. string
  48. Combine(vector<string> const &rvsNames)
  49. {
  50. static string::value_type const cBlank = ' ';
  51. static string const sBlank(1, cBlank);
  52. return accumulate(rvsNames.begin() + 1, rvsNames.end(),
  53. *rvsNames.begin(), JoinWith(sBlank));
  54. }
  55. } // namespace
  56. /////////////////////////// PUBLIC /////////////////////////////////
  57. // Types
  58. // C'tors/D'tors
  59. Pkcs11Attributes::Pkcs11Attributes(Blob const &rCertificate,
  60. HCRYPTPROV hprovContext)
  61. : m_x509cert(AsString(rCertificate)),
  62. m_hprovContext(hprovContext)
  63. {
  64. }
  65. // Operators
  66. // Operations
  67. // Access
  68. Blob
  69. Pkcs11Attributes::ContainerId()
  70. {
  71. AuxHash ah(AuxContext(m_hprovContext), CALG_MD5);
  72. return ah.Value(AsBlob(Subject()));
  73. }
  74. Blob
  75. Pkcs11Attributes::EndDate() const
  76. {
  77. return Blob(3, 0); // TO DO: Set date
  78. }
  79. Blob
  80. Pkcs11Attributes::Issuer()
  81. {
  82. return AsBlob(m_x509cert.Issuer());
  83. }
  84. string
  85. Pkcs11Attributes::Label()
  86. {
  87. string sFullName(Combine(m_x509cert.SubjectCommonName()));
  88. string sLabel(sFullName);
  89. static string const sNameSuffix = "'s ";
  90. sLabel.append(sNameSuffix);
  91. string sOrganizationName(Combine(m_x509cert.IssuerOrg()));
  92. sLabel.append(sOrganizationName);
  93. static string const sLabelSuffix = " ID";
  94. sLabel.append(sLabelSuffix);
  95. return sLabel;
  96. }
  97. Blob
  98. Pkcs11Attributes::Modulus()
  99. {
  100. return AsBlob(m_x509cert.Modulus());
  101. }
  102. Blob
  103. Pkcs11Attributes::RawModulus()
  104. {
  105. return AsBlob(m_x509cert.RawModulus());
  106. }
  107. Blob
  108. Pkcs11Attributes::SerialNumber()
  109. {
  110. return AsBlob(m_x509cert.SerialNumber());
  111. }
  112. Blob
  113. Pkcs11Attributes::StartDate() const
  114. {
  115. return Blob(3, 0); // TO DO: Set date
  116. }
  117. string
  118. Pkcs11Attributes::Subject()
  119. {
  120. return m_x509cert.Subject();
  121. }
  122. // Predicates
  123. // Static Variables
  124. /////////////////////////// PROTECTED /////////////////////////////////
  125. // Types
  126. // C'tors/D'tors
  127. // Operators
  128. // Operations
  129. // Access
  130. // Predicates
  131. // Static Variables
  132. /////////////////////////// PRIVATE /////////////////////////////////
  133. // Types
  134. // C'tors/D'tors
  135. // Operators
  136. // Operations
  137. // Access
  138. // Predicates
  139. // Static Variables