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 PKCS#7 on stdin, parse it into plain text, 9 # 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 rfc2315 18 19 if len(sys.argv) != 1: 20 print("""Usage: 21 $ cat pkcs7Certificate.pem | %s""" % sys.argv[0]) 22 sys.exit(-1) 23 24 idx, substrate = pem.readPemBlocksFromFile( 25 sys.stdin, ('-----BEGIN PKCS7-----', '-----END PKCS7-----') 26 ) 27 28 assert substrate, 'bad PKCS7 data on input' 29 30 contentInfo, rest = decoder.decode(substrate, asn1Spec=rfc2315.ContentInfo()) 31 32 if rest: 33 substrate = substrate[:-len(rest)] 34 35 print(contentInfo.prettyPrint()) 36 37 assert encoder.encode(contentInfo) == substrate, 're-encode fails' 38 39 contentType = contentInfo.getComponentByName('contentType') 40 41 contentInfoMap = { 42 (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(), 43 (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(), 44 (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(), 45 (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(), 46 (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(), 47 (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData() 48 } 49 50 content, _ = decoder.decode( 51 contentInfo.getComponentByName('content'), 52 asn1Spec=contentInfoMap[contentType] 53 ) 54 55 print(content.prettyPrint()) 56