Skip to content

ssh2_pem_parse_memory() skips the first line of an unencrypted OpenSSL key #2130

Closed

Description

@R6502

The line

*line = '\0';

skips the first line of the key so it is not decoeded correctly

Activity

  1. changed the title [-]ssh2_pem_parse_memory() does not reat private RSA keys[/-] [+]ssh2_pem_parse_memory() does not read private RSA keys[/+] on Jun 28, 2026
  2. willco007 commented on Jun 29, 2026

    @willco007
    Member

    Can you attach a sample key that fails? Thanks.

  3. R6502 commented on Jun 29, 2026

    @R6502
    Author

    Don't wanna post my private RSA key, but it's in the format

    -----BEGIN RSA PRIVATE KEY-----
    MIIEowIBAAK.....................................................
    .......................
    -----END RSA PRIVATE KEY-----
    

    And the problem should be easily to see:
    When the loop

        do {
            *line = '\0';
            if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off))
                return -1;
        } while(strcmp(line, headerbegin));
    
    

    ends, line contains "-----BEGIN RSA PRIVATE KEY-----"

    Then

        if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off))
            return -1;
    

    reads the next line of the key (first line of the actual key)
    As I have no passphrase the next code
    if(passphrase && ...
    is skipped, and then
    *line = '\0';
    clears the first line of the key.

  4. willco007 commented on Jun 29, 2026

    @willco007
    Member

    Looks like it was broken with this MR #1746

  5. willco007 commented on Jun 29, 2026

    @willco007
    Member

    If you remove *line = '\0'; does it fix it for you? That seems to be the only change required as far as I can tell.

  6. changed the title [-]ssh2_pem_parse_memory() does not read private RSA keys[/-] [+]ssh2_pem_parse_memory() skips the first line of an unencrypted OpenSSL key[/+] on Jun 29, 2026
  7. R6502 commented on Jun 29, 2026

    @R6502
    Author

    If you remove *line = '\0'; does it fix it for you? That seems to be the only change required as far as I can tell.

    Yes, I removed it here and it's working now.
    However, I never use the passphrase so I tested only without it.

  8. willco007 commented on Jun 29, 2026

    @willco007
    Member

    Thanks for the conformation. I was just diff'ing the #1746 commit and it seems to have spuriously been added. I will create an MR removing it.

  9. added a commit that references this issue on Jun 30, 2026
    d78905f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions