1#!/usr/bin/env python 2# 3# This file is part of pyasn1-modules software. 4# 5# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> 6# License: http://snmplabs.com/pyasn1/license.html 7# 8# Read ASN.1/PEM X.509 certificate requests (PKCS#10 format) on stdin, 9# parse each into plain text, then build substrate from it 10# 11import sys 12 13from pyasn1.codec.der import decoder 14from pyasn1.codec.der import encoder 15 16from pyasn1_modules import pem 17from pyasn1_modules import rfc2314 18 19if len(sys.argv) != 1: 20 print("""Usage: 21$ cat certificateRequest.pem | %s""" % sys.argv[0]) 22 sys.exit(-1) 23 24certType = rfc2314.CertificationRequest() 25 26certCnt = 0 27 28while True: 29 idx, substrate = pem.readPemBlocksFromFile( 30 sys.stdin, ('-----BEGIN CERTIFICATE REQUEST-----', 31 '-----END CERTIFICATE REQUEST-----') 32 ) 33 if not substrate: 34 break 35 36 cert, rest = decoder.decode(substrate, asn1Spec=certType) 37 38 if rest: 39 substrate = substrate[:-len(rest)] 40 41 print(cert.prettyPrint()) 42 43 assert encoder.encode(cert) == substrate, 'cert recode fails' 44 45 certCnt += 1 46 47print('*** %s PEM certificate request(s) de/serialized' % certCnt) 48