bcgit / bc-csharp

BouncyCastle.NET Cryptography Library (Mirror)
https://www.bouncycastle.org/csharp
MIT License
1.66k stars 555 forks source link

public key format is not OpenSSH public key format #461

Closed scott-xu closed 1 year ago

scott-xu commented 1 year ago

The prefix, in below case, "ssh-rsa" should be in text format but not in binary format. However, the test cases and the EncodePublicKey method combined the prefix and the public key in byte array, which is not compatible with OpenSsh format.

Test cases: https://github.com/bcgit/bc-csharp/blob/636fa069f181f2eb8f7c5c125a980622cc84b4ba/crypto/test/src/crypto/test/OpenSshKeyParsingTest.cs#L322 https://github.com/bcgit/bc-csharp/blob/636fa069f181f2eb8f7c5c125a980622cc84b4ba/crypto/test/src/crypto/test/OpenSshKeyParsingTest.cs#L337 https://github.com/bcgit/bc-csharp/blob/636fa069f181f2eb8f7c5c125a980622cc84b4ba/crypto/test/src/crypto/test/OpenSshKeyParsingTest.cs#L425

EncodePublicKey method: https://github.com/bcgit/bc-csharp/blob/636fa069f181f2eb8f7c5c125a980622cc84b4ba/crypto/src/crypto/util/OpenSshPublicKeyUtilities.cs#L38-L93

Here's a sample output from OpenSSH. Note the public key format.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/scott/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/scott/.ssh/id_rsa
Your public key has been saved in /c/Users/scott/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:c++8AXAogXD/5ZGj6MeUGbRc9b89mNGz9u9719SRAU4 scott@SomePC
The key's randomart image is:
+---[RSA 3072]----+
|  ..... . ...E.  |
|   ... + + .o. . |
|      o * *  .. o|
|       + X o  .+ |
|      . S +  . o+|
|     . o o o  + B|
|      . o   oo *o|
|       .   o .. *|
|            +. oO|
+----[SHA256]-----+
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC/Kpc0fMT0zaaJ28XBvjSzln7vHI8dpURBQAGvj1t1QasKyXnIJEGO8fwlZUQimUSB0YngiTXFN92OwZeaRNsq2ipazKC+2Ci2ffoO2SuGjHgecUBTwlFPmhdcY8hXBd+lPDiL19iq4cnWBUYoGUjukg+V3ugY3bp8Y3+KpHDqJOQFHCYqTQDGJXa37K2JjCUa46OERJaTE2XmUWtzdnNp6rAPHVhpzHM4D4IJ3OE/BtqfiibH9HFuSRkYRKMm7p6ameGX/B3ofNNN31gpkqBnLHAmC5N5oGkPTeX9ywQtWsYpsfYNrs1hCoRrWhGSvCkZpjI5seXKVV6U7W5/SWRHMWmVsDMWnK7a+unS8W/o5BhwGISdLo1v88g9NPOzsKbQZqrxDSUQeBuvwY7qH4UPhOLK0y/V4MoZN9nQvncrUbAwSv582RZ/HQR1ijzknOknmOiwSP6LdP0/s9OtjzT5vW7fr1CH1b46lJx2x9lgHo+cNNI/gZ4uTgLAa+qT6lk= scott@SomePC
scott-xu commented 1 year ago

There's nothing wrong with current implmentation according to the spec, for example https://www.rfc-editor.org/rfc/rfc5656#section-3.1 However, OpenSSH and PuTTY both are using below format: {prefix:string} {keydata:base64encoded} {comments:string}

scott-xu commented 1 year ago

Sorry my bad. The {keydata:base64encoded} includes the prefix according the RFC