作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经签署了与工作相关的 xml 快一年了,今天我注意到我做错了(请原谅我的英语不好,这不是我的主要语言)
我将 xmlseclibs 库用于 php,我希望签名是这样的:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod
Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#XXXXX">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>... </DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>... </SignatureValue>
<KeyInfo>
<KeyValue>
</KeyValue>
<X509Data>
<X509Certificate>... </X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
<node1>
<node2>
somedata
</node2>
</node1>
<node1>
<node2>
somedata
</node2>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod
Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#XXXXX">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>... </DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>... </SignatureValue>
<KeyInfo>
<KeyValue>
</KeyValue>
<X509Data>
<X509Certificate>... </X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</node1>
<node1>
<node2>
somedata
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod
Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#XXXXX">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>... </DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>... </SignatureValue>
<KeyInfo>
<KeyValue>
</KeyValue>
<X509Data>
<X509Certificate>... </X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</node2>
</node1>
function firmarEnvio2($xmlr){
$doc = new DOMDocument();
$xml = file_get_contents($xmlr);
$doc->loadXML($xml);
$doc->preserveWhiteSpace = true;
$doc->encoding = 'ISO-8859-1';
$objDSig = new XMLSecurityDSig(FALSE);
$objDSig->setCanonicalMethod(XMLSecurityDSig::C14N);
echo "<pre>";
$options['prefix'] = '';
$options['prefix_ns'] = '';
$options['force_uri'] = TRUE;
$options['id_name'] = 'ID';
$objDSig->addReference($doc, XMLSecurityDSig::SHA1, array(XMLSecurityDSig::TR_ENV_SIG), $options);
/*
$objDSig->addReference($doc->documentElement, XMLSecurityDSig::SHA1,
array(
'http://www.w3.org/2000/09/xmldsig#enveloped-signature',
'http://www.w3.org/2001/10/xml-exc-c14n#'
),
array('id_name' => 'Id', 'overwrite' => false));
*/
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));
$pass = "some string";
$pfx = file_get_contents("someroute/certificate.pfx");
openssl_pkcs12_read($pfx, $key, $pass);
$objKey->loadKey($key["pkey"]);
$objDSig->add509Cert($key["cert"]);
$tag="x";
//the following is to compare the digest that i want with the one i always got
echo "<br><br>Digest1<br>";
print_r (base64_encode(sha1($doc->documentElement->C14N(), true)));
echo "<br><br>Digest2<br>";
print_r (base64_encode(sha1($doc->documentElement->getElementsByTagName($tag)->item(0)->C14N(), true)));
print_r ($firma);
$objDSig->sign($objKey, $doc->documentElement);
//print_r($doc);
$doc->save('test_s.xml');
return true;
}
最佳答案
我最近在 xmlseclibs 库中遇到了同样的问题。
以下是对我有用的内容:
function signXML($xml) {
$doc = new \DOMDocument('1.0','UTF-8');
$doc->loadXML($xml);
$objDSig = new XMLSecurityDSig('');
$objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
$node = $objDSig->addObject($doc->documentElement);
$objDSig->addReference(
$node,
XMLSecurityDSig::SHA1
);
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type'=>'private'));
$privkey = $this->serverroot.'/certs/dev.key';
$objKey->loadKey($privkey, TRUE);
$objDSig->sign($objKey);
$pubkey = $this->serverroot.'/certs/dev-pub.cer';
$objDSig->add509Cert(file_get_contents($pubkey));
$node->ownerDocument->encoding = "UTF-8";
$node->ownerDocument->save(dirname(__FILE__).'/test.xml');
return $node->ownerDocument->saveXML();
}
关于php - xmlseclibs 无法签署我想要的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35565255/
我已经签署了与工作相关的 xml 快一年了,今天我注意到我做错了(请原谅我的英语不好,这不是我的主要语言) 我将 xmlseclibs 库用于 php,我希望签名是这样的: ...
我已经签署了 XML,但我不知道如何在签名中包含 KeyValue 元素。拥有一些文档会节省很多时间。 下面的代码(如果您有兴趣的话)是我到目前为止使用 xmlseclibs 所做的: Rando
我是一名优秀的程序员,十分优秀!