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