Package Peach :: Package Transformers :: Module asn1
[hide private]

Source Code for Module Peach.Transformers.asn1

  1   
  2  ''' 
  3  ASN.1 transformers.  These transformers perform correct ASN.1 encoding.  The 
  4  data Generators module contains a couple of additional ASN.1 classes 
  5  that perform incorrect encodings. 
  6   
  7  @author: Michael Eddington 
  8  @version: $Id: Peach.Transformers.asn1-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  9  ''' 
 10   
 11  # 
 12  # Copyright (c) 2007-2008 Michael Eddington 
 13  # 
 14  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 15  # of this software and associated documentation files (the "Software"), to deal 
 16  # in the Software without restriction, including without limitation the rights  
 17  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 18  # copies of the Software, and to permit persons to whom the Software is  
 19  # furnished to do so, subject to the following conditions: 
 20  # 
 21  # The above copyright notice and this permission notice shall be included in     
 22  # all copies or substantial portions of the Software. 
 23  # 
 24  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 25  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 26  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 27  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 28  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 29  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 30  # SOFTWARE. 
 31  # 
 32   
 33  # Authors: 
 34  #   Michael Eddington (mike@phed.org) 
 35   
 36  # $Id: Peach.Transformers.asn1-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 37   
 38  try: 
 39          from struct import * 
 40          from Peach.transformer import Transformer 
 41          from pyasn1.type import univ 
 42          from pyasn1.codec import der, ber, cer 
 43           
44 - class DerEncodeOctetString(Transformer):
45 ''' 46 DER encode an octect string ASN.1 style 47 ''' 48
49 - def realEncode(self, data):
50 return der.encoder.encode(univ.OctetString(data))
51
52 - class DerEncodeBitString(Transformer):
53 ''' 54 DER encode a bit string ASN.1 style 55 ''' 56
57 - def realEncode(self, data):
58 return der.encoder.encode(univ.BitString(data))
59
60 - class DerEncodeInteger(Transformer):
61 ''' 62 DER encode an integer ASN.1 style 63 ''' 64
65 - def realEncode(self, data):
66 return der.encoder.encode(univ.Integer(int(data)))
67
68 - class DerEncodeBoolean(Transformer):
69 ''' 70 DER encode a boolean ASN.1 style. Expects 0 or 1. 71 ''' 72
73 - def realEncode(self, data):
74 data = int(data) 75 if data != 0 and data != 1: 76 raise Exception("DerEncodeBoolean transformer expects 0 or 1") 77 78 return der.encoder.encode(univ.Boolean(data))
79
80 - class DerEncodeObjectIdentifier(Transformer):
81 ''' 82 DER encode an object identifierASN.1 style. 83 ''' 84
85 - def realEncode(self, data):
86 return der.encoder.encode(univ.ObjectIdentifier(data))
87 88 # ############################################################# 89
90 - class BerEncodeOctetString(Transformer):
91 ''' 92 BER encode a string ASN.1 style 93 ''' 94
95 - def realEncode(self, data):
96 return ber.encoder.encode(univ.OctetString(data))
97
98 - class BerEncodeBitString(Transformer):
99 ''' 100 BER encode a bit string ASN.1 style 101 ''' 102
103 - def realEncode(self, data):
104 return ber.encoder.encode(univ.BitString(data))
105
106 - class BerEncodeInteger(Transformer):
107 ''' 108 BER encode an integer ASN.1 style 109 ''' 110
111 - def realEncode(self, data):
112 return ber.encoder.encode(univ.Integer(int(data)))
113
114 - class BerEncodeBoolean(Transformer):
115 ''' 116 BER encode a boolean ASN.1 style. Expects 0 or 1. 117 ''' 118
119 - def realEncode(self, data):
120 data = int(data) 121 if data != 0 and data != 1: 122 raise Exception("BerEncodeBoolean transformer expects 0 or 1") 123 124 return ber.encoder.encode(univ.Boolean(data))
125
126 - class BerEncodeObjectIdentifier(Transformer):
127 ''' 128 BER encode an object identifierASN.1 style. 129 ''' 130
131 - def realEncode(self, data):
132 return ber.encoder.encode(univ.ObjectIdentifier(data))
133 134 # ############################################################# 135
136 - class CerEncodeOctetString(Transformer):
137 ''' 138 CER encode a string ASN.1 style 139 ''' 140
141 - def realEncode(self, data):
142 return cer.encoder.encode(univ.OctetString(data))
143
144 - class CerEncodeBitString(Transformer):
145 ''' 146 CER encode a bit string ASN.1 style 147 ''' 148
149 - def realEncode(self, data):
150 return cer.encoder.encode(univ.BitString(data))
151
152 - class CerEncodeInteger(Transformer):
153 ''' 154 CER encode an integer ASN.1 style 155 ''' 156
157 - def realEncode(self, data):
158 return cer.encoder.encode(univ.Integer(int(data)))
159
160 - class CerEncodeBoolean(Transformer):
161 ''' 162 CER encode a boolean ASN.1 style. Expects 0 or 1. 163 ''' 164
165 - def realEncode(self, data):
166 data = int(data) 167 if data != 0 and data != 1: 168 raise Exception("CerEncodeBoolean transformer expects 0 or 1") 169 170 return cer.encoder.encode(univ.Boolean(data))
171
172 - class CerEncodeObjectIdentifier(Transformer):
173 ''' 174 CER encode an object identifierASN.1 style. 175 ''' 176
177 - def realEncode(self, data):
178 return cer.encoder.encode(univ.ObjectIdentifier(data))
179 180 except: 181 pass 182 183 184 # end 185