Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python
      2 #
      3 # This file is part of pyasn1-modules software.
      4 #
      5 # Copyright (c) 2005-2017, Ilya Etingof <etingof (at] gmail.com>
      6 # License: http://pyasn1.sf.net/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 #
     11 import sys
     12 
     13 from pyasn1.codec.der import decoder
     14 from pyasn1.codec.der import encoder
     15 
     16 from pyasn1_modules import pem
     17 from pyasn1_modules import rfc2314
     18 
     19 if len(sys.argv) != 1:
     20     print("""Usage:
     21 $ cat certificateRequest.pem | %s""" % sys.argv[0])
     22     sys.exit(-1)
     23 
     24 certType = rfc2314.CertificationRequest()
     25 
     26 certCnt = 0
     27 
     28 while 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 
     47 print('*** %s PEM certificate request(s) de/serialized' % certCnt)
     48