def sign_x509(xml, key, certificate, algorithm=None, digest_algorithm=None,
    canonicalization_method=None):
    """Sign PSKC data using X.509 certificate and private key.

    xml: an XML document
    key: the private key in binary format
    certificate: the X.509 certificate
    """
    import signxml
    algorithm = algorithm or 'rsa-sha256'
    digest_algorithm = digest_algorithm or 'sha256'
    canonicalization_method = (canonicalization_method or signxml.
        XMLSignatureProcessor.default_c14n_algorithm)
    return signxml.XMLSigner(method=signxml.methods.enveloped,
        signature_algorithm=algorithm.rsplit('#', 1)[-1].lower(),
        digest_algorithm=digest_algorithm.rsplit('#', 1)[-1].lower(),
        c14n_algorithm=canonicalization_method).sign(xml, key=key, cert=
        certificate)
