Giter Site home page Giter Site logo

signature-validation's Introduction

Logo

Signature Validation

License

Core components for validating electronic signatures and to extend signed documents with Signature Validation Tokens (SVT). This includes the following independent modules:

Module Description Depends on
cert-extensions Extensions to BouncyCastle to create and process X.509 certificate extensions. -
cert-validation Functions to support certificate validation with certificate path building and certificate revocation checking. cert-extensions
sigval-commons Common functions to support signature validation and SVT creation independent of signed document format. cert-validation
sigval-pdf Signature validation and SVT creation for PDF documents. sigval-commons
sigval-xml Signature validation and SVT creation for XML documents. sigval-commons

The signature validation modules builds upon the SignService Commons library available from signservice-commons on GitHub.


cert-extensions module

Maven

Maven Central

<dependency>
    <groupId>se.swedenconnect.sigval</groupId>
    <artifactId>cert-extensions</artifactId>
    <version>${cert-extensions.version}</version>
</dependency>
API documentation

Java API documentation for se.swedenconnect.sigval:cert-extensions.

cert-validation module

Maven

Maven Central

<dependency>
    <groupId>se.swedenconnect.sigval</groupId>
    <artifactId>cert-validation</artifactId>
    <version>${cert-validation.version}</version>
</dependency>

API documentation

Java API documentation for se.swedenconnect.sigval:cert-validation.

Usage

The main feature provided by this module is the certificate validity checker implementation and its supporting implementation of certificate path building and certificate status checking.

CRL Cache

The CRLCache is an interface that provides the basic functions to locate a CRL based on an URL or a CRLDistributionPoint extension from an X.509 certificate as well as a function to update cached CRL data.

The default implementation CRLCacheImpl implements a cache where downloaded CRL:s are cached and stored in a specified location.

The following code demonstrate creation of the default CRL cache:

CRLCache crlCache = new CRLCacheImpl(cacheFolderFile, recacheGracePeriod);

The recacheGracePeriod parameter indicates the time in milliseconds before a recently cached CRL is allowed to be re-cached. If the time since last re-cache is less than this time, no re-caching is performed even if the re-cache function is called.

An in memory storage CRL Cache is available for simplified usage of the certificate validator where the projected amount of cacehed data can safely be stored and processed in memory. This implementation sotres all information, including all CRLs in memory until program restart.

The following code demonstrate creation of the in memory CRL cache:

CRLCache crlCache = new InMemoryCRLCache()

Validity Checker

The StatusCheckingCertificateValidatorImpl class implements the CertificateValidator interface to provide certificate validation based on CRL and OCSP certificate status checking as well as PKIX path validation.

The following code demonstrate creation of a certificate validator:

CertificateValidator createCertificateValidator(
  final CRLCache crlCache,
  final CertStore certStore,
  final X509Certificate[] defaultTrustAnchors) {
  return new StatusCheckingCertificateValidatorImpl(crlCache, certStore, defaultTrustAnchors);
}

The crlCache is a CRLCache object according the example above. The certStore is a java.security.cert.CertStore holding any intermediary certificates available to the certificate path builder and the defaultTrustAnchors provides default trust anchor certificates for the path validator. These certificates are always available/trusted by the certificate validator in addition to any certificates specified at function calls to the validator.

sigval-commons module

Maven

Maven Central

<dependency>
    <groupId>se.swedenconnect.sigval</groupId>
    <artifactId>sigval-commons</artifactId>
    <version>${sigval-commons.version}</version>
</dependency>
API documentation

Java API documentation for se.swedenconnect.sigval:sigval-commons.

sigval-pdf module

Maven

Maven Central

<dependency>
    <groupId>se.swedenconnect.sigval</groupId>
    <artifactId>sigval-pdf</artifactId>
    <version>${sigval-pdf.version}</version>
</dependency>

API documentation

Java API documentation for se.swedenconnect.sigval:sigval-pdf.

Usage

The PDF module provides the functionality to validate PDF signatures and to issue SVT tokens for signed PDF documents.

PDF signature validation

The following code example creates a PDF signature validator:

