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