Search in sources :

Example 1 with C14NMethodParameterSpec

use of javax.xml.crypto.dsig.spec.C14NMethodParameterSpec in project jdk8u_jdk by JetBrains.

the class XMLDSigWithSecMgr method setup.

private void setup() throws Exception {
    ss = new ServerSocket(0);
    Thread thr = new Thread(this);
    thr.start();
    fac = XMLSignatureFactory.getInstance();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    db = dbf.newDocumentBuilder();
    sha1 = fac.newDigestMethod(DigestMethod.SHA1, null);
    withoutComments = fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) C14NMethodParameterSpec(javax.xml.crypto.dsig.spec.C14NMethodParameterSpec)

Example 2 with C14NMethodParameterSpec

use of javax.xml.crypto.dsig.spec.C14NMethodParameterSpec in project santuario-java by apache.

the class CanonicalizationMethodTest method testConstructor.

@org.junit.Test
public void testConstructor() throws Exception {
    // test newAlgorithmMethod(String algorithm,
    // AlgorithmParameterSpec params)
    // for generating CanonicalizationMethod objects
    CanonicalizationMethod cm;
    for (int i = 0; i < C14N_ALGOS.length; i++) {
        String algo = C14N_ALGOS[i];
        cm = factory.newCanonicalizationMethod(algo, (C14NMethodParameterSpec) null);
        assertNotNull(cm);
        assertEquals(cm.getAlgorithm(), algo);
        assertNull(cm.getParameterSpec());
        try {
            cm = factory.newCanonicalizationMethod(algo, new TestUtils.MyOwnC14nParameterSpec());
            fail("Should raise an IAPE for invalid c14n parameters");
        } catch (InvalidAlgorithmParameterException iape) {
        } catch (Exception ex) {
            fail("Should raise a IAPE instead of " + ex);
        }
        if (algo.equals(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS) || algo.equals(CanonicalizationMethod.EXCLUSIVE)) {
            cm = factory.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, new ExcC14NParameterSpec());
            AlgorithmParameterSpec aps = cm.getParameterSpec();
            assertNotNull(aps);
            assertTrue(aps instanceof ExcC14NParameterSpec);
        }
    }
    try {
        cm = factory.newCanonicalizationMethod(null, (C14NMethodParameterSpec) null);
        fail("Should raise a NPE for null algo");
    } catch (NullPointerException npe) {
    } catch (Exception ex) {
        fail("Should raise a NPE instead of " + ex);
    }
    try {
        cm = factory.newCanonicalizationMethod("non-existent", (C14NMethodParameterSpec) null);
        fail("Should raise an NSAE for non-existent algos");
    } catch (NoSuchAlgorithmException nsae) {
    } catch (Exception ex) {
        fail("Should raise an NSAE instead of " + ex);
    }
}
Also used : ExcC14NParameterSpec(javax.xml.crypto.dsig.spec.ExcC14NParameterSpec) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) C14NMethodParameterSpec(javax.xml.crypto.dsig.spec.C14NMethodParameterSpec)

Example 3 with C14NMethodParameterSpec

use of javax.xml.crypto.dsig.spec.C14NMethodParameterSpec in project santuario-java by apache.

the class DetachedTest method test.

