org.apache.xml.security.signature.SignedInfo Java Examples

The following examples show how to use org.apache.xml.security.signature.SignedInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PropertiesDataGenerationContext.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * A simple constructor to be used when only unsigned signature properties
 * will be processed.
 * @param targetXmlSignature the target signature
 * @param algorithmsProvider algorithms in use
 */
PropertiesDataGenerationContext(XMLSignature targetXmlSignature) throws XAdES4jXMLSigException
{
    this.targetXmlSignature = targetXmlSignature;
    this.sigDocument = targetXmlSignature.getDocument();
    this.referencesMappings = null;

    SignedInfo signedInfo = targetXmlSignature.getSignedInfo();
    List<Reference> refs = new ArrayList<Reference>(signedInfo.getLength());
    for (int i = 0; i < signedInfo.getLength(); i++)
    {
        try
        {
            refs.add(signedInfo.item(i));
        } catch (XMLSecurityException ex)
        {
            throw new XAdES4jXMLSigException(String.format("Cannot process the %dth reference", i), ex);
        }
    }
    this.references = Collections.unmodifiableList(refs);
}
 
Example #2
Source File: KeyInfoBuilderTest.java    From xades4j with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testSignKeyInfo() throws Exception
{
    System.out.println("signKeyInfo");

    KeyInfoBuilder keyInfoBuilder = new KeyInfoBuilder(
            new BasicSignatureOptions().signKeyInfo(true),
            new TestAlgorithmsProvider(),
            new TestAlgorithmsParametersMarshallingProvider(),
            new DefaultX500NameStyleProvider());
    XMLSignature xmlSignature = getTestSignature();

    keyInfoBuilder.buildKeyInfo(certificates, xmlSignature);

    SignedInfo signedInfo = xmlSignature.getSignedInfo();
    Assert.assertEquals(1, signedInfo.getLength());

    Node refNode = signedInfo.item(0).getContentsBeforeTransformation().getSubNode();
    Assert.assertSame(xmlSignature.getKeyInfo().getElement(), refNode);
}
 
Example #3
Source File: SignatureUnmarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public Signature unmarshall(Element signatureElement) throws UnmarshallingException {
    log.debug("Starting to unmarshall Apache XML-Security-based SignatureImpl element");

    SignatureImpl signature = new SignatureImpl(signatureElement.getNamespaceURI(),
            signatureElement.getLocalName(), signatureElement.getPrefix());

    try {
        log.debug("Constructing Apache XMLSignature object");

        XMLSignature xmlSignature = new XMLSignature(signatureElement, "");

        SignedInfo signedInfo = xmlSignature.getSignedInfo();

        log.debug("Adding canonicalization and signing algorithms, and HMAC output length to Signature");
        signature.setCanonicalizationAlgorithm(signedInfo.getCanonicalizationMethodURI());
        signature.setSignatureAlgorithm(signedInfo.getSignatureMethodURI());
        signature.setHMACOutputLength(getHMACOutputLengthValue(signedInfo.getSignatureMethodElement()));

        org.apache.xml.security.keys.KeyInfo xmlSecKeyInfo = xmlSignature.getKeyInfo();
        if (xmlSecKeyInfo != null) {
            log.debug("Adding KeyInfo to Signature");
            Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(
                    xmlSecKeyInfo.getElement());
            KeyInfo keyInfo = (KeyInfo) unmarshaller.unmarshall(xmlSecKeyInfo.getElement());
            signature.setKeyInfo(keyInfo);
        }
        signature.setXMLSignature(xmlSignature);
        signature.setDOM(signatureElement);
        return signature;
    } catch (XMLSecurityException e) {
        log.error("Error constructing Apache XMLSignature instance from Signature element: {}", e.getMessage());
        throw new UnmarshallingException("Unable to unmarshall Signature with Apache XMLSignature", e);
    }
}
 
Example #4
Source File: XAdESSignature.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void extractReferences() {
	references = new ArrayList<>();
	final XMLSignature currentSantuarioSignature = getSantuarioSignature();
	final SignedInfo signedInfo = currentSantuarioSignature.getSignedInfo();
	final int numberOfReferences = signedInfo.getLength();
	for (int ii = 0; ii < numberOfReferences; ii++) {
		try {
			final Reference reference = signedInfo.item(ii);
			references.add(reference);
		} catch (XMLSecurityException e) {
			LOG.warn("Unable to retrieve reference #{} : {}", ii, e.getMessage());
		}
	}
}
 
Example #5
Source File: CounterSignatureVerifier.java    From xades4j with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public QualifyingProperty verify(
        GenericDOMData propData,
        QualifyingPropertyVerificationContext ctx) throws InvalidPropertyException
{


    XAdESVerificationResult res;
    try
    {
        Element sigElem = DOMHelper.getFirstChildElement(propData.getPropertyElement());
        res = verifier.verify(sigElem, null);
    } catch (XAdES4jException ex)
    {
        throw new CounterSignatureXadesVerificationException(ex);
    }

    // "Check that the enclosed signature correctly references the ds:SignatureValue
    // present in the countersigned XAdES signature."

    Node targetSigValueElem = ctx.getSignature().getElement().getElementsByTagNameNS(
            Constants.SignatureSpecNS, Constants._TAG_SIGNATUREVALUE).item(0);

    try
    {
        SignedInfo si = res.getXmlSignature().getSignedInfo();
        for (int i = 0; i < si.getLength(); i++)
        {
            Reference r = si.item(i);
            if (r.getContentsAfterTransformation().getSubNode() == targetSigValueElem)
            {
                // The signature references the SignatureValue element.
                return new CounterSignatureProperty(res);
            }
            else if (r.getContentsBeforeTransformation().getSubNode() == targetSigValueElem && CanonicalizerUtils.allTransformsAreC14N(r))
            {
                // The signature references the SignatureValue element with
                // C14N transforms only.
                return new CounterSignatureProperty(res);
            }
        }
        throw new CounterSignatureSigValueRefException();
    } catch (XMLSecurityException e)
    {
        // Shouldn't happen because the signature was already verified.
        throw new CounterSignatureVerificationException(e);
    }
}