x509dump.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 X.509 certificates on stdin, parse each 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 rfc2459
  16. if len(sys.argv) != 1:
  17. print("""Usage:
  18. $ cat CACertificate.pem | %s
  19. $ cat userCertificate.pem | %s""" % (sys.argv[0], sys.argv[0]))
  20. sys.exit(-1)
  21. certType = rfc2459.Certificate()
  22. certCnt = 0
  23. while True:
  24. idx, substrate = pem.readPemBlocksFromFile(
  25. sys.stdin, ('-----BEGIN CERTIFICATE-----',
  26. '-----END CERTIFICATE-----')
  27. )
  28. if not substrate:
  29. break
  30. cert, rest = decoder.decode(substrate, asn1Spec=certType)
  31. if rest:
  32. substrate = substrate[:-len(rest)]
  33. print(cert.prettyPrint())
  34. assert encoder.encode(cert) == substrate, 'cert recode fails'
  35. certCnt += 1
  36. print('*** %s PEM cert(s) de/serialized' % certCnt)