| | | 1 | | using System; |
| | | 2 | | using Renci.SshNet.Common; |
| | | 3 | | using Renci.SshNet.Security.Cryptography; |
| | | 4 | | |
| | | 5 | | namespace Renci.SshNet.Security |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Contains DSA private and public key. |
| | | 9 | | /// </summary> |
| | | 10 | | public class DsaKey : Key, IDisposable |
| | | 11 | | { |
| | | 12 | | private DsaDigitalSignature _digitalSignature; |
| | | 13 | | private bool _isDisposed; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the P. |
| | | 17 | | /// </summary> |
| | | 18 | | public BigInteger P |
| | | 19 | | { |
| | | 20 | | get |
| | 17 | 21 | | { |
| | 17 | 22 | | return _privateKey[0]; |
| | 17 | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the Q. |
| | | 28 | | /// </summary> |
| | | 29 | | public BigInteger Q |
| | | 30 | | { |
| | | 31 | | get |
| | 28 | 32 | | { |
| | 28 | 33 | | return _privateKey[1]; |
| | 28 | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the G. |
| | | 39 | | /// </summary> |
| | | 40 | | public BigInteger G |
| | | 41 | | { |
| | | 42 | | get |
| | 8 | 43 | | { |
| | 8 | 44 | | return _privateKey[2]; |
| | 8 | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets public key Y. |
| | | 50 | | /// </summary> |
| | | 51 | | public BigInteger Y |
| | | 52 | | { |
| | | 53 | | get |
| | 7 | 54 | | { |
| | 7 | 55 | | return _privateKey[3]; |
| | 7 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets private key X. |
| | | 61 | | /// </summary> |
| | | 62 | | public BigInteger X |
| | | 63 | | { |
| | | 64 | | get |
| | 1 | 65 | | { |
| | 1 | 66 | | return _privateKey[4]; |
| | 1 | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the length of the key. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <value> |
| | | 74 | | /// The length of the key. |
| | | 75 | | /// </value> |
| | | 76 | | public override int KeyLength |
| | | 77 | | { |
| | | 78 | | get |
| | 3 | 79 | | { |
| | 3 | 80 | | return P.BitLength; |
| | 3 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets the digital signature. |
| | | 86 | | /// </summary> |
| | | 87 | | protected internal override DigitalSignature DigitalSignature |
| | | 88 | | { |
| | | 89 | | get |
| | 16 | 90 | | { |
| | 16 | 91 | | _digitalSignature ??= new DsaDigitalSignature(this); |
| | 16 | 92 | | return _digitalSignature; |
| | 16 | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Gets or sets the public. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <value> |
| | | 100 | | /// The public. |
| | | 101 | | /// </value> |
| | | 102 | | public override BigInteger[] Public |
| | | 103 | | { |
| | | 104 | | get |
| | 4 | 105 | | { |
| | 4 | 106 | | return new[] { P, Q, G, Y }; |
| | 4 | 107 | | } |
| | | 108 | | set |
| | 3 | 109 | | { |
| | 3 | 110 | | if (value.Length != 4) |
| | 0 | 111 | | { |
| | 0 | 112 | | throw new InvalidOperationException("Invalid public key."); |
| | | 113 | | } |
| | | 114 | | |
| | 3 | 115 | | _privateKey = value; |
| | 3 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Initializes a new instance of the <see cref="DsaKey"/> class. |
| | | 121 | | /// </summary> |
| | 3 | 122 | | public DsaKey() |
| | 3 | 123 | | { |
| | 3 | 124 | | _privateKey = new BigInteger[5]; |
| | 3 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Initializes a new instance of the <see cref="DsaKey"/> class. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="data">DER encoded private key data.</param> |
| | | 131 | | public DsaKey(byte[] data) |
| | 0 | 132 | | : base(data) |
| | 0 | 133 | | { |
| | 0 | 134 | | if (_privateKey.Length != 5) |
| | 0 | 135 | | { |
| | 0 | 136 | | throw new InvalidOperationException("Invalid private key."); |
| | | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Initializes a new instance of the <see cref="DsaKey" /> class. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="p">The p.</param> |
| | | 144 | | /// <param name="q">The q.</param> |
| | | 145 | | /// <param name="g">The g.</param> |
| | | 146 | | /// <param name="y">The y.</param> |
| | | 147 | | /// <param name="x">The x.</param> |
| | 13 | 148 | | public DsaKey(BigInteger p, BigInteger q, BigInteger g, BigInteger y, BigInteger x) |
| | 13 | 149 | | { |
| | 13 | 150 | | _privateKey = new BigInteger[5] { p, q, g, y, x }; |
| | 13 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 155 | | /// </summary> |
| | | 156 | | public void Dispose() |
| | 0 | 157 | | { |
| | 0 | 158 | | Dispose(disposing: true); |
| | 0 | 159 | | GC.SuppressFinalize(this); |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 166 | | protected virtual void Dispose(bool disposing) |
| | 16 | 167 | | { |
| | 16 | 168 | | if (_isDisposed) |
| | 0 | 169 | | { |
| | 0 | 170 | | return; |
| | | 171 | | } |
| | | 172 | | |
| | 16 | 173 | | if (disposing) |
| | 0 | 174 | | { |
| | 0 | 175 | | var digitalSignature = _digitalSignature; |
| | 0 | 176 | | if (digitalSignature != null) |
| | 0 | 177 | | { |
| | 0 | 178 | | digitalSignature.Dispose(); |
| | 0 | 179 | | _digitalSignature = null; |
| | 0 | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | _isDisposed = true; |
| | 0 | 183 | | } |
| | 16 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Finalizes an instance of the <see cref="DsaKey"/> class. |
| | | 188 | | /// </summary> |
| | | 189 | | ~DsaKey() |
| | 32 | 190 | | { |
| | 16 | 191 | | Dispose(disposing: false); |
| | 32 | 192 | | } |
| | | 193 | | } |
| | | 194 | | } |