Search in sources :

Example 6 with SignatureAlgorithm

use of org.apache.xml.security.algorithms.SignatureAlgorithm in project santuario-java by apache.

the class CreateSignatureTest method doSign.

private String doSign() throws Exception {
    PrivateKey privateKey = kp.getPrivate();
    Document doc = db.newDocument();
    doc.appendChild(doc.createComment(" Comment before "));
    Element root = doc.createElementNS("", "RootElement");
    doc.appendChild(root);
    root.appendChild(doc.createTextNode("Some simple text\n"));
    Element canonElem = XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_CANONICALIZATIONMETHOD);
    canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
    SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
    XMLSignature sig = new XMLSignature(doc, null, signatureAlgorithm.getElement(), canonElem);
    root.appendChild(sig.getElement());
    doc.appendChild(doc.createComment(" Comment after "));
    Transforms transforms = new Transforms(doc);
    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
    sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
    sig.addKeyInfo(kp.getPublic());
    sig.sign(privateKey);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLUtils.outputDOMc14nWithComments(doc, bos);
    return new String(bos.toByteArray());
}
Also used : PrivateKey(java.security.PrivateKey) XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) SignatureAlgorithm(org.apache.xml.security.algorithms.SignatureAlgorithm) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document)

Example 7 with SignatureAlgorithm

use of org.apache.xml.security.algorithms.SignatureAlgorithm in project santuario-java by apache.

the class CreateSignatureTest method doSignWithCert.

private String doSignWithCert() throws Exception {
    KeyStore ks = KeyStore.getInstance("JKS");
    FileInputStream fis = null;
    if (BASEDIR != null && !"".equals(BASEDIR)) {
        fis = new FileInputStream(BASEDIR + SEP + "src/test/resources/test.jks");
    } else {
        fis = new FileInputStream("src/test/resources/test.jks");
    }
    ks.load(fis, "changeit".toCharArray());
    PrivateKey privateKey = (PrivateKey) ks.getKey("mullan", "changeit".toCharArray());
    Document doc = db.newDocument();
    X509Certificate signingCert = (X509Certificate) ks.getCertificate("mullan");
    doc.appendChild(doc.createComment(" Comment before "));
    Element root = doc.createElementNS("", "RootElement");
    doc.appendChild(root);
    root.appendChild(doc.createTextNode("Some simple text\n"));
    Element canonElem = XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_CANONICALIZATIONMETHOD);
    canonElem.setAttributeNS(null, Constants._ATT_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
    SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_DSA);
    XMLSignature sig = new XMLSignature(doc, null, signatureAlgorithm.getElement(), canonElem);
    root.appendChild(sig.getElement());
    doc.appendChild(doc.createComment(" Comment after "));
    Transforms transforms = new Transforms(doc);
    transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
    transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
    sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
    sig.addKeyInfo(signingCert);
    sig.sign(privateKey);
    X509Certificate cert = sig.getKeyInfo().getX509Certificate();
    sig.checkSignatureValue(cert.getPublicKey());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLUtils.outputDOMc14nWithComments(doc, bos);
    return new String(bos.toByteArray());
}
Also used : PrivateKey(java.security.PrivateKey) XMLSignature(org.apache.xml.security.signature.XMLSignature) Element(org.w3c.dom.Element) Transforms(org.apache.xml.security.transforms.Transforms) SignatureAlgorithm(org.apache.xml.security.algorithms.SignatureAlgorithm) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate)

Example 8 with SignatureAlgorithm

use of org.apache.xml.security.algorithms.SignatureAlgorithm in project santuario-java by apache.

the class SignatureAlgorithmTest method testSameKeySeveralAlgorithmSigning.

@org.junit.Test
public void testSameKeySeveralAlgorithmSigning() throws Exception {
    Document doc = XMLUtils.createDocumentBuilder(false).newDocument();
    SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
    PrivateKey pk = KeyPairGenerator.getInstance("RSA").genKeyPair().getPrivate();
    signatureAlgorithm.initSign(pk);
    signatureAlgorithm.update((byte) 2);
    signatureAlgorithm.sign();
    SignatureAlgorithm otherSignatureAlgorithm = new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256);
    try {
        otherSignatureAlgorithm.initSign(pk);
    } catch (XMLSecurityException ex) {
        LOG.warn("Test testSameKeySeveralAlgorithmSigning skipped as necessary algorithms " + "not available");
        return;
    }
    otherSignatureAlgorithm.update((byte) 2);
    otherSignatureAlgorithm.sign();
}
Also used : PrivateKey(java.security.PrivateKey) SignatureAlgorithm(org.apache.xml.security.algorithms.SignatureAlgorithm) Document(org.w3c.dom.Document) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Aggregations

SignatureAlgorithm (org.apache.xml.security.algorithms.SignatureAlgorithm)8 PrivateKey (java.security.PrivateKey)5 XMLSignature (org.apache.xml.security.signature.XMLSignature)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Transforms (org.apache.xml.security.transforms.Transforms)4 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 PublicKey (java.security.PublicKey)2 X509Certificate (java.security.cert.X509Certificate)2 SignerOutputStream (org.apache.xml.security.utils.SignerOutputStream)2 UnsyncBufferedOutputStream (org.apache.xml.security.utils.UnsyncBufferedOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 KeyStore (java.security.KeyStore)1 XPath (javax.xml.xpath.XPath)1 XPathFactory (javax.xml.xpath.XPathFactory)1