Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change code according to Clean Code rules #281

Merged
merged 10 commits into from
Dec 6, 2023
4 changes: 2 additions & 2 deletions src/main/java/xades4j/production/DataGenArchiveTimeStamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package xades4j.production;

import xades4j.algorithms.Algorithm;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.List;
Expand All @@ -26,6 +25,7 @@
import org.apache.xml.security.signature.Reference;
import org.apache.xml.security.utils.Constants;
import org.w3c.dom.Element;
import xades4j.algorithms.Algorithm;
import xades4j.properties.ArchiveTimeStampProperty;
import xades4j.properties.CertificateValuesProperty;
import xades4j.properties.CompleteCertificateRefsProperty;
Expand Down Expand Up @@ -109,7 +109,7 @@ protected void addPropSpecificTimeStampInput(

Integer pCnt = propsCnt.get(e.getLocalName());
if (pCnt != null)
propsCnt.put(e.getLocalName(), pCnt += 1);
propsCnt.put(e.getLocalName(), pCnt + 1);

} while ((e = DOMHelper.getNextSiblingElement(e)) != null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
package xades4j.production;

import xades4j.algorithms.Algorithm;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import org.apache.xml.security.utils.Constants;
import org.w3c.dom.Element;
import xades4j.algorithms.Algorithm;
import xades4j.properties.CompleteCertificateRefsProperty;
import xades4j.properties.CompleteRevocationRefsProperty;
import xades4j.properties.QualifyingProperty;
Expand Down Expand Up @@ -94,7 +94,7 @@ protected void addPropSpecificTimeStampInput(
Integer pCnt = elegiblePropsCnt.get(e.getLocalName());
if (pCnt != null)
{
elegiblePropsCnt.put(e.getLocalName(), pCnt += 1);
elegiblePropsCnt.put(e.getLocalName(), pCnt + 1);
digestInput.addNode(e);
}

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/xades4j/production/DataObjectReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
package xades4j.production;

import xades4j.properties.DataObjectDesc;
import java.net.URI;
import xades4j.properties.DataObjectDesc;

/**
* A reference to a signed data object. Each instance of this class will result
Expand Down Expand Up @@ -49,10 +49,7 @@ public DataObjectReference(String uri)
{
throw new NullPointerException("Reference URI cannot be null");
}

uri = uri.trim();
URI.create(uri.trim());
this.uri = uri;
this.uri = URI.create(uri.trim()).toString();
}

/**
Expand Down
55 changes: 32 additions & 23 deletions src/main/java/xades4j/production/KeyInfoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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 @@ -62,28 +61,7 @@ void buildKeyInfo(
List<X509Certificate> signingCertificateChain,
XMLSignature xmlSig) throws KeyingDataException, UnsupportedAlgorithmException
{
X509Certificate signingCertificate = signingCertificateChain.get(0);

if (this.basicSignatureOptions.checkKeyUsage())
{
// Check key usage.
// - KeyUsage[0] = digitalSignature
// - KeyUsage[1] = nonRepudiation
boolean[] keyUsage = signingCertificate.getKeyUsage();
if (keyUsage != null && !keyUsage[0] && !keyUsage[1])
{
throw new SigningCertKeyUsageException(signingCertificate);
}
}

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

if (this.basicSignatureOptions.includeSigningCertificate() != SigningCertificateMode.NONE
|| this.basicSignatureOptions.includeIssuerSerial()
Expand Down Expand Up @@ -152,4 +130,35 @@ void buildKeyInfo(
}
}
}

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

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

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

if (this.basicSignatureOptions.checkKeyUsage())
{
// Check key usage.
// - KeyUsage[0] = digitalSignature
// - KeyUsage[1] = nonRepudiation
boolean[] keyUsage = signingCertificate.getKeyUsage();
if (keyUsage != null && !keyUsage[0] && !keyUsage[1])
{
throw new SigningCertKeyUsageException(signingCertificate);
}
}
return signingCertificate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
*/
package xades4j.production;

import xades4j.properties.QualifyingProperty;
import xades4j.XAdES4jException;
import xades4j.properties.QualifyingProperty;

/**
* Thrown when there is an error generating a property data object.
* @author Luís
*/
public class PropertyDataGenerationException extends XAdES4jException
{
private final QualifyingProperty sourceProperty;
private final transient QualifyingProperty sourceProperty;

public PropertyDataGenerationException(QualifyingProperty sourceProperty, String message)
{
Expand Down
39 changes: 19 additions & 20 deletions src/main/java/xades4j/production/SignerBES.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
*/
package xades4j.production;

import static xades4j.utils.CanonicalizerUtils.checkC14NAlgorithm;

import jakarta.inject.Inject;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.signature.Manifest;
import org.apache.xml.security.signature.ObjectContainer;
Expand Down Expand Up @@ -53,15 +61,6 @@
import xades4j.xml.marshalling.UnsignedPropertiesMarshaller;
import xades4j.xml.marshalling.algorithms.AlgorithmsParametersMarshallingProvider;

import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import static xades4j.utils.CanonicalizerUtils.checkC14NAlgorithm;

/**
* Base logic for producing XAdES signatures (XAdES-BES).
* @author Luís
Expand Down Expand Up @@ -241,12 +240,8 @@ public final XadesSignatureResult sign(
// (...) use the Type attribute of this particular ds:Reference element,
// with its value set to: http://uri.etsi.org/01903#SignedProperties."

String digestAlgUri = this.signatureAlgorithms.getDigestAlgorithmForDataObjectReferences();
if (StringUtils.isNullOrEmptyString(digestAlgUri))
{
throw new NullPointerException("Digest algorithm URI not provided");
}

String digestAlgUri = getDigestAlgUri();

// Use same canonicalization URI as specified in the ds:CanonicalizationMethod for Signature.
Algorithm canonAlg = this.signatureAlgorithms.getCanonicalizationAlgorithmForSignature();

Expand Down Expand Up @@ -301,16 +296,20 @@ public final XadesSignatureResult sign(
return new XadesSignatureResult(signature, qualifProps);
}

private String getDigestAlgUri() {
String digestAlgUri = this.signatureAlgorithms.getDigestAlgorithmForDataObjectReferences();
if (StringUtils.isNullOrEmptyString(digestAlgUri))
{
throw new NullPointerException("Digest algorithm URI not provided");
}
return digestAlgUri;
}

private XMLSignature createSignature(Document signatureDocument, String baseUri, String signingKeyAlgorithm) throws XAdES4jXMLSigException, UnsupportedAlgorithmException
{
Algorithm signatureAlg = this.signatureAlgorithms.getSignatureAlgorithm(signingKeyAlgorithm);
if (null == signatureAlg)
{
throw new NullPointerException("Signature algorithm not provided");
}
Element signatureAlgElem = createElementForAlgorithm(signatureAlg, Constants._TAG_SIGNATUREMETHOD, signatureDocument);


Algorithm canonAlg = this.signatureAlgorithms.getCanonicalizationAlgorithmForSignature();
if (null == canonAlg)
{
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/xades4j/production/SignerT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package xades4j.production;

import jakarta.inject.Inject;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import xades4j.properties.SignedSignatureProperty;
import xades4j.properties.UnsignedSignatureProperty;
import xades4j.providers.DataObjectPropertiesProvider;
Expand All @@ -30,11 +34,6 @@
import xades4j.xml.marshalling.UnsignedPropertiesMarshaller;
import xades4j.xml.marshalling.algorithms.AlgorithmsParametersMarshallingProvider;

import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* Produces XAdES-T signatures. Doesn't extend SignerEPES because XAdES-T may be
* based on XAdES-BES. If T+EPES is needed, a {@code SignaturePolicyInfoProvider}
Expand All @@ -43,7 +42,7 @@
*/
class SignerT extends SignerBES
{
private Optional<SignaturePolicyInfoProvider> policyInfoProvider;
private final Optional<SignaturePolicyInfoProvider> policyInfoProvider;

@Inject
protected SignerT(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import com.google.inject.Module;
import xades4j.properties.QualifyingProperty;
import xades4j.utils.XadesProfileCore;
import xades4j.utils.XadesProfileResolutionException;
import xades4j.providers.MessageDigestEngineProvider;
import xades4j.providers.TimeStampTokenProvider;
import xades4j.utils.UtilsBindingsModule;
import xades4j.utils.XadesProfileCore;
import xades4j.utils.XadesProfileResolutionException;
import xades4j.xml.marshalling.MarshallingBindingsModule;
import xades4j.xml.marshalling.UnsignedPropertiesMarshaller;
import xades4j.xml.marshalling.algorithms.AlgorithmParametersBindingsModule;
Expand Down Expand Up @@ -63,7 +63,7 @@ public final <T> XadesFormatExtenderProfile withBinding(
}

public final XadesFormatExtenderProfile with(Object instance) {
this.profileCore.addBinding((Class)instance.getClass(), instance);
this.profileCore.addBinding((Class<Object>)instance.getClass(), instance);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/xades4j/production/XadesSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface XadesSigner
* @see SignedDataObjects
* @throws XAdES4jException if an error occurs
*/
public XadesSignatureResult sign(
XadesSignatureResult sign(
SignedDataObjects signedDataObjects,
Node parent) throws XAdES4jException;

Expand All @@ -52,7 +52,7 @@ public XadesSignatureResult sign(
* or last child of a node.
* @see SignatureAppendingStrategies
*/
public interface SignatureAppendingStrategy
interface SignatureAppendingStrategy
{
/**
* Appends the signature element to the DOM tree using the given node as
Expand Down Expand Up @@ -82,7 +82,7 @@ public interface SignatureAppendingStrategy
* @see SignatureAppendingStrategies
* @throws XAdES4jException if an error occurs
*/
public XadesSignatureResult sign(
XadesSignatureResult sign(
SignedDataObjects signedDataObjects,
Node referenceNode,
SignatureAppendingStrategy appendingStrategy) throws XAdES4jException;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/xades4j/production/XadesSigningProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

import com.google.inject.Module;
import xades4j.properties.QualifyingProperty;
import xades4j.providers.X500NameStyleProvider;
import xades4j.utils.XadesProfileCore;
import xades4j.utils.XadesProfileResolutionException;
import xades4j.providers.DataObjectPropertiesProvider;
import xades4j.providers.KeyingDataProvider;
import xades4j.providers.MessageDigestEngineProvider;
import xades4j.providers.SignaturePropertiesProvider;
import xades4j.providers.TimeStampTokenProvider;
import xades4j.providers.X500NameStyleProvider;
import xades4j.utils.UtilsBindingsModule;
import xades4j.utils.XadesProfileCore;
import xades4j.utils.XadesProfileResolutionException;
import xades4j.xml.marshalling.MarshallingBindingsModule;
import xades4j.xml.marshalling.SignedPropertiesMarshaller;
import xades4j.xml.marshalling.UnsignedPropertiesMarshaller;
Expand Down Expand Up @@ -161,7 +161,7 @@ public final <T> XadesSigningProfile withBinding(
* @return this profile
*/
public final XadesSigningProfile with(Object instance) {
this.profileCore.addBinding((Class)instance.getClass(), instance);
this.profileCore.addBinding((Class<Object>)instance.getClass(), instance);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/xades4j/properties/DataObjectProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ protected enum TargetMultiplicity
private final int multiplicity;
private final int initialSize;

private TargetMultiplicity(int mult)
TargetMultiplicity(int mult)
{
this(mult, mult);
}

private TargetMultiplicity(int mult, int size)
TargetMultiplicity(int mult, int size)
{
this.multiplicity = mult;
this.initialSize = size;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/xades4j/properties/IdentifierType.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@
public enum IdentifierType
{
/**
* The identifier is an URI.
* The identifier is a URI.
*/
URI,
/**
* The identifier is an Object IDentifier encoded as an URI
* The identifier is an Object Identifier encoded as a URI
*/
OID_AS_URI,
@Deprecated(since="2.2.2")
OIDAsURI,
/**
* The identifier is an Object IDentifier encoded as an URN
* The identifier is an Object Identifier encoded as a URN
*/
OID_AS_URN,
@Deprecated(since="2.2.2")
OIDAsURN
mjechow marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading