Skip to content

Commit

Permalink
Fix missing OpenSSH private key padding
Browse files Browse the repository at this point in the history
  • Loading branch information
carlreinke committed Mar 21, 2022
1 parent f4fb153 commit a92dee8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions SKSshAgent/Ssh/SshKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public static (SshKey Key, string Comment) ParseOpenSshPrivateKey(ReadOnlySpan<c

string comment = privateReader.ReadString();

if (reader.BytesRemaining != 0)
throw new InvalidDataException("Excess data.");
for (byte i = 1; privateReader.BytesRemaining > 0; ++i)
if (privateReader.ReadByte() != i)
throw new InvalidDataException("Invalid padding.");

if (!publicKey.Equals(privateKey, publicOnly: true))
throw new InvalidDataException("Mismatched public and private keys.");
Expand Down Expand Up @@ -227,6 +228,12 @@ public char[] FormatOpenSshPrivateKey(string comment)
privateWriter.WriteString(comment);
privateWriter.Flush();

for (byte i = 1; innerBuffer.WrittenCount % 8 != 0; ++i)
{
innerBuffer.GetSpan(1)[0] = i;
innerBuffer.Advance(1);
}

writer.WriteByteString(innerBuffer.WrittenSpan);

writer.Flush();
Expand Down

0 comments on commit a92dee8

Please sign in to comment.