| | | 1 | | using System; |
| | | 2 | | #if NETFRAMEWORK |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | #endif // NETFRAMEWORK |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | using Renci.SshNet.Common; |
| | | 11 | | using Renci.SshNet.Security.Cryptography; |
| | | 12 | | |
| | | 13 | | namespace Renci.SshNet.Security |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Contains ECDSA (ecdsa-sha2-nistp{256,384,521}) private and public key. |
| | | 17 | | /// </summary> |
| | | 18 | | public class EcdsaKey : Key, IDisposable |
| | | 19 | | { |
| | | 20 | | #pragma warning disable SA1310 // Field names should not contain underscore |
| | | 21 | | private const string ECDSA_P256_OID_VALUE = "1.2.840.10045.3.1.7"; // Also called nistP256 or secP256r1 |
| | | 22 | | private const string ECDSA_P384_OID_VALUE = "1.3.132.0.34"; // Also called nistP384 or secP384r1 |
| | | 23 | | private const string ECDSA_P521_OID_VALUE = "1.3.132.0.35"; // Also called nistP521or secP521r1 |
| | | 24 | | #pragma warning restore SA1310 // Field names should not contain underscore |
| | | 25 | | |
| | | 26 | | private EcdsaDigitalSignature _digitalSignature; |
| | | 27 | | private bool _isDisposed; |
| | | 28 | | |
| | | 29 | | #if NETFRAMEWORK |
| | | 30 | | private CngKey _key; |
| | | 31 | | |
| | | 32 | | internal enum KeyBlobMagicNumber |
| | | 33 | | { |
| | | 34 | | BCRYPT_ECDSA_PUBLIC_P256_MAGIC = 0x31534345, |
| | | 35 | | BCRYPT_ECDSA_PRIVATE_P256_MAGIC = 0x32534345, |
| | | 36 | | BCRYPT_ECDSA_PUBLIC_P384_MAGIC = 0x33534345, |
| | | 37 | | BCRYPT_ECDSA_PRIVATE_P384_MAGIC = 0x34534345, |
| | | 38 | | BCRYPT_ECDSA_PUBLIC_P521_MAGIC = 0x35534345, |
| | | 39 | | BCRYPT_ECDSA_PRIVATE_P521_MAGIC = 0x36534345, |
| | | 40 | | |
| | | 41 | | BCRYPT_ECDH_PUBLIC_P256_MAGIC = 0x314B4345, |
| | | 42 | | BCRYPT_ECDH_PRIVATE_P256_MAGIC = 0x324B4345, |
| | | 43 | | BCRYPT_ECDH_PUBLIC_P384_MAGIC = 0x334B4345, |
| | | 44 | | BCRYPT_ECDH_PRIVATE_P384_MAGIC = 0x344B4345, |
| | | 45 | | BCRYPT_ECDH_PUBLIC_P521_MAGIC = 0x354B4345, |
| | | 46 | | BCRYPT_ECDH_PRIVATE_P521_MAGIC = 0x364B4345, |
| | | 47 | | |
| | | 48 | | BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC = 0x504B4345, |
| | | 49 | | BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC = 0x564B4345, |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | [StructLayout(LayoutKind.Sequential)] |
| | | 53 | | internal struct BCRYPT_ECCKEY_BLOB |
| | | 54 | | { |
| | | 55 | | internal KeyBlobMagicNumber Magic; |
| | | 56 | | internal int CbKey; |
| | | 57 | | } |
| | | 58 | | #endif |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the SSH name of the ECDSA Key. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <returns> |
| | | 64 | | /// The SSH name of the ECDSA Key. |
| | | 65 | | /// </returns> |
| | | 66 | | public override string ToString() |
| | 39 | 67 | | { |
| | 39 | 68 | | return string.Format("ecdsa-sha2-nistp{0}", KeyLength); |
| | 39 | 69 | | } |
| | | 70 | | |
| | | 71 | | #if NETFRAMEWORK |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the HashAlgorithm to use. |
| | | 74 | | /// </summary> |
| | | 75 | | public CngAlgorithm HashAlgorithm |
| | | 76 | | { |
| | | 77 | | get |
| | 0 | 78 | | { |
| | 0 | 79 | | switch (Ecdsa.KeySize) |
| | | 80 | | { |
| | | 81 | | case 256: |
| | 0 | 82 | | return CngAlgorithm.Sha256; |
| | | 83 | | case 384: |
| | 0 | 84 | | return CngAlgorithm.Sha384; |
| | | 85 | | case 521: |
| | 0 | 86 | | return CngAlgorithm.Sha512; |
| | | 87 | | default: |
| | 0 | 88 | | throw new SshException("Unknown KeySize: " + Ecdsa.KeySize.ToString(CultureInfo.InvariantCulture |
| | | 89 | | } |
| | 0 | 90 | | } |
| | | 91 | | } |
| | | 92 | | #else |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the HashAlgorithm to use. |
| | | 95 | | /// </summary> |
| | | 96 | | public HashAlgorithmName HashAlgorithm |
| | | 97 | | { |
| | | 98 | | get |
| | 3 | 99 | | { |
| | 3 | 100 | | switch (KeyLength) |
| | | 101 | | { |
| | | 102 | | case 256: |
| | 1 | 103 | | return HashAlgorithmName.SHA256; |
| | | 104 | | case 384: |
| | 1 | 105 | | return HashAlgorithmName.SHA384; |
| | | 106 | | case 521: |
| | 1 | 107 | | return HashAlgorithmName.SHA512; |
| | | 108 | | default: |
| | 0 | 109 | | return HashAlgorithmName.SHA256; |
| | | 110 | | } |
| | 3 | 111 | | } |
| | | 112 | | } |
| | | 113 | | #endif |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the length of the key. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <value> |
| | | 119 | | /// The length of the key. |
| | | 120 | | /// </value> |
| | | 121 | | public override int KeyLength |
| | | 122 | | { |
| | | 123 | | get |
| | 42 | 124 | | { |
| | 42 | 125 | | return Ecdsa.KeySize; |
| | 42 | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the digital signature. |
| | | 131 | | /// </summary> |
| | | 132 | | protected internal override DigitalSignature DigitalSignature |
| | | 133 | | { |
| | | 134 | | get |
| | 39 | 135 | | { |
| | 39 | 136 | | _digitalSignature ??= new EcdsaDigitalSignature(this); |
| | | 137 | | |
| | 39 | 138 | | return _digitalSignature; |
| | 39 | 139 | | } |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets or sets the public. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <value> |
| | | 146 | | /// The public. |
| | | 147 | | /// </value> |
| | | 148 | | public override BigInteger[] Public |
| | | 149 | | { |
| | | 150 | | get |
| | 3 | 151 | | { |
| | | 152 | | byte[] curve; |
| | | 153 | | byte[] qx; |
| | | 154 | | byte[] qy; |
| | | 155 | | #if NETFRAMEWORK |
| | 0 | 156 | | var blob = _key.Export(CngKeyBlobFormat.EccPublicBlob); |
| | | 157 | | |
| | | 158 | | KeyBlobMagicNumber magic; |
| | 0 | 159 | | using (var br = new BinaryReader(new MemoryStream(blob))) |
| | 0 | 160 | | { |
| | 0 | 161 | | magic = (KeyBlobMagicNumber)br.ReadInt32(); |
| | 0 | 162 | | var cbKey = br.ReadInt32(); |
| | 0 | 163 | | qx = br.ReadBytes(cbKey); |
| | 0 | 164 | | qy = br.ReadBytes(cbKey); |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | #pragma warning disable IDE0010 // Add missing cases |
| | 0 | 168 | | switch (magic) |
| | | 169 | | { |
| | | 170 | | case KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC: |
| | 0 | 171 | | curve = Encoding.ASCII.GetBytes("nistp256"); |
| | 0 | 172 | | break; |
| | | 173 | | case KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC: |
| | 0 | 174 | | curve = Encoding.ASCII.GetBytes("nistp384"); |
| | 0 | 175 | | break; |
| | | 176 | | case KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC: |
| | 0 | 177 | | curve = Encoding.ASCII.GetBytes("nistp521"); |
| | 0 | 178 | | break; |
| | | 179 | | default: |
| | 0 | 180 | | throw new SshException("Unexpected Curve Magic: " + magic); |
| | | 181 | | } |
| | | 182 | | #pragma warning restore IDE0010 // Add missing cases |
| | | 183 | | #else |
| | 3 | 184 | | var parameter = Ecdsa.ExportParameters(includePrivateParameters: false); |
| | 3 | 185 | | qx = parameter.Q.X; |
| | 3 | 186 | | qy = parameter.Q.Y; |
| | 3 | 187 | | switch (parameter.Curve.Oid.FriendlyName) |
| | | 188 | | { |
| | | 189 | | case "ECDSA_P256": |
| | | 190 | | case "nistP256": |
| | 1 | 191 | | curve = Encoding.ASCII.GetBytes("nistp256"); |
| | 1 | 192 | | break; |
| | | 193 | | case "ECDSA_P384": |
| | | 194 | | case "nistP384": |
| | 1 | 195 | | curve = Encoding.ASCII.GetBytes("nistp384"); |
| | 1 | 196 | | break; |
| | | 197 | | case "ECDSA_P521": |
| | | 198 | | case "nistP521": |
| | 1 | 199 | | curve = Encoding.ASCII.GetBytes("nistp521"); |
| | 1 | 200 | | break; |
| | | 201 | | default: |
| | 0 | 202 | | throw new SshException("Unexpected Curve Name: " + parameter.Curve.Oid.FriendlyName); |
| | | 203 | | } |
| | | 204 | | #endif |
| | | 205 | | |
| | | 206 | | // Make ECPoint from x and y |
| | | 207 | | // Prepend 04 (uncompressed format) + qx-bytes + qy-bytes |
| | 3 | 208 | | var q = new byte[1 + qx.Length + qy.Length]; |
| | 3 | 209 | | Buffer.SetByte(q, 0, 4); |
| | 3 | 210 | | Buffer.BlockCopy(qx, 0, q, 1, qx.Length); |
| | 3 | 211 | | Buffer.BlockCopy(qy, 0, q, qx.Length + 1, qy.Length); |
| | | 212 | | |
| | | 213 | | // returns Curve-Name and x/y as ECPoint |
| | 3 | 214 | | return new[] { new BigInteger(curve.Reverse()), new BigInteger(q.Reverse()) }; |
| | 3 | 215 | | } |
| | | 216 | | set |
| | 0 | 217 | | { |
| | 0 | 218 | | var curve_s = Encoding.ASCII.GetString(value[0].ToByteArray().Reverse()); |
| | 0 | 219 | | var curve_oid = GetCurveOid(curve_s); |
| | | 220 | | |
| | 0 | 221 | | var publickey = value[1].ToByteArray().Reverse(); |
| | 0 | 222 | | Import(curve_oid, publickey, privatekey: null); |
| | 0 | 223 | | } |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Gets the PrivateKey Bytes. |
| | | 228 | | /// </summary> |
| | 39 | 229 | | public byte[] PrivateKey { get; private set; } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Gets the <see cref="ECDsa"/> object. |
| | | 233 | | /// </summary> |
| | 87 | 234 | | public ECDsa Ecdsa { get; private set; } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Initializes a new instance of the <see cref="EcdsaKey"/> class. |
| | | 238 | | /// </summary> |
| | 0 | 239 | | public EcdsaKey() |
| | 0 | 240 | | { |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Initializes a new instance of the <see cref="EcdsaKey"/> class. |
| | | 245 | | /// </summary> |
| | | 246 | | /// <param name="curve">The curve name.</param> |
| | | 247 | | /// <param name="publickey">Value of publickey.</param> |
| | | 248 | | /// <param name="privatekey">Value of privatekey.</param> |
| | 20 | 249 | | public EcdsaKey(string curve, byte[] publickey, byte[] privatekey) |
| | 20 | 250 | | { |
| | 20 | 251 | | Import(GetCurveOid(curve), publickey, privatekey); |
| | 20 | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Initializes a new instance of the <see cref="EcdsaKey"/> class. |
| | | 256 | | /// </summary> |
| | | 257 | | /// <param name="data">DER encoded private key data.</param> |
| | 19 | 258 | | public EcdsaKey(byte[] data) |
| | 19 | 259 | | { |
| | 19 | 260 | | var der = new DerData(data); |
| | 19 | 261 | | _ = der.ReadBigInteger(); // skip version |
| | | 262 | | |
| | | 263 | | // PrivateKey |
| | 19 | 264 | | var privatekey = der.ReadOctetString().TrimLeadingZeros(); |
| | | 265 | | |
| | | 266 | | // Construct |
| | 19 | 267 | | var s0 = der.ReadByte(); |
| | 19 | 268 | | if ((s0 & 0xe0) != 0xa0) |
| | 0 | 269 | | { |
| | 0 | 270 | | throw new SshException(string.Format("UnexpectedDER: wanted constructed tag (0xa0-0xbf), got: {0:X}", s0 |
| | | 271 | | } |
| | | 272 | | |
| | 19 | 273 | | var tag = s0 & 0x1f; |
| | 19 | 274 | | if (tag != 0) |
| | 0 | 275 | | { |
| | 0 | 276 | | throw new SshException(string.Format("expected tag 0 in DER privkey, got: {0}", tag)); |
| | | 277 | | } |
| | | 278 | | |
| | 19 | 279 | | var construct = der.ReadBytes(der.ReadLength()); // object length |
| | | 280 | | |
| | | 281 | | // curve OID |
| | 19 | 282 | | var curve_der = new DerData(construct, construct: true); |
| | 19 | 283 | | var curve = curve_der.ReadObject(); |
| | | 284 | | |
| | | 285 | | // Construct |
| | 19 | 286 | | s0 = der.ReadByte(); |
| | 19 | 287 | | if ((s0 & 0xe0) != 0xa0) |
| | 0 | 288 | | { |
| | 0 | 289 | | throw new SshException(string.Format("UnexpectedDER: wanted constructed tag (0xa0-0xbf), got: {0:X}", s0 |
| | | 290 | | } |
| | | 291 | | |
| | 19 | 292 | | tag = s0 & 0x1f; |
| | 19 | 293 | | if (tag != 1) |
| | 0 | 294 | | { |
| | 0 | 295 | | throw new SshException(string.Format("expected tag 1 in DER privkey, got: {0}", tag)); |
| | | 296 | | } |
| | | 297 | | |
| | 19 | 298 | | construct = der.ReadBytes(der.ReadLength()); // object length |
| | | 299 | | |
| | | 300 | | // PublicKey |
| | 19 | 301 | | var pubkey_der = new DerData(construct, construct: true); |
| | 19 | 302 | | var pubkey = pubkey_der.ReadBitString().TrimLeadingZeros(); |
| | | 303 | | |
| | 19 | 304 | | Import(OidByteArrayToString(curve), pubkey, privatekey); |
| | 19 | 305 | | } |
| | | 306 | | |
| | | 307 | | private void Import(string curve_oid, byte[] publickey, byte[] privatekey) |
| | 39 | 308 | | { |
| | | 309 | | #if NETFRAMEWORK |
| | | 310 | | KeyBlobMagicNumber curve_magic; |
| | | 311 | | |
| | 12 | 312 | | switch (GetCurveName(curve_oid)) |
| | | 313 | | { |
| | | 314 | | case "nistp256": |
| | 4 | 315 | | if (privatekey != null) |
| | 4 | 316 | | { |
| | 4 | 317 | | curve_magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC; |
| | 4 | 318 | | } |
| | | 319 | | else |
| | 0 | 320 | | { |
| | 0 | 321 | | curve_magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC; |
| | 0 | 322 | | } |
| | | 323 | | |
| | 4 | 324 | | break; |
| | | 325 | | case "nistp384": |
| | 4 | 326 | | if (privatekey != null) |
| | 4 | 327 | | { |
| | 4 | 328 | | curve_magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P384_MAGIC; |
| | 4 | 329 | | } |
| | | 330 | | else |
| | 0 | 331 | | { |
| | 0 | 332 | | curve_magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC; |
| | 0 | 333 | | } |
| | | 334 | | |
| | 4 | 335 | | break; |
| | | 336 | | case "nistp521": |
| | 4 | 337 | | if (privatekey != null) |
| | 4 | 338 | | { |
| | 4 | 339 | | curve_magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P521_MAGIC; |
| | 4 | 340 | | } |
| | | 341 | | else |
| | 0 | 342 | | { |
| | 0 | 343 | | curve_magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC; |
| | 0 | 344 | | } |
| | | 345 | | |
| | 4 | 346 | | break; |
| | | 347 | | default: |
| | 0 | 348 | | throw new SshException("Unknown: " + curve_oid); |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | // ECPoint as BigInteger(2) |
| | 12 | 352 | | var cord_size = (publickey.Length - 1) / 2; |
| | 12 | 353 | | var qx = new byte[cord_size]; |
| | 12 | 354 | | Buffer.BlockCopy(publickey, 1, qx, 0, qx.Length); |
| | | 355 | | |
| | 12 | 356 | | var qy = new byte[cord_size]; |
| | 12 | 357 | | Buffer.BlockCopy(publickey, cord_size + 1, qy, 0, qy.Length); |
| | | 358 | | |
| | 12 | 359 | | if (privatekey != null) |
| | 12 | 360 | | { |
| | 12 | 361 | | privatekey = privatekey.Pad(cord_size); |
| | 12 | 362 | | PrivateKey = privatekey; |
| | 12 | 363 | | } |
| | | 364 | | |
| | 12 | 365 | | var headerSize = Marshal.SizeOf(typeof(BCRYPT_ECCKEY_BLOB)); |
| | 12 | 366 | | var blobSize = headerSize + qx.Length + qy.Length; |
| | 12 | 367 | | if (privatekey != null) |
| | 12 | 368 | | { |
| | 12 | 369 | | blobSize += privatekey.Length; |
| | 12 | 370 | | } |
| | | 371 | | |
| | 12 | 372 | | var blob = new byte[blobSize]; |
| | 12 | 373 | | using (var bw = new BinaryWriter(new MemoryStream(blob))) |
| | 12 | 374 | | { |
| | 12 | 375 | | bw.Write((int)curve_magic); |
| | 12 | 376 | | bw.Write(cord_size); |
| | 12 | 377 | | bw.Write(qx); // q.x |
| | 12 | 378 | | bw.Write(qy); // q.y |
| | 12 | 379 | | if (privatekey != null) |
| | 12 | 380 | | { |
| | 12 | 381 | | bw.Write(privatekey); // d |
| | 12 | 382 | | } |
| | 12 | 383 | | } |
| | | 384 | | |
| | 12 | 385 | | _key = CngKey.Import(blob, privatekey is null ? CngKeyBlobFormat.EccPublicBlob : CngKeyBlobFormat.EccPrivate |
| | | 386 | | |
| | 12 | 387 | | Ecdsa = new ECDsaCng(_key); |
| | | 388 | | #else |
| | 27 | 389 | | var curve = ECCurve.CreateFromValue(curve_oid); |
| | 27 | 390 | | var parameter = new ECParameters |
| | 27 | 391 | | { |
| | 27 | 392 | | Curve = curve |
| | 27 | 393 | | }; |
| | | 394 | | |
| | | 395 | | // ECPoint as BigInteger(2) |
| | 27 | 396 | | var cord_size = (publickey.Length - 1) / 2; |
| | 27 | 397 | | var qx = new byte[cord_size]; |
| | 27 | 398 | | Buffer.BlockCopy(publickey, 1, qx, 0, qx.Length); |
| | | 399 | | |
| | 27 | 400 | | var qy = new byte[cord_size]; |
| | 27 | 401 | | Buffer.BlockCopy(publickey, cord_size + 1, qy, 0, qy.Length); |
| | | 402 | | |
| | 27 | 403 | | parameter.Q.X = qx; |
| | 27 | 404 | | parameter.Q.Y = qy; |
| | | 405 | | |
| | 27 | 406 | | if (privatekey != null) |
| | 27 | 407 | | { |
| | 27 | 408 | | parameter.D = privatekey.TrimLeadingZeros().Pad(cord_size); |
| | 27 | 409 | | PrivateKey = parameter.D; |
| | 27 | 410 | | } |
| | | 411 | | |
| | 27 | 412 | | Ecdsa = ECDsa.Create(parameter); |
| | | 413 | | #endif |
| | 39 | 414 | | } |
| | | 415 | | |
| | | 416 | | private static string GetCurveOid(string curve_s) |
| | 20 | 417 | | { |
| | 20 | 418 | | switch (curve_s.ToUpperInvariant()) |
| | | 419 | | { |
| | | 420 | | case "NISTP256": |
| | 6 | 421 | | return ECDSA_P256_OID_VALUE; |
| | | 422 | | case "NISTP384": |
| | 7 | 423 | | return ECDSA_P384_OID_VALUE; |
| | | 424 | | case "NISTP521": |
| | 7 | 425 | | return ECDSA_P521_OID_VALUE; |
| | | 426 | | default: |
| | 0 | 427 | | throw new SshException("Unexpected Curve Name: " + curve_s); |
| | | 428 | | } |
| | 20 | 429 | | } |
| | | 430 | | |
| | | 431 | | #if NETFRAMEWORK |
| | | 432 | | private static string GetCurveName(string oid) |
| | 12 | 433 | | { |
| | 12 | 434 | | switch (oid) |
| | | 435 | | { |
| | | 436 | | case ECDSA_P256_OID_VALUE: |
| | 4 | 437 | | return "nistp256"; |
| | | 438 | | case ECDSA_P384_OID_VALUE: |
| | 4 | 439 | | return "nistp384"; |
| | | 440 | | case ECDSA_P521_OID_VALUE: |
| | 4 | 441 | | return "nistp521"; |
| | | 442 | | default: |
| | 0 | 443 | | throw new SshException("Unexpected OID: " + oid); |
| | | 444 | | } |
| | 12 | 445 | | } |
| | | 446 | | #endif // NETFRAMEWORK |
| | | 447 | | |
| | | 448 | | private static string OidByteArrayToString(byte[] oid) |
| | 19 | 449 | | { |
| | 19 | 450 | | var retVal = new StringBuilder(); |
| | | 451 | | |
| | 218 | 452 | | for (var i = 0; i < oid.Length; i++) |
| | 90 | 453 | | { |
| | 90 | 454 | | if (i == 0) |
| | 19 | 455 | | { |
| | 19 | 456 | | var b = oid[0] % 40; |
| | 19 | 457 | | var a = (oid[0] - b) / 40; |
| | 19 | 458 | | _ = retVal.AppendFormat("{0}.{1}", a, b); |
| | 19 | 459 | | } |
| | | 460 | | else |
| | 71 | 461 | | { |
| | 71 | 462 | | if (oid[i] < 128) |
| | 45 | 463 | | { |
| | 45 | 464 | | _ = retVal.AppendFormat(".{0}", oid[i]); |
| | 45 | 465 | | } |
| | | 466 | | else |
| | 26 | 467 | | { |
| | 26 | 468 | | _ = retVal.AppendFormat(".{0}", ((oid[i] - 128) * 128) + oid[i + 1]); |
| | 26 | 469 | | i++; |
| | 26 | 470 | | } |
| | 71 | 471 | | } |
| | 90 | 472 | | } |
| | | 473 | | |
| | 19 | 474 | | return retVal.ToString(); |
| | 19 | 475 | | } |
| | | 476 | | |
| | | 477 | | /// <summary> |
| | | 478 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 479 | | /// </summary> |
| | | 480 | | public void Dispose() |
| | 0 | 481 | | { |
| | 0 | 482 | | Dispose(disposing: true); |
| | 0 | 483 | | GC.SuppressFinalize(this); |
| | 0 | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 488 | | /// </summary> |
| | | 489 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 490 | | protected virtual void Dispose(bool disposing) |
| | 39 | 491 | | { |
| | 39 | 492 | | if (_isDisposed) |
| | 0 | 493 | | { |
| | 0 | 494 | | return; |
| | | 495 | | } |
| | | 496 | | |
| | 39 | 497 | | if (disposing) |
| | 0 | 498 | | { |
| | 0 | 499 | | _isDisposed = true; |
| | 0 | 500 | | } |
| | 39 | 501 | | } |
| | | 502 | | |
| | | 503 | | /// <summary> |
| | | 504 | | /// Finalizes an instance of the <see cref="EcdsaKey"/> class. |
| | | 505 | | /// </summary> |
| | | 506 | | ~EcdsaKey() |
| | 78 | 507 | | { |
| | 39 | 508 | | Dispose(disposing: false); |
| | 78 | 509 | | } |
| | | 510 | | } |
| | | 511 | | } |