robrichards / xmlseclibs

A PHP library for XML Security
BSD 3-Clause "New" or "Revised" License
393 stars 182 forks source link

Problem with namespaces in canonicalization #210

Open wilfredcom opened 4 years ago

wilfredcom commented 4 years ago

I'm trying to sign an XML withe de first lines like this

`<?xml version="1.0" encoding="UTF-8" standalone="no"?>

` When try to sign this I have the following warnings when the sytem try to use `DOMNode::C14N` to canonicalize and the result is wrong: ` DOMNode::C14N(): Relative namespace UR is invalid here : dian DOMNode::C14N(): Internal error : checking for relative namespaces DOMNode::C14N(): Internal error : processing docs children list ` How can I fix them?
robrichards commented 4 years ago

I don't see any of your document so no idea why the errors.

On 6/11/20 9:31 PM, Wilfredo Pachón López wrote:

I'm trying to sign an XML withe de first lines like this

When try to sign this I have the following warnings and the result is wrong: DOMNode::C14N(): Relative namespace UR is invalid here : dian DOMNode::C14N(): Internal error : checking for relative namespaces DOMNode::C14N(): Internal error : processing docs children list

How can I fix them?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/robrichards/xmlseclibs/issues/210, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABTNRISQ65WR5ATKCUDBQLRWGAP3ANCNFSM4N3426QQ.

wilfredcom commented 4 years ago

Thanks for the response. The problem is issued when try to canonicalize de XML before the sign:

$objDSig = new XMLSecurityDSig(); $objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);

And the warning and failure is reported in line 296 of the script robrichards/xmlseclibs/src/XMLSecurityDSig.php:

return $node->C14N($exclusive, $withComments, $arXPath, $prefixList);

All seems to be good but always throw the error: DOMNode::C14N(): Relative namespace UR is invalid here : dian DOMNode::C14N() Internal error : checking for relative namespaces DOMNode::C14N() Internal error : processing docs children list, except if the line xmlns:sts="dian:gov:co:facturaelectronica:Structures-2-1" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#"
Is removed, but i cannot do this because this NS is essential part of my XML

robrichards commented 4 years ago

I would need to see the full document and code being used to know what you are trying to sign and what's being passed in.

wilfredcom commented 4 years ago

Thanks, the XML document to be signed looks like: `

CO 800197268 UBL 2.1 05 DIAN 2.1 2 COP 1 CO Colombia CO Colombia CO Colombia CO Colombia 0.00 0.00 0.00 0.00 0.00 0.00 ` And the code used to sign: ` use RobRichards\XMLSecLibs\XMLSecurityDSig; use RobRichards\XMLSecLibs\XMLSecurityKey; $doc = new DOMDocument(); $doc->load($xml_without_sign); // Create a new Security object $objDSig = new XMLSecurityDSig(); // Use the c14n exclusive canonicalization $objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N); // Sign using SHA-256 $objDSig->addReference( $doc, XMLSecurityDSig::SHA256, array('http://www.w3.org/2000/09/xmldsig#enveloped-signature') ); // Create a new (private) Security key $objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'private')); // Load the private key $objKey->loadKey($KEY, TRUE); // Sign the XML file $objDSig->sign($objKey); // Add the associated public key to the signature $objDSig->add509Cert(file_get_contents($CERTIFICATE)); // Append the signature to the XML $objDSig->appendSignature($doc->documentElement); // Save the signed XML $doc->save($xml_signed); ` And the warning is: ` DOMNode::C14N(): Relative namespace UR is invalid here : dian DOMNode::C14N() Internal error : checking for relative namespaces DOMNode::C14N() Internal error : processing docs children list ` Causing the xml output without sign.
yeisonlo commented 3 years ago

Alguna solución a este problema? Tengo el mismo problema

yeisonlo commented 3 years ago

I'm trying to sign an XML withe de first lines like this

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sts="dian:gov:co:facturaelectronica:Structures-2-1" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd">

When try to sign this I have the following warnings when the sytem try to use DOMNode::C14N to canonicalize and the result is wrong: DOMNode::C14N(): Relative namespace UR is invalid here : dian DOMNode::C14N(): Internal error : checking for relative namespaces DOMNode::C14N(): Internal error : processing docs children list

How can I fix them?

se pudo solucionar este problema?

moltam commented 3 years ago

I am no expert in this topic but did a little research as I recently run into this issue myself. Here are my foundings:

bseddon commented 3 years ago

@moltam Thanks for posting your analysis which solved the issue for me. I'm using canonicalization in PHP and, as a third party test, using the canonicalization class built into the Microsoft .NET Framework cryptography package. PHP (libxml) does report errors with relative namespaces but the .NET Framework implementation does not which caught me out.

andresflorez12 commented 2 years ago

Or

andresflorez12 commented 2 years ago

I am sorry. xmlns:sts="https://www.dian.gov.co/contratos/facturaelectronica/v1/Structures" OR xmlns:sts="urn:dian:gov:co:facturaelectronica:Structures-2-1"

Jhocasber commented 2 years ago

I have found a solution to this issue, this is in https://es.stackoverflow.com/a/532015/283864

Regards!