33from . import ecdsa
44from . import der
55from . import rfc6979
6+ from . import ellipticcurve
67from .curves import NIST192p , find_curve
78from .util import string_to_number , number_to_string , randrange
89from .util import sigencode_string , sigdecode_string
9- from .util import oid_ecPublicKey , encoded_oid_ecPublicKey
10+ from .util import oid_ecPublicKey , encoded_oid_ecPublicKey , MalformedSignature
1011from .six import PY3 , b
1112from hashlib import sha1
1213
@@ -15,6 +16,11 @@ class BadSignatureError(Exception):
1516class BadDigestError (Exception ):
1617 pass
1718
19+
20+ class MalformedPointError (AssertionError ):
21+ pass
22+
23+
1824class VerifyingKey :
1925 def __init__ (self , _error__please_use_generate = None ):
2026 if not _error__please_use_generate :
@@ -33,17 +39,21 @@ def from_public_point(klass, point, curve=NIST192p, hashfunc=sha1):
3339 def from_string (klass , string , curve = NIST192p , hashfunc = sha1 ,
3440 validate_point = True ):
3541 order = curve .order
36- assert len (string ) == curve .verifying_key_length , \
37- (len (string ), curve .verifying_key_length )
42+ if len (string ) != curve .verifying_key_length :
43+ raise MalformedPointError (
44+ "Malformed encoding of public point. Expected string {0} bytes"
45+ " long, received {1} bytes long string" .format (
46+ curve .verifying_key_length , len (string )))
3847 xs = string [:curve .baselen ]
3948 ys = string [curve .baselen :]
40- assert len (xs ) == curve .baselen , (len (xs ), curve .baselen )
41- assert len (ys ) == curve .baselen , (len (ys ), curve .baselen )
49+ if len (xs ) != curve .baselen :
50+ raise MalformedPointError ("Unexpected length of encoded x" )
51+ if len (ys ) != curve .baselen :
52+ raise MalformedPointError ("Unexpected length of encoded y" )
4253 x = string_to_number (xs )
4354 y = string_to_number (ys )
44- if validate_point :
45- assert ecdsa .point_is_valid (curve .generator , x , y )
46- from . import ellipticcurve
55+ if validate_point and not ecdsa .point_is_valid (curve .generator , x , y ):
56+ raise MalformedPointError ("Point does not lie on the curve" )
4757 point = ellipticcurve .Point (curve .curve , x , y , order )
4858 return klass .from_public_point (point , curve , hashfunc )
4959
@@ -65,13 +75,18 @@ def from_der(klass, string):
6575 if empty != b ("" ):
6676 raise der .UnexpectedDER ("trailing junk after DER pubkey objects: %s" %
6777 binascii .hexlify (empty ))
68- assert oid_pk == oid_ecPublicKey , (oid_pk , oid_ecPublicKey )
78+ if oid_pk != oid_ecPublicKey :
79+ raise der .UnexpectedDER (
80+ "Unexpected OID in encoding, received {0}, expected {1}"
81+ .format (oid_pk , oid_ecPublicKey ))
6982 curve = find_curve (oid_curve )
7083 point_str , empty = der .remove_bitstring (point_str_bitstring )
7184 if empty != b ("" ):
7285 raise der .UnexpectedDER ("trailing junk after pubkey pointstring: %s" %
7386 binascii .hexlify (empty ))
74- assert point_str .startswith (b ("\x00 \x04 " ))
87+ if not point_str .startswith (b ("\x00 \x04 " )):
88+ raise der .UnexpectedDER (
89+ "Unsupported or invalid encoding of pubcli key" )
7590 return klass .from_string (point_str [2 :], curve )
7691
7792 def to_string (self ):
@@ -106,11 +121,14 @@ def verify_digest(self, signature, digest, sigdecode=sigdecode_string):
106121 "for your digest (%d)" % (self .curve .name ,
107122 8 * len (digest )))
108123 number = string_to_number (digest )
109- r , s = sigdecode (signature , self .pubkey .order )
124+ try :
125+ r , s = sigdecode (signature , self .pubkey .order )
126+ except (der .UnexpectedDER , MalformedSignature ) as e :
127+ raise BadSignatureError ("Malformed formatting of signature" , e )
110128 sig = ecdsa .Signature (r , s )
111129 if self .pubkey .verifies (number , sig ):
112130 return True
113- raise BadSignatureError
131+ raise BadSignatureError ( "Signature verification failed" )
114132
115133class SigningKey :
116134 def __init__ (self , _error__please_use_generate = None ):
@@ -134,7 +152,10 @@ def from_secret_exponent(klass, secexp, curve=NIST192p, hashfunc=sha1):
134152 self .default_hashfunc = hashfunc
135153 self .baselen = curve .baselen
136154 n = curve .order
137- assert 1 <= secexp < n
155+ if not 1 <= secexp < n :
156+ raise MalformedPointError (
157+ "Invalid value for secexp, expected integer between 1 and {0}"
158+ .format (n ))
138159 pubkey_point = curve .generator * secexp
139160 pubkey = ecdsa .Public_key (curve .generator , pubkey_point )
140161 pubkey .order = n
@@ -146,7 +167,10 @@ def from_secret_exponent(klass, secexp, curve=NIST192p, hashfunc=sha1):
146167
147168 @classmethod
148169 def from_string (klass , string , curve = NIST192p , hashfunc = sha1 ):
149- assert len (string ) == curve .baselen , (len (string ), curve .baselen )
170+ if len (string ) != curve .baselen :
171+ raise MalformedPointError (
172+ "Invalid length of private key, received {0}, expected {1}"
173+ .format (len (string ), curve .baselen ))
150174 secexp = string_to_number (string )
151175 return klass .from_secret_exponent (secexp , curve , hashfunc )
152176
0 commit comments