RequestMessagePublicKey.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections.Generic;
  2. namespace Renci.SshNet.Messages.Authentication
  3. {
  4. /// <summary>
  5. /// Represents "publickey" SSH_MSG_USERAUTH_REQUEST message.
  6. /// </summary>
  7. public class RequestMessagePublicKey : RequestMessage
  8. {
  9. /// <summary>
  10. /// Gets the name of the authentication method.
  11. /// </summary>
  12. /// <value>
  13. /// The name of the method.
  14. /// </value>
  15. public override string MethodName
  16. {
  17. get
  18. {
  19. return "publickey";
  20. }
  21. }
  22. /// <summary>
  23. /// Gets the name of the public key algorithm.
  24. /// </summary>
  25. /// <value>
  26. /// The name of the public key algorithm.
  27. /// </value>
  28. public string PublicKeyAlgorithmName { get; private set; }
  29. /// <summary>
  30. /// Gets the public key data.
  31. /// </summary>
  32. public byte[] PublicKeyData { get; private set; }
  33. /// <summary>
  34. /// Gets or sets public key signature.
  35. /// </summary>
  36. /// <value>
  37. /// The signature.
  38. /// </value>
  39. public byte[] Signature { get; set; }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="RequestMessagePublicKey"/> class.
  42. /// </summary>
  43. /// <param name="serviceName">Name of the service.</param>
  44. /// <param name="username">Authentication username.</param>
  45. /// <param name="keyAlgorithmName">Name of private key algorithm.</param>
  46. /// <param name="keyData">Private key data.</param>
  47. public RequestMessagePublicKey(ServiceName serviceName, string username, string keyAlgorithmName, byte[] keyData)
  48. : base(serviceName, username)
  49. {
  50. this.PublicKeyAlgorithmName = keyAlgorithmName;
  51. this.PublicKeyData = keyData;
  52. }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="RequestMessagePublicKey"/> class.
  55. /// </summary>
  56. /// <param name="serviceName">Name of the service.</param>
  57. /// <param name="username">Authentication username.</param>
  58. /// <param name="keyAlgorithmName">Name of private key algorithm.</param>
  59. /// <param name="keyData">Private key data.</param>
  60. /// <param name="signature">Private key signature.</param>
  61. public RequestMessagePublicKey(ServiceName serviceName, string username, string keyAlgorithmName, byte[] keyData, byte[] signature)
  62. : this(serviceName, username, keyAlgorithmName, keyData)
  63. {
  64. this.Signature = signature;
  65. }
  66. /// <summary>
  67. /// Called when type specific data need to be saved.
  68. /// </summary>
  69. protected override void SaveData()
  70. {
  71. base.SaveData();
  72. if (this.Signature == null)
  73. {
  74. this.Write(false);
  75. }
  76. else
  77. {
  78. this.Write(true);
  79. }
  80. this.WriteAscii(this.PublicKeyAlgorithmName);
  81. this.WriteBinaryString(this.PublicKeyData);
  82. if (this.Signature != null)
  83. this.WriteBinaryString(this.Signature);
  84. }
  85. }
  86. }