Skip to content

Commit dee93f3

Browse files
committed

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

src/Renci.SshNet/PrivateKeyFile.OpenSSH.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public OpenSSH(byte[] data, string? passPhrase)
3232
/// </summary>
3333
public Key Parse()
3434
{
35-
var keyReader = new SshDataStream(_data);
35+
using var keyReader = new SshDataStream(_data);
3636

3737
// check magic header
3838
var authMagic = "openssh-key-v1\0"u8;
@@ -171,7 +171,7 @@ public Key Parse()
171171
// now parse the data we called the private key, it actually contains the public key again
172172
// so we need to parse through it to get the private key bytes, plus there's some
173173
// validation we need to do.
174-
var privateKeyReader = new SshDataStream(privateKeyBytes);
174+
using var privateKeyReader = new SshDataStream(privateKeyBytes);
175175

176176
// check ints should match, they wouldn't match for example if the wrong passphrase was supplied
177177
var checkInt1 = (int)privateKeyReader.ReadUInt32();
@@ -200,7 +200,9 @@ public Key Parse()
200200

201201
// k || ENC(A)
202202
unencryptedPrivateKey = privateKeyReader.ReadBinary();
203+
#pragma warning disable CA2000 // Dispose objects before losing scope
203204
parsedKey = new ED25519Key(unencryptedPrivateKey);
205+
#pragma warning restore CA2000 // Dispose objects before losing scope
204206
break;
205207
case "ecdsa-sha2-nistp256":
206208
case "ecdsa-sha2-nistp384":
@@ -210,7 +212,9 @@ public Key Parse()
210212
publicKey = privateKeyReader.ReadBinary();
211213

212214
unencryptedPrivateKey = privateKeyReader.ReadBinary();
215+
#pragma warning disable CA2000 // Dispose objects before losing scope
213216
parsedKey = new EcdsaKey(curve, publicKey, unencryptedPrivateKey.TrimLeadingZeros());
217+
#pragma warning restore CA2000 // Dispose objects before losing scope
214218
break;
215219
case "ssh-rsa":
216220
var modulus = privateKeyReader.ReadBigInt();
@@ -219,7 +223,9 @@ public Key Parse()
219223
var inverseQ = privateKeyReader.ReadBigInt();
220224
var p = privateKeyReader.ReadBigInt();
221225
var q = privateKeyReader.ReadBigInt();
226+
#pragma warning disable CA2000 // Dispose objects before losing scope
222227
parsedKey = new RsaKey(modulus, exponent, d, p, q, inverseQ);
228+
#pragma warning restore CA2000 // Dispose objects before losing scope
223229
break;
224230
default:
225231
throw new SshException("OpenSSH key type '" + keyType + "' is not supported.");

src/Renci.SshNet/PrivateKeyFile.PuTTY.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ public Key Parse()
163163
throw new SshException("MAC verification failed for PuTTY key file");
164164
}
165165

166-
var publicKeyReader = new SshDataStream(_publicKey);
166+
using var publicKeyReader = new SshDataStream(_publicKey);
167167
var keyType = publicKeyReader.ReadString(Encoding.UTF8);
168168
Debug.Assert(keyType == _algorithmName, $"{nameof(keyType)} is not the same as {nameof(_algorithmName)}");
169169

170-
var privateKeyReader = new SshDataStream(privateKey);
170+
using var privateKeyReader = new SshDataStream(privateKey);
171171

172172
Key parsedKey;
173173

src/Renci.SshNet/PrivateKeyFile.SSHCOM.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ public SSHCOM(byte[] data, string? passPhrase)
2828

2929
public Key Parse()
3030
{
31-
var reader = new SshDataStream(_data);
32-
var magicNumber = reader.ReadUInt32();
31+
using var dataReader = new SshDataStream(_data);
32+
var magicNumber = dataReader.ReadUInt32();
3333
if (magicNumber != 0x3f6ff9eb)
3434
{
3535
throw new SshException("Invalid SSH2 private key.");
3636
}
3737

38-
_ = reader.ReadUInt32(); // Read total bytes length including magic number
39-
var keyType = reader.ReadString(SshData.Ascii);
40-
var ssh2CipherName = reader.ReadString(SshData.Ascii);
41-
var blobSize = (int)reader.ReadUInt32();
38+
_ = dataReader.ReadUInt32(); // Read total bytes length including magic number
39+
var keyType = dataReader.ReadString(SshData.Ascii);
40+
var ssh2CipherName = dataReader.ReadString(SshData.Ascii);
41+
var blobSize = (int)dataReader.ReadUInt32();
4242

4343
byte[] keyData;
4444
if (ssh2CipherName == "none")
4545
{
46-
keyData = reader.ReadBytes(blobSize);
46+
keyData = dataReader.ReadBytes(blobSize);
4747
}
4848
else if (ssh2CipherName == "3des-cbc")
4949
{
@@ -53,17 +53,17 @@ public Key Parse()
5353
}
5454

5555
var key = GetCipherKey(_passPhrase, 192 / 8);
56-
var ssh2Сipher = new TripleDesCipher(key, new byte[8], CipherMode.CBC, pkcs7Padding: false);
57-
keyData = ssh2Сipher.Decrypt(reader.ReadBytes(blobSize));
56+
using var ssh2Сipher = new TripleDesCipher(key, new byte[8], CipherMode.CBC, pkcs7Padding: false);
57+
keyData = ssh2Сipher.Decrypt(dataReader.ReadBytes(blobSize));
5858
}
5959
else
6060
{
6161
throw new SshException(string.Format("Cipher method '{0}' is not supported.", ssh2CipherName));
6262
}
6363

64-
reader = new SshDataStream(keyData);
64+
using var keyReader = new SshDataStream(keyData);
6565

66-
var decryptedLength = reader.ReadUInt32();
66+
var decryptedLength = keyReader.ReadUInt32();
6767

6868
if (decryptedLength > blobSize - 4)
6969
{
@@ -72,12 +72,12 @@ public Key Parse()
7272

7373
if (keyType.Contains("rsa"))
7474
{
75-
var exponent = ReadBigIntWithBits(reader);
76-
var d = ReadBigIntWithBits(reader);
77-
var modulus = ReadBigIntWithBits(reader);
78-
var inverseQ = ReadBigIntWithBits(reader);
79-
var q = ReadBigIntWithBits(reader);
80-
var p = ReadBigIntWithBits(reader);
75+
var exponent = ReadBigIntWithBits(keyReader);
76+
var d = ReadBigIntWithBits(keyReader);
77+
var modulus = ReadBigIntWithBits(keyReader);
78+
var inverseQ = ReadBigIntWithBits(keyReader);
79+
var q = ReadBigIntWithBits(keyReader);
80+
var p = ReadBigIntWithBits(keyReader);
8181
return new RsaKey(modulus, exponent, d, p, q, inverseQ);
8282
}
8383

0 commit comments

Comments
 (0)