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.

70 lines
2.1 KiB

  1. // PublicKeyHelper.cpp -- Helper routines to deal with CCI public keys
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 2000. 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 "PublicKeyHelper.h"
  8. using namespace cci;
  9. /////////////////////////// HELPERS /////////////////////////////////
  10. CPublicKey
  11. AsPublicKey(Blob const &rblbModulus, // little endian
  12. DWORD dwExponent,
  13. cci::CCard &rhcard)
  14. {
  15. Blob blbExponent(reinterpret_cast<Blob::value_type *>(&dwExponent),
  16. sizeof dwExponent);
  17. return AsPublicKey(rblbModulus, blbExponent, rhcard);
  18. }
  19. CPublicKey
  20. AsPublicKey(Blob const &rblbModulus, // little endian
  21. Blob const &rblbExponent, // little endian
  22. CCard &rhcard)
  23. {
  24. Blob blbTmpModulus(rblbModulus);
  25. Blob blbTmpExponent(rblbExponent);
  26. if (rhcard->IsPKCS11Enabled())
  27. {
  28. // store modulus and exponent compressed
  29. TrimExtraZeroes(blbTmpModulus);
  30. TrimExtraZeroes(blbTmpExponent);
  31. }
  32. CPublicKey hpubkey(rhcard);
  33. hpubkey->Modulus(AsString(blbTmpModulus));
  34. hpubkey->Exponent(AsString(blbTmpExponent));
  35. return hpubkey;
  36. }
  37. void
  38. TrimExtraZeroes(Blob &rblob)
  39. {
  40. Blob::size_type const cLength = rblob.length();
  41. if (0 != cLength)
  42. {
  43. Blob::value_type const Zero = 0;
  44. Blob::size_type const cLastNonZero =
  45. rblob.find_last_not_of(Zero); // little endian
  46. Blob::size_type const cLastPos = cLength - 1;
  47. if (cLastPos != cLastNonZero)
  48. {
  49. Blob::size_type cCharToKeep =
  50. (Blob::npos == cLastNonZero)
  51. ? 0
  52. : cLastNonZero + 1;
  53. if (cLastPos != cCharToKeep) // keep one zero
  54. rblob.erase(cCharToKeep + 1);
  55. }
  56. }
  57. }