Reputation: 75
I have a .pem file which contains my certificate and xml file which also has details regarding the signature. Following is the piece of code I am using to verify
# Parse the XML document
template = etree.parse("test.xml").getroot()
print("Template", template.tag)
# Add ID attributes
xmlsec.tree.add_ids(template, ["ID"])
# Print the loaded XML template
print(etree.tostring(template, pretty_print=True).decode())
# Find the signature node
signature_node = xmlsec.tree.find_node(template, xmlsec.constants.NodeSignature)
# Print the signature node
if signature_node is not None:
print(etree.tostring(signature_node, pretty_print=True).decode())
else:
print("Signature node not found")
exit(1)
# Create a digital signature context (no key manager is needed).
ctx = xmlsec.SignatureContext()
print("Digital Signature", ctx)
# Load the key from file
try:
key = xmlsec.Key.from_file(
"test.pem",
xmlsec.constants.KeyDataFormatPem,
)
print("Key loaded successfully.")
except Exception as e:
print(f"Error loading key: {e}")
exit(1)
# Set the key on the context.
ctx.key = key
# Verify the signature
try:
ctx.verify(signature_node)
print("Validated")
except xmlsec.VerificationError as e:
print(f"Not Verified: {e}")
except Exception as e:
print(f"Error: {e}")
Error I am getting is: Error loading key: (1, 'cannot read key')
It is in line
key = xmlsec.Key.from_file(
"test.pem",
xmlsec.constants.KeyDataFormatPem,
)
If someone has already resolved it, please guide.
Upvotes: 0
Views: 17