2
0

KeyHostAlgorithm.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections.Generic;
  2. using Renci.SshNet.Common;
  3. namespace Renci.SshNet.Security
  4. {
  5. /// <summary>
  6. /// Implements key support for host algorithm.
  7. /// </summary>
  8. public class KeyHostAlgorithm : HostAlgorithm
  9. {
  10. /// <summary>
  11. /// Gets the key.
  12. /// </summary>
  13. public Key Key { get; private set; }
  14. /// <summary>
  15. /// Gets the public key data.
  16. /// </summary>
  17. public override byte[] Data
  18. {
  19. get
  20. {
  21. return new SshKeyData(this.Name, this.Key.Public).GetBytes();
  22. }
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="KeyHostAlgorithm"/> class.
  26. /// </summary>
  27. /// <param name="name">Host key name.</param>
  28. /// <param name="key">Host key.</param>
  29. public KeyHostAlgorithm(string name, Key key)
  30. : base(name)
  31. {
  32. this.Key = key;
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="HostAlgorithm"/> class.
  36. /// </summary>
  37. /// <param name="name">Host key name.</param>
  38. /// <param name="key">Host key.</param>
  39. /// <param name="data">Host key encoded data.</param>
  40. public KeyHostAlgorithm(string name, Key key, byte[] data)
  41. : base(name)
  42. {
  43. this.Key = key;
  44. var sshKey = new SshKeyData();
  45. sshKey.Load(data);
  46. this.Key.Public = sshKey.Keys;
  47. }
  48. /// <summary>
  49. /// Signs the specified data.
  50. /// </summary>
  51. /// <param name="data">The data.</param>
  52. /// <returns>
  53. /// Signed data.
  54. /// </returns>
  55. public override byte[] Sign(byte[] data)
  56. {
  57. return new SignatureKeyData(this.Name, this.Key.Sign(data)).GetBytes();
  58. }
  59. /// <summary>
  60. /// Verifies the signature.
  61. /// </summary>
  62. /// <param name="data">The data.</param>
  63. /// <param name="signature">The signature.</param>
  64. /// <returns>
  65. /// <c>True</c> is signature was successfully verifies; otherwise <c>false</c>.
  66. /// </returns>
  67. public override bool VerifySignature(byte[] data, byte[] signature)
  68. {
  69. var signatureData = new SignatureKeyData();
  70. signatureData.Load(signature);
  71. return this.Key.VerifySignature(data, signatureData.Signature);
  72. }
  73. private class SshKeyData : SshData
  74. {
  75. public BigInteger[] Keys { get; private set; }
  76. private string Name { get; set; }
  77. public SshKeyData()
  78. {
  79. }
  80. public SshKeyData(string name, params BigInteger[] keys)
  81. {
  82. this.Name = name;
  83. this.Keys = keys;
  84. }
  85. protected override void LoadData()
  86. {
  87. this.Name = this.ReadString();
  88. var keys = new List<BigInteger>();
  89. while (!this.IsEndOfData)
  90. {
  91. keys.Add(this.ReadBigInt());
  92. }
  93. this.Keys = keys.ToArray();
  94. }
  95. protected override void SaveData()
  96. {
  97. this.Write(this.Name);
  98. foreach (var key in this.Keys)
  99. {
  100. this.Write(key);
  101. }
  102. }
  103. }
  104. private class SignatureKeyData : SshData
  105. {
  106. /// <summary>
  107. /// Gets or sets the name of the algorithm.
  108. /// </summary>
  109. /// <value>
  110. /// The name of the algorithm.
  111. /// </value>
  112. private string AlgorithmName { get; set; }
  113. /// <summary>
  114. /// Gets or sets the signature.
  115. /// </summary>
  116. /// <value>
  117. /// The signature.
  118. /// </value>
  119. public byte[] Signature { get; private set; }
  120. public SignatureKeyData()
  121. {
  122. }
  123. public SignatureKeyData(string name, byte[] signature)
  124. {
  125. this.AlgorithmName = name;
  126. this.Signature = signature;
  127. }
  128. /// <summary>
  129. /// Called when type specific data need to be loaded.
  130. /// </summary>
  131. protected override void LoadData()
  132. {
  133. this.AlgorithmName = this.ReadString();
  134. this.Signature = this.ReadBinaryString();
  135. }
  136. /// <summary>
  137. /// Called when type specific data need to be saved.
  138. /// </summary>
  139. protected override void SaveData()
  140. {
  141. this.Write(this.AlgorithmName);
  142. this.WriteBinaryString(this.Signature);
  143. }
  144. }
  145. }
  146. }