Search in sources :

Example 1 with Algorithm

use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.

the class KeyInfoBuilder method buildKeyInfo.

void buildKeyInfo(X509Certificate signingCertificate, XMLSignature xmlSig) throws KeyingDataException, UnsupportedAlgorithmException {
    // Check key usage.
    // - KeyUsage[0] = digitalSignature
    // - KeyUsage[1] = nonRepudiation
    boolean[] keyUsage = signingCertificate.getKeyUsage();
    if (keyUsage != null && !keyUsage[0] && !keyUsage[1]) {
        throw new SigningCertKeyUsageException(signingCertificate);
    }
    try {
        signingCertificate.checkValidity();
    } catch (CertificateException ce) {
        // CertificateExpiredException or CertificateNotYetValidException
        throw new SigningCertValidityException(signingCertificate);
    }
    if (this.basicSignatureOptionsProvider.includeSigningCertificate()) {
        try {
            X509Data x509Data = new X509Data(xmlSig.getDocument());
            x509Data.addCertificate(signingCertificate);
            x509Data.addSubjectName(signingCertificate);
            x509Data.addIssuerSerial(signingCertificate.getIssuerX500Principal().getName(), signingCertificate.getSerialNumber());
            xmlSig.getKeyInfo().add(x509Data);
            if (this.basicSignatureOptionsProvider.signSigningCertificate()) {
                String keyInfoId = xmlSig.getId() + "-keyinfo";
                xmlSig.getKeyInfo().setId(keyInfoId);
                // Use same canonicalization URI as specified in the ds:CanonicalizationMethod for Signature.
                Algorithm canonAlg = this.algorithmsProvider.getCanonicalizationAlgorithmForSignature();
                CanonicalizerUtils.checkC14NAlgorithm(canonAlg);
                Transforms transforms = TransformUtils.createTransforms(canonAlg, this.algorithmsParametersMarshaller, xmlSig.getDocument());
                xmlSig.addDocument('#' + keyInfoId, transforms, this.algorithmsProvider.getDigestAlgorithmForDataObjsReferences());
            }
        } catch (XMLSignatureException ex) {
            throw new UnsupportedAlgorithmException("Digest algorithm not supported in the XML Signature provider", this.algorithmsProvider.getDigestAlgorithmForDataObjsReferences(), ex);
        } catch (XMLSecurityException ex) {
            throw new KeyingDataException(ex.getMessage(), ex);
        }
    }
    if (this.basicSignatureOptionsProvider.includePublicKey()) {
        xmlSig.addKeyInfo(signingCertificate.getPublicKey());
    }
}
Also used : Transforms(org.apache.xml.security.transforms.Transforms) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) CertificateException(java.security.cert.CertificateException) X509Data(org.apache.xml.security.keys.content.X509Data) Algorithm(xades4j.algorithms.Algorithm) XMLSignatureException(org.apache.xml.security.signature.XMLSignatureException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Example 2 with Algorithm

use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.

the class DataGenBaseTimeStamp method generatePropertyData.

@Override
public final PropertyDataObject generatePropertyData(TProp prop, PropertiesDataGenerationContext ctx) throws PropertyDataGenerationException {
    Algorithm c14n = this.algsProvider.getCanonicalizationAlgorithmForTimeStampProperties();
    try {
        TimeStampDigestInput digestInput = this.tsInputFactory.newTimeStampDigestInput(c14n);
        addPropSpecificTimeStampInput(prop, digestInput, ctx);
        TimeStampTokenRes tsTknRes = this.tsTokenProvider.getTimeStampToken(digestInput.getBytes(), this.algsProvider.getDigestAlgorithmForTimeStampProperties());
        return createPropDataObj(prop, c14n, tsTknRes, ctx);
    } catch (UnsupportedAlgorithmException ex) {
        throw new PropertyDataGenerationException(prop, ex.getMessage(), ex);
    } catch (CannotAddDataToDigestInputException ex) {
        throw new PropertyDataGenerationException(prop, "cannot create time stamp input", ex);
    } catch (TimeStampTokenGenerationException ex) {
        throw new PropertyDataGenerationException(prop, "cannot get a time-stamp", ex);
    }
}
Also used : CannotAddDataToDigestInputException(xades4j.utils.CannotAddDataToDigestInputException) TimeStampDigestInput(xades4j.utils.TimeStampDigestInput) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) TimeStampTokenRes(xades4j.providers.TimeStampTokenProvider.TimeStampTokenRes) Algorithm(xades4j.algorithms.Algorithm) TimeStampTokenGenerationException(xades4j.providers.TimeStampTokenGenerationException)

Example 3 with Algorithm

use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.

the class FromXmlBaseTimeStampConverter method convertTimeStamps.

protected void convertTimeStamps(List<XmlXAdESTimeStampType> xmlTimeStamps, QualifyingPropertiesDataCollector propertyDataCollector) throws PropertyUnmarshalException {
    if (null == xmlTimeStamps || xmlTimeStamps.isEmpty())
        return;
    for (XmlXAdESTimeStampType xmlTS : xmlTimeStamps) {
        if (!xmlTS.getReferenceInfo().isEmpty())
            throw new PropertyUnmarshalException("ReferenceInfo is not supported in XAdESTimeStamp", propName);
        Algorithm c14n;
        XmlCanonicalizationMethodType xmlCanonMethod = xmlTS.getCanonicalizationMethod();
        if (null == xmlCanonMethod) {
            c14n = new GenericAlgorithm(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
        } else {
            List params = CollectionUtils.filterByType(xmlCanonMethod.getContent(), Element.class);
            c14n = new GenericAlgorithm(xmlCanonMethod.getAlgorithm(), params);
        }
        TPropData tsData = createTSData(c14n);
        List<Object> tsTokens = xmlTS.getEncapsulatedTimeStampOrXMLTimeStamp();
        if (tsTokens.isEmpty())
            throw new PropertyUnmarshalException("No time-stamp tokens", propName);
        for (Object tkn : tsTokens) {
            if (!(tkn instanceof XmlEncapsulatedPKIDataType))
                throw new PropertyUnmarshalException("XML time-stamps are not supported", propName);
            tsData.addTimeStampToken(((XmlEncapsulatedPKIDataType) tkn).getValue());
        }
        doSpecificConvert(xmlTS, tsData);
        setTSData(tsData, propertyDataCollector);
    }
}
Also used : XmlEncapsulatedPKIDataType(xades4j.xml.bind.xades.XmlEncapsulatedPKIDataType) XmlXAdESTimeStampType(xades4j.xml.bind.xades.XmlXAdESTimeStampType) List(java.util.List) Algorithm(xades4j.algorithms.Algorithm) GenericAlgorithm(xades4j.algorithms.GenericAlgorithm) GenericAlgorithm(xades4j.algorithms.GenericAlgorithm) XmlCanonicalizationMethodType(xades4j.xml.bind.xmldsig.XmlCanonicalizationMethodType)

Example 4 with Algorithm

use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.

the class ToXmlBaseTimeStampConverter method convertIntoObjectTree.

@Override
public final void convertIntoObjectTree(PropertyDataObject propData, TXml xmlProps, Document doc) {
    TData tsData = (TData) propData;
    XmlXAdESTimeStampType xmlTimeStamp = new XmlXAdESTimeStampType();
    // Canonicalization method
    XmlCanonicalizationMethodType xmlCanon = new XmlCanonicalizationMethodType();
    xmlTimeStamp.setCanonicalizationMethod(xmlCanon);
    Algorithm c14n = tsData.getCanonicalizationAlgorithm();
    xmlCanon.setAlgorithm(c14n.getUri());
    try {
        List<Node> c14nParams = this.algorithmsParametersMarshallingProvider.marshalParameters(c14n, doc);
        if (c14nParams != null) {
            xmlCanon.getContent().addAll(c14nParams);
        }
    } catch (UnsupportedAlgorithmException ex) {
        // Do not throw any specific exception for now.
        throw new IllegalArgumentException("Cannot marshall algorithm parameters", ex);
    }
    // Time-stamp tokens
    List<byte[]> tsTokens = tsData.getTimeStampTokens();
    List<Object> xmlTSTokens = xmlTimeStamp.getEncapsulatedTimeStampOrXMLTimeStamp();
    for (byte[] tsToken : tsTokens) {
        XmlEncapsulatedPKIDataType xmlTSTkn = new XmlEncapsulatedPKIDataType();
        xmlTSTkn.setValue(tsToken);
        xmlTSTokens.add(xmlTSTkn);
    }
    insertIntoObjectTree(xmlTimeStamp, xmlProps, tsData);
}
Also used : Node(org.w3c.dom.Node) Algorithm(xades4j.algorithms.Algorithm) UnsupportedAlgorithmException(xades4j.UnsupportedAlgorithmException) XmlEncapsulatedPKIDataType(xades4j.xml.bind.xades.XmlEncapsulatedPKIDataType) XmlXAdESTimeStampType(xades4j.xml.bind.xades.XmlXAdESTimeStampType) PropertyDataObject(xades4j.properties.data.PropertyDataObject) XmlCanonicalizationMethodType(xades4j.xml.bind.xmldsig.XmlCanonicalizationMethodType)

Example 5 with Algorithm

use of xades4j.algorithms.Algorithm in project xades4j by luisgoncalves.

the class SignerBES method createSignature.

private XMLSignature createSignature(Document signatureDocument, String baseUri, String signingKeyAlgorithm) throws XAdES4jXMLSigException, UnsupportedAlgorithmException {
    Algorithm signatureAlg = this.algorithmsProvider.getSignatureAlgorithm(signingKeyAlgorithm);
    if (null == signatureAlg) {
        throw new NullPointerException("Signature algorithm not provided");
    }
    Element signatureAlgElem = createElementForAlgorithm(signatureAlg, Constants._TAG_SIGNATUREMETHOD, signatureDocument);
    Algorithm canonAlg = this.algorithmsProvider.getCanonicalizationAlgorithmForSignature();
    if (null == canonAlg) {
        throw new NullPointerException("Canonicalization algorithm not provided");
    }
    Element canonAlgElem = createElementForAlgorithm(canonAlg, Constants._TAG_CANONICALIZATIONMETHOD, signatureDocument);
    try {
        return new XMLSignature(signatureDocument, baseUri, signatureAlgElem, canonAlgElem);
    } catch (XMLSecurityException ex) {
        // Following the code, doesn't seem to be thrown at all.
        throw new XAdES4jXMLSigException(ex.getMessage(), ex);
    }
}
Also used : XAdES4jXMLSigException(xades4j.XAdES4jXMLSigException) XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) Algorithm(xades4j.algorithms.Algorithm) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Aggregations

Algorithm (xades4j.algorithms.Algorithm)7 UnsupportedAlgorithmException (xades4j.UnsupportedAlgorithmException)4 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)2 XMLSignature (org.apache.xml.security.signature.XMLSignature)2 XMLSignatureException (org.apache.xml.security.signature.XMLSignatureException)2 Transforms (org.apache.xml.security.transforms.Transforms)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 XAdES4jXMLSigException (xades4j.XAdES4jXMLSigException)2 GenericAlgorithm (xades4j.algorithms.GenericAlgorithm)2 XmlEncapsulatedPKIDataType (xades4j.xml.bind.xades.XmlEncapsulatedPKIDataType)2 XmlXAdESTimeStampType (xades4j.xml.bind.xades.XmlXAdESTimeStampType)2 XmlCanonicalizationMethodType (xades4j.xml.bind.xmldsig.XmlCanonicalizationMethodType)2 PrivateKey (java.security.PrivateKey)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 X509Data (org.apache.xml.security.keys.content.X509Data)1 ObjectContainer (org.apache.xml.security.signature.ObjectContainer)1