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.

154 lines
4.5 KiB

  1. // MsRsaKeyBlob.cpp -- MicroSoft RSA Key Blob class implementation
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #include "NoWarning.h"
  8. #include "ForceLib.h"
  9. #include <limits>
  10. #include <scuOsExc.h>
  11. #include "MsRsaKB.h"
  12. using namespace std;
  13. using namespace scu;
  14. /////////////////////////// PUBLIC /////////////////////////////////
  15. // Types
  16. // C'tors/D'tors
  17. MsRsaKeyBlob::MsRsaKeyBlob(KeyBlobType kbt,
  18. ALG_ID ai,
  19. StrengthType strength,
  20. Blob const &rbPublicExponent,
  21. SizeType cReserve)
  22. : MsKeyBlob(kbt, ai, sizeof HeaderElementType + cReserve),
  23. RsaKey()
  24. {
  25. if (!((CALG_RSA_SIGN == ai) || (CALG_RSA_KEYX == ai)))
  26. throw scu::OsException(ERROR_INVALID_PARAMETER);
  27. MagicConstant mc;
  28. switch (kbt)
  29. {
  30. case PRIVATEKEYBLOB:
  31. mc = mcPrivate;
  32. break;
  33. case PUBLICKEYBLOB:
  34. mc = mcPublic;
  35. break;
  36. default:
  37. throw scu::OsException(ERROR_INVALID_PARAMETER);
  38. break;
  39. }
  40. HeaderElementType rsapubkey =
  41. {
  42. mc,
  43. strength,
  44. 0 // help pad exponent below
  45. };
  46. // finish constructing rsapubkey header by appending the exponent,
  47. // guarding against buffer overflow
  48. if (sizeof rsapubkey.pubexp <
  49. (rbPublicExponent.size() * sizeof BlobElemType))
  50. throw scu::OsException(NTE_BAD_DATA);
  51. // store exponent left-justified to pad with zeroes
  52. memcpy(&rsapubkey.pubexp, rbPublicExponent.data(),
  53. rbPublicExponent.length());
  54. Append(reinterpret_cast<BlobElemType *>(&rsapubkey),
  55. sizeof rsapubkey);
  56. }
  57. MsRsaKeyBlob::MsRsaKeyBlob(BYTE const *pbData,
  58. DWORD dwDataLength)
  59. : MsKeyBlob(pbData, dwDataLength),
  60. RsaKey()
  61. {
  62. switch (MsKeyBlob::Data()->bType)
  63. {
  64. case PRIVATEKEYBLOB:
  65. if (mcPrivate != Data()->rsapubkey.magic)
  66. throw scu::OsException(NTE_BAD_TYPE);
  67. break;
  68. case PUBLICKEYBLOB:
  69. if (mcPublic != Data()->rsapubkey.magic)
  70. throw scu::OsException(NTE_BAD_TYPE);
  71. break;
  72. default:
  73. throw scu::OsException(NTE_BAD_TYPE);
  74. break;
  75. }
  76. ALG_ID const ai = Data()->keyblob.aiKeyAlg;
  77. if (!((CALG_RSA_KEYX == ai) || (CALG_RSA_SIGN == ai)))
  78. throw scu::OsException(NTE_BAD_TYPE);
  79. }
  80. MsRsaKeyBlob::~MsRsaKeyBlob()
  81. {}
  82. // Operators
  83. // Operations
  84. // Access
  85. MsRsaKeyBlob::BitLengthType
  86. MsRsaKeyBlob::BitLength() const
  87. {
  88. return Data()->rsapubkey.bitlen;
  89. }
  90. MsRsaKeyBlob::ValueType const *
  91. MsRsaKeyBlob::Data() const
  92. {
  93. return reinterpret_cast<ValueType const *>(MsKeyBlob::Data());
  94. }
  95. MsRsaKeyBlob::ModulusLengthType
  96. MsRsaKeyBlob::Length() const
  97. {
  98. return BitLength() / numeric_limits<ElementValueType>::digits;
  99. }
  100. MsRsaKeyBlob::PublicExponentType
  101. MsRsaKeyBlob::PublicExponent() const
  102. {
  103. return Data()->rsapubkey.pubexp;
  104. }
  105. // Predicates
  106. // Static Variables
  107. /////////////////////////// PROTECTED /////////////////////////////////
  108. // C'tors/D'tors
  109. // Operators
  110. // Operations
  111. // Access
  112. // Predicates
  113. // Static Variables
  114. /////////////////////////// PRIVATE /////////////////////////////////
  115. // C'tors/D'tors
  116. // Operators
  117. // Operations
  118. // Access
  119. // Predicates
  120. // Static Variables