Conversation
The originally generator code systematically encoded RSA private keys in PEM wrong. Fixes C2SP#178. ``` import base64 import json import sys def key_to_pem(key: bytes) -> str: ret = "-----BEGIN PRIVATE KEY-----\n" b64 = base64.b64encode(key).decode("ascii") while b64: l = min(len(b64), 64) ret += b64[:l] ret += "\n" b64 = b64[l:] ret += "-----END PRIVATE KEY-----\n" return ret def fix(path: str): with open(path) as f: orig = f.read() j = json.loads(orig) changed = False for g in j["testGroups"]: if g["type"] not in ("RsaesOaepDecrypt", "RsaesPkcs1Decrypt"): continue pk8 = bytes.fromhex(g["privateKeyPkcs8"]) # Spot-check that this is a PKCS C2SP#8 blob and not, say, RSAPrivateKey. # This is the rsaEncryption OID. assert bytes.fromhex("2a864886f70d010101") in pk8 g["privateKeyPem"] = key_to_pem(pk8) changed = True if changed: with open(path, "w") as f: json.dump(j, f, indent=2, separators=(",", " : ")) f.write("\n") for arg in sys.argv[1:]: fix(arg) ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The originally generator code systematically encoded RSA private keys in PEM wrong. Fixes #178.