Skip to content

Commit 12567ae

Browse files
committed
bytes input for VerifyingKey.from_der()
1 parent e7d9b3e commit 12567ae

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/ecdsa/der.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def remove_constructed(string):
135135
def remove_sequence(string):
136136
if not string:
137137
raise UnexpectedDER("Empty string does not encode a sequence")
138-
if not string.startswith(b("\x30")):
138+
if string[:1] != b"\x30":
139139
n = string[0] if isinstance(string[0], integer_types) else \
140140
ord(string[0])
141141
raise UnexpectedDER("wanted type 'sequence' (0x30), got 0x%02x" % n)
@@ -157,7 +157,7 @@ def remove_octet_string(string):
157157

158158

159159
def remove_object(string):
160-
if not string.startswith(b("\x06")):
160+
if string[:1] != b"\x06":
161161
n = string[0] if isinstance(string[0], integer_types) else ord(string[0])
162162
raise UnexpectedDER("wanted type 'object' (0x06), got 0x%02x" % n)
163163
length, lengthlength = read_length(string[1:])
@@ -302,7 +302,7 @@ def remove_bitstring(string, expect_unused=_sentry):
302302
" specified",
303303
DeprecationWarning)
304304
num = string[0] if isinstance(string[0], integer_types) else ord(string[0])
305-
if not string.startswith(b("\x03")):
305+
if string[:1] != b"\x03":
306306
raise UnexpectedDER("wanted bitstring (0x03), got 0x%02x" % num)
307307
length, llen = read_length(string[1:])
308308
if not length:

src/ecdsa/keys.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,24 +328,25 @@ def from_der(cls, string):
328328
:return: Initialised VerifyingKey object
329329
:rtype: VerifyingKey
330330
"""
331+
string = normalise_bytes(string)
331332
# [[oid_ecPublicKey,oid_curve], point_str_bitstring]
332333
s1, empty = der.remove_sequence(string)
333-
if empty != b(""):
334+
if empty != b"":
334335
raise der.UnexpectedDER("trailing junk after DER pubkey: %s" %
335336
binascii.hexlify(empty))
336337
s2, point_str_bitstring = der.remove_sequence(s1)
337338
# s2 = oid_ecPublicKey,oid_curve
338339
oid_pk, rest = der.remove_object(s2)
339340
oid_curve, empty = der.remove_object(rest)
340-
if empty != b(""):
341+
if empty != b"":
341342
raise der.UnexpectedDER("trailing junk after DER pubkey objects: %s" %
342343
binascii.hexlify(empty))
343344
if not oid_pk == oid_ecPublicKey:
344345
raise der.UnexpectedDER("Unexpected object identifier in DER "
345346
"encoding: {0!r}".format(oid_pk))
346347
curve = find_curve(oid_curve)
347348
point_str, empty = der.remove_bitstring(point_str_bitstring, 0)
348-
if empty != b(""):
349+
if empty != b"":
349350
raise der.UnexpectedDER("trailing junk after pubkey pointstring: %s" %
350351
binascii.hexlify(empty))
351352
# raw encoding of point is invalid in DER files

src/ecdsa/test_keys.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def test_bytes(self):
127127

128128
self.assertEqual(self.vk.to_string(), vk.to_string())
129129

130-
@unittest.expectedFailure
131130
def test_bytes_memoryview(self):
132131
vk = VerifyingKey.from_der(buffer(self.key_bytes))
133132

@@ -138,15 +137,19 @@ def test_bytearray(self):
138137

139138
self.assertEqual(self.vk.to_string(), vk.to_string())
140139

141-
@unittest.expectedFailure
142140
def test_bytesarray_memoryview(self):
143141
vk = VerifyingKey.from_der(buffer(bytearray(self.key_bytes)))
144142

145143
self.assertEqual(self.vk.to_string(), vk.to_string())
146144

147-
@unittest.expectedFailure
148145
def test_array_array_of_bytes(self):
149146
arr = array.array('B', self.key_bytes)
150147
vk = VerifyingKey.from_der(arr)
151148

152149
self.assertEqual(self.vk.to_string(), vk.to_string())
150+
151+
def test_array_array_of_bytes_memoryview(self):
152+
arr = array.array('B', self.key_bytes)
153+
vk = VerifyingKey.from_der(buffer(arr))
154+
155+
self.assertEqual(self.vk.to_string(), vk.to_string())

0 commit comments

Comments
 (0)