@@ -62,16 +62,17 @@ def parse_message(data):
6262 return message_type , message_payload
6363
6464
65- def load_private_key (key_data ):
65+ def load_private_key (key_data , passphrase = None ):
6666 """
6767 Load the key based on key data. .pem and .der keys can be loaded.
6868 Returns a private key object.
6969 """
70+ passphrase_bytes = passphrase .encode () if passphrase else None
7071 try :
71- key = serialization .load_pem_private_key (key_data , password = None )
72+ key = serialization .load_pem_private_key (key_data , passphrase_bytes )
7273 except ValueError :
7374 try :
74- key = serialization .load_der_private_key (key_data , password = None )
75+ key = serialization .load_der_private_key (key_data , passphrase_bytes )
7576 except ValueError :
7677 logger .warning (f"Could not load key: unknown key file format." )
7778 return key
@@ -93,13 +94,13 @@ def get_private_key_type(key):
9394 logger .warning ("Unknown key type." )
9495
9596
96- def get_signature_algorithm_from_private_key (key_data , default_algorithm = "rsa-sha256" ):
97+ def get_signature_algorithm_from_private_key (key_data , passphrase = None , default_algorithm = "rsa-sha256" ):
9798 """
9899 Derive a signature algorithm based on the private key type. Accepted key types are EC, DSA and RSA keys.
99100 Returns a string that can be used to lookup a signature algorithm by fragment.
100101 By default the lookup will return rsa-sha256, which is the default signature algorithm for XMLSigner objects.
101102 """
102- key = load_private_key (key_data )
103+ key = load_private_key (key_data , passphrase )
103104 key_type = get_private_key_type (key )
104105 if key_type == "rsa" :
105106 return "rsa-sha256"
@@ -122,7 +123,7 @@ def create_message(message_type, cert=None, key=None, passphrase=None, disable_s
122123 SIGNER = XMLSigner (method = methods .detached ,
123124 c14n_algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" )
124125 SIGNER .namespaces ['oadr' ] = "http://openadr.org/oadr-2.0b/2012/07"
125- SIGNER .sign_alg = SignatureMethod .from_fragment (get_signature_algorithm_from_private_key (key ))
126+ SIGNER .sign_alg = SignatureMethod .from_fragment (get_signature_algorithm_from_private_key (key , passphrase ))
126127 signature_tree = SIGNER .sign (tree ,
127128 key = key ,
128129 cert = cert ,
0 commit comments