Код: Выделить всё
Exception in thread "main" org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID IHEManifest
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID IHEManifest
Original Exception was org.apache.xml.security.signature.ReferenceNotInitializedException: Cannot resolve element with ID IHEManifest
Original Exception was org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID IHEManifest
< /code>
Что я уже проверяю: < /p>
manifestelem.setidattribute("ID ", true); < /li>
Элемент #IHemanifest находится в DOM перед подписью < /li>
< /ol>
wihemanifest.
org.apache.santuario
xmlsec
1.5.3
< /code>
метод сборки XML и знака: < /p>
public static Document sign(byte[] bytes, String signatureId, String documentId, String keystorePath, String keystorePassword) throws Exception {
// Compute SHA1 digest of the document
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = sha1.digest(bytes);
StringBuilder hashHex = new StringBuilder();
for (byte b : hashBytes) hashHex.append(String.format("%02x", b));
String base64Hash = Base64.getEncoder().encodeToString(hashHex.toString().getBytes("UTF-8"));
// Load KeyStore and get PrivateKey and Certificate
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new java.io.FileInputStream(keystorePath), keystorePassword.toCharArray());
String alias = ks.aliases().nextElement();
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, keystorePassword.toCharArray());
X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
// Create empty XML document
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
// Create Signature element
XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
sig.getElement().setAttribute("Id", signatureId);
// Manifest element (inside ds:Object)
Element manifestElem = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:Manifest");
manifestElem.setAttribute("Id", "IHEManifest");
manifestElem.setIdAttribute("Id", true);
// Reference inside Manifest
Element manifestRef = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:Reference");
manifestRef.setAttribute("URI", documentId);
Element digestMethod = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:DigestMethod");
digestMethod.setAttribute("Algorithm", org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
Element digestValue = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:DigestValue");
digestValue.setTextContent(base64Hash);
manifestRef.appendChild(digestMethod);
manifestRef.appendChild(digestValue);
manifestElem.appendChild(manifestRef);
// KeyInfo: X509Data and KeyValue
KeyInfo ki = sig.getKeyInfo();
X509Data x509Data = new X509Data(doc);
x509Data.addCertificate(cert);
ki.add(x509Data);
KeyValue keyValue = new KeyValue(doc, cert.getPublicKey());
ki.add(keyValue);
// ds:Object with XAdES properties
Element objectElem = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:Object");
objectElem.setAttribute("Id", "object");
// XAdES QualifyingProperties
Element qualifyingProperties = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "QualifyingProperties");
qualifyingProperties.setAttribute("Target", "#" + signatureId);
// SignedProperties
Element signedProperties = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SignedProperties");
signedProperties.setAttribute("Id", "SignedPropertiesId");
// SignedSignatureProperties
Element signedSignatureProperties = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SignedSignatureProperties");
// SigningTime
Element signingTime = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SigningTime");
signingTime.setTextContent(java.time.Instant.now().toString());
signedSignatureProperties.appendChild(signingTime);
// SigningCertificate
Element signingCertificate = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SigningCertificate");
Element certElem = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "Cert");
// CertDigest
Element certDigest = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "CertDigest");
Element digestMethodCert = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:DigestMethod");
digestMethodCert.setAttribute("Algorithm", org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
Element digestValueCert = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:DigestValue");
digestValueCert.setTextContent(Base64.getEncoder().encodeToString(cert.getEncoded()));
certDigest.appendChild(digestMethodCert);
certDigest.appendChild(digestValueCert);
// IssuerSerial
Element issuerSerial = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "IssuerSerial");
Element x509IssuerName = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:X509IssuerName");
x509IssuerName.setTextContent(cert.getIssuerX500Principal().getName());
Element x509SerialNumber = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:X509SerialNumber");
x509SerialNumber.setTextContent(new BigInteger(cert.getSerialNumber().toString()).toString());
issuerSerial.appendChild(x509IssuerName);
issuerSerial.appendChild(x509SerialNumber);
certElem.appendChild(certDigest);
certElem.appendChild(issuerSerial);
signingCertificate.appendChild(certElem);
signedSignatureProperties.appendChild(signingCertificate);
// SignaturePolicyIdentifier
Element signaturePolicyIdentifier = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SignaturePolicyIdentifier");
Element signaturePolicyId = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SignaturePolicyId");
Element sigPolicyId = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SigPolicyId");
Element identifier = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "Identifier");
identifier.setTextContent(documentId);
sigPolicyId.appendChild(identifier);
signaturePolicyId.appendChild(sigPolicyId);
Element sigPolicyHash = doc.createElementNS("http://uri.etsi.org/01903/v1.1.1#", "SigPolicyHash");
Element digestMethodPolicy = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:DigestMethod");
digestMethodPolicy.setAttribute("Algorithm", org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
Element digestValuePolicy = doc.createElementNS(org.apache.xml.security.utils.Constants.SignatureSpecNS, "ds:DigestValue");
byte[] policyHash = new byte[] {/*...*/};
digestValuePolicy.setTextContent(Base64.getEncoder().encodeToString(policyHash));
sigPolicyHash.appendChild(digestMethodPolicy);
sigPolicyHash.appendChild(digestValuePolicy);
signaturePolicyId.appendChild(sigPolicyHash);
signaturePolicyIdentifier.appendChild(signaturePolicyId);
signedSignatureProperties.appendChild(signaturePolicyIdentifier);
signedProperties.appendChild(signedSignatureProperties);
qualifyingProperties.appendChild(signedProperties);
objectElem.appendChild(qualifyingProperties);
Element signatureProperties = doc.createElement("ds:SignatureProperties");
Element signatureProperty = doc.createElement("ds:SignatureProperty");
signatureProperty.setAttribute("Id", "purposeOfSignature");
signatureProperty.setAttribute("Target", "oid:" + documentId);
signatureProperty.setTextContent("Source");
signatureProperties.appendChild(signatureProperty);
objectElem.appendChild(signatureProperties);
objectElem.appendChild(manifestElem);
// Add XAdES Object to Signature
sig.getElement().appendChild(objectElem);
// Reference to Manifest in SignedInfo
sig.addDocument("#IHEManifest", null, org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1, null, "http://www.w3.org/2000/09/xmldsig#Manifest");
// Sign
sig.sign(privateKey);
// Add XML declaration
doc.setXmlStandalone(false);
// Add Signature to document root
doc.appendChild(sig.getElement());
// Register the Id attribute on the document root (Signature element)
doc.getDocumentElement().setIdAttribute("Id", true);
return doc;
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... itializede