Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions mauth_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import re
from hashlib import sha512

HEADER = '-----BEGIN RSA PRIVATE KEY-----'
FOOTER = '-----END RSA PRIVATE KEY-----'
PEM_BOUNDARY_RE = re.compile(r"-----(?:BEGIN|END) ([A-Za-z0-9 -]+?)-----")


def make_bytes(val):
Expand Down Expand Up @@ -39,19 +38,30 @@ def decode(byte_string: bytes) -> str:


def to_rsa_format(key: str) -> str:
"""Convert a private key to RSA format with proper newlines."""

if "\n" in key and HEADER in key and FOOTER in key:
return key
"""Convert a private key to PEM format with proper newlines (RFC 7468)."""
stripped = key.strip()
labels = PEM_BOUNDARY_RE.findall(stripped)

# Already well-formed if we have at least a BEGIN and END boundary with newlines
if len(labels) >= 2 and "\n" in stripped:
return stripped
Comment thread
Khushalijp marked this conversation as resolved.

if labels:
label = labels[0]
header = f"-----BEGIN {label}-----"
footer = f"-----END {label}-----"
else:
# Fallback: treat as a bare RSA private key body
header = "-----BEGIN RSA PRIVATE KEY-----"
footer = "-----END RSA PRIVATE KEY-----"

body = key.strip()
body = body.replace(HEADER, "").replace(FOOTER, "").strip()
body = PEM_BOUNDARY_RE.sub("", stripped).strip()

# Replace whitespace with newlines or chunk into 64-char lines
if " " in body or "\t" in body:
body = re.sub(r'\s+', '\n', body)
body = re.sub(r"\s+", "\n", body)
else:
# PEM-encoded keys are typically split into lines of 64 characters as per RFC 7468 (section 2)
body = '\n'.join(body[i:i + 64] for i in range(0, len(body), 64))
body = "\n".join(body[i : i + 64] for i in range(0, len(body), 64))
Comment on lines 61 to +65

return f"{HEADER}\n{body}\n{FOOTER}"
return f"{header}\n{body}\n{footer}"
Loading