Opened 9 years ago

Closed 9 years ago

#3448 defect closed duplicate (duplicate)

EOL character in ssh private key causes decryption to fail

Reported by: akelya Owned by:
Priority: normal Milestone:
Component: conch Keywords:
Cc: Jean-Paul Calderone Branch:
Author:

Description (last modified by Jean-Paul Calderone)

In following method of class Keys in twisted/conch/ssh/keys.py, length of key is calculated by assuming that there is no EOL at end in key and presence of EOL results in length miscalcluation, thus throws errors: "Input string must be multiple of 8"

def _fromString_PRIVATE_OPENSSH(Class, data, passphrase):


     lines = [x + '\n' for x in data.split('\n')]
        kind = lines[0][11:14]
        if lines[1].startswith('Proc-Type: 4,ENCRYPTED'): # encrypted key
            ivdata = lines[2].split(',')[1][:-1]
            iv = ''.join([chr(int(ivdata[i:i + 2], 16)) for i in range(0,
                len(ivdata), 2)])
            if not passphrase:
                raise EncryptedKeyError('encrypted key with no passphrase')
            ba = md5.new(passphrase + iv).digest()
            bb = md5.new(ba + passphrase + iv).digest()
            decKey = (ba + bb)[:24]
            #################suspected bug here#####################
            b64Data = base64.decodestring(''.join(lines[3:-1]))
            ########################################################
            keyData = DES3.new(decKey, DES3.MODE_CBC, iv).decrypt(b64Data)
            removeLen = ord(keyData[-1])
            keyData = keyData[:-removeLen]

solution:

  • should check for presence of EOL character before calculating length

OR

  • remove EOL character and then calculate length

Attachments (1)

foobar (1.7 KB) - added by ericf 9 years ago.
private key file with passphrase asdfg, generated by ssh-keygen from OpenSSH, 4.7p1

Download all attachments as: .zip

Change History (8)

comment:1 Changed 9 years ago by Jean-Paul Calderone

Description: modified (diff)

fixing description markup

comment:2 Changed 9 years ago by Jean-Paul Calderone

Can you give an example of such a key?

comment:3 Changed 9 years ago by Jean-Paul Calderone

Cc: Jean-Paul Calderone added
Component: coreconch
Owner: changed from Glyph to z3p

comment:4 Changed 9 years ago by ericf

any key you can generate with ssh-keygen(1) has a newline at the end of the file. I'll attach one with passphrase 'asdfg'

Changed 9 years ago by ericf

Attachment: foobar added

private key file with passphrase asdfg, generated by ssh-keygen from OpenSSH, 4.7p1

comment:5 Changed 9 years ago by Jean-Paul Calderone

I think this may be a duplicate of #3008 and/or #3391. I think #3008 may have been fixed properly but for the wrong reason. Conch's ASN1 parser makes inputs that have extra trailing whitespace look like they have more nesting (rather than emitting an error or ignored the whitespace or whatever would really be correct). So #3008 handled the nesting, but that's the wrong way to fix this problem. The right fix is to strip the input or fix the ASN1 parser. #3391 is the same as #3008, but for the DSA code path rather than the RSA code path.

Can you reproduce this problem on Twisted trunk@HEAD (which includes the RSA fix but not the DSA fix) or only with the most recent release (which includes neither fix)?

comment:6 Changed 9 years ago by Jean-Paul Calderone

Resolution: duplicate
Status: newclosed

Alright, a duplicate of #3008/#3391.

comment:7 Changed 7 years ago by <automation>

Owner: z3p deleted
Note: See TracTickets for help on using tickets.