Skip to content

from_pem error for an OpenSSH-format key mentions only PKCS8 — wrong remediation #335

Closed

Description

@TexasCoding

Problem

ssh-keygen's default output since OpenSSH 7.8 is -----BEGIN OPENSSH PRIVATE KEY-----, not PKCS8. cryptography.serialization.load_pem_private_key does not parse that format — it requires load_ssh_private_key. A user who generated their Kalshi key with ssh-keygen -t rsa gets KalshiAuthError: Invalid PEM private key: Could not deserialize key data.... The current hint tells them to verify PKCS8 format — but does not tell them their key is actually OpenSSH or how to convert it.

First-run friction; the v2.x passphrase work added password handling but kept the PKCS8-only error string.

Evidence

  • kalshi/auth.py:137-143:
except (ValueError, UnsupportedAlgorithm) as e:
    # Wrong password and malformed PEM both surface here as ValueError.
    raise KalshiAuthError(
        f"Invalid PEM private key: {e}. Ensure the key is an RSA private key "
        "in PKCS8 PEM format (-----BEGIN PRIVATE KEY-----) and that "
        "the supplied `password=` (if any) is correct."
    ) from e

Suggested fix

Before calling load_pem_private_key, peek at the first line of pem_bytes. If it starts with -----BEGIN OPENSSH PRIVATE KEY-----, raise a targeted error:

raise KalshiAuthError(
    "OpenSSH private-key format detected. Kalshi requires PKCS8 PEM. "
    "Convert in place with: ssh-keygen -p -m PEM -f <path>  "
    "or: openssl rsa -in <path> -out <path>.pem"
)

Leave the generic PKCS8 message for genuinely malformed bytes. Add a unit test that feeds an OpenSSH-format key and asserts the targeted message.

Source

Round-3 independent audit (reviewer: security_auth).

Activity

  1. added
    documentationImprovements or additions to documentation
    polishCode-quality and DX improvements; non-functional
    on May 22, 2026
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

    documentationImprovements or additions to documentationpolishCode-quality and DX improvements; non-functional

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions