@@ -314,25 +314,80 @@ private void Open(Stream privateKey, string? passPhrase)
314
314
switch ( keyName )
315
315
{
316
316
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
318
324
break ;
319
325
case "DSA PRIVATE KEY" :
320
326
_key = new DsaKey ( decryptedData ) ;
327
+ _hostAlgorithms . Add ( new KeyHostAlgorithm ( "ssh-dss" , _key ) ) ;
321
328
break ;
322
329
case "EC PRIVATE KEY" :
323
330
_key = new EcdsaKey ( decryptedData ) ;
331
+ _hostAlgorithms . Add ( new KeyHostAlgorithm ( _key . ToString ( ) , _key ) ) ;
324
332
break ;
325
333
case "PRIVATE KEY" :
326
334
var privateKeyInfo = PrivateKeyInfo . GetInstance ( binaryData ) ;
327
335
_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
+
328
353
break ;
329
354
case "ENCRYPTED PRIVATE KEY" :
330
355
var encryptedPrivateKeyInfo = EncryptedPrivateKeyInfo . GetInstance ( binaryData ) ;
331
356
privateKeyInfo = PrivateKeyInfoFactory . CreatePrivateKeyInfo ( passPhrase ? . ToCharArray ( ) , encryptedPrivateKeyInfo ) ;
332
357
_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
+
333
375
break ;
334
376
case "OPENSSH PRIVATE KEY" :
335
377
_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
+
336
391
break ;
337
392
case "SSH2 ENCRYPTED PRIVATE KEY" :
338
393
var reader = new SshDataReader ( decryptedData ) ;
@@ -389,7 +444,13 @@ private void Open(Stream privateKey, string? passPhrase)
389
444
var inverseQ = reader . ReadBigIntWithBits ( ) ; // u
390
445
var q = reader . ReadBigIntWithBits ( ) ; // p
391
446
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
393
454
}
394
455
else if ( keyType . Contains ( "dsa" ) )
395
456
{
@@ -405,6 +466,7 @@ private void Open(Stream privateKey, string? passPhrase)
405
466
var y = reader . ReadBigIntWithBits ( ) ;
406
467
var x = reader . ReadBigIntWithBits ( ) ;
407
468
_key = new DsaKey ( p , q , g , y , x ) ;
469
+ _hostAlgorithms . Add ( new KeyHostAlgorithm ( "ssh-dss" , _key ) ) ;
408
470
}
409
471
else
410
472
{
@@ -415,19 +477,6 @@ private void Open(Stream privateKey, string? passPhrase)
415
477
default :
416
478
throw new NotSupportedException ( string . Format ( CultureInfo . CurrentCulture , "Key '{0}' is not supported." , keyName ) ) ;
417
479
}
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
- }
431
480
}
432
481
433
482
private static byte [ ] GetCipherKey ( string passphrase , int length )
@@ -692,15 +741,14 @@ private static Key ParseOpenSshV1Key(byte[] keyFileData, string? passPhrase)
692
741
case "ecdsa-sha2-nistp521" :
693
742
// curve
694
743
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 ) ) ;
697
745
698
746
// public key
699
747
publicKey = privateKeyReader . ReadBignum2 ( ) ;
700
748
701
749
// private key
702
750
unencryptedPrivateKey = privateKeyReader . ReadBignum2 ( ) ;
703
- parsedKey = new EcdsaKey ( curveOid , publicKey , unencryptedPrivateKey ) ;
751
+ parsedKey = new EcdsaKey ( curve , publicKey , unencryptedPrivateKey . TrimLeadingZeros ( ) ) ;
704
752
break ;
705
753
case "ssh-rsa" :
706
754
var modulus = privateKeyReader . ReadBignum ( ) ; // n
@@ -796,7 +844,7 @@ private static Key ParseOpenSslPkcs8PrivateKey(PrivateKeyInfo privateKeyInfo)
796
844
797
845
sequenceReader . ThrowIfNotEmpty ( ) ;
798
846
799
- return new EcdsaKey ( curve , publickey , privatekey ) ;
847
+ return new EcdsaKey ( curve , publickey , privatekey . TrimLeadingZeros ( ) ) ;
800
848
}
801
849
802
850
if ( algorithmOid . Equals ( EdECObjectIdentifiers . id_Ed25519 ) )
0 commit comments