CryptoPrivateKeyDss.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Security.Cryptography;
  8. namespace Renci.SshNet.Security
  9. {
  10. /// <summary>
  11. /// Represents DSS private key
  12. /// </summary>
  13. internal class CryptoPrivateKeyDss : CryptoPrivateKey
  14. {
  15. private byte[] _p;
  16. private byte[] _q;
  17. private byte[] _g;
  18. private byte[] _publicKey;
  19. private byte[] _privateKey;
  20. /// <summary>
  21. /// Gets key name.
  22. /// </summary>
  23. public override string Name
  24. {
  25. get { return "ssh-dss"; }
  26. }
  27. /// <summary>
  28. /// Loads key specific data.
  29. /// </summary>
  30. /// <param name="data">The data.</param>
  31. public override void Load(IEnumerable<byte> data)
  32. {
  33. MemoryStream ms = null;
  34. try
  35. {
  36. ms = new MemoryStream(data.ToArray());
  37. using (var binr = new BinaryReader(ms)) //wrap Memory Stream with BinaryReader for easy reading
  38. {
  39. byte bt = 0;
  40. ushort twobytes = 0;
  41. int elems = 0;
  42. twobytes = binr.ReadUInt16();
  43. if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
  44. binr.ReadByte(); //advance 1 byte
  45. else if (twobytes == 0x8230)
  46. binr.ReadInt16(); //advance 2 bytes
  47. else
  48. throw new SshException("DSS key is not valid for use in specified state");
  49. twobytes = binr.ReadUInt16();
  50. if (twobytes != 0x0102) //version number
  51. throw new SshException("DSS key version is not supported.");
  52. bt = binr.ReadByte();
  53. if (bt != 0x00)
  54. throw new SshException("DSS key is not valid for use in specified state");
  55. //------ all private key components are Integer sequences ----
  56. elems = CryptoPrivateKeyDss.GetIntegerSize(binr);
  57. this._p = binr.ReadBytes(elems);
  58. elems = CryptoPrivateKeyDss.GetIntegerSize(binr);
  59. this._q = binr.ReadBytes(elems);
  60. elems = CryptoPrivateKeyDss.GetIntegerSize(binr);
  61. this._g = binr.ReadBytes(elems);
  62. elems = CryptoPrivateKeyDss.GetIntegerSize(binr);
  63. this._publicKey = binr.ReadBytes(elems);
  64. elems = CryptoPrivateKeyDss.GetIntegerSize(binr);
  65. this._privateKey = binr.ReadBytes(elems);
  66. }
  67. }
  68. finally
  69. {
  70. if (ms != null)
  71. {
  72. ms.Dispose();
  73. ms = null;
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// Gets the public key.
  79. /// </summary>
  80. /// <returns></returns>
  81. public override CryptoPublicKey GetPublicKey()
  82. {
  83. return new CryptoPublicKeyDss(this._p, this._q, this._g, this._publicKey);
  84. }
  85. /// <summary>
  86. /// Gets the signature.
  87. /// </summary>
  88. /// <param name="key">The key data bytes.</param>
  89. /// <returns></returns>
  90. public override byte[] GetSignature(IEnumerable<byte> key)
  91. {
  92. var ss1 = new DsaDigitalSignature(this._p, this._q, this._g, this._privateKey, null);
  93. var signature = ss1.CreateSignature(key.ToArray());
  94. return new SignatureKeyData
  95. {
  96. AlgorithmName = this.Name,
  97. Signature = signature,
  98. }.GetBytes().ToArray();
  99. }
  100. /// <summary>
  101. /// Gets key data byte array.
  102. /// </summary>
  103. /// <returns>
  104. /// The data byte array.
  105. /// </returns>
  106. public override IEnumerable<byte> GetBytes()
  107. {
  108. throw new NotImplementedException();
  109. }
  110. private static int GetIntegerSize(BinaryReader binr)
  111. {
  112. byte bt = 0;
  113. byte lowbyte = 0x00;
  114. byte highbyte = 0x00;
  115. int count = 0;
  116. bt = binr.ReadByte();
  117. if (bt != 0x02) //expect integer
  118. return 0;
  119. bt = binr.ReadByte();
  120. if (bt == 0x81)
  121. count = binr.ReadByte(); // data size in next byte
  122. else
  123. if (bt == 0x82)
  124. {
  125. highbyte = binr.ReadByte(); // data size in next 2 bytes
  126. lowbyte = binr.ReadByte();
  127. byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
  128. count = (int)(modint[3] << 24 | modint[2] << 16 | modint[1] << 8 | modint[0]);
  129. }
  130. else
  131. {
  132. count = bt; // we already have the data size
  133. }
  134. return count;
  135. }
  136. }
  137. }