mirror of https://github.com/apache/poi.git
only refactorings to break up the long method
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849130 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
17a7cb609b
commit
9f3c99c25c
|
@ -24,10 +24,12 @@
|
||||||
|
|
||||||
package org.apache.poi.poifs.crypt.dsig.facets;
|
package org.apache.poi.poifs.crypt.dsig.facets;
|
||||||
|
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.cert.CertificateEncodingException;
|
import java.security.cert.CertificateEncodingException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -58,21 +60,18 @@ import org.etsi.uri.x01903.v13.CertIDType;
|
||||||
import org.etsi.uri.x01903.v13.ClaimedRolesListType;
|
import org.etsi.uri.x01903.v13.ClaimedRolesListType;
|
||||||
import org.etsi.uri.x01903.v13.DataObjectFormatType;
|
import org.etsi.uri.x01903.v13.DataObjectFormatType;
|
||||||
import org.etsi.uri.x01903.v13.DigestAlgAndValueType;
|
import org.etsi.uri.x01903.v13.DigestAlgAndValueType;
|
||||||
import org.etsi.uri.x01903.v13.IdentifierType;
|
|
||||||
import org.etsi.uri.x01903.v13.ObjectIdentifierType;
|
import org.etsi.uri.x01903.v13.ObjectIdentifierType;
|
||||||
import org.etsi.uri.x01903.v13.QualifyingPropertiesDocument;
|
import org.etsi.uri.x01903.v13.QualifyingPropertiesDocument;
|
||||||
import org.etsi.uri.x01903.v13.QualifyingPropertiesType;
|
import org.etsi.uri.x01903.v13.QualifyingPropertiesType;
|
||||||
import org.etsi.uri.x01903.v13.SigPolicyQualifiersListType;
|
|
||||||
import org.etsi.uri.x01903.v13.SignaturePolicyIdType;
|
import org.etsi.uri.x01903.v13.SignaturePolicyIdType;
|
||||||
import org.etsi.uri.x01903.v13.SignaturePolicyIdentifierType;
|
import org.etsi.uri.x01903.v13.SignaturePolicyIdentifierType;
|
||||||
import org.etsi.uri.x01903.v13.SignedDataObjectPropertiesType;
|
|
||||||
import org.etsi.uri.x01903.v13.SignedPropertiesType;
|
import org.etsi.uri.x01903.v13.SignedPropertiesType;
|
||||||
import org.etsi.uri.x01903.v13.SignedSignaturePropertiesType;
|
import org.etsi.uri.x01903.v13.SignedSignaturePropertiesType;
|
||||||
import org.etsi.uri.x01903.v13.SignerRoleType;
|
import org.etsi.uri.x01903.v13.SignerRoleType;
|
||||||
import org.w3.x2000.x09.xmldsig.DigestMethodType;
|
import org.w3.x2000.x09.xmldsig.DigestMethodType;
|
||||||
import org.w3.x2000.x09.xmldsig.X509IssuerSerialType;
|
import org.w3.x2000.x09.xmldsig.X509IssuerSerialType;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XAdES Signature Facet. Implements XAdES v1.4.1 which is compatible with XAdES
|
* XAdES Signature Facet. Implements XAdES v1.4.1 which is compatible with XAdES
|
||||||
|
@ -92,7 +91,7 @@ public class XAdESSignatureFacet extends SignatureFacet {
|
||||||
|
|
||||||
private static final String XADES_TYPE = "http://uri.etsi.org/01903#SignedProperties";
|
private static final String XADES_TYPE = "http://uri.etsi.org/01903#SignedProperties";
|
||||||
|
|
||||||
private Map<String, String> dataObjectFormatMimeTypes = new HashMap<>();
|
private final Map<String, String> dataObjectFormatMimeTypes = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -116,24 +115,50 @@ public class XAdESSignatureFacet extends SignatureFacet {
|
||||||
SignedSignaturePropertiesType signedSignatureProperties = signedProperties.addNewSignedSignatureProperties();
|
SignedSignaturePropertiesType signedSignatureProperties = signedProperties.addNewSignedSignatureProperties();
|
||||||
|
|
||||||
// SigningTime
|
// SigningTime
|
||||||
|
addSigningTime(signedSignatureProperties);
|
||||||
|
|
||||||
|
// SigningCertificate
|
||||||
|
addCertificate(signedSignatureProperties);
|
||||||
|
|
||||||
|
// ClaimedRole
|
||||||
|
addXadesRole(signedSignatureProperties);
|
||||||
|
|
||||||
|
// XAdES-EPES
|
||||||
|
addPolicy(signedSignatureProperties);
|
||||||
|
|
||||||
|
// DataObjectFormat
|
||||||
|
addMimeTypes(signedProperties);
|
||||||
|
|
||||||
|
// add XAdES ds:Object
|
||||||
|
objects.add(addXadesObject(document, qualifyingProperties));
|
||||||
|
|
||||||
|
// add XAdES ds:Reference
|
||||||
|
references.add(addXadesReference());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addSigningTime(SignedSignaturePropertiesType signedSignatureProperties) {
|
||||||
Calendar xmlGregorianCalendar = Calendar.getInstance(TimeZone.getTimeZone("Z"), Locale.ROOT);
|
Calendar xmlGregorianCalendar = Calendar.getInstance(TimeZone.getTimeZone("Z"), Locale.ROOT);
|
||||||
xmlGregorianCalendar.setTime(signatureConfig.getExecutionTime());
|
xmlGregorianCalendar.setTime(signatureConfig.getExecutionTime());
|
||||||
xmlGregorianCalendar.clear(Calendar.MILLISECOND);
|
xmlGregorianCalendar.clear(Calendar.MILLISECOND);
|
||||||
signedSignatureProperties.setSigningTime(xmlGregorianCalendar);
|
signedSignatureProperties.setSigningTime(xmlGregorianCalendar);
|
||||||
|
}
|
||||||
|
|
||||||
// SigningCertificate
|
private void addCertificate(SignedSignaturePropertiesType signedSignatureProperties) {
|
||||||
if (signatureConfig.getSigningCertificateChain() == null
|
List<X509Certificate> chain = signatureConfig.getSigningCertificateChain();
|
||||||
|| signatureConfig.getSigningCertificateChain().isEmpty()) {
|
if (chain == null || chain.isEmpty()) {
|
||||||
throw new RuntimeException("no signing certificate chain available");
|
throw new RuntimeException("no signing certificate chain available");
|
||||||
}
|
}
|
||||||
CertIDListType signingCertificates = signedSignatureProperties.addNewSigningCertificate();
|
CertIDListType signingCertificates = signedSignatureProperties.addNewSigningCertificate();
|
||||||
CertIDType certId = signingCertificates.addNewCert();
|
CertIDType certId = signingCertificates.addNewCert();
|
||||||
X509Certificate certificate = signatureConfig.getSigningCertificateChain().get(0);
|
setCertID(certId, signatureConfig, signatureConfig.isXadesIssuerNameNoReverseOrder(), chain.get(0));
|
||||||
setCertID(certId, signatureConfig, signatureConfig.isXadesIssuerNameNoReverseOrder(), certificate);
|
}
|
||||||
|
|
||||||
// ClaimedRole
|
private void addXadesRole(SignedSignaturePropertiesType signedSignatureProperties) {
|
||||||
String role = signatureConfig.getXadesRole();
|
String role = signatureConfig.getXadesRole();
|
||||||
if (role != null && !role.isEmpty()) {
|
if (role == null || role.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
SignerRoleType signerRole = signedSignatureProperties.addNewSignerRole();
|
SignerRoleType signerRole = signedSignatureProperties.addNewSignerRole();
|
||||||
signedSignatureProperties.setSignerRole(signerRole);
|
signedSignatureProperties.setSignerRole(signerRole);
|
||||||
ClaimedRolesListType claimedRolesList = signerRole.addNewClaimedRoles();
|
ClaimedRolesListType claimedRolesList = signerRole.addNewClaimedRoles();
|
||||||
|
@ -143,69 +168,69 @@ public class XAdESSignatureFacet extends SignatureFacet {
|
||||||
insertXChild(claimedRole, roleString);
|
insertXChild(claimedRole, roleString);
|
||||||
}
|
}
|
||||||
|
|
||||||
// XAdES-EPES
|
private void addPolicy(SignedSignaturePropertiesType signedSignatureProperties) {
|
||||||
SignaturePolicyService policyService = signatureConfig.getSignaturePolicyService();
|
SignaturePolicyService policyService = signatureConfig.getSignaturePolicyService();
|
||||||
if (policyService != null) {
|
if (policyService == null) {
|
||||||
SignaturePolicyIdentifierType signaturePolicyIdentifier =
|
if (signatureConfig.isXadesSignaturePolicyImplied()) {
|
||||||
|
signedSignatureProperties.
|
||||||
|
addNewSignaturePolicyIdentifier().
|
||||||
|
addNewSignaturePolicyImplied();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SignaturePolicyIdentifierType policyId =
|
||||||
signedSignatureProperties.addNewSignaturePolicyIdentifier();
|
signedSignatureProperties.addNewSignaturePolicyIdentifier();
|
||||||
|
|
||||||
SignaturePolicyIdType signaturePolicyId = signaturePolicyIdentifier.addNewSignaturePolicyId();
|
SignaturePolicyIdType signaturePolicyId = policyId.addNewSignaturePolicyId();
|
||||||
|
|
||||||
ObjectIdentifierType objectIdentifier = signaturePolicyId.addNewSigPolicyId();
|
ObjectIdentifierType oit = signaturePolicyId.addNewSigPolicyId();
|
||||||
objectIdentifier.setDescription(policyService.getSignaturePolicyDescription());
|
oit.setDescription(policyService.getSignaturePolicyDescription());
|
||||||
|
oit.addNewIdentifier().setStringValue(policyService.getSignaturePolicyIdentifier());
|
||||||
IdentifierType identifier = objectIdentifier.addNewIdentifier();
|
|
||||||
identifier.setStringValue(policyService.getSignaturePolicyIdentifier());
|
|
||||||
|
|
||||||
byte[] signaturePolicyDocumentData = policyService.getSignaturePolicyDocument();
|
byte[] signaturePolicyDocumentData = policyService.getSignaturePolicyDocument();
|
||||||
DigestAlgAndValueType sigPolicyHash = signaturePolicyId.addNewSigPolicyHash();
|
DigestAlgAndValueType sigPolicyHash = signaturePolicyId.addNewSigPolicyHash();
|
||||||
setDigestAlgAndValue(sigPolicyHash, signaturePolicyDocumentData, signatureConfig.getDigestAlgo());
|
setDigestAlgAndValue(sigPolicyHash, signaturePolicyDocumentData, signatureConfig.getDigestAlgo());
|
||||||
|
|
||||||
String signaturePolicyDownloadUrl = policyService.getSignaturePolicyDownloadUrl();
|
String signaturePolicyDownloadUrl = policyService.getSignaturePolicyDownloadUrl();
|
||||||
if (null != signaturePolicyDownloadUrl) {
|
if (signaturePolicyDownloadUrl == null) {
|
||||||
SigPolicyQualifiersListType sigPolicyQualifiers = signaturePolicyId.addNewSigPolicyQualifiers();
|
return;
|
||||||
AnyType sigPolicyQualifier = sigPolicyQualifiers.addNewSigPolicyQualifier();
|
}
|
||||||
|
AnyType sigPolicyQualifier =
|
||||||
|
signaturePolicyId.addNewSigPolicyQualifiers().addNewSigPolicyQualifier();
|
||||||
XmlString spUriElement = XmlString.Factory.newInstance();
|
XmlString spUriElement = XmlString.Factory.newInstance();
|
||||||
spUriElement.setStringValue(signaturePolicyDownloadUrl);
|
spUriElement.setStringValue(signaturePolicyDownloadUrl);
|
||||||
insertXChild(sigPolicyQualifier, spUriElement);
|
insertXChild(sigPolicyQualifier, spUriElement);
|
||||||
}
|
}
|
||||||
} else if (signatureConfig.isXadesSignaturePolicyImplied()) {
|
|
||||||
SignaturePolicyIdentifierType signaturePolicyIdentifier =
|
private void addMimeTypes(SignedPropertiesType signedProperties) {
|
||||||
signedSignatureProperties.addNewSignaturePolicyIdentifier();
|
if (dataObjectFormatMimeTypes.isEmpty()) {
|
||||||
signaturePolicyIdentifier.addNewSignaturePolicyImplied();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DataObjectFormat
|
List<DataObjectFormatType> dataObjectFormats =
|
||||||
if (!dataObjectFormatMimeTypes.isEmpty()) {
|
signedProperties.
|
||||||
SignedDataObjectPropertiesType signedDataObjectProperties =
|
addNewSignedDataObjectProperties().
|
||||||
signedProperties.addNewSignedDataObjectProperties();
|
getDataObjectFormatList();
|
||||||
|
|
||||||
List<DataObjectFormatType> dataObjectFormats = signedDataObjectProperties
|
dataObjectFormatMimeTypes.forEach((key,value) -> {
|
||||||
.getDataObjectFormatList();
|
DataObjectFormatType dof = DataObjectFormatType.Factory.newInstance();
|
||||||
for (Map.Entry<String, String> dataObjectFormatMimeType : this.dataObjectFormatMimeTypes
|
dof.setObjectReference("#" + key);
|
||||||
.entrySet()) {
|
dof.setMimeType(value);
|
||||||
DataObjectFormatType dataObjectFormat = DataObjectFormatType.Factory.newInstance();
|
dataObjectFormats.add(dof);
|
||||||
dataObjectFormat.setObjectReference("#" + dataObjectFormatMimeType.getKey());
|
});
|
||||||
dataObjectFormat.setMimeType(dataObjectFormatMimeType.getValue());
|
|
||||||
dataObjectFormats.add(dataObjectFormat);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add XAdES ds:Object
|
private XMLObject addXadesObject(Document document, QualifyingPropertiesType qualifyingProperties) {
|
||||||
List<XMLStructure> xadesObjectContent = new ArrayList<>();
|
Node qualDocElSrc = qualifyingProperties.getDomNode();
|
||||||
Element qualDocElSrc = (Element)qualifyingProperties.getDomNode();
|
Node qualDocEl = document.importNode(qualDocElSrc, true);
|
||||||
Element qualDocEl = (Element)document.importNode(qualDocElSrc, true);
|
List<XMLStructure> xadesObjectContent = Arrays.asList(new DOMStructure(qualDocEl));
|
||||||
xadesObjectContent.add(new DOMStructure(qualDocEl));
|
return getSignatureFactory().newXMLObject(xadesObjectContent, null, null, null);
|
||||||
XMLObject xadesObject = getSignatureFactory().newXMLObject(xadesObjectContent, null, null, null);
|
}
|
||||||
objects.add(xadesObject);
|
|
||||||
|
|
||||||
// add XAdES ds:Reference
|
private Reference addXadesReference() throws XMLSignatureException {
|
||||||
List<Transform> transforms = new ArrayList<>();
|
List<Transform> transforms = singletonList(newTransform(CanonicalizationMethod.INCLUSIVE));
|
||||||
Transform exclusiveTransform = newTransform(CanonicalizationMethod.INCLUSIVE);
|
return newReference("#"+signatureConfig.getXadesSignatureId(), transforms, XADES_TYPE, null, null);
|
||||||
transforms.add(exclusiveTransform);
|
|
||||||
Reference reference = newReference
|
|
||||||
("#"+signatureConfig.getXadesSignatureId(), transforms, XADES_TYPE, null, null);
|
|
||||||
references.add(reference);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue