1#!/usr/bin/python
2# Read ASN.1/PEM X.509 CRMF request on stdin, parse into
3# plain text, then build substrate from it
4from pyasn1.codec.der import decoder, encoder
5from pyasn1_modules import rfc2511, pem
6import sys
7
8if len(sys.argv) != 1:
9    print("""Usage:
10$ cat crmf.pem | %s""" % sys.argv[0])
11    sys.exit(-1)
12
13certReq = rfc2511.CertReqMessages()
14
15substrate = pem.readBase64FromFile(sys.stdin)
16if not substrate:
17    sys.exit(0)
18
19cr, rest = decoder.decode(substrate, asn1Spec=certReq)
20
21print(cr.prettyPrint())
22
23assert encoder.encode(cr, defMode=False) == substrate or \
24       encoder.encode(cr, defMode=True) == substrate, \
25       'crmf recode fails'
26