summaryrefslogtreecommitdiff
path: root/lib/Crypto/Util
diff options
context:
space:
mode:
authorLegrandin <gooksankoo@hoiptorrow.mailexpire.com>2011-01-16 22:05:54 +0100
committerLegrandin <gooksankoo@hoiptorrow.mailexpire.com>2011-01-16 22:05:54 +0100
commit1221bebb3eb2eb1b6e075cf13c0fb4f550404134 (patch)
treeee69d43d001baa7e9908153b85878d46af20d176 /lib/Crypto/Util
parent894bbc23e986ca21ae6d712604b000169dd88317 (diff)
downloadpycrypto-1221bebb3eb2eb1b6e075cf13c0fb4f550404134.tar.gz
Pure tab-to-space conversion with :retab in vim.
Diffstat (limited to 'lib/Crypto/Util')
-rw-r--r--lib/Crypto/Util/asn1.py222
1 files changed, 111 insertions, 111 deletions
diff --git a/lib/Crypto/Util/asn1.py b/lib/Crypto/Util/asn1.py
index 06ed175..4d2f9e6 100644
--- a/lib/Crypto/Util/asn1.py
+++ b/lib/Crypto/Util/asn1.py
@@ -31,31 +31,31 @@ class DerObject:
"""
# Known TAG types
- typeTags = { 'SEQUENCE':'\x30', 'BIT STRING':'\x03', 'INTEGER':'\x02' }
+ typeTags = { 'SEQUENCE':'\x30', 'BIT STRING':'\x03', 'INTEGER':'\x02' }
- def __init__(self, ASN1Type=None):
- """Initialize the DER object according to a specific type.
+ def __init__(self, ASN1Type=None):
+ """Initialize the DER object according to a specific type.
The ASN.1 type is either specified as the ASN.1 string (e.g.
'SEQUENCE'), directly with its numerical tag or with no tag
atl all (None)."""
self.typeTag = self.typeTags.get(ASN1Type, ASN1Type)
- self.payload = ''
+ self.payload = ''
- def _lengthOctets(self, payloadLen):
+ def _lengthOctets(self, payloadLen):
"""Return a string that encodes the given payload length (in
bytes) in a format suitable for a DER length tag (L).
"""
- if payloadLen>127:
- encoding = long_to_bytes(payloadLen)
- return chr(len(encoding)+128) + encoding
- return chr(payloadLen)
+ if payloadLen>127:
+ encoding = long_to_bytes(payloadLen)
+ return chr(len(encoding)+128) + encoding
+ return chr(payloadLen)
- def encode(self):
+ def encode(self):
"""Return a complete DER element, fully encoded as a TLV."""
- return self.typeTag + self._lengthOctets(len(self.payload)) + self.payload
+ return self.typeTag + self._lengthOctets(len(self.payload)) + self.payload
- def _decodeLen(self, idx, str):
+ def _decodeLen(self, idx, str):
"""Given a (part of a) DER element, and an index to the first byte of
a DER length tag (L), return a tuple with the payload size,
and the index of the first byte of the such payload (V).
@@ -63,15 +63,15 @@ class DerObject:
Raises a ValueError exception if the DER length is invalid.
Raises an IndexError exception if the DER element is too short.
"""
- length = ord(str[idx])
- if length<=127:
- return (length,idx+1)
- payloadLength = bytes_to_long(str[idx+1:idx+1+(length & 0x7F)])
- if payloadLength<=127:
- raise ValueError("Not a DER length tag.")
- return (payloadLength, idx+1+(length & 0x7F))
-
- def decode(self, derEle, noLeftOvers=0):
+ length = ord(str[idx])
+ if length<=127:
+ return (length,idx+1)
+ payloadLength = bytes_to_long(str[idx+1:idx+1+(length & 0x7F)])
+ if payloadLength<=127:
+ raise ValueError("Not a DER length tag.")
+ return (payloadLength, idx+1+(length & 0x7F))
+
+ def decode(self, derEle, noLeftOvers=0):
"""Decode a complete DER element, and re-initializes this
object with it.
@@ -85,35 +85,35 @@ class DerObject:
Raises a ValueError exception in case of parsing errors.
Raises an IndexError exception if the DER element is too short.
"""
- try:
- self.typeTag = derEle[0]
- if (ord(self.typeTag) & 0x1F)==0x1F:
- raise ValueError("Unsupported DER tag")
- (length,idx) = self._decodeLen(1, derEle)
- if noLeftOvers and len(derEle) != (idx+length):
- raise ValueError("Not a DER structure")
- self.payload = derEle[idx:idx+length]
- except IndexError:
- raise ValueError("Not a valid DER SEQUENCE.")
- return idx+length
+ try:
+ self.typeTag = derEle[0]
+ if (ord(self.typeTag) & 0x1F)==0x1F:
+ raise ValueError("Unsupported DER tag")
+ (length,idx) = self._decodeLen(1, derEle)
+ if noLeftOvers and len(derEle) != (idx+length):
+ raise ValueError("Not a DER structure")
+ self.payload = derEle[idx:idx+length]
+ except IndexError:
+ raise ValueError("Not a valid DER SEQUENCE.")
+ return idx+length
class DerInteger(DerObject):
- def __init__(self, value = 0):
+ def __init__(self, value = 0):
"""Class to model an INTEGER DER element.
Limitation: only non-negative values are supported.
"""
- DerObject.__init__(self, 'INTEGER')
- self.value = value
+ DerObject.__init__(self, 'INTEGER')
+ self.value = value
- def encode(self):
+ def encode(self):
"""Return a complete INTEGER DER element, fully encoded as a TLV."""
- self.payload = long_to_bytes(self.value)
- if ord(self.payload[0])>127:
- self.payload = '\x00' + self.payload
- return DerObject.encode(self)
+ self.payload = long_to_bytes(self.value)
+ if ord(self.payload[0])>127:
+ self.payload = '\x00' + self.payload
+ return DerObject.encode(self)
- def decode(self, derEle, noLeftOvers=0):
+ def decode(self, derEle, noLeftOvers=0):
"""Decode a complete INTEGER DER element, and re-initializes this
object with it.
@@ -128,14 +128,14 @@ class DerInteger(DerObject):
valid non-negative INTEGER.
Raises an IndexError exception if the DER element is too short.
"""
- tlvLength = DerObject.decode(self, derEle, noLeftOvers)
- if self.typeTag!=self.typeTags['INTEGER']:
+ tlvLength = DerObject.decode(self, derEle, noLeftOvers)
+ if self.typeTag!=self.typeTags['INTEGER']:
raise ValueError ("Not a DER INTEGER.")
- if ord(self.payload[0])>127:
- raise ValueError ("Negative INTEGER.")
- self.value = bytes_to_long(self.payload)
- return tlvLength
-
+ if ord(self.payload[0])>127:
+ raise ValueError ("Negative INTEGER.")
+ self.value = bytes_to_long(self.payload)
+ return tlvLength
+
class DerSequence(DerObject):
"""Class to model a SEQUENCE DER element.
@@ -145,61 +145,61 @@ class DerSequence(DerObject):
sub-element (TLV).
"""
- def __init__(self):
+ def __init__(self):
"""Initialize the SEQUENCE DER object. Always empty
initially."""
- DerObject.__init__(self, 'SEQUENCE')
- self._seq = []
+ DerObject.__init__(self, 'SEQUENCE')
+ self._seq = []
## A few methods to make it behave like a python sequence
- def __delitem__(self, n):
- del self._seq[n]
- def __getitem__(self, n):
- return self._seq[n]
- def __setitem__(self, key, value):
- self._seq[key] = value
- def __setslice__(self,i,j,sequence):
- self._seq[i:j] = sequence
- def __delslice__(self,i,j):
- del self._seq[i:j]
- def __getslice__(self, i, j):
- return self._seq[max(0, i):max(0, j)]
- def __len__(self):
- return len(self._seq)
- def append(self, item):
- return self._seq.append(item)
-
- def hasOnlyInts(self):
+ def __delitem__(self, n):
+ del self._seq[n]
+ def __getitem__(self, n):
+ return self._seq[n]
+ def __setitem__(self, key, value):
+ self._seq[key] = value
+ def __setslice__(self,i,j,sequence):
+ self._seq[i:j] = sequence
+ def __delslice__(self,i,j):
+ del self._seq[i:j]
+ def __getslice__(self, i, j):
+ return self._seq[max(0, i):max(0, j)]
+ def __len__(self):
+ return len(self._seq)
+ def append(self, item):
+ return self._seq.append(item)
+
+ def hasOnlyInts(self):
"""Return 1/True is all items in this sequence are numbers."""
- if not self._seq: return 0
- test = 0
- for item in self._seq:
- try:
- test += item
- except TypeError:
- return 0
- return 1
-
- def encode(self):
+ if not self._seq: return 0
+ test = 0
+ for item in self._seq:
+ try:
+ test += item
+ except TypeError:
+ return 0
+ return 1
+
+ def encode(self):
"""Return the DER encoding for the ASN.1 SEQUENCE, containing
the non-negative integers and longs added to this object.
Limitation: Raises a ValueError exception if it some elements
in the sequence are neither Python integers nor complete DER INTEGERs.
"""
- self.payload = ''
- for item in self._seq:
- try:
- self.payload += item
- except:
- try:
- self.payload += DerInteger(item).encode()
- except:
- raise ValueError("Trying to DER encode an unknown object")
- return DerObject.encode(self)
-
- def decode(self, derEle, noLeftOvers=0):
+ self.payload = ''
+ for item in self._seq:
+ try:
+ self.payload += item
+ except:
+ try:
+ self.payload += DerInteger(item).encode()
+ except:
+ raise ValueError("Trying to DER encode an unknown object")
+ return DerObject.encode(self)
+
+ def decode(self, derEle, noLeftOvers=0):
"""Decode a complete SEQUENCE DER element, and re-initializes this
object with it.
@@ -218,24 +218,24 @@ class DerSequence(DerObject):
Raises an IndexError exception if the DER element is too short.
"""
- self._seq = []
- try:
- tlvLength = DerObject.decode(self, derEle, noLeftOvers)
- if self.typeTag!=self.typeTags['SEQUENCE']:
- raise ValueError("Not a DER SEQUENCE.")
- # Scan one TLV at once
- idx = 0
- while idx<len(self.payload):
- typeTag = self.payload[idx]
- if typeTag==self.typeTags['INTEGER']:
- newInteger = DerInteger()
- idx += newInteger.decode(self.payload[idx:])
- self._seq.append(newInteger.value)
- else:
- itemLen,itemIdx = self._decodeLen(idx+1,self.payload)
- self._seq.append(self.payload[idx:itemIdx+itemLen])
- idx = itemIdx + itemLen
- except IndexError:
- raise ValueError("Not a valid DER SEQUENCE.")
- return tlvLength
+ self._seq = []
+ try:
+ tlvLength = DerObject.decode(self, derEle, noLeftOvers)
+ if self.typeTag!=self.typeTags['SEQUENCE']:
+ raise ValueError("Not a DER SEQUENCE.")
+ # Scan one TLV at once
+ idx = 0
+ while idx<len(self.payload):
+ typeTag = self.payload[idx]
+ if typeTag==self.typeTags['INTEGER']:
+ newInteger = DerInteger()
+ idx += newInteger.decode(self.payload[idx:])
+ self._seq.append(newInteger.value)
+ else:
+ itemLen,itemIdx = self._decodeLen(idx+1,self.payload)
+ self._seq.append(self.payload[idx:itemIdx+itemLen])
+ idx = itemIdx + itemLen
+ except IndexError:
+ raise ValueError("Not a valid DER SEQUENCE.")
+ return tlvLength