ExtendedPDFSignatureValidator createPdfSignatureValidator(
  final CertificateValidator certificateValidator) {

  final TimeStampPolicyVerifier timeStampPolicyVerifier =
    new BasicTimstampPolicyVerifier(certificateValidator)
  final PDFSignaturePolicyValidator signaturePolicyValidator =
    new PkixPdfSignaturePolicyValidator();

  final PDFSingleSignatureValidator pdfSignatureVerifier = new PDFSingleSignatureValidatorImpl(
    certificateValidator, signaturePolicyValidator, timeStampPolicyVerifier);

  // Setup SVT validator
  final PDFSVTValidator pdfsvtValidator = new PDFSVTValidator(
    certificateValidator, timeStampPolicyVerifier);

  // Get the PDF validator
  return new SVTenabledPDFDocumentSigVerifier(
    pdfSignatureVerifier, pdfsvtValidator, DefaultPDFSignatureContextFactory());
}

The following replaceable components are used to construct the signature verifier:

Component  Description
TimeStampPolicyVerifier A verifier capable of validating timestamps against a defined validation policy.
PDFSignaturePolicyValidator This is a policy validator which examines the signature validation results and applies a validation policy to determine the signature validity.
PDFSingleSignatureValidator This is the main PDF signature validator performing signature validation of individual PDF signatures.
PDFSVTValidator This is a PDF SVT validator capable of validating a PDF signature based on an existing SVT token. If no PDFSVTValidator is provided, no SVT validation is performed.

PDF SVT issuance

The PDF SVT token is created by the SVT claims issuer. This component issues the SVT token which reflects the validation of the PDF signature. The following code example creates the PDF claims issuer:

PDFSVTSigValClaimsIssuer createPdfsvtSigValClaimsIssuer(
  final JWSAlgorithm svtJWSAlgorithm,
  final PrivateKey privateKey,
  final List<X509Certificate> certificates,
  final ExtendedPDFSignatureValidator pdfSignatureValidator)
{
  return new PDFSVTSigValClaimsIssuer(
    svtJWSAlgorithm, privateKey, certificates, pdfSignatureValidator);
}

Finally the signed PDF is extended with a document timestamp which includes the PDF SVT token. This requires a document timestamp signer that can be created according to the following example:

DefaultPDFDocTimestampSignatureInterface createTimeStampSigner(
  final PrivateKey privateKey, final List<X509Certificate> certificates, final String sigAlgoUri)
{
  return new DefaultPDFDocTimestampSignatureInterface(privateKey, certificates, sigAlgoUri);
}

The private key, certificates and algorithm declarations provided to the creation of both SVT issuer and timestamp signer is the private key used to sign and the certificate used to validate the respective token/timestamp as well as the algorithm used to sign them.

Note that it is recommended to use a complete path to support timestamp token signing, while an SVT normally is issued directly from a trust anchor cert in order to avoid path validation of SVT tokens.

The following code example then issues an SVT token and extends the PDF document with this token based on the SVT issuer and the timestamp signer:

byte[] extendPDFwithSVT(
  final byte[] signedDoc, final SVTModel svtModel,
  final PDFSVTSigValClaimsIssuer pdfsvtSigValClaimsIssuer,
  final DefaultPDFDocTimestampSignatureInterface timeStampSigner) {

  // Create SVT token
  final SignedJWT signedSvtJWT = pdfsvtSigValClaimsIssuer.getSignedSvtJWT(signedDoc, svtModel);

  // Extend PDF document
  return PDFDocTimstampProcessor.createSVTSealedPDF(
    signedDoc, signedSvtJWT.serialize(), timeStampSigner).getDocument();
}

sigval-xml module

Maven

Maven Central

<dependency>
    <groupId>se.swedenconnect.sigval</groupId>
    <artifactId>sigval-xml</artifactId>
    <version>${sigval-xml.version}</version>
</dependency>
API documentation

Java API documentation for se.swedenconnect.sigval:sigval-xml.

Usage

The XML module provides the functionality to validate XML signatures and to issue SVT tokens to signed XML documents.

XML signature validation

The following code example creates an XML signature validator:

ExtendedXMLSignedDocumentValidator createXmlSignedDocumentValidator() {
  return new XMLSignedDocumentValidator(xmlSignatureElementValidator);
}

