| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Common; |
| | | 5 | | using Renci.SshNet.Security.Cryptography; |
| | | 6 | | |
| | | 7 | | namespace Renci.SshNet.Security |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Base class for asymmetric cipher algorithms. |
| | | 11 | | /// </summary> |
| | | 12 | | public abstract class Key |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Specifies array of big integers that represent private key. |
| | | 16 | | /// </summary> |
| | | 17 | | #pragma warning disable SA1401 // Fields should be private |
| | | 18 | | protected BigInteger[] _privateKey; |
| | | 19 | | #pragma warning restore SA1401 // Fields should be private |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the default digital signature implementation for this key. |
| | | 23 | | /// </summary> |
| | | 24 | | protected internal abstract DigitalSignature DigitalSignature { get; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets or sets the public key. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <value> |
| | | 30 | | /// The public. |
| | | 31 | | /// </value> |
| | | 32 | | public abstract BigInteger[] Public { get; set; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the length of the key. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <value> |
| | | 38 | | /// The length of the key. |
| | | 39 | | /// </value> |
| | | 40 | | public abstract int KeyLength { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets or sets the key comment. |
| | | 44 | | /// </summary> |
| | 33 | 45 | | public string Comment { get; set; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="Key"/> class. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="data">DER encoded private key data.</param> |
| | 499 | 51 | | protected Key(byte[] data) |
| | 499 | 52 | | { |
| | 499 | 53 | | if (data is null) |
| | 0 | 54 | | { |
| | 0 | 55 | | throw new ArgumentNullException(nameof(data)); |
| | | 56 | | } |
| | | 57 | | |
| | 499 | 58 | | var der = new DerData(data); |
| | 499 | 59 | | _ = der.ReadBigInteger(); // skip version |
| | | 60 | | |
| | 499 | 61 | | var keys = new List<BigInteger>(); |
| | 4491 | 62 | | while (!der.IsEndOfData) |
| | 3992 | 63 | | { |
| | 3992 | 64 | | keys.Add(der.ReadBigInteger()); |
| | 3992 | 65 | | } |
| | | 66 | | |
| | 499 | 67 | | _privateKey = keys.ToArray(); |
| | 499 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Initializes a new instance of the <see cref="Key"/> class. |
| | | 72 | | /// </summary> |
| | 1291 | 73 | | protected Key() |
| | 1291 | 74 | | { |
| | 1291 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Signs the specified data with the key. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="data">The data to sign.</param> |
| | | 81 | | /// <returns> |
| | | 82 | | /// Signed data. |
| | | 83 | | /// </returns> |
| | | 84 | | public byte[] Sign(byte[] data) |
| | 3 | 85 | | { |
| | 3 | 86 | | return DigitalSignature.Sign(data); |
| | 3 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Verifies the signature. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="data">The data to verify.</param> |
| | | 93 | | /// <param name="signature">The signature to verify against.</param> |
| | | 94 | | /// <returns><see langword="true"/> is signature was successfully verifies; otherwise <see langword="false"/>.</ |
| | | 95 | | public bool VerifySignature(byte[] data, byte[] signature) |
| | 0 | 96 | | { |
| | 0 | 97 | | return DigitalSignature.Verify(data, signature); |
| | 0 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |