CryptoPrivateKeyDss.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 data = key.ToArray();
  93. //using (var sha1 = new Renci.SshNet.Security.Cryptography.SHA1Hash())
  94. using (var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider())
  95. {
  96. using (var cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, sha1, System.Security.Cryptography.CryptoStreamMode.Write))
  97. {
  98. cs.Write(data, 0, data.Length);
  99. }
  100. var dsaKeyInfo = new System.Security.Cryptography.DSAParameters();
  101. dsaKeyInfo.X = this._privateKey.TrimLeadingZero().ToArray();
  102. dsaKeyInfo.P = this._p.TrimLeadingZero().ToArray();
  103. dsaKeyInfo.Q = this._q.TrimLeadingZero().ToArray();
  104. dsaKeyInfo.G = this._g.TrimLeadingZero().ToArray();
  105. using (var DSA = new System.Security.Cryptography.DSACryptoServiceProvider())
  106. {
  107. DSA.ImportParameters(dsaKeyInfo);
  108. var DSAFormatter = new DSASignatureFormatter(DSA);
  109. DSAFormatter.SetHashAlgorithm("SHA1");
  110. var signature = DSAFormatter.CreateSignature(sha1);
  111. return new SignatureKeyData
  112. {
  113. AlgorithmName = this.Name,
  114. Signature = signature,
  115. }.GetBytes().ToArray();
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Gets key data byte array.
  121. /// </summary>
  122. /// <returns>
  123. /// The data byte array.
  124. /// </returns>
  125. public override IEnumerable<byte> GetBytes()
  126. {
  127. throw new NotImplementedException();
  128. }
  129. private static int GetIntegerSize(BinaryReader binr)
  130. {
  131. byte bt = 0;
  132. byte lowbyte = 0x00;
  133. byte highbyte = 0x00;
  134. int count = 0;
  135. bt = binr.ReadByte();
  136. if (bt != 0x02) //expect integer
  137. return 0;
  138. bt = binr.ReadByte();
  139. if (bt == 0x81)
  140. count = binr.ReadByte(); // data size in next byte
  141. else
  142. if (bt == 0x82)
  143. {
  144. highbyte = binr.ReadByte(); // data size in next 2 bytes
  145. lowbyte = binr.ReadByte();
  146. byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
  147. count = (int)(modint[3] << 24 | modint[2] << 16 | modint[1] << 8 | modint[0]);
  148. }
  149. else
  150. {
  151. count = bt; // we already have the data size
  152. }
  153. return count;
  154. }
  155. }
  156. }