1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
# -*- coding: utf-8 -*-
#
# This file is part of Python-ASN1. Python-ASN1 is free software that is
# made available under the MIT license. Consult the file "LICENSE" that
# is distributed together with this file for the exact licensing terms.
#
# Python-ASN1 is copyright (c) 2007-2016 by the Python-ASN1 authors. See
# the file "AUTHORS" for a complete overview.
from builtins import open, bytes, str
import sys
import base64
import binascii
import asn1
import optparse
def read_pem(input_file):
"""Read PEM formatted input."""
data = []
state = 0
for line in input_file:
if state == 0:
if line.startswith('-----BEGIN'):
state = 1
elif state == 1:
if line.startswith('-----END'):
state = 2
else:
data.append(line)
elif state == 2:
break
if state != 2:
raise ValueError('No PEM encoded input found')
data = ''.join(data)
return base64.b64decode(data)
tag_id_to_string_map = {
asn1.Numbers.Boolean: "BOOLEAN",
asn1.Numbers.Integer: "INTEGER",
asn1.Numbers.BitString: "BIT STRING",
asn1.Numbers.OctetString: "OCTET STRING",
asn1.Numbers.Null: "NULL",
asn1.Numbers.ObjectIdentifier: "OBJECT",
asn1.Numbers.PrintableString: "PRINTABLESTRING",
asn1.Numbers.IA5String: "IA5STRING",
asn1.Numbers.UTCTime: "UTCTIME",
asn1.Numbers.GeneralizedTime: "GENERALIZED TIME",
asn1.Numbers.Enumerated: "ENUMERATED",
asn1.Numbers.Sequence: "SEQUENCE",
asn1.Numbers.Set: "SET"
}
class_id_to_string_map = {
asn1.Classes.Universal: "U",
asn1.Classes.Application: "A",
asn1.Classes.Context: "C",
asn1.Classes.Private: "P"
}
object_id_to_string_map = {
"1.2.840.113549.1.1.1": "rsaEncryption",
"1.2.840.113549.1.1.5": "sha1WithRSAEncryption",
"1.3.6.1.5.5.7.1.1": "authorityInfoAccess",
"2.5.4.3": "commonName",
"2.5.4.4": "surname",
"2.5.4.5": "serialNumber",
"2.5.4.6": "countryName",
"2.5.4.7": "localityName",
"2.5.4.8": "stateOrProvinceName",
"2.5.4.9": "streetAddress",
"2.5.4.10": "organizationName",
"2.5.4.11": "organizationalUnitName",
"2.5.4.12": "title",
"2.5.4.13": "description",
"2.5.4.42": "givenName",
"1.2.840.113549.1.9.1": "emailAddress",
"2.5.29.14": "X509v3 Subject Key Identifier",
"2.5.29.15": "X509v3 Key Usage",
"2.5.29.16": "X509v3 Private Key Usage Period",
"2.5.29.17": "X509v3 Subject Alternative Name",
"2.5.29.18": "X509v3 Issuer Alternative Name",
"2.5.29.19": "X509v3 Basic Constraints",
"2.5.29.30": "X509v3 Name Constraints",
"2.5.29.31": "X509v3 CRL Distribution Points",
"2.5.29.32": "X509v3 Certificate Policies Extension",
"2.5.29.33": "X509v3 Policy Mappings",
"2.5.29.35": "X509v3 Authority Key Identifier",
"2.5.29.36": "X509v3 Policy Constraints",
"2.5.29.37": "X509v3 Extended Key Usage"
}
def tag_id_to_string(identifier):
"""Return a string representation of a ASN.1 id."""
if identifier in tag_id_to_string_map:
return tag_id_to_string_map[identifier]
return '{:#02x}'.format(identifier)
def class_id_to_string(identifier):
"""Return a string representation of an ASN.1 class."""
if identifier in class_id_to_string_map:
return class_id_to_string_map[identifier]
raise ValueError('Illegal class: {:#02x}'.format(identifier))
def object_identifier_to_string(identifier):
if identifier in object_id_to_string_map:
return object_id_to_string_map[identifier]
return identifier
def value_to_string(tag_number, value):
if tag_number == asn1.Numbers.ObjectIdentifier:
return object_identifier_to_string(value)
elif isinstance(value, bytes):
return '0x' + str(binascii.hexlify(value).upper())
elif isinstance(value, str):
return value
else:
return repr(value)
def pretty_print(input_stream, output_stream, indent=0):
"""Pretty print ASN.1 data."""
while not input_stream.eof():
tag = input_stream.peek()
if tag.typ == asn1.Types.Primitive:
tag, value = input_stream.read()
output_stream.write(' ' * indent)
output_stream.write('[{}] {}: {}\n'.format(class_id_to_string(tag.cls), tag_id_to_string(tag.nr), value_to_string(tag.nr, value)))
elif tag.typ == asn1.Types.Constructed:
output_stream.write(' ' * indent)
output_stream.write('[{}] {}\n'.format(class_id_to_string(tag.cls), tag_id_to_string(tag.nr)))
input_stream.enter()
pretty_print(input_stream, output_stream, indent + 2)
input_stream.leave()
# Main script
parser = optparse.OptionParser()
parser.add_option('-p', '--pem', dest='mode', action='store_const', const='pem', help='PEM encoded input')
parser.add_option('-r', '--raw', dest='mode', action='store_const', const='raw', help='raw input')
parser.add_option('-o', '--output', dest='output', help='output to FILE instead', metavar='FILE')
parser.set_default('mode', 'pem')
(opts, args) = parser.parse_args()
if opts.mode == 'pem':
input_file = open(args[0], 'r')
input_data = read_pem(input_file)
else:
input_file = open(args[0], 'rb')
input_data = input_file.read()
if opts.output:
output_file = open(opts.output, 'w')
else:
output_file = sys.stdout
decoder = asn1.Decoder()
decoder.start(input_data)
pretty_print(decoder, output_file)
|