Skip to content

Commit

Permalink
Minor method refactorings for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
luisgoncalves committed Dec 17, 2023
1 parent a2d8046 commit 81ab015
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 62 deletions.
58 changes: 40 additions & 18 deletions src/main/java/xades4j/production/KeyInfoBuilder.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* XAdES4j - A Java library for generation and verification of XAdES signatures.
* Copyright (C) 2011 Luis Goncalves.
*
*
* XAdES4j is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or any later version.
*
*
* XAdES4j is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
*
* You should have received a copy of the GNU Lesser General Public License along
* with XAdES4j. If not, see <http://www.gnu.org/licenses/>.
*/
Expand All @@ -19,6 +19,7 @@
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;

import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.keys.content.X509Data;
import org.apache.xml.security.signature.XMLSignature;
Expand Down Expand Up @@ -63,9 +64,18 @@ void buildKeyInfo(
{
X509Certificate signingCertificate = getSigningCertificate(signingCertificateChain);

addSigningCertificateElements(signingCertificateChain, signingCertificate, xmlSig);

addPublicKey(signingCertificate, xmlSig);

addKeyInfoReference(xmlSig);
}

private void addSigningCertificateElements(List<X509Certificate> signingCertificateChain, X509Certificate signingCertificate, XMLSignature xmlSig) throws KeyingDataException
{
if (this.basicSignatureOptions.includeSigningCertificate() != SigningCertificateMode.NONE
|| this.basicSignatureOptions.includeIssuerSerial()
|| this.basicSignatureOptions.includeSubjectName())
|| this.basicSignatureOptions.includeIssuerSerial()
|| this.basicSignatureOptions.includeSubjectName())
{
X509Data x509Data = new X509Data(xmlSig.getDocument());
xmlSig.getKeyInfo().add(x509Data);
Expand All @@ -75,13 +85,13 @@ void buildKeyInfo(
int loopLimit = this.basicSignatureOptions.includeSigningCertificate() == SigningCertificateMode.SIGNING_CERTIFICATE
? 1
: signingCertificateChain.size();
for(int i = 0; i < loopLimit; ++i)

for (int i = 0; i < loopLimit; ++i)
{
try
{
x509Data.addCertificate(signingCertificateChain.get(i));
}
}
catch (XMLSecurityException ex)
{
throw new KeyingDataException(ex.getMessage(), ex);
Expand All @@ -99,12 +109,18 @@ void buildKeyInfo(
x509Data.addSubjectName(this.x500NameStyleProvider.toString(signingCertificate.getSubjectX500Principal()));
}
}
}

private void addPublicKey(X509Certificate signingCertificate, XMLSignature xmlSig)
{
if (this.basicSignatureOptions.includePublicKey())
{
xmlSig.addKeyInfo(signingCertificate.getPublicKey());
}
}

private void addKeyInfoReference(XMLSignature xmlSig) throws UnsupportedAlgorithmException
{
if (this.basicSignatureOptions.signKeyInfo())
{
try
Expand All @@ -118,34 +134,40 @@ void buildKeyInfo(
Transforms transforms = TransformUtils.createTransforms(canonAlg, this.algorithmsParametersMarshaller, xmlSig.getDocument());

xmlSig.addDocument(
'#' + keyInfoId,
transforms,
this.signatureAlgorithms.getDigestAlgorithmForDataObjectReferences());
'#' + keyInfoId,
transforms,
this.signatureAlgorithms.getDigestAlgorithmForDataObjectReferences());
}
catch (XMLSignatureException ex)
{
throw new UnsupportedAlgorithmException(
"Digest algorithm not supported in the XML Signature provider",
this.signatureAlgorithms.getDigestAlgorithmForDataObjectReferences(), ex);
"Digest algorithm not supported in the XML Signature provider",
this.signatureAlgorithms.getDigestAlgorithmForDataObjectReferences(), ex);
}
}
}

private X509Certificate getSigningCertificate(List<X509Certificate> signingCertificateChain) throws SigningCertKeyUsageException, SigningCertValidityException {
private X509Certificate getSigningCertificate(List<X509Certificate> signingCertificateChain) throws SigningCertKeyUsageException, SigningCertValidityException
{
X509Certificate signingCertificate = getX509Certificate(signingCertificateChain);

if (this.basicSignatureOptions.checkCertificateValidity()) {
try {
if (this.basicSignatureOptions.checkCertificateValidity())
{
try
{
signingCertificate.checkValidity();
} catch (final CertificateException ce) {
}
catch (final CertificateException ce)
{
// CertificateExpiredException or CertificateNotYetValidException
throw new SigningCertValidityException(signingCertificate);
}
}
return signingCertificate;
}

private X509Certificate getX509Certificate(List<X509Certificate> signingCertificateChain) throws SigningCertKeyUsageException {
private X509Certificate getX509Certificate(List<X509Certificate> signingCertificateChain) throws SigningCertKeyUsageException
{
X509Certificate signingCertificate = signingCertificateChain.get(0);

if (this.basicSignatureOptions.checkKeyUsage())
Expand Down
61 changes: 35 additions & 26 deletions src/main/java/xades4j/verification/SignatureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ static ReferencesRes processReferences(

for (int i = 0; i < signedInfo.getLength(); i++)
{
Reference ref;
try
{
ref = signedInfo.item(i);
} catch (XMLSecurityException ex)
{
throw new XAdES4jXMLSigException(String.format("Cannot process the %dth reference", i), ex);
}

Reference ref = getReference(signedInfo, i);
String refTypeUri = ref.getType();

// XAdES 6.3.1: "In order to protect the properties with the signature,
Expand All @@ -97,25 +89,11 @@ static ReferencesRes processReferences(
throw new QualifyingPropertiesIncorporationException("Multiple references to SignedProperties");
}
signedPropsRef = ref;
} else
}
else
{
RawDataObjectDesc dataObj = new RawDataObjectDesc(ref);
RawDataObjectDesc dataObj = createDataObjectDesc(ref);
dataObjsReferences.add(dataObj);
try
{
Transforms transfs = ref.getTransforms();
if (transfs != null)
{
for (int j = 0; j < transfs.getLength(); ++j)
{
dataObj.withTransform(new GenericAlgorithm(transfs.item(j).getURI()));
}
}
} catch (XMLSecurityException ex)
{
throw new XAdES4jXMLSigException("Cannot process transfroms", ex);
}

}
}

Expand All @@ -130,6 +108,37 @@ static ReferencesRes processReferences(
return new ReferencesRes(dataObjsReferences, signedPropsRef);
}

private static Reference getReference(SignedInfo signedInfo, int i) throws XAdES4jXMLSigException
{
try
{
return signedInfo.item(i);
} catch (XMLSecurityException ex)
{
throw new XAdES4jXMLSigException(String.format("Cannot process the %dth reference", i), ex);
}
}

private static RawDataObjectDesc createDataObjectDesc(Reference ref) throws XAdES4jXMLSigException
{
RawDataObjectDesc dataObj = new RawDataObjectDesc(ref);
try
{
Transforms transfs = ref.getTransforms();
if (transfs != null)
{
for (int j = 0; j < transfs.getLength(); ++j)
{
dataObj.withTransform(new GenericAlgorithm(transfs.item(j).getURI()));
}
}
} catch (XMLSecurityException ex)
{
throw new XAdES4jXMLSigException("Cannot process transfroms", ex);
}
return dataObj;
}

/***************************************************************************/
static Element getQualifyingPropertiesElement(XMLSignature signature) throws QualifyingPropertiesIncorporationException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import xades4j.properties.CommitmentTypePropertyBase;
import xades4j.properties.data.CommitmentTypeData;
import xades4j.xml.bind.xades.XmlAnyType;
Expand All @@ -27,7 +28,6 @@
import xades4j.xml.bind.xades.XmlSignedDataObjectPropertiesType;

/**
*
* @author Luís
*/
class FromXmlCommitmentTypeConverter implements SignedDataObjPropFromXmlConv
Expand Down Expand Up @@ -55,28 +55,15 @@ public void convertFromObjectTree(
XmlCommitmentTypeQualifiersListType xmlQualifiers = xmlCommitment.getCommitmentTypeQualifiers();
if (xmlQualifiers != null)
{
Collection<Object> qualifiers = new ArrayList<>();
for (XmlAnyType xmlQualifier : xmlQualifiers.getCommitmentTypeQualifier())
{
if (!xmlQualifier.getContent().isEmpty())
{
if (xmlQualifier.getContent().size() > 1)
{
throw new PropertyUnmarshalException("Qualifiers with multiple children are not support", CommitmentTypePropertyBase.PROP_NAME);
}

qualifiers.add(xmlQualifier.getContent().get(0));
}
}

commTypeData.setQualifiers(qualifiers);
commTypeData.setQualifiers(getQualifiers(xmlQualifiers));
}

propertyDataCollector.addCommitmentType(commTypeData);
}
}

private static List<String> getObjsRefs(XmlCommitmentTypeIndicationType xmlCommitment) throws PropertyUnmarshalException {
private static List<String> getObjsRefs(XmlCommitmentTypeIndicationType xmlCommitment) throws PropertyUnmarshalException
{
List<String> objsRefs = xmlCommitment.getObjectReference();
Object allDataObjs = xmlCommitment.getAllSignedDataObjects();

Expand All @@ -88,10 +75,29 @@ private static List<String> getObjsRefs(XmlCommitmentTypeIndicationType xmlCommi
{
throw new PropertyUnmarshalException("ObjectReference or AllSignedDataObjects have to be present", CommitmentTypePropertyBase.PROP_NAME);
}
} else if (allDataObjs != null)
}
else if (allDataObjs != null)
{
throw new PropertyUnmarshalException("Both ObjectReference and AllSignedDataObjects are present", CommitmentTypePropertyBase.PROP_NAME);
}
return objsRefs;
}

private static Collection<Object> getQualifiers(XmlCommitmentTypeQualifiersListType xmlQualifiers) throws PropertyUnmarshalException
{
Collection<Object> qualifiers = new ArrayList<>();
for (XmlAnyType xmlQualifier : xmlQualifiers.getCommitmentTypeQualifier())
{
if (!xmlQualifier.getContent().isEmpty())
{
if (xmlQualifier.getContent().size() > 1)
{
throw new PropertyUnmarshalException("Qualifiers with multiple children are not support", CommitmentTypePropertyBase.PROP_NAME);
}

qualifiers.add(xmlQualifier.getContent().get(0));
}
}
return qualifiers;
}
}

0 comments on commit 81ab015

Please sign in to comment.