如何在Python中使用Xades EPES标准进行签名?

如何在Python中使用Xades EPES标准进行签名?,python,cryptography,lib,Python,Cryptography,Lib,我找到了GitHub。我的计划是将Xades EPES签名应用于XML文件。根据GitHub的工作,它能够执行此过程,但我无法运行此过程 有两种方法。我尝试运行时收到一条错误消息。嗯,问题是我不确定lib是否可以签署Xades-EPES或者如何实现它 先谢谢你 代码: 导入单元测试 从日期时间导入日期时间 从操作系统导入路径 从OpenSSL导入加密 导入xmlsig 从xades导入XAdESContext、模板、UTIL、ObjectIdentifier 从xades.policy导入Ge

我找到了GitHub。我的计划是将Xades EPES签名应用于XML文件。根据GitHub的工作,它能够执行此过程,但我无法运行此过程

有两种方法。我尝试运行时收到一条错误消息。嗯,问题是我不确定lib是否可以签署Xades-EPES或者如何实现它

先谢谢你


代码

导入单元测试
从日期时间导入日期时间
从操作系统导入路径
从OpenSSL导入加密
导入xmlsig
从xades导入XAdESContext、模板、UTIL、ObjectIdentifier
从xades.policy导入GenericPolicyId,暗示策略
从basex导入parse_xml,BASE_DIR
类TestXadeSignature(unittest.TestCase):
def测试与验证(自我):
root=parse_xml('data/sample.xml'))
sign=root.xpath(
“//ds:Signature”,名称空间={'ds':xmlsig.constants.DSigNs}
)[0]
ctx=XAdESContext()
ctx.验证(签名)
def测试标志(自身):
root=parse_xml('data/unsigned sample.xml'))
sign=root.xpath(
“//ds:Signature”,名称空间={'ds':xmlsig.constants.DSigNs}
)[0]
policy=genericpolicid(
'http://www.facturae.es/politica_de_firma_formato_facturae/'
“公司的政治形式和事实”v3\u 1.pdf,
u“事实政治3.1版”,
xmlsig.constants.TransformSha1
)
ctx=XAdESContext(策略)
使用open(path.join(BASE_DIR,“data/keyStore.p12”),“rb”)作为密钥文件:
ctx.load_pkcs12(crypto.load_pkcs12(key_file.read()))
ctx.标志(标志)
ctx.验证(签名)
def测试_创建(自):
root=parse_xml('data/free sample.xml'))
签名=xmlsig.template.create(
xmlsig.constants.TransformInclC14N,
xmlsig.constants.TransformRsaSha1,
“签名”
)
signature\u id=utils.get\u unique\u id()
ref=xmlsig.template.add\u参考(
签名,xmlsig.constants.TransformSha1,uri=“”,name=“REF”
)
xmlsig.template.add_转换(ref,xmlsig.constants.TransformEnveloped)
xmlsig.template.add_引用(
签名,xmlsig.constants.TransformSha1,uri=“#KI”
)
xmlsig.template.add_引用(
签名,xmlsig.constants.TransformSha1,uri=“#”+签名\u id
)
ki=xmlsig.template.确保密钥信息(签名,name='ki')
data=xmlsig.template.add_x509_数据(ki)
xmlsig.template.x509\u数据\u添加\u证书(数据)
serial=xmlsig.template.x509\u数据\u添加\u颁发者\u序列(数据)
xmlsig.template.x509\u发行人\u序列号\u添加\u发行人\u名称(序列号)
xmlsig.template.x509发行人序列号添加序列号(序列号)
xmlsig.template.add_key_值(ki)
限定=模板。创建\u限定\u属性(
签名,name=utils.get\u unique\u id()
)
props=模板。创建\u签名\u属性(
限定,名称=签名\u id
)
模板。添加角色(道具,“支持2”)
模板。添加制作地点(道具,城市=‘马德里’)
template.add\u生产地点(
道具,州='BCN',邮政编码='08000',国家='ES')
模板。添加角色(道具,“支持”)
policy=genericpolicid(
'http://www.facturae.es/politica_de_firma_formato_facturae/'
“公司的政治形式和事实”v3\u 1.pdf,
u“事实政治3.1版”,
xmlsig.constants.TransformSha1
)
root.append(签名)
ctx=XAdESContext(策略)
使用open(path.join(BASE_DIR,“data/keyStore.p12”),“rb”)作为密钥文件:
ctx.load_pkcs12(crypto.load_pkcs12(key_file.read()))
ctx.签名(签名)
ctx.验证(签名)
def测试_创建_2(自身):
root=parse_xml('data/free sample.xml'))
签名=xmlsig.template.create(
xmlsig.constants.TransformInclC14N,
xmlsig.constants.TransformRsaSha1,
“签名”
)
ref=xmlsig.template.add\u参考(
签名,xmlsig.constants.TransformSha1,uri=“”,name=“R1”
)
xmlsig.template.add_转换(ref,xmlsig.constants.TransformEnveloped)
xmlsig.template.add_引用(
签名,xmlsig.constants.TransformSha1,uri=“#KI”,name=“RKI”
)
ki=xmlsig.template.确保密钥信息(签名,name='ki')
data=xmlsig.template.add_x509_数据(ki)
xmlsig.template.x509\u数据\u添加\u证书(数据)
serial=xmlsig.template.x509\u数据\u添加\u颁发者\u序列(数据)
xmlsig.template.x509\u发行人\u序列号\u添加\u发行人\u名称(序列号)
xmlsig.template.x509发行人序列号添加序列号(序列号)
xmlsig.template.add_key_值(ki)
限定=模板。创建限定属性(签名)
utils.确保id(合格)
utils.确保id(合格)
props=模板。创建\u签名\u属性(
限定,datetime=datetime.now()
)
模板。添加角色(道具,“支持”)
签名的\u do=模板。确保\u签名的\u数据\u对象\u属性(道具)
template.add\数据\对象\格式(
签名(u do),,
“#R1”,
标识符=对象标识符(“Idenfitier0”、“描述”)
)
模板.添加\承诺\类型\指示(
签名(u do),,
ObjectIdentifier(“Idenfitier0”、“描述”),
限定符类型=[“Tipo”]
)
模板.添加\承诺\类型\指示(
签名(u do),,
ObjectIdentifier(“Idenfitier1”,references=[“#R1”]),
参考文献=[“#R1”]
)
template.add\数据\对象\格式(
签名(u do),,
“#RKI”,
description=“Desc”,
mime_type=“应用程序/xml”,
编码='UTF-8'
)
root.ap
import xmlsig
from lxml import etree
from OpenSSL import crypto
from xades import XAdESContext, template, utils
from xades.policy import GenericPolicyId
POLICY_ENDPOINT = "politicadefirma/v2/politicadefirmav2.pdf"
SIGN_POLICY = f"http://facturaelectronica.dian.gov.co/{POLICY_ENDPOINT}"
CERTICAMARA_PFX = os.environ.get(
    'CERTICAMARA_PFX',
    '/path/to/certificate.pfx')
signature = xmlsig.template.create(
    xmlsig.constants.TransformInclC14N,
    xmlsig.constants.TransformRsaSha256,
    "Signature",
)

    signature_id = utils.get_unique_id()
    ref = xmlsig.template.add_reference(
        signature, xmlsig.constants.TransformSha256, uri="", name="REF"
    )
    xmlsig.template.add_transform(ref, xmlsig.constants.TransformEnveloped)
   xmlsig.template.add_reference(
        signature, xmlsig.constants.TransformSha256, uri="#" + signature_id
    )

   xmlsig.template.add_reference(
        signature, xmlsig.constants.TransformSha256, uri="#" + signature_id
    )
    ki = xmlsig.template.ensure_key_info(signature, name="KI")
    data = xmlsig.template.add_x509_data(ki)
    xmlsig.template.x509_data_add_certificate(data)
    serial = xmlsig.template.x509_data_add_issuer_serial(data)
    xmlsig.template.x509_issuer_serial_add_issuer_name(serial)
    xmlsig.template.x509_issuer_serial_add_serial_number(serial)
    xmlsig.template.add_key_value(ki)
    qualifying = template.create_qualifying_properties(
        signature, name=utils.get_unique_id(), etsi='xades'
    )
    props = template.create_signed_properties(qualifying, name=signature_id)
    # Additional data for signature
    template.add_claimed_role(props, "Supp2")
    template.add_production_place(props, city="Bogotá Colombia")
    template.add_production_place(
        props, state="BCN", postal_code="08000", country="CO"
    )
    template.add_claimed_role(props, "SNE")
    policy = GenericPolicyId(
        SIGN_POLICY,
        u"Política de firma para facturas"
        " electrónicas de la República de Colombia",
        xmlsig.constants.TransformSha256,
    )
   parsed_file.append(signature)
   with open(CERTICAMARA_ANDES_PFX, "rb") as key_file:
        certificate = crypto.load_pkcs12(key_file.read(), 'filepassword')
    ctx = XAdESContext(
        policy,
        certificate.get_certificate().to_cryptography(),
    )
   ctx.load_pkcs12(certificate)
   ctx.sign(signature)
    parsed_file[0][position][0].append(signature) 
    et = etree.ElementTree(parsed_file)
    
    nfs_name = 'Name of the signed file'
    et.write(nfs_name, pretty_print=True,
             encoding='utf-8', xml_declaration=True)