Como firmar documentos XML usando XMLDSig
almirantuxTutorial26 de Julio de 2022
422 Palabras (2 Páginas)147 Visitas
Cómo firmar documentos XML usando XMLDSig (Firma XML)
Crear documento
<?xml version="1.0" encoding="UTF-8"?>
<document>
<hello>All XML is doomed to fail.</hello>
<!-- Signature contains the signature definition -->
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference>
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue />
</Reference>
</SignedInfo>
<SignatureValue />
<KeyInfo>
<X509Data />
</KeyInfo>
</Signature>
</document>
Firmar documento
xmlsec1 --sign --privkey-pem xxx.com.key,xxx.com.cer --output signed.xml tosign.xml
Verificar documento
xmlsec1 --verify --trusted-pem vrkthsp.pem --trusted-pem vrktestc.pem signed.xml
Tenga en cuenta que un archivo PEM concatenado, es decir, cat vrkthsp.pem vrktestc.pem > concat.pem, no funciona con xmlsec1.
Cómo especificar qué elementos firmar con ds:Reference
Agregue uno o más elementos ds:Reference para especificar qué elementos se deben firmar. Cada elemento debe tener una ID única en el atributo URI. La ID debe tener un prefijo con un hash, por ejemplo, #your-id :
<ds:Reference URI="#secret-xml-sauce">
Asegúrese de que su documento contenga un elemento que tenga la ID exacta sin el prefijo hash:
<Dog ID="secret-xml-sauce" name="Christian" />
Luego, use el interruptor “—id-attr” para especificar el elemento y el nombre del atributo:
xmlsec1 --sign --privkey-pem signing.key,signing.pem --id-attr:ID Dog --id-attr:ID structuredBody --output signed.xml tosign.xml
Tenga en cuenta que "id" es el nombre de atributo predeterminado. Solo necesita el interruptor —id-attr si tiene la ID en un atributo que tiene un nombre diferente.
Cómo firmar varios elementos
Simplemente agregue otro “—id-attr: " cambiar:
xmlsec1 --sign --privkey-pem signing.key,signing.pem --id-attr:ID signatureTimestamp --id-attr:ID structuredBody --output signed.xml tosign.xml
Luego agregue otro elemento que tenga la ID dada.
Solución de problemas
- Este error significa que no tienes el pem de confianza correcto.
certificate issuer check failed:err=2;msg=unable to get issuer certificate;issuer=/C=FI/ST=Finland/O=Vaestorekisterikeskus TEST/OU=Certification Authority Services/OU=Varmennepalvelut/CN=VRK TEST Root CA
Para corregir el error, analice detenidamente el Emisor y el Sujeto de todos los certificados en la cadena de certificados. Por ejemplo:
openssl x509 -inform DER -in vrktestc.crt -text | grep "Issuer\|Subject"
- xmlsec1 no puede encontrar el elemento que contiene ID
func=xmlSecXPathDataExecute:file=xpath.c:line=273:obj=unknown:subj=xmlXPtrEval:error=5:libxml2 library function failed:expr=xpointer(id('ID_OF_ELEMENT_TO_SIGN'))
...