Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: net/tools/testserver/asn1.py

Issue 9663017: net: add OCSP tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 # This file implements very minimal ASN.1, DER serialization.
6
7 import types
8
9
10 def ToDER(obj):
11 '''ToDER converts the given object into DER encoding'''
12 if type(obj) == types.NoneType:
13 # None turns into NULL
14 return TagAndLength(5, 0)
15 if type(obj) == types.StringType:
16 # Strings are PRINTABLESTRING
17 return TagAndLength(19, len(obj)) + obj
18 if type(obj) == types.BooleanType:
19 val = "\x00"
20 if obj:
21 val = "\xff"
22 return TagAndLength(1, 1) + val
23 if type(obj) == types.IntType or type(obj) == types.LongType:
24 big_endian = []
25 val = obj
26 while val != 0:
27 big_endian.append(val & 0xff)
28 val >>= 8
29
30 if len(big_endian) == 0 or big_endian[-1] >= 128:
31 big_endian.append(0)
32
33 big_endian.reverse()
34 return TagAndLength(2, len(big_endian)) + ToBytes(big_endian)
35
36 return obj.ToDER()
37
38
39 def ToBytes(array_of_bytes):
40 '''ToBytes converts the array of byte values into a binary string'''
41 return ''.join([chr(x) for x in array_of_bytes])
42
43 def TagAndLength(tag, length):
44 der = [tag]
45 if length < 128:
46 der.append(length)
47 elif length < 256:
48 der.append(0x81)
49 der.append(length)
50 elif length < 65535:
51 der.append(0x82)
52 der.append(length >> 8)
53 der.append(length & 0xff)
54 else:
55 assert False
56
57 return ToBytes(der)
58
59
60 class Raw(object):
61 '''Raw contains raw DER encoded bytes that are used verbatim'''
62 def __init__(self, der):
63 self.der = der
64
65 def ToDER(self):
66 return self.der
67
68
69 class Explicit(object):
70 '''Explicit prepends an explicit tag'''
71 def __init__(self, tag, child):
72 self.tag = tag
73 self.child = child
74
75 def ToDER(self):
76 der = ToDER(self.child)
77 tag = self.tag
78 tag |= 0x80 # content specific
79 tag |= 0x20 # complex
80 return TagAndLength(tag, len(der)) + der
81
82
83 class ENUMERATED(object):
84 def __init__(self, value):
85 self.value = value
86
87 def ToDER(self):
88 return TagAndLength(10, 1) + chr(self.value)
89
90
91 class SEQUENCE(object):
92 def __init__(self, children):
93 self.children = children
94
95 def ToDER(self):
96 der = ''.join([ToDER(x) for x in self.children])
97 return TagAndLength(0x30, len(der)) + der
98
99
100 class SET(object):
101 def __init__(self, children):
102 self.children = children
103
104 def ToDER(self):
105 der = ''.join([ToDER(x) for x in self.children])
106 return TagAndLength(0x31, len(der)) + der
107
108
109 class OCTETSTRING(object):
110 def __init__(self, val):
111 self.val = val
112
113 def ToDER(self):
114 return TagAndLength(4, len(self.val)) + self.val
115
116
117 class OID(object):
118 def __init__(self, parts):
119 self.parts = parts
120
121 def ToDER(self):
122 if len(self.parts) < 2 or self.parts[0] > 6 or self.parts[1] >= 40:
123 assert False
124
125 der = [self.parts[0]*40 + self.parts[1]]
126 for x in self.parts[2:]:
127 if x == 0:
128 der.append(0)
129 else:
130 octets = []
131 while x != 0:
132 v = x & 0x7f
133 if len(octets) > 0:
134 v |= 0x80
135 octets.append(v)
136 x >>= 7
137 octets.reverse()
138 der = der + octets
139
140 return TagAndLength(6, len(der)) + ToBytes(der)
141
142
143 class UTCTime(object):
144 def __init__(self, time_str):
145 self.time_str = time_str
146
147 def ToDER(self):
148 return TagAndLength(23, len(self.time_str)) + self.time_str
149
150
151 class GeneralizedTime(object):
152 def __init__(self, time_str):
153 self.time_str = time_str
154
155 def ToDER(self):
156 return TagAndLength(24, len(self.time_str)) + self.time_str
157
158
159 class BitString(object):
160 def __init__(self, bits):
161 self.bits = bits
162
163 def ToDER(self):
164 return TagAndLength(3, 1 + len(self.bits)) + "\x00" + self.bits
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698