Commit 999222b
authored
Fix PEM encoding for RSA private keys (#180)
The originally generator code systematically encoded RSA private keys in
PEM wrong. Fixes #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 #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)
```1 parent 78acd95 commit 999222b
File tree
28 files changed
+251
-251
lines changed- testvectors_v1
28 files changed
+251
-251
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
0 commit comments