Xamarin. Pair to Mac. Key 'OPENSSH' is not

2020-07-05 06:11发布

问题:

When I try to connect my Mac, then I get this error.

I read a log where to found it:

System.NotSupportedException: Key 'OPENSSH' is not supported.
at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase)
at Renci.SshNet.PrivateKeyFile..ctor(String fileName, String passPhrase)
at Xamarin.Messaging.Ssh.MessagingAuthenticationMethod.InitializePrivateKeyAuthentication(String username, ISshInformationProvider sshInformationProvider) in E:\A\_work\157\s\External\messaging\src\Xamarin.Messaging.Ssh\MessagingAuthenticationMethod.cs:line 76
at Xamarin.Messaging.Ssh.MessagingService.GetMessagingConnection(Func`1 passwordProvider, ISshInformationProvider sshInformationProvider) in E:\A\_work\157\s\External\messaging\src\Xamarin.Messaging.Ssh\MessagingService.cs:line 418
at Xamarin.Messaging.Ssh.MessagingService.<ConnectAsync>d__64.MoveNext() in E:\A\_work\157\s\External\messaging\src\Xamarin.Messaging.Ssh\MessagingService.cs:line 167

I found library and code that it's using: https://github.com/sshnet/SSH.NET/blob/bd01d971790a7c1fa73bad35b79ada90bf69e62d/src/Renci.SshNet/PrivateKeyFile.cs#L190

and there is nothing about OpenSSH like a keyName.

I check my private keys in folder %LOCALAPPDATA%\Xamarin\MonoTouch:

-----BEGIN OPENSSH PRIVATE KEY-----
...
-----END OPENSSH PRIVATE KEY-----

I began to receive this error when I reinstalled Windows 10 and Visual Studio 2017 instead of Windows 7

Please help me solve it

回答1:

I execute below command in command-prompt, and retry connecting Mac.

ssh-keygen -t rsa -b 8192 -N "" -f "%LOCALAPPDATA%\Xamarin\MonoTouch\id_rsa"

-t specifies encryption type 'RSA'

-b use 8192-bit key

-N explicitly specifies empty keyphrase



回答2:

I can confirm that user10613465's answer worked in my case.

ssh-keygen -t rsa -N "" -f "%LOCALAPPDATA%\Xamarin\MonoTouch\id_rsa"

In my case I had the correct versions of Windows, Visual Studio and Xamarin. The system passed until tests number 8 and 9 described here: link from official Xamarin on MS Docs

I know it's manner to comment on his asnwer, but I don't have the privillege yet (using work email's StackOverflow account)



回答3:

It's indeed caused by a bug in Xamarin. The solution suggested by user10613465 does not work with recent versions of Xamarin (at least not for me), however I've found a workaround.

Background

Feel free to skip this section* Xamarin generates a pair of SSH keys stored in in %LOCALAPPDATA%\AppData\Local\Xamarin\MonoTouch. SSH key files comes in multiple flavors. One is RSA, another is OPENSSH. The flavor can be found by inspecting the first line in the key file. The problem here is Xamarin generates OPENSSH keys; but can't read them.

In recent versions of Xamarin the private key is encrypted and the passphrase itself is encrypted and stored in a file named passphrase.key. If you attempt to erase the files and generate a new keyset of the correct flavor as suggested, Xamarin will reject the keys a generate a new set it doesn't understand.

Stategy

Feel free to skip this one too. My research suggests you can't convert an OPENSSH key to a RSA key. As far as I can see, Xamarin does not use any external program to generate the key set, so replacing/redirecting is not a viable solution.

The solution I found is to generate a new keyset of the supported flavor, with the same passphrase and leaving passphrase.key in place. Getting the passphrase is the tricky part. I've used VS to debug another instance of VS to obtain it.

Procedure

I assume you already have attempted ot pair and have a defective key pair (id_rsa, id_rsa.pub and passphrase.key)

  1. Fire up Visual Studio (VS), open some project. We'll call this instance A of VS.

  2. Select Tools -> IOS -> Pair to mac.

  3. Enter IP address or name of your Mac, but DO NOT enter name and password yet.

  4. Start another instance of VS. We call this instance B.

  5. In instance B, select Debug -> Options

  6. In Debuging -> Options, deselect "Enable Just my code", Click OK.

  7. Select Debug, Attach to Process.

  8. Select the Instance A of VS. The process is called devenv.exe.

  9. In instance B, Select Debug -> Windows -> Breakpoints

  10. In the breakpoints window, click on New -> Function breakpoint. Select
    Renci.SshNet.PrivateKeyFile.Open.

  11. Now, in instance A, enter name and password and click OK. The breakpoint is now hit by instance A so it freeze.

  12. In instance B you should see some assembler code. The code itself is not important; but we should be able to see the arguments to the method. Open the local variable window by clicking through Debug -> Windows -> Locals

  13. One variable is the passphrase. It looks like a uuid. Copy it to the clipboard and store it somewhere convenient.

  14. Resume execution, close both VS instances.

  15. Enter C:\Users\mk\AppData\Local\Xamarin\MonoTouch

  16. Verify you can connect to your mac using the key and the passphrase we just recovered:

    ssh -i id_rsa mymac

  17. Move the old key out of the way:

    mkdir old move id_rsa old move id_rsa.pub old

  18. Generate a new set of keys, use the same passphrase.

    ssh-keygen -t rsa -N "" -f "%LOCALAPPDATA%\Xamarin\MonoTouch\id_rsa"

  19. Verify the header in id_rsa looks like this

    ----BEGIN RSA PRIVATE KEY-----

and not like this

-----BEGIN OPENSSH PRIVATE KEY-----
  1. If it doesn't, you need to use another ssh-keygen. Perhaps putty can help you there.

  2. Log in to the mac computer. Edit .ssh/authorized_keys and remove the old key. (The one you find in "%LOCALAPPDATA%\Xamarin\MonoTouch\old\id_rsa.pub"

  3. Add the new public from "%LOCALAPPDATA%\Xamarin\MonoTouch\id_rsa.pub"

This solved the problem for me