7777from . import der
7878from . import rfc6979
7979from . import ellipticcurve
80- from .curves import NIST192p , find_curve
80+ from .curves import NIST192p , Curve
8181from .ecdsa import RSZeroError
8282from .util import string_to_number , number_to_string , randrange
8383from .util import sigencode_string , sigdecode_string , bit_length
@@ -315,7 +315,13 @@ def from_string(
315315 return cls .from_public_point (point , curve , hashfunc , validate_point )
316316
317317 @classmethod
318- def from_pem (cls , string , hashfunc = sha1 , valid_encodings = None ):
318+ def from_pem (
319+ cls ,
320+ string ,
321+ hashfunc = sha1 ,
322+ valid_encodings = None ,
323+ valid_curve_encodings = None ,
324+ ):
319325 """
320326 Initialise from public key stored in :term:`PEM` format.
321327
@@ -324,7 +330,7 @@ def from_pem(cls, string, hashfunc=sha1, valid_encodings=None):
324330 See the :func:`~VerifyingKey.from_der()` method for details of the
325331 format supported.
326332
327- Note: only a single PEM object encoding is supported in provided
333+ Note: only a single PEM object decoding is supported in provided
328334 string.
329335
330336 :param string: text with PEM-encoded public ECDSA key
@@ -334,6 +340,11 @@ def from_pem(cls, string, hashfunc=sha1, valid_encodings=None):
334340 :term:`hybrid`. To read malformed files, include
335341 :term:`raw encoding` with ``raw`` in the list.
336342 :type valid_encodings: :term:`set-like object
343+ :param valid_curve_encodings: list of allowed encoding formats
344+ for curve parameters. By default (``None``) all are supported:
345+ ``named_curve`` and ``explicit``.
346+ :type valid_curve_encodings: :term:`set-like object`
347+
337348
338349 :return: Initialised VerifyingKey object
339350 :rtype: VerifyingKey
@@ -342,10 +353,17 @@ def from_pem(cls, string, hashfunc=sha1, valid_encodings=None):
342353 der .unpem (string ),
343354 hashfunc = hashfunc ,
344355 valid_encodings = valid_encodings ,
356+ valid_curve_encodings = valid_curve_encodings ,
345357 )
346358
347359 @classmethod
348- def from_der (cls , string , hashfunc = sha1 , valid_encodings = None ):
360+ def from_der (
361+ cls ,
362+ string ,
363+ hashfunc = sha1 ,
364+ valid_encodings = None ,
365+ valid_curve_encodings = None ,
366+ ):
349367 """
350368 Initialise the key stored in :term:`DER` format.
351369
@@ -375,6 +393,10 @@ def from_der(cls, string, hashfunc=sha1, valid_encodings=None):
375393 :term:`hybrid`. To read malformed files, include
376394 :term:`raw encoding` with ``raw`` in the list.
377395 :type valid_encodings: :term:`set-like object
396+ :param valid_curve_encodings: list of allowed encoding formats
397+ for curve parameters. By default (``None``) all are supported:
398+ ``named_curve`` and ``explicit``.
399+ :type valid_curve_encodings: :term:`set-like object`
378400
379401 :return: Initialised VerifyingKey object
380402 :rtype: VerifyingKey
@@ -391,18 +413,12 @@ def from_der(cls, string, hashfunc=sha1, valid_encodings=None):
391413 s2 , point_str_bitstring = der .remove_sequence (s1 )
392414 # s2 = oid_ecPublicKey,oid_curve
393415 oid_pk , rest = der .remove_object (s2 )
394- oid_curve , empty = der .remove_object (rest )
395- if empty != b"" :
396- raise der .UnexpectedDER (
397- "trailing junk after DER pubkey objects: %s"
398- % binascii .hexlify (empty )
399- )
400416 if not oid_pk == oid_ecPublicKey :
401417 raise der .UnexpectedDER (
402418 "Unexpected object identifier in DER "
403419 "encoding: {0!r}" .format (oid_pk )
404420 )
405- curve = find_curve ( oid_curve )
421+ curve = Curve . from_der ( rest , valid_curve_encodings )
406422 point_str , empty = der .remove_bitstring (point_str_bitstring , 0 )
407423 if empty != b"" :
408424 raise der .UnexpectedDER (
@@ -849,7 +865,7 @@ def from_string(cls, string, curve=NIST192p, hashfunc=sha1):
849865 return cls .from_secret_exponent (secexp , curve , hashfunc )
850866
851867 @classmethod
852- def from_pem (cls , string , hashfunc = sha1 ):
868+ def from_pem (cls , string , hashfunc = sha1 , valid_curve_encodings = None ):
853869 """
854870 Initialise from key stored in :term:`PEM` format.
855871
@@ -869,6 +885,11 @@ def from_pem(cls, string, hashfunc=sha1):
869885
870886 :param string: text with PEM-encoded private ECDSA key
871887 :type string: str
888+ :param valid_curve_encodings: list of allowed encoding formats
889+ for curve parameters. By default (``None``) all are supported:
890+ ``named_curve`` and ``explicit``.
891+ :type valid_curve_encodings: :term:`set-like object`
892+
872893
873894 :raises MalformedPointError: if the length of encoding doesn't match
874895 the provided curve or the encoded values is too large
@@ -889,10 +910,14 @@ def from_pem(cls, string, hashfunc=sha1):
889910 if private_key_index == - 1 :
890911 private_key_index = string .index (b"-----BEGIN PRIVATE KEY-----" )
891912
892- return cls .from_der (der .unpem (string [private_key_index :]), hashfunc )
913+ return cls .from_der (
914+ der .unpem (string [private_key_index :]),
915+ hashfunc ,
916+ valid_curve_encodings ,
917+ )
893918
894919 @classmethod
895- def from_der (cls , string , hashfunc = sha1 ):
920+ def from_der (cls , string , hashfunc = sha1 , valid_curve_encodings = None ):
896921 """
897922 Initialise from key stored in :term:`DER` format.
898923
@@ -913,8 +938,8 @@ def from_der(cls, string, hashfunc=sha1):
913938 `publicKey` field is ignored completely (errors, if any, in it will
914939 be undetected).
915940
916- The only format supported for the `parameters` field is the named
917- curve method. Explicit encoding of curve parameters is not supported .
941+ Two formats are supported for the `parameters` field: the named
942+ curve and the explicit encoding of curve parameters.
918943 In the legacy ssleay format, this implementation requires the optional
919944 `parameters` field to get the curve name. In PKCS #8 format, the curve
920945 is part of the PrivateKeyAlgorithmIdentifier.
@@ -937,6 +962,10 @@ def from_der(cls, string, hashfunc=sha1):
937962
938963 :param string: binary string with DER-encoded private ECDSA key
939964 :type string: bytes like object
965+ :param valid_curve_encodings: list of allowed encoding formats
966+ for curve parameters. By default (``None``) all are supported:
967+ ``named_curve`` and ``explicit``.
968+ :type valid_curve_encodings: :term:`set-like object`
940969
941970 :raises MalformedPointError: if the length of encoding doesn't match
942971 the provided curve or the encoded values is too large
@@ -971,8 +1000,7 @@ def from_der(cls, string, hashfunc=sha1):
9711000
9721001 sequence , s = der .remove_sequence (s )
9731002 algorithm_oid , algorithm_identifier = der .remove_object (sequence )
974- curve_oid , empty = der .remove_object (algorithm_identifier )
975- curve = find_curve (curve_oid )
1003+ curve = Curve .from_der (algorithm_identifier , valid_curve_encodings )
9761004
9771005 if algorithm_oid not in (oid_ecPublicKey , oid_ecDH , oid_ecMQV ):
9781006 raise der .UnexpectedDER (
@@ -1014,13 +1042,7 @@ def from_der(cls, string, hashfunc=sha1):
10141042 raise der .UnexpectedDER (
10151043 "expected tag 0 in DER privkey, got %d" % tag
10161044 )
1017- curve_oid , empty = der .remove_object (curve_oid_str )
1018- if empty != b ("" ):
1019- raise der .UnexpectedDER (
1020- "trailing junk after DER privkey "
1021- "curve_oid: %s" % binascii .hexlify (empty )
1022- )
1023- curve = find_curve (curve_oid )
1045+ curve = Curve .from_der (curve_oid_str , valid_curve_encodings )
10241046
10251047 # we don't actually care about the following fields
10261048 #
0 commit comments