XMLSignatureElementValidator createXmlSignatureElementValidator(
  final CertificateValidator certificateValidator){

  final TimeStampPolicyVerifier timeStampPolicyVerifier =
    new BasicTimstampPolicyVerifier(certificateValidator)
  final XMLSignaturePolicyValidator xmlSignaturePolicyValidator =
    new PkixXmlSignaturePolicyValidator();

  // Setup SVT validator
  final XMLSVTValidator xmlSvtValidator = new XMLSVTValidator(certificateValidator, null);

  return new XMLSignatureElementValidatorImpl(
    certificateValidator,
    xmlSignaturePolicyValidator,
    timeStampPolicyVerifier,
    xmlSvtValidator
  );
}

The following replaceable components are used to construct the signature verifier:

Component  Description
TimeStampPolicyVerifier A verifier capable of validating timestamps against a defined validation policy.
XMLSignaturePolicyValidator This is a policy validator which examines the signature validation results and applies a validation policy to determine the signature validity.
XMLSignatureElementValidator This is the main XML signature validator performing signature validation of individual XML signatures.
XMLSVTValidator This is a XML SVT validator capable of validating a PDF signature based on an existing SVT token. If no XMLSVTValidator is provided, no SVT validation is performed.

XML SVT issuance

The XML SVT token is created by the SVT claims issuer. This component issues the SVT token which reflects the validation of the XML signature. The following code example creates the XML claims issuer:

XMLSVTSigValClaimsIssuer createClaimsIssuer(
  final JWSAlgorithm svtJWSAlgorithm, final PrivateKey privateKey,
  final List<X509Certificate> certificates,
  final XMLSignatureElementValidator xmlSignatureElementValidator)
{
  return new XMLSVTSigValClaimsIssuer(svtJWSAlgorithm, privateKey,
    certificates, xmlSignatureElementValidator);
}

Finally the signed XML document signatures are extended with SVT tokens. This requires an XML document SVT issuer that can be created according to the following example:

XMLDocumentSVTIssuer createXmlDocumentSVTIssuer(final XMLSVTSigValClaimsIssuer claimsIssuer) {
  return new XMLDocumentSVTIssuer(claimsIssuer);
}

The private key, certificates and algorithm declarations provided to the creation of the SVT issuer is the private key used to sign and the certificate used to validate the SVT token and the algorithm used to sign it.

Note that it is recommended to issue an SVT directly from a trust anchor cert in order to avoid path validation of SVT tokens.

The following code example then issues an SVT token and extends the XML document with SVT tokens based on the SVT issuer:

byte[] extendXMLwithSVT(final Document signedXmlDocument,
  final SVTModel svtModel, final XMLDocumentSVTIssuer xmlDocumentSVTIssuer) {

  // Extend XML document
  return xmlDocumentSVTIssuer.issueSvt(
    signedXmlDocument,
    svtModel,
    XMLDocumentSVTMethod.EXTEND);
}

The XMLDocumentSVTMethod parameter instructs the SVT issuer what to do if a current SVT is already present in the XML signature. a value of XMLDocumentSVTMethod.REPLACE will replace the existing SVT while the value XMLDocumentSVTMethod.EXTEND as shown in the example will simply add the new SVT after the old one.


Copyright © The Swedish Agency for Digital Government (DIGG), 2019-2023. All Rights Reserved.

signature-validation's People

Contributors

martin-lindstrom avatar razumain avatar snyk-bot avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

signature-validation's Issues

OCSP nonce wrapping

Original RFC 2560 OCSP specified that OCSP nonce should be stored as the extValue bytes and most OCSP clients today, including this one create OCSP requests this way.

However, clarifications in RFC 6960 makes it clear that the OCSP nonce should be encoded in an OCTETSTRING before being DER encoded as extension data.

The code should be updated to always wrap nonce data in a DER encoded OCTETSTRING. This wrapping should use a 30 byte nonce causing the wrapped nonce to be 32 bytes, making sure that it works against RFC 8954 compliant responders.

Fix XML XAdES check

Currently the results reports "invalidSigningCert" if there is no XAdES reference available.

This should only be set IF this is an ETSI AdES signature AND the references is missing or in error.

Fix should be applied to "XMLSignatureElementValidatorImpl" row 216 and 458

Add In memory CRL cache

Several limited implementations of certificate and signature validation would benefit from a CRL checker with a CRL cache that does not require cache storage on the hard disk.

A recent implementation of an In memory CRL cache was made in another project. That implementation should be imported to this library.

Removing xalan dependency

There is a dependency on xalan via sign service pdf commons. This is being updated but not released.

Once this is fixed, we must update the dependency here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.