Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSAKey.from_private_key doesn't work with io.BytesIO object in python3 #1513

Open
mirrt opened this issue Sep 25, 2019 · 1 comment · May be fixed by #1515
Open

RSAKey.from_private_key doesn't work with io.BytesIO object in python3 #1513

mirrt opened this issue Sep 25, 2019 · 1 comment · May be fixed by #1515

Comments

@mirrt
Copy link

mirrt commented Sep 25, 2019

when using io.BytesIO to create file like object and pass than object to RSAKey.from_private_key in python3 like this

ssh_key = '-----BEGIN DSA PRIVATE KEY-----\n'\
'<keydata>'\
'-----END DSA PRIVATE KEY-----\n'

file_obj = io.BytestIO(ssh_key)
pkey = RSAKey.from_private_key(file_obj)

it raises an error

the issue is here

    def _read_private_key(self, tag, f, password=None):
        lines = f.readlines()
        start = 0
        beginning_of_key = "-----BEGIN " + tag + " PRIVATE KEY-----"
        while start < len(lines) and lines[start].strip() != beginning_of_key:
            start += 1
        if start >= len(lines):
            raise SSHException("not a valid " + tag + " private key file")

readline returns a list of binary and this comparison fails

lines[start].strip() != beginning_of_key
@ploxiln
Copy link
Contributor

ploxiln commented Oct 18, 2019

you could use io.StringIO(ssh_key) instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants