Skip to content

RSAKey.from_private_key fails to load key with "Bag Attributes" #1601

Open

Description

@rectalogic

openssl adds "Bag Attributes" to private keys extracted from pfx, Paramiko fails to parse these keys. These used to work long ago, not sure which paramiko release.
bagkey.pem.zip

$ openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365
$ openssl pkcs12 -export -in cert.pem -inkey key.pem -out output.pfx
$ openssl pkcs12 -in output.pfx -nocerts -nodes -out bagkey.pem
import paramiko

with open("bagkey.pem") as f:
    key = paramiko.RSAKey.from_private_key(f)
$ python keybag.py
Traceback (most recent call last):
  File "keybag.py", line 4, in <module>
    key = paramiko.RSAKey.from_private_key(f)
  File "/private/tmp/venv/lib/python3.7/site-packages/paramiko/pkey.py", line 256, in from_private_key
    key = cls(file_obj=file_obj, password=password)
  File "/private/tmp/venv/lib/python3.7/site-packages/paramiko/rsakey.py", line 52, in __init__
    self._from_private_key(file_obj, password)
  File "/private/tmp/venv/lib/python3.7/site-packages/paramiko/rsakey.py", line 179, in _from_private_key
    data = self._read_private_key("RSA", file_obj, password)
  File "/private/tmp/venv/lib/python3.7/site-packages/paramiko/pkey.py", line 324, in _read_private_key
    raise SSHException("not a valid {} private key file".format(tag))
paramiko.ssh_exception.SSHException: not a valid RSA private key file

Activity

  1. ploxiln commented on Jan 16, 2020

    @ploxiln
    Contributor

    Yes, it was in paramiko-2.7.0 when support for new-format openssh private keys was added, that support was lost for any private keys with some stuff before the key starts. The key you included starts like this:

    Bag Attributes
        localKeyID: 32 CB FA 64 B9 D8 C5 D3 BC 4B 20 04 3D EC 38 6B 32 2D C4 9A 
    Key Attributes: <No Attributes>
    -----BEGIN PRIVATE KEY-----
    MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQCcJTduzIlepEYo
    

    I plan to get around to submitting a pretty simple fix for that soon ...

    ... although "BEGIN PRIVATE KEY" rather than "BEGIN RSA PRIVATE KEY" is not expected, I don't think that will work even after fixing the bug, and not sure it worked before, I think paramiko expects/needs the RSA in there ...

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