in src/key_server_common.py [0:0]
def insert_encrypted_value(self, element, encryption_algorithm, encrypted_string):
"""
Add an encrypted value (key) to the document.
"""
encrypted_value = element_tree.SubElement(element, "{urn:ietf:params:xml:ns:keyprov:pskc}EncryptedValue")
encryption_method = element_tree.SubElement(encrypted_value, "{http://www.w3.org/2001/04/xmlenc#}EncryptionMethod")
encryption_method.set("Algorithm", encryption_algorithm)
cipher_data = element_tree.SubElement(encrypted_value, "{http://www.w3.org/2001/04/xmlenc#}CipherData")
cipher_value = element_tree.SubElement(cipher_data, "{http://www.w3.org/2001/04/xmlenc#}CipherValue")
cipher_value.text = encrypted_string
# calculate and set MAC using HMAC-SHA512 over data in CipherValue
if not self.hmac_key:
raise Exception("Missing HMAC key")
value_mac = element_tree.SubElement(element, "{urn:ietf:params:xml:ns:keyprov:pskc}ValueMAC")
hmac_instance = hmac.HMAC(self.hmac_key, hashes.SHA512(), backend=default_backend())
hmac_instance.update(base64.b64decode(encrypted_string))
value_mac.text = base64.b64encode(hmac_instance.finalize()).decode('utf-8')