@org.junit.Test
public void test() {
    try {
        // 
        // PART 1 : Creating the detached signature
        // 
        // Create a factory that will be used to generate the signature
        // structures
        XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", new org.apache.jcp.xml.dsig.internal.dom.XMLDSigRI());
        // Create a Reference to an external URI that will be digested
        Reference ref = fac.newReference("http://www.w3.org/TR/xml-stylesheet", fac.newDigestMethod(DigestMethod.SHA1, null));
        // Create a DSA KeyPair
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        kpg.initialize(1024, new SecureRandom("not so random bytes".getBytes()));
        KeyPair kp = kpg.generateKeyPair();
        // Create a KeyValue containing the generated DSA PublicKey
        KeyInfoFactory kif = fac.getKeyInfoFactory();
        KeyValue kv = kif.newKeyValue(kp.getPublic());
        // Create a KeyInfo and add the KeyValue to it
        KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
        // Create SignedInfo
        SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), Collections.singletonList(ref));
        // Create XMLSignature
        XMLSignature signature = fac.newXMLSignature(si, ki, null, null, null);
        // Create an XMLSignContext and set the
        // DSA PrivateKey for signing
        Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
        DOMSignContext signContext = new DOMSignContext(kp.getPrivate(), doc);
        signContext.putNamespacePrefix(XMLSignature.XMLNS, "ds");
        URIDereferencer ud = new LocalHttpCacheURIDereferencer();
        signContext.setURIDereferencer(ud);
        // Generate (and sign) the XMLSignature
        signature.sign(signContext);
        TestUtils.validateSecurityOrEncryptionElement(doc.getDocumentElement());
        // 
        // PART 2 : Validating the detached signature
        // 
        // Create a XMLValidateContext & set the DSAPublicKey for validating
        XMLValidateContext vc = new DOMValidateContext(kp.getPublic(), doc.getDocumentElement());
        vc.setURIDereferencer(ud);
        // Validate the Signature (generated above)
        boolean coreValidity = signature.validate(vc);
        // Check core validation status
        if (coreValidity == false) {
            // check the validation status of each Reference
            @SuppressWarnings("unchecked") Iterator<Reference> i = signature.getSignedInfo().getReferences().iterator();
            while (i.hasNext()) {
                Reference reference = i.next();
                reference.validate(vc);
            }
            fail("Signature failed core validation");
        }
        // You can also validate an XML Signature which is in XML format.
        // Unmarshal and validate an XMLSignature from a DOMValidateContext
        signature = fac.unmarshalXMLSignature(vc);
        coreValidity = signature.validate(vc);
        assertTrue("Core validity of unmarshalled XMLSignature is false", coreValidity);
    } catch (Exception ex) {
        fail("Exception: " + ex);
    }
}
Also used : javax.xml.crypto.dsig(javax.xml.crypto.dsig) Document(org.w3c.dom.Document) URIDereferencer(javax.xml.crypto.URIDereferencer) DOMValidateContext(javax.xml.crypto.dsig.dom.DOMValidateContext) C14NMethodParameterSpec(javax.xml.crypto.dsig.spec.C14NMethodParameterSpec) DOMSignContext(javax.xml.crypto.dsig.dom.DOMSignContext)

Example 4 with C14NMethodParameterSpec

use of javax.xml.crypto.dsig.spec.C14NMethodParameterSpec in project wildfly by wildfly.

the class TestServlet method signDocument.

private static void signDocument(final Document doc, final PrivateKey privateKey) throws Exception {
    final XMLSignatureFactory xsf = XMLSignatureFactory.getInstance("DOM");
    final Reference ref = xsf.newReference("", xsf.newDigestMethod(DigestMethod.SHA256, null), Collections.singletonList(xsf.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null);
    final SignedInfo si = xsf.newSignedInfo(xsf.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null), xsf.newSignatureMethod("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null), Collections.singletonList(ref));
    final KeyInfo ki = KeyInfoFactory.getInstance().newKeyInfo(Collections.singletonList(KeyInfoFactory.getInstance().newKeyName("dummy")));
    xsf.newXMLSignature(si, ki).sign(new DOMSignContext(privateKey, doc.getDocumentElement()));
}
Also used : XMLSignatureFactory(javax.xml.crypto.dsig.XMLSignatureFactory) KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) Reference(javax.xml.crypto.dsig.Reference) DOMSignContext(javax.xml.crypto.dsig.dom.DOMSignContext) SignedInfo(javax.xml.crypto.dsig.SignedInfo) C14NMethodParameterSpec(javax.xml.crypto.dsig.spec.C14NMethodParameterSpec)

Example 5 with C14NMethodParameterSpec

use of javax.xml.crypto.dsig.spec.C14NMethodParameterSpec in project cxf by apache.

the class MetadataWriter method signMetaInfo.

private static Document signMetaInfo(X509Certificate signingCert, Key signingKey, Document doc, String referenceID) throws Exception {
    final String signatureMethod;
    if ("SHA1withDSA".equals(signingCert.getSigAlgName())) {
        signatureMethod = SignatureMethod.DSA_SHA1;
    } else if ("SHA1withRSA".equals(signingCert.getSigAlgName())) {
        signatureMethod = SignatureMethod.RSA_SHA1;
    } else if ("SHA256withRSA".equals(signingCert.getSigAlgName())) {
        signatureMethod = SignatureMethod.RSA_SHA1;
    } else {
        LOG.error("Unsupported signature method: " + signingCert.getSigAlgName());
        throw new RuntimeException("Unsupported signature method: " + signingCert.getSigAlgName());
    }
    List<Transform> transformList = Arrays.asList(XML_SIGNATURE_FACTORY.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null), XML_SIGNATURE_FACTORY.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null));
    // Create a Reference to the enveloped document (in this case,
    // you are signing the whole document, so a URI of "" signifies
    // that, and also specify the SHA1 digest algorithm and
    // the ENVELOPED Transform.
    Reference ref = XML_SIGNATURE_FACTORY.newReference("#" + referenceID, XML_SIGNATURE_FACTORY.newDigestMethod(DigestMethod.SHA1, null), transformList, null, null);
    // Create the SignedInfo.
    SignedInfo si = XML_SIGNATURE_FACTORY.newSignedInfo(XML_SIGNATURE_FACTORY.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null), XML_SIGNATURE_FACTORY.newSignatureMethod(signatureMethod, null), Collections.singletonList(ref));
    // Create the KeyInfo containing the X509Data.
    KeyInfoFactory kif = XML_SIGNATURE_FACTORY.getKeyInfoFactory();
    List<Object> x509Content = Arrays.asList(signingCert.getSubjectX500Principal().getName(), signingCert);
    X509Data xd = kif.newX509Data(x509Content);
    KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
    // Create a DOMSignContext and specify the RSA PrivateKey and
    // location of the resulting XMLSignature's parent element.
    // DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), doc.getDocumentElement());
    DOMSignContext dsc = new DOMSignContext(signingKey, doc.getDocumentElement());
    dsc.setIdAttributeNS(doc.getDocumentElement(), null, "ID");
    dsc.setNextSibling(doc.getDocumentElement().getFirstChild());
    // Create the XMLSignature, but don't sign it yet.
    XMLSignature signature = XML_SIGNATURE_FACTORY.newXMLSignature(si, ki);
    // Marshal, generate, and sign the enveloped signature.
    signature.sign(dsc);
    // Output the resulting document.
    return doc;
}
Also used : Reference(javax.xml.crypto.dsig.Reference) X509Data(javax.xml.crypto.dsig.keyinfo.X509Data) SignedInfo(javax.xml.crypto.dsig.SignedInfo) KeyInfoFactory(javax.xml.crypto.dsig.keyinfo.KeyInfoFactory) KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) DOMSignContext(javax.xml.crypto.dsig.dom.DOMSignContext) XMLSignature(javax.xml.crypto.dsig.XMLSignature) TransformParameterSpec(javax.xml.crypto.dsig.spec.TransformParameterSpec) Transform(javax.xml.crypto.dsig.Transform) C14NMethodParameterSpec(javax.xml.crypto.dsig.spec.C14NMethodParameterSpec)

Aggregations

C14NMethodParameterSpec (javax.xml.crypto.dsig.spec.C14NMethodParameterSpec)7 DOMSignContext (javax.xml.crypto.dsig.dom.DOMSignContext)5 Reference (javax.xml.crypto.dsig.Reference)4 SignedInfo (javax.xml.crypto.dsig.SignedInfo)4 XMLSignature (javax.xml.crypto.dsig.XMLSignature)3 Transform (javax.xml.crypto.dsig.Transform)2 XMLSignatureFactory (javax.xml.crypto.dsig.XMLSignatureFactory)2 DOMValidateContext (javax.xml.crypto.dsig.dom.DOMValidateContext)2 KeyInfo (javax.xml.crypto.dsig.keyinfo.KeyInfo)2 KeyInfoFactory (javax.xml.crypto.dsig.keyinfo.KeyInfoFactory)2 X509Data (javax.xml.crypto.dsig.keyinfo.X509Data)2 TransformParameterSpec (javax.xml.crypto.dsig.spec.TransformParameterSpec)2 Document (org.w3c.dom.Document)2 InputStream (java.io.InputStream)1 Key (java.security.Key)1 KeyStore (java.security.KeyStore)1 CertificateExpiredException (java.security.cert.CertificateExpiredException)1 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)1 X509Certificate (java.security.cert.X509Certificate)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1