Skip to content

Commit 171aea8

Browse files
committed
Add tool to validate all payloads with pyasn1
1 parent 8ca75bc commit 171aea8

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

cmp/validator/readme.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Overview
2+
3+
Use the RFC9480 parser from pyasn1-alt-modules to parse all the payloads collected in this repository and see if they're valid `PKIMessage` structures.
4+
5+
# Usage
6+
`python validate.py work/pqc-certificates/cmp/oqs-openssl/artifacts`.
7+
8+
9+
## How it works
10+
- Go through all the directories and look for files that match the `*.pkimessage` name.
11+
- Attempt to parse them as `RFC9480.PKIMessage`.
12+
- If it fails, show the path to the problematic payload and the error message.
13+
14+
# Installation
15+
Create a Python virtualenv and run:
16+
17+
- `pip install pyasn1`
18+
- `pip install -e git+https://github.com/russhousley/pyasn1-alt-modules.git@master#egg=pyasn1-alt-modules`
19+
20+
Note that it uses the newest definition of PKIMessage from RFC9480, currently available only in the Github repository. Eventually it will become part of a `pyasn1-alt-modules` release, so you'd be able to install that right away.

cmp/validator/validate.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import argparse
3+
from pyasn1.codec.der import decoder
4+
from pyasn1_alt_modules import rfc9480
5+
6+
7+
def parse_payload(raw):
8+
"""Attempt to parse the raw buffer as a PKIMessage"""
9+
parsed_data, _ = decoder.decode(raw, asn1Spec=rfc9480.PKIMessage())
10+
return parsed_data
11+
12+
13+
14+
def process_pki_messages(directory):
15+
files_parsed = 0
16+
errors = 0
17+
for root, _, files in os.walk(directory):
18+
for file in files:
19+
if file.endswith('.pkimessage'):
20+
file_path = os.path.join(root, file)
21+
try:
22+
with open(file_path, 'rb') as f:
23+
raw_data = f.read()
24+
parsed_data = parse_payload(raw_data)
25+
print(f"OK '{file}'")
26+
except Exception as e:
27+
errors += 1
28+
print(f"ERR '{file_path}': {str(e)[:60]}...")
29+
else:
30+
files_parsed += 1
31+
32+
print(f'OK: {files_parsed}\tERR: {errors}')
33+
34+
35+
36+
if __name__ == '__main__':
37+
parser = argparse.ArgumentParser(description='Parse PKIMessage files in directories')
38+
parser.add_argument('directory', help='Path to the directory to start from')
39+
args = parser.parse_args()
40+
41+
if os.path.isdir(args.directory):
42+
process_pki_messages(args.directory)
43+
else:
44+
print(f"Error: The provided path '{args.directory}' is not a directory.")

0 commit comments

Comments
 (0)