x509dump-rfc5280.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #
  13. import sys
  14. from pyasn1.codec.der import decoder
  15. from pyasn1.codec.der import encoder
  16. from pyasn1_modules import pem
  17. from pyasn1_modules import rfc5280
  18. if len(sys.argv) != 1:
  19. print("""Usage:
  20. $ cat CACertificate.pem | %s
  21. $ cat userCertificate.pem | %s""" % (sys.argv[0], sys.argv[0]))
  22. sys.exit(-1)
  23. certType = rfc5280.Certificate()
  24. certCnt = 0
  25. while 1:
  26. idx, substrate = pem.readPemBlocksFromFile(
  27. sys.stdin, ('-----BEGIN CERTIFICATE-----',
  28. '-----END CERTIFICATE-----')
  29. )
  30. if not substrate:
  31. break
  32. cert, rest = decoder.decode(substrate, asn1Spec=certType)
  33. if rest:
  34. substrate = substrate[:-len(rest)]
  35. print(cert.prettyPrint())
  36. assert encoder.encode(cert) == substrate, 'cert recode fails'
  37. certCnt += 1
  38. print('*** %s PEM cert(s) de/serialized' % certCnt)