1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Pure, simple, BER encoding and decoding"""
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 import string
36
37
38
39
40
41
42
43
44
45
46 CLASS_MASK = 0xc0
47 CLASS_UNIVERSAL = 0x00
48 CLASS_APPLICATION = 0x40
49 CLASS_CONTEXT = 0x80
50 CLASS_PRIVATE = 0xc0
51
52 STRUCTURED_MASK = 0x20
53 STRUCTURED = 0x20
54 NOT_STRUCTURED = 0x00
55
56 TAG_MASK = 0x1f
57
58
59
60
61
62
65 Exception.__init__(self)
66 self.tag = tag
67 self.context = context
68
70 return "BERDecoderContext has no tag 0x%02x: %s" \
71 % (self.tag, self.context)
72
73 import UserList
74
76 """
77 Return a tuple of (length, lengthLength).
78 m must be atleast one byte long.
79 """
80 l=ber2int(m[offset+0])
81 ll=1
82 if l&0x80:
83 ll=1+(l&0x7F)
84 need(m, offset+ll)
85 l=ber2int(m[offset+1:offset+ll], signed=0)
86 return (l, ll)
87
89 assert i>=0
90 e=int2ber(i, signed=False)
91 if i <= 127:
92 return e
93 else:
94 l=len(e)
95 assert l>0
96 assert l<=127
97 return chr(0x80|l) + e
98
100 encoded=''
101 while ((signed and (i>127 or i<-128))
102 or (not signed and (i>255))):
103 encoded=chr(i%256)+encoded
104 i=i>>8
105 encoded=chr(i%256)+encoded
106 return encoded
107
109 need(e, 1)
110 v=0L+ord(e[0])
111 if v&0x80 and signed:
112 v=v-256
113 for i in range(1, len(e)):
114 v=(v<<8) | ord(e[i])
115 return v
116
118 tag = None
119
122
126
128 return len(str(self))
129
131 if isinstance(other, BERBase):
132 return cmp(str(self), str(other))
133 else:
134 return -1
135
137 if isinstance(other, BERBase):
138 return str(self) == str(other)
139 else:
140 return False
141
143 if isinstance(other, BERBase):
144 return str(self) != str(other)
145 else:
146 return False
147
151
153
155
160
162 tag = 0x02
163 value = None
164
165 - def fromBER(klass, tag, content, berdecoder=None):
170 fromBER = classmethod(fromBER)
171
172 - def __init__(self, value=None, tag=None):
179
185
187 if self.tag==self.__class__.tag:
188 return self.__class__.__name__+"(value=%r)"%self.value
189 else:
190 return self.__class__.__name__+"(value=%r, tag=%d)" \
191 %(self.value, self.tag)
192
194 tag = 0x04
195
196 value = None
197
198 - def fromBER(klass, tag, content, berdecoder=None):
199 assert len(content)>=0
200 r = klass(value=content, tag=tag)
201 return r
202 fromBER = classmethod(fromBER)
203
204 - def __init__(self, value=None, tag=None):
208
214
216 if self.tag==self.__class__.tag:
217 return self.__class__.__name__+"(value=%s)" \
218 %repr(self.value)
219 else:
220 return self.__class__.__name__ \
221 +"(value=%s, tag=%d)" \
222 %(repr(self.value), self.tag)
223
225 tag = 0x05
226
227 - def fromBER(klass, tag, content, berdecoder=None):
228 assert len(content) == 0
229 r = klass(tag=tag)
230 return r
231 fromBER = classmethod(fromBER)
232
235
238
240 if self.tag==self.__class__.tag:
241 return self.__class__.__name__+"()"
242 else:
243 return self.__class__.__name__+"(tag=%d)"%self.tag
244
246 tag = 0x01
247
248 - def fromBER(klass, tag, content, berdecoder=None):
253 fromBER = classmethod(fromBER)
254
255 - def __init__(self, value=None, tag=None):
264
270
272 if self.tag==self.__class__.tag:
273 return self.__class__.__name__+"(value=%d)"%self.value
274 else:
275 return self.__class__.__name__+"(value=%d, tag=%d)" \
276 %(self.value, self.tag)
277
278
281
283
284 tag = 0x10
285
286 - def fromBER(klass, tag, content, berdecoder=None):
290 fromBER = classmethod(fromBER)
291
292 - def __init__(self, value=None, tag=None):
297
301
303 if self.tag==self.__class__.tag:
304 return self.__class__.__name__+"(value=%s)"%repr(self.data)
305 else:
306 return self.__class__.__name__+"(value=%s, tag=%d)" \
307 %(repr(self.data), self.tag)
308
309
312
316
317
318
320 Identities = {
321 BERInteger.tag: BERInteger,
322 BEROctetString.tag: BEROctetString,
323 BERNull.tag: BERNull,
324 BERBoolean.tag: BERBoolean,
325 BEREnumerated.tag: BEREnumerated,
326 BERSequence.tag: BERSequence,
327 BERSet.tag: BERSet,
328 }
329
330 - def __init__(self, fallback=None, inherit=None):
331 self.fallback=fallback
332 self.inherit_context=inherit
333
334 - def lookup_id(self, id):
335 try:
336 return self.Identities[id]
337 except KeyError:
338 if self.fallback:
339 return self.fallback.lookup_id(id)
340 else:
341 return None
342
344 return self.inherit_context or self
345
346 - def __repr__(self):
347 identities = []
348 for tag, class_ in self.Identities.items():
349 identities.append('0x%02x: %s' % (tag, class_.__name__))
350 return "<"+self.__class__.__name__ \
351 +" identities={%s}" % ', '.join(identities) \
352 +" fallback="+repr(self.fallback) \
353 +" inherit="+repr(self.inherit_context) \
354 +">"
355
357 """berDecodeObject(context, string) -> (berobject, bytesUsed)
358 berobject may be None.
359 """
360 while m:
361 need(m, 2)
362 i=ber2int(m[0], signed=0)&(CLASS_MASK|TAG_MASK)
363
364 length, lenlen = berDecodeLength(m, offset=1)
365 need(m, 1+lenlen+length)
366 m2 = m[1+lenlen:1+lenlen+length]
367
368 berclass=context.lookup_id(i)
369 if berclass:
370 inh=context.inherit()
371 assert inh
372 r = berclass.fromBER(tag=i,
373 content=m2,
374 berdecoder=inh)
375 return (r, 1+lenlen+length)
376 else:
377
378 print str(UnknownBERTag(i, context))
379 return (None, 1+lenlen+length)
380 return (None, 0)
381
383 """berDecodeMultiple(content, berdecoder) -> [objects]
384
385 Decodes everything in content and returns a list of decoded
386 objects.
387
388 All of content will be decoded, and content must contain complete
389 BER objects.
390 """
391 l = []
392 while content:
393 n, bytes = berDecodeObject(berdecoder, content)
394 if n is not None:
395 l.append(n)
396 assert bytes <= len(content)
397 content = content[bytes:]
398 return l
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425