1#!/usr/bin/env python 2# coding: utf-8 3# 4# This file is part of pyasn1-modules software. 5# 6# Created by Stanisław Pitucha with asn1ate tool. 7# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> 8# License: http://snmplabs.com/pyasn1/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# 13import sys 14 15from pyasn1.codec.der import decoder 16from pyasn1.codec.der import encoder 17 18from pyasn1_modules import pem 19from pyasn1_modules import rfc5280 20 21if 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 27certType = rfc5280.Certificate() 28 29certCnt = 0 30 31while 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 50print('*** %s PEM cert(s) de/serialized' % certCnt) 51