Skip to content

Commit 0c3bcae

Browse files
committed
Minimize the change
1 parent 623308f commit 0c3bcae

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

src/Renci.SshNet/PrivateKeyFile.cs

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -314,25 +314,80 @@ private void Open(Stream privateKey, string? passPhrase)
314314
switch (keyName)
315315
{
316316
case "RSA PRIVATE KEY":
317-
_key = new RsaKey(decryptedData);
317+
var rsaKey = new RsaKey(decryptedData);
318+
_key = rsaKey;
319+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
320+
#pragma warning disable CA2000 // Dispose objects before losing scope
321+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA512)));
322+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA256)));
323+
#pragma warning restore CA2000 // Dispose objects before losing scope
318324
break;
319325
case "DSA PRIVATE KEY":
320326
_key = new DsaKey(decryptedData);
327+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-dss", _key));
321328
break;
322329
case "EC PRIVATE KEY":
323330
_key = new EcdsaKey(decryptedData);
331+
_hostAlgorithms.Add(new KeyHostAlgorithm(_key.ToString(), _key));
324332
break;
325333
case "PRIVATE KEY":
326334
var privateKeyInfo = PrivateKeyInfo.GetInstance(binaryData);
327335
_key = ParseOpenSslPkcs8PrivateKey(privateKeyInfo);
336+
if (_key is RsaKey parsedRsaKey)
337+
{
338+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
339+
#pragma warning disable CA2000 // Dispose objects before losing scope
340+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(parsedRsaKey, HashAlgorithmName.SHA512)));
341+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(parsedRsaKey, HashAlgorithmName.SHA256)));
342+
#pragma warning restore CA2000 // Dispose objects before losing scope
343+
}
344+
else if (_key is DsaKey parsedDsaKey)
345+
{
346+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-dss", _key));
347+
}
348+
else
349+
{
350+
_hostAlgorithms.Add(new KeyHostAlgorithm(_key.ToString(), _key));
351+
}
352+
328353
break;
329354
case "ENCRYPTED PRIVATE KEY":
330355
var encryptedPrivateKeyInfo = EncryptedPrivateKeyInfo.GetInstance(binaryData);
331356
privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(passPhrase?.ToCharArray(), encryptedPrivateKeyInfo);
332357
_key = ParseOpenSslPkcs8PrivateKey(privateKeyInfo);
358+
if (_key is RsaKey parsedRsaKey2)
359+
{
360+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
361+
#pragma warning disable CA2000 // Dispose objects before losing scope
362+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(parsedRsaKey2, HashAlgorithmName.SHA512)));
363+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(parsedRsaKey2, HashAlgorithmName.SHA256)));
364+
#pragma warning restore CA2000 // Dispose objects before losing scope
365+
}
366+
else if (_key is DsaKey parsedDsaKey)
367+
{
368+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-dss", _key));
369+
}
370+
else
371+
{
372+
_hostAlgorithms.Add(new KeyHostAlgorithm(_key.ToString(), _key));
373+
}
374+
333375
break;
334376
case "OPENSSH PRIVATE KEY":
335377
_key = ParseOpenSshV1Key(decryptedData, passPhrase);
378+
if (_key is RsaKey parsedRsaKey3)
379+
{
380+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
381+
#pragma warning disable CA2000 // Dispose objects before losing scope
382+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(parsedRsaKey3, HashAlgorithmName.SHA512)));
383+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(parsedRsaKey3, HashAlgorithmName.SHA256)));
384+
#pragma warning restore CA2000 // Dispose objects before losing scope
385+
}
386+
else
387+
{
388+
_hostAlgorithms.Add(new KeyHostAlgorithm(_key.ToString(), _key));
389+
}
390+
336391
break;
337392
case "SSH2 ENCRYPTED PRIVATE KEY":
338393
var reader = new SshDataReader(decryptedData);
@@ -389,7 +444,13 @@ private void Open(Stream privateKey, string? passPhrase)
389444
var inverseQ = reader.ReadBigIntWithBits(); // u
390445
var q = reader.ReadBigIntWithBits(); // p
391446
var p = reader.ReadBigIntWithBits(); // q
392-
_key = new RsaKey(modulus, exponent, d, p, q, inverseQ);
447+
var decryptedRsaKey = new RsaKey(modulus, exponent, d, p, q, inverseQ);
448+
_key = decryptedRsaKey;
449+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
450+
#pragma warning disable CA2000 // Dispose objects before losing scope
451+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(decryptedRsaKey, HashAlgorithmName.SHA512)));
452+
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(decryptedRsaKey, HashAlgorithmName.SHA256)));
453+
#pragma warning restore CA2000 // Dispose objects before losing scope
393454
}
394455
else if (keyType.Contains("dsa"))
395456
{
@@ -405,6 +466,7 @@ private void Open(Stream privateKey, string? passPhrase)
405466
var y = reader.ReadBigIntWithBits();
406467
var x = reader.ReadBigIntWithBits();
407468
_key = new DsaKey(p, q, g, y, x);
469+
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-dss", _key));
408470
}
409471
else
410472
{
@@ -415,19 +477,6 @@ private void Open(Stream privateKey, string? passPhrase)
415477
default:
416478
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Key '{0}' is not supported.", keyName));
417479
}
418-
419-
if (_key is RsaKey parsedRsaKey)
420-
{
421-
_hostAlgorithms.Add(new KeyHostAlgorithm("ssh-rsa", _key));
422-
#pragma warning disable CA2000 // Dispose objects before losing scope
423-
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-512", _key, new RsaDigitalSignature(parsedRsaKey, HashAlgorithmName.SHA512)));
424-
_hostAlgorithms.Add(new KeyHostAlgorithm("rsa-sha2-256", _key, new RsaDigitalSignature(parsedRsaKey, HashAlgorithmName.SHA256)));
425-
#pragma warning restore CA2000 // Dispose objects before losing scope
426-
}
427-
else
428-
{
429-
_hostAlgorithms.Add(new KeyHostAlgorithm(_key.ToString(), _key));
430-
}
431480
}
432481

433482
private static byte[] GetCipherKey(string passphrase, int length)
@@ -692,15 +741,14 @@ private static Key ParseOpenSshV1Key(byte[] keyFileData, string? passPhrase)
692741
case "ecdsa-sha2-nistp521":
693742
// curve
694743
var len = (int)privateKeyReader.ReadUInt32();
695-
var curveName = Encoding.ASCII.GetString(privateKeyReader.ReadBytes(len));
696-
var curveOid = SshNamedCurves.GetOid(curveName).GetID();
744+
var curve = Encoding.ASCII.GetString(privateKeyReader.ReadBytes(len));
697745

698746
// public key
699747
publicKey = privateKeyReader.ReadBignum2();
700748

701749
// private key
702750
unencryptedPrivateKey = privateKeyReader.ReadBignum2();
703-
parsedKey = new EcdsaKey(curveOid, publicKey, unencryptedPrivateKey);
751+
parsedKey = new EcdsaKey(curve, publicKey, unencryptedPrivateKey.TrimLeadingZeros());
704752
break;
705753
case "ssh-rsa":
706754
var modulus = privateKeyReader.ReadBignum(); // n
@@ -796,7 +844,7 @@ private static Key ParseOpenSslPkcs8PrivateKey(PrivateKeyInfo privateKeyInfo)
796844

797845
sequenceReader.ThrowIfNotEmpty();
798846

799-
return new EcdsaKey(curve, publickey, privatekey);
847+
return new EcdsaKey(curve, publickey, privatekey.TrimLeadingZeros());
800848
}
801849

802850
if (algorithmOid.Equals(EdECObjectIdentifiers.id_Ed25519))

src/Renci.SshNet/Security/Cryptography/DsaKey.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ public class DsaKey : Key, IDisposable
1616
{
1717
private DsaDigitalSignature? _digitalSignature;
1818

19-
/// <summary>
20-
/// Gets the SSH name of the DSA Key.
21-
/// </summary>
22-
/// <returns>
23-
/// The SSH name of the DSA Key.
24-
/// </returns>
25-
public override string ToString()
26-
{
27-
return "ssh-dss";
28-
}
29-
3019
internal DSA DSA { get; }
3120

3221
/// <summary>
@@ -122,10 +111,7 @@ public DsaKey(SshKeyData publicKeyData)
122111
/// <param name="privateKeyData">DER encoded private key data.</param>
123112
public DsaKey(byte[] privateKeyData)
124113
{
125-
if (privateKeyData is null)
126-
{
127-
throw new ArgumentNullException(nameof(privateKeyData));
128-
}
114+
ThrowHelper.ThrowIfNull(privateKeyData);
129115

130116
var der = new AsnReader(privateKeyData, AsnEncodingRules.DER).ReadSequence();
131117
_ = der.ReadInteger(); // skip version

src/Renci.SshNet/Security/Cryptography/EcdsaKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public EcdsaKey(SshKeyData publicKeyData)
209209
/// <param name="privatekey">Value of privatekey.</param>
210210
public EcdsaKey(string curve, byte[] publickey, byte[] privatekey)
211211
{
212-
_impl = Import(GetCurveOid(curve), publickey, privatekey.TrimLeadingZeros());
212+
_impl = Import(GetCurveOid(curve), publickey, privatekey);
213213
}
214214

215215
/// <summary>

src/Renci.SshNet/Security/Cryptography/RsaKey.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public RsaKey(byte[] privateKeyData)
174174
InverseQ = der.ReadInteger();
175175

176176
der.ThrowIfNotEmpty();
177+
177178
RSA = RSA.Create();
178179
RSA.ImportParameters(GetRSAParameters());
179180
}

0 commit comments

Comments
 (0)