|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# This file is part of Python-ASN1. Python-ASN1 is free software that is |
| 4 | +# made available under the MIT license. Consult the file "LICENSE" that |
| 5 | +# is distributed together with this file for the exact licensing terms. |
| 6 | +# |
| 7 | +# Python-ASN1 is copyright (c) 2007-2016 by the Python-ASN1 authors. See |
| 8 | +# the file "AUTHORS" for a complete overview. |
| 9 | + |
| 10 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 11 | + |
| 12 | +from builtins import open, str |
| 13 | +import asn1 |
| 14 | +import binascii |
| 15 | +import pprint |
| 16 | + |
| 17 | + |
| 18 | +def example1(): |
| 19 | + print('Example 1') |
| 20 | + encoder = asn1.Encoder() |
| 21 | + encoder.start() |
| 22 | + encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier) |
| 23 | + print(str(binascii.hexlify(encoder.output(), ' ', 1).upper(), encoding='ascii')) |
| 24 | + print() |
| 25 | + |
| 26 | + |
| 27 | +def example2(): |
| 28 | + print('Example 2') |
| 29 | + with open('example2.der', 'wb') as f: |
| 30 | + encoder = asn1.Encoder() |
| 31 | + encoder.start(f) |
| 32 | + encoder.write('1.2.3', asn1.Numbers.ObjectIdentifier) |
| 33 | + |
| 34 | +def example3(): |
| 35 | + print('Example 3') |
| 36 | + with open('example3.der', 'wb') as f: |
| 37 | + encoder = asn1.Encoder() |
| 38 | + encoder.start(f) |
| 39 | + encoder.write(['test1', 'test2', [ |
| 40 | + 1, |
| 41 | + 0.125, |
| 42 | + b'\x01\x02\x03' |
| 43 | + ]]) |
| 44 | + print() |
| 45 | + |
| 46 | + |
| 47 | +def example4(): |
| 48 | + print('Example 4') |
| 49 | + with open('example3.der', 'rb') as f: |
| 50 | + decoder = asn1.Decoder() |
| 51 | + decoder.start(f) |
| 52 | + tag, value = decoder.read() |
| 53 | + print(tag) |
| 54 | + print(value) |
| 55 | + print() |
| 56 | + |
| 57 | + |
| 58 | +def example5(): |
| 59 | + print('Example 5') |
| 60 | + example5 = b'\x23\x0C\x03\x02\x00\x0B\x03\x02\x00\x0B\x03\x02\x04\x0F' |
| 61 | + decoder = asn1.Decoder() |
| 62 | + decoder.start(example5) |
| 63 | + tag, (val, unused) = decoder.read(asn1.ReadFlags.WithUnused) |
| 64 | + print('Tag:', tag) |
| 65 | + print('Value:', val) |
| 66 | + print('Unused bits:', unused) |
| 67 | + print() |
| 68 | + |
| 69 | + |
| 70 | +def example6(): |
| 71 | + print('Example 6') |
| 72 | + encoder = asn1.Encoder() |
| 73 | + encoder.start() |
| 74 | + encoder.enter(asn1.Numbers.Sequence) |
| 75 | + encoder.write('test1', asn1.Numbers.PrintableString) |
| 76 | + encoder.write('test2', asn1.Numbers.PrintableString) |
| 77 | + encoder.enter(asn1.Numbers.Sequence) |
| 78 | + encoder.write(1, asn1.Numbers.Integer) |
| 79 | + encoder.write(0.125, asn1.Numbers.Real) |
| 80 | + encoder.write(b'\x01\x02\x03', asn1.Numbers.OctetString) |
| 81 | + encoder.leave() |
| 82 | + encoder.leave() |
| 83 | + print(str(binascii.hexlify(encoder.output(), ' ', 1).upper(), encoding='ascii')) |
| 84 | + print() |
| 85 | + |
| 86 | + |
| 87 | +def example7(): |
| 88 | + print('Example 7') |
| 89 | + example7 = b'\x30\x80\x13\x05\x74\x65\x73\x74\x31\x13\x05\x74\x65\x73\x74\x32\x30\x80\x02\x01\x01\x09\x03\x80\xFD\x01\x04\x03\x01\x02\x03\x00\x00\x00\x00' |
| 90 | + decoder = asn1.Decoder() |
| 91 | + decoder.start(example7) |
| 92 | + tag, value = decoder.read() |
| 93 | + print(tag) |
| 94 | + pprint.pprint(value) |
| 95 | + print() |
| 96 | + |
| 97 | + |
| 98 | +def example8(): |
| 99 | + print('Example 8') |
| 100 | + example8 = (b'\x30\x82\x04\x0e\x30\x82\x03\x77\xa0\x03\x02\x01\x02\x02\x02\x15' |
| 101 | + b'\x30\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00' |
| 102 | + b'\x30\x81\xbb\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x2d\x2d' |
| 103 | + b'\x31\x12\x30\x10\x06\x03\x55\x04\x08\x13\x09\x53\x6f\x6d\x65\x53' |
| 104 | + b'\x74\x61\x74\x65\x31\x11\x30\x0f\x06\x03\x55\x04\x07\x13\x08\x53' |
| 105 | + b'\x6f\x6d\x65\x43\x69\x74\x79\x31\x19\x30\x17\x06\x03\x55\x04\x0a' |
| 106 | + b'\x13\x10\x53\x6f\x6d\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69' |
| 107 | + b'\x6f\x6e\x31\x1f\x30\x1d\x06\x03\x55\x04\x0b\x13\x16\x53\x6f\x6d' |
| 108 | + b'\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e\x61\x6c\x55' |
| 109 | + b'\x6e\x69\x74\x31\x1e\x30\x1c\x06\x03\x55\x04\x03\x13\x15\x6c\x6f' |
| 110 | + b'\x63\x61\x6c\x68\x6f\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d' |
| 111 | + b'\x61\x69\x6e\x31\x29\x30\x27\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01' |
| 112 | + b'\x09\x01\x16\x1a\x72\x6f\x6f\x74\x40\x6c\x6f\x63\x61\x6c\x68\x6f' |
| 113 | + b'\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69\x6e\x30\x1e' |
| 114 | + b'\x17\x0d\x30\x38\x30\x32\x30\x35\x30\x39\x32\x33\x33\x31\x5a\x17' |
| 115 | + b'\x0d\x30\x39\x30\x32\x30\x34\x30\x39\x32\x33\x33\x31\x5a\x30\x81' |
| 116 | + b'\xbb\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x2d\x2d\x31\x12' |
| 117 | + b'\x30\x10\x06\x03\x55\x04\x08\x13\x09\x53\x6f\x6d\x65\x53\x74\x61' |
| 118 | + b'\x74\x65\x31\x11\x30\x0f\x06\x03\x55\x04\x07\x13\x08\x53\x6f\x6d' |
| 119 | + b'\x65\x43\x69\x74\x79\x31\x19\x30\x17\x06\x03\x55\x04\x0a\x13\x10' |
| 120 | + b'\x53\x6f\x6d\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e' |
| 121 | + b'\x31\x1f\x30\x1d\x06\x03\x55\x04\x0b\x13\x16\x53\x6f\x6d\x65\x4f' |
| 122 | + b'\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e\x61\x6c\x55\x6e\x69' |
| 123 | + b'\x74\x31\x1e\x30\x1c\x06\x03\x55\x04\x03\x13\x15\x6c\x6f\x63\x61' |
| 124 | + b'\x6c\x68\x6f\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69' |
| 125 | + b'\x6e\x31\x29\x30\x27\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09\x01' |
| 126 | + b'\x16\x1a\x72\x6f\x6f\x74\x40\x6c\x6f\x63\x61\x6c\x68\x6f\x73\x74' |
| 127 | + b'\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69\x6e\x30\x81\x9f\x30' |
| 128 | + b'\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03\x81' |
| 129 | + b'\x8d\x00\x30\x81\x89\x02\x81\x81\x00\xd5\x18\xcd\x40\x91\x90\x27' |
| 130 | + b'\x5a\x77\x37\x22\xca\xba\x05\xdf\x13\x31\xe8\x74\x43\x4f\x7e\x08' |
| 131 | + b'\xa3\xa5\x76\xcd\x7b\xdd\x37\xd0\x7f\x12\x9e\x81\x73\x87\x55\x66' |
| 132 | + b'\x0d\xda\x68\xee\x38\xeb\x34\xe2\xf4\xeb\x95\xd5\xe0\xde\xef\x08' |
| 133 | + b'\x57\xf9\x03\x14\x69\xa8\x6f\x7c\xa4\xfa\x64\x51\x39\x36\xd5\x09' |
| 134 | + b'\x37\x61\x83\x13\x8c\x41\x25\xba\x60\x91\x20\x86\x5b\x60\xb5\xe2' |
| 135 | + b'\x83\x65\x66\xad\x06\xb3\x45\x71\x83\x67\xd2\xe5\x5f\x40\x42\x4b' |
| 136 | + b'\x37\xf8\x87\xd0\x09\x49\xb8\xad\x34\x76\xa3\x1b\xbf\xc1\x0f\xb7' |
| 137 | + b'\xfb\x43\xbe\x62\x33\x02\x02\x10\x61\x02\x03\x01\x00\x01\xa3\x82' |
| 138 | + b'\x01\x1d\x30\x82\x01\x19\x30\x1d\x06\x03\x55\x1d\x0e\x04\x16\x04' |
| 139 | + b'\x14\x0a\x4b\xfa\x87\x54\x17\x7e\x30\xb4\x21\x71\x56\x51\x0f\xd2' |
| 140 | + b'\x91\xc3\x30\x02\x36\x30\x81\xe9\x06\x03\x55\x1d\x23\x04\x81\xe1' |
| 141 | + b'\x30\x81\xde\x80\x14\x0a\x4b\xfa\x87\x54\x17\x7e\x30\xb4\x21\x71' |
| 142 | + b'\x56\x51\x0f\xd2\x91\xc3\x30\x02\x36\xa1\x81\xc1\xa4\x81\xbe\x30' |
| 143 | + b'\x81\xbb\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x2d\x2d\x31' |
| 144 | + b'\x12\x30\x10\x06\x03\x55\x04\x08\x13\x09\x53\x6f\x6d\x65\x53\x74' |
| 145 | + b'\x61\x74\x65\x31\x11\x30\x0f\x06\x03\x55\x04\x07\x13\x08\x53\x6f' |
| 146 | + b'\x6d\x65\x43\x69\x74\x79\x31\x19\x30\x17\x06\x03\x55\x04\x0a\x13' |
| 147 | + b'\x10\x53\x6f\x6d\x65\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f' |
| 148 | + b'\x6e\x31\x1f\x30\x1d\x06\x03\x55\x04\x0b\x13\x16\x53\x6f\x6d\x65' |
| 149 | + b'\x4f\x72\x67\x61\x6e\x69\x7a\x61\x74\x69\x6f\x6e\x61\x6c\x55\x6e' |
| 150 | + b'\x69\x74\x31\x1e\x30\x1c\x06\x03\x55\x04\x03\x13\x15\x6c\x6f\x63' |
| 151 | + b'\x61\x6c\x68\x6f\x73\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61' |
| 152 | + b'\x69\x6e\x31\x29\x30\x27\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09' |
| 153 | + b'\x01\x16\x1a\x72\x6f\x6f\x74\x40\x6c\x6f\x63\x61\x6c\x68\x6f\x73' |
| 154 | + b'\x74\x2e\x6c\x6f\x63\x61\x6c\x64\x6f\x6d\x61\x69\x6e\x82\x02\x15' |
| 155 | + b'\x30\x30\x0c\x06\x03\x55\x1d\x13\x04\x05\x30\x03\x01\x01\xff\x30' |
| 156 | + b'\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x03\x81' |
| 157 | + b'\x81\x00\x4e\x12\x46\x58\xa3\x57\xc5\x9a\xab\xfa\x32\xf5\xde\x87' |
| 158 | + b'\xfb\x77\xa8\x79\x38\x1d\x4f\xd3\x7c\x3a\x16\x60\x82\x7d\x92\xa1' |
| 159 | + b'\x58\xd2\x53\x7b\x11\x90\xec\x6d\xb0\xb0\x58\xee\x33\xb4\x7b\x1d' |
| 160 | + b'\xb8\x95\xd8\x98\xc3\x10\x81\x83\x08\x46\xe8\x9a\xb9\x6c\xbf\x8f' |
| 161 | + b'\x9e\x73\xf7\x61\x89\xc4\x6a\x1b\xc1\x98\xc6\xab\xfc\x91\xb6\x59' |
| 162 | + b'\xb8\xa5\x05\x91\x2a\xbb\xc4\x30\x16\x53\xbf\x1a\xfe\x2f\x01\x25' |
| 163 | + b'\xae\xef\xc7\xb9\xfa\xa5\x53\xf8\xd9\xf5\x8f\xae\x91\xea\x57\x28' |
| 164 | + b'\xfa\xdf\x34\x03\x29\xe8\x97\xee\x2e\x9e\x8a\x62\x45\xc7\xfc\x58' |
| 165 | + b'\xb4\x5a') |
| 166 | + |
| 167 | + decoder = asn1.Decoder() |
| 168 | + decoder.start(example8) |
| 169 | + tag, value = decoder.read() |
| 170 | + print(tag) |
| 171 | + pprint.pprint(value) |
| 172 | + print() |
| 173 | + |
| 174 | + |
| 175 | +example1() |
| 176 | +example2() |
| 177 | +example3() |
| 178 | +example4() |
| 179 | +example5() |
| 180 | +example6() |
| 181 | +example7() |
| 182 | +example8() |
0 commit comments