pkcs7dump.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 PKCS#7 on stdin, parse it into plain text,
  9. # then build substrate from it
  10. #
  11. import sys
  12. from pyasn1.codec.der import decoder
  13. from pyasn1.codec.der import encoder
  14. from pyasn1_modules import pem
  15. from pyasn1_modules import rfc2315
  16. if len(sys.argv) != 1:
  17. print("""Usage:
  18. $ cat pkcs7Certificate.pem | %s""" % sys.argv[0])
  19. sys.exit(-1)
  20. idx, substrate = pem.readPemBlocksFromFile(
  21. sys.stdin, ('-----BEGIN PKCS7-----', '-----END PKCS7-----')
  22. )
  23. assert substrate, 'bad PKCS7 data on input'
  24. contentInfo, rest = decoder.decode(substrate, asn1Spec=rfc2315.ContentInfo())
  25. if rest:
  26. substrate = substrate[:-len(rest)]
  27. print(contentInfo.prettyPrint())
  28. assert encoder.encode(contentInfo) == substrate, 're-encode fails'
  29. contentType = contentInfo.getComponentByName('contentType')
  30. contentInfoMap = {
  31. (1, 2, 840, 113549, 1, 7, 1): rfc2315.Data(),
  32. (1, 2, 840, 113549, 1, 7, 2): rfc2315.SignedData(),
  33. (1, 2, 840, 113549, 1, 7, 3): rfc2315.EnvelopedData(),
  34. (1, 2, 840, 113549, 1, 7, 4): rfc2315.SignedAndEnvelopedData(),
  35. (1, 2, 840, 113549, 1, 7, 5): rfc2315.DigestedData(),
  36. (1, 2, 840, 113549, 1, 7, 6): rfc2315.EncryptedData()
  37. }
  38. content, _ = decoder.decode(
  39. contentInfo.getComponentByName('content'),
  40. asn1Spec=contentInfoMap[contentType]
  41. )
  42. print(content.prettyPrint())