Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jjcarstens
Copy link
Contributor

Encoding of OpenSSH Key V1 is technically listed as an experimental feature so this isn't the most crucial. But in playing with it I discovered the encoding is incorrect when writing the "encrypted" section of Ed25519/Ed448 keys

Currently, we only write the 32 bytes of the Private key. However, the encrypted section is 64 bytes of a "secret" key which includes the private key and public key combined. This is confirmed through a few sources:

This experimental feature seems untested (or I haven't found the right place) so below is some console output. Note the current behavior is missing the private key binary when decoding a key generated from ssh_file:encode/2. Happy to add tests if there is an expected place

Current behavior (from console)
1> {Pub, Priv} = crypto:generate_key(eddsa, ed25519).
{<<215,125,77,247,17,30,232,110,152,159,130,127,155,239,
   39,228,237,136,11,249,153,125,21,51,185,76,228,190,...>>,
 <<194,101,237,100,119,133,35,110,181,76,5,224,211,5,192,
   70,54,228,216,2,165,220,229,249,243,202,130,...>>}

2> Keypairs = [{{ed_pri, ed25519, Pub, Priv}, <<"[email protected]">>}].
[{{ed_pri,ed25519,
          <<215,125,77,247,17,30,232,110,152,159,130,127,155,239,39,
            228,237,136,11,249,153,125,21,51,...>>,
          <<194,101,237,100,119,133,35,110,181,76,5,224,211,5,192,
            70,54,228,216,2,165,220,229,...>>},
  <<"[email protected]">>}]

3> Encoded = ssh_file:encode([{Keypairs, []}], openssh_key_v1).
<<"-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gt\nZWQyNTUxOQA"...>>

4> ssh_file:decode(Encoded, openssh_key_v1).
[{{ed_pri,ed25519,
          <<215,125,77,247,17,30,232,110,152,159,130,127,155,239,39,
            228,237,136,11,249,153,125,21,51,...>>,
          <<>>},
% NOTE: The missing private Key ^
  []},
 {{ed_pub,ed25519,
          <<215,125,77,247,17,30,232,110,152,159,130,127,155,239,
            39,228,237,136,11,249,153,125,21,...>>},
  []}]
With proposed fix
1> {Pub, Priv} = crypto:generate_key(eddsa, ed25519).
{<<242,112,197,146,120,78,235,14,133,240,247,77,162,57,45,
   50,179,218,243,60,91,106,214,240,55,84,255,0,...>>,
 <<11,157,146,182,123,184,78,30,164,173,8,76,179,133,19,
   122,197,107,143,105,129,118,194,115,127,23,141,...>>}

2> Keypairs = [{{ed_pri, ed25519, Pub, Priv}, <<"[email protected]">>}].
[{{ed_pri,ed25519,
          <<242,112,197,146,120,78,235,14,133,240,247,77,162,57,45,
            50,179,218,243,60,91,106,214,240,...>>,
          <<11,157,146,182,123,184,78,30,164,173,8,76,179,133,19,
            122,197,107,143,105,129,118,194,...>>},
  <<"[email protected]">>}]

3> Encoded = ssh_file:encode([{Keypairs, []}], openssh_key_v1).
<<"-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gt\nZWQyNTUxOQA"...>>

4> ssh_file:decode(Encoded, openssh_key_v1).
[{{ed_pri,ed25519,
          <<242,112,197,146,120,78,235,14,133,240,247,77,162,57,45,
            50,179,218,243,60,91,106,214,240,...>>,
          <<11,157,146,182,123,184,78,30,164,173,8,76,179,133,19,
            122,197,107,143,105,129,118,194,...>>},
  []},
 {{ed_pub,ed25519,
          <<242,112,197,146,120,78,235,14,133,240,247,77,162,57,45,
            50,179,218,243,60,91,106,214,...>>},
  []}]
Proposed fix with fixture
1> {ok,Encoded} = file:read_file(<<"lib/ssh/test/ssh_algorithms_SUITE_data/id_ed25519">>).
{ok,<<"-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\nQ"...>>}

2> ssh_file:decode(Encoded, openssh_key_v1).
[{{ed_pri,ed25519,
          <<230,244,255,63,128,45,8,56,169,176,28,180,175,146,97,45,
            75,101,241,209,20,106,80,58,...>>,
          <<115,11,246,118,88,98,97,103,121,119,180,128,103,81,206,
            131,9,186,126,149,118,54,201,...>>},
  []},
 {{ed_pub,ed25519,
          <<230,244,255,63,128,45,8,56,169,176,28,180,175,146,97,
            45,75,101,241,209,20,106,80,...>>},
  []}]

@IngelaAndin IngelaAndin added the team:PS Assigned to OTP team PS label Dec 20, 2021
@HansN HansN added the bug Issue is reported as a bug label Jan 10, 2022
@jjcarstens jjcarstens force-pushed the fix-openssh-v1-encode-ed branch from 91cb0d1 to 032780e Compare January 11, 2022 15:31
@HansN HansN added the testing currently being tested, tag is used by OTP internal CI label Jan 11, 2022
@HansN HansN merged commit 567f5a3 into erlang:maint Jan 13, 2022
@jjcarstens jjcarstens deleted the fix-openssh-v1-encode-ed branch January 13, 2022 16:16
@HansN
Copy link
Contributor

HansN commented Jan 13, 2022

Thanks for the PR! Always nice to get errors fixed!

@HansN HansN removed the testing currently being tested, tag is used by OTP internal CI label Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue is reported as a bug team:PS Assigned to OTP team PS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants