def verify(xml, stream):
<EOL>import xmlsec<EOL>signature_node = xmlsec.tree.find_node(xml, xmlsec.Node.SIGNATURE)<EOL>if signature_node is None:<EOL><INDENT>return False<EOL><DEDENT>ctx = xmlsec.SignatureContext()<EOL>ctx.register_id(xml)<EOL>for assertion in xml.xpath("<STR_LIT>"):<EOL><INDENT>ctx.register_id(assertion)<EOL><DEDENT>key = None<EOL>for fmt in [<EOL>xmlsec.KeyFormat.PEM,<EOL>xmlsec.KeyFormat.CERT_PEM]:<EOL><INDENT>stream.seek(<NUM_LIT:0>)<EOL>try:<EOL><INDENT>key = xmlsec.Key.from_memory(stream, fmt)<EOL>break<EOL><DEDENT>except ValueError:  <EOL><INDENT>pass<EOL><DEDENT><DEDENT>ctx.key = key<EOL>try:<EOL><INDENT>ctx.verify(signature_node)<EOL>return True<EOL><DEDENT>except Exception:<EOL><INDENT>return False<EOL><DEDENT>
Verify the signaure of an XML document with the given certificate.
Returns `True` if the document is signed with a valid signature.
Returns `False` if the document is not signed or if the signature is
invalid.

:param lxml.etree._Element xml: The document to sign
:param file stream: The private key to sign the document with

:rtype: Boolean