jdk/test/javax/xml/crypto/dsig/GenerationTests.java
author tbell
Thu, 23 Apr 2009 21:32:44 -0700
changeset 2636 3d0e25588136
parent 1639 a97859015238
child 3462 4477fb399895
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1337
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4635230
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 6283345
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @bug 6303830
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary Basic unit tests for generating XML Signatures with JSR 105
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *     X509KeySelector.java GenerationTests.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main GenerationTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.spec.DSAPrivateKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.spec.DSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.spec.RSAPrivateKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.spec.RSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.xml.XMLConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.xml.parsers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import org.w3c.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.xml.crypto.Data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.xml.crypto.KeySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.xml.crypto.OctetStreamData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.xml.crypto.URIDereferencer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.xml.crypto.URIReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.xml.crypto.URIReferenceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.xml.crypto.XMLCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.xml.crypto.XMLStructure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.xml.crypto.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.xml.crypto.dsig.dom.DOMSignContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.xml.crypto.dsig.dom.DOMValidateContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.xml.crypto.dsig.keyinfo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.xml.crypto.dsig.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.xml.transform.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.xml.transform.dom.DOMSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.xml.transform.stream.StreamResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Test that recreates merlin-xmldsig-twenty-three test vectors but with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * different keys and X.509 data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public class GenerationTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static XMLSignatureFactory fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static KeyInfoFactory kifac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static DocumentBuilder db;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static CanonicalizationMethod withoutComments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static SignatureMethod dsaSha1, rsaSha1, rsaSha256, rsaSha384, rsaSha512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static DigestMethod sha1, sha256, sha384, sha512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static KeyInfo dsa, rsa, rsa1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static KeySelector kvks = new KeySelectors.KeyValueKeySelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static KeySelector sks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static Key signingKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static PublicKey validatingKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static Certificate signingCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private final static String DIR = System.getProperty("test.src", ".");
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    95
//    private final static String DIR = ".";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final static String DATA_DIR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        DIR + System.getProperty("file.separator") + "data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private final static String KEYSTORE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        DATA_DIR + System.getProperty("file.separator") + "certs" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.getProperty("file.separator") + "test.jks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final static String CRL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        DATA_DIR + System.getProperty("file.separator") + "certs" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.getProperty("file.separator") + "crl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private final static String ENVELOPE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        DATA_DIR + System.getProperty("file.separator") + "envelope.xml";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static URIDereferencer httpUd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private final static String STYLESHEET =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        "http://www.w3.org/TR/xml-stylesheet";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private final static String STYLESHEET_B64 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        "http://www.w3.org/Signature/2002/04/xml-stylesheet.b64";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        test_create_signature_enveloped_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        test_create_signature_enveloping_b64_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        test_create_signature_enveloping_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        test_create_signature_enveloping_hmac_sha1_40();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        test_create_signature_enveloping_hmac_sha256();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        test_create_signature_enveloping_hmac_sha384();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        test_create_signature_enveloping_hmac_sha512();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        test_create_signature_enveloping_rsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        test_create_signature_external_b64_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        test_create_signature_external_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        test_create_signature_keyname();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        test_create_signature_retrievalmethod_rawx509crt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        test_create_signature_x509_crt_crl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        test_create_signature_x509_crt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        test_create_signature_x509_is();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        test_create_signature_x509_ski();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        test_create_signature_x509_sn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
//      test_create_signature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        test_create_exc_signature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        test_create_sign_spec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        test_create_signature_enveloping_sha256_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        test_create_signature_enveloping_sha384_rsa_sha256();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        test_create_signature_enveloping_sha512_rsa_sha384();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        test_create_signature_enveloping_sha512_rsa_sha512();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static void setup() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        fac = XMLSignatureFactory.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        kifac = fac.getKeyInfoFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        dbf.setNamespaceAware(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // get key & self-signed certificate from keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        FileInputStream fis = new FileInputStream(KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ks.load(fis, "changeit".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        signingKey = ks.getKey("user", "changeit".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        signingCert = ks.getCertificate("user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        validatingKey = signingCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // create common objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        withoutComments = fac.newCanonicalizationMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        dsaSha1 = fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        sha1 = fac.newDigestMethod(DigestMethod.SHA1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        sha256 = fac.newDigestMethod(DigestMethod.SHA256, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        sha384 = fac.newDigestMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            ("http://www.w3.org/2001/04/xmldsig-more#sha384", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        sha512 = fac.newDigestMethod(DigestMethod.SHA512, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        dsa = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            (kifac.newKeyValue(validatingKey)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        rsa = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            (kifac.newKeyValue(getPublicKey("RSA"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        rsa1024 = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            (kifac.newKeyValue(getPublicKey("RSA", 1024))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        rsaSha1 = fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        rsaSha256 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        rsaSha384 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        rsaSha512 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        sks = new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        httpUd = new HttpURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static void test_create_signature_enveloped_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        System.out.println("* Generating signature-enveloped-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        SignedInfo si = fac.newSignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            (withoutComments, dsaSha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                (fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    ("", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        (fac.newTransform(Transform.ENVELOPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                 null, null)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        XMLSignature sig = fac.newXMLSignature(si, dsa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Element envelope = doc.createElementNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            ("http://example.org/envelope", "Envelope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        envelope.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            "xmlns", "http://example.org/envelope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        doc.appendChild(envelope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        DOMSignContext dsc = new DOMSignContext(signingKey, envelope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        sig.sign(dsc);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   206
//      StringWriter sw = new StringWriter();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   207
//      dumpDocument(doc, sw);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   208
//      System.out.println(sw.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            (kvks, envelope.getFirstChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static void test_create_signature_enveloping_b64_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        System.out.println("* Generating signature-enveloping-b64-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        test_create_signature_enveloping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            (sha1, dsaSha1, dsa, signingKey, kvks, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static void test_create_signature_enveloping_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        System.out.println("* Generating signature-enveloping-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        test_create_signature_enveloping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            (sha1, dsaSha1, dsa, signingKey, kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    static void test_create_signature_enveloping_sha256_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        System.out.println("* Generating signature-enveloping-sha256-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        test_create_signature_enveloping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            (sha256, dsaSha1, dsa, signingKey, kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    static void test_create_signature_enveloping_hmac_sha1_40()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        System.out.println("* Generating signature-enveloping-hmac-sha1-40.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        SignatureMethod hmacSha1 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            (SignatureMethod.HMAC_SHA1, new HMACParameterSpec(40));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        test_create_signature_enveloping(sha1, hmacSha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static void test_create_signature_enveloping_hmac_sha256()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        System.out.println("* Generating signature-enveloping-hmac-sha256.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        SignatureMethod hmacSha256 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        test_create_signature_enveloping(sha1, hmacSha256, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    static void test_create_signature_enveloping_hmac_sha384()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        System.out.println("* Generating signature-enveloping-hmac-sha384.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        SignatureMethod hmacSha384 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        test_create_signature_enveloping(sha1, hmacSha384, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    static void test_create_signature_enveloping_hmac_sha512()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        System.out.println("* Generating signature-enveloping-hmac-sha512.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        SignatureMethod hmacSha512 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        test_create_signature_enveloping(sha1, hmacSha512, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    static void test_create_signature_enveloping_rsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        System.out.println("* Generating signature-enveloping-rsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        test_create_signature_enveloping(sha1, rsaSha1, rsa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            getPrivateKey("RSA"), kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    static void test_create_signature_enveloping_sha384_rsa_sha256()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        System.out.println("* Generating signature-enveloping-sha384-rsa_sha256.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        test_create_signature_enveloping(sha384, rsaSha256, rsa,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            getPrivateKey("RSA"), kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    static void test_create_signature_enveloping_sha512_rsa_sha384()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        System.out.println("* Generating signature-enveloping-sha512-rsa_sha384.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        test_create_signature_enveloping(sha512, rsaSha384, rsa1024,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            getPrivateKey("RSA", 1024), kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    static void test_create_signature_enveloping_sha512_rsa_sha512()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        System.out.println("* Generating signature-enveloping-sha512-rsa_sha512.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        test_create_signature_enveloping(sha512, rsaSha512, rsa1024,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            getPrivateKey("RSA", 1024), kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    static void test_create_signature_external_b64_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        System.out.println("* Generating signature-external-b64-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        test_create_signature_external(dsaSha1, dsa, signingKey, kvks, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static void test_create_signature_external_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        System.out.println("* Generating signature-external-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        test_create_signature_external(dsaSha1, dsa, signingKey, kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static void test_create_signature_keyname() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        System.out.println("* Generating signature-keyname.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        KeyInfo kn = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            (kifac.newKeyName("user")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        test_create_signature_external(dsaSha1, kn, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    static void test_create_signature_retrievalmethod_rawx509crt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            "* Generating signature-retrievalmethod-rawx509crt.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        KeyInfo rm = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            (kifac.newRetrievalMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ("certs/user.crt", X509Data.RAW_X509_CERTIFICATE_TYPE, null)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        test_create_signature_external(dsaSha1, rm, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static void test_create_signature_x509_crt_crl() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        System.out.println("* Generating signature-x509-crt-crl.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        List<Object> xds = new ArrayList<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        xds.add(signingCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        FileInputStream fis = new FileInputStream(CRL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        X509CRL crl = (X509CRL) cf.generateCRL(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        xds.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        KeyInfo crt_crl = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            (kifac.newX509Data(xds)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        test_create_signature_external(dsaSha1, crt_crl, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    static void test_create_signature_x509_crt() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        System.out.println("* Generating signature-x509-crt.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        KeyInfo crt = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            (kifac.newX509Data(Collections.singletonList(signingCert))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        test_create_signature_external(dsaSha1, crt, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    static void test_create_signature_x509_is() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        System.out.println("* Generating signature-x509-is.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        KeyInfo is = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            (kifac.newX509Data(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            (kifac.newX509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            ("CN=User", new BigInteger("45ef2729", 16))))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        test_create_signature_external(dsaSha1, is, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    static void test_create_signature_x509_ski() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        System.out.println("* Generating signature-x509-ski.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        KeyInfo ski = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            (kifac.newX509Data(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            ("keyid".getBytes("ASCII")))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        test_create_signature_external(dsaSha1, ski, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            KeySelector.singletonKeySelector(validatingKey), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    static void test_create_signature_x509_sn() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        System.out.println("* Generating signature-x509-sn.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        KeyInfo sn = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            (kifac.newX509Data(Collections.singletonList("CN=User"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        test_create_signature_external(dsaSha1, sn, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    static void test_create_signature() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        System.out.println("* Generating signature.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // create references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        List<Reference> refs = new ArrayList<Reference>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // Reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        refs.add(fac.newReference(STYLESHEET, sha1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // Reference 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            (STYLESHEET_B64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            (fac.newTransform(Transform.BASE64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                (TransformParameterSpec) null)), null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // Reference 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        refs.add(fac.newReference("#object-1", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            (fac.newTransform(Transform.XPATH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            new XPathFilterParameterSpec("self::text()"))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // Reference 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        String expr = "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
          + " ancestor-or-self::dsig:SignedInfo                  " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
          + "  and                                               " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
          + " count(ancestor-or-self::dsig:Reference |           " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
          + "      here()/ancestor::dsig:Reference[1]) >         " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
          + " count(ancestor-or-self::dsig:Reference)            " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
          + "  or                                                " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
          + " count(ancestor-or-self::node() |                   " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
          + "      id('notaries')) =                             " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
          + " count(ancestor-or-self::node())                    " + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        XPathFilterParameterSpec xfp = new XPathFilterParameterSpec(expr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            Collections.singletonMap("dsig", XMLSignature.XMLNS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        refs.add(fac.newReference("", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            (fac.newTransform(Transform.XPATH, xfp)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // Reference 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        refs.add(fac.newReference("#object-2", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            (fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                (Transform.BASE64, (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Reference 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            ("#manifest-1", sha1, null, Manifest.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // Reference 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        refs.add(fac.newReference("#signature-properties-1", sha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            SignatureProperties.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // Reference 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        List<Transform> transforms = new ArrayList<Transform>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            (Transform.ENVELOPED, (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        refs.add(fac.newReference("", sha1, transforms, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // Reference 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        refs.add(fac.newReference("", sha1, transforms, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // Reference 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        Transform env = fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            (Transform.ENVELOPED, (TransformParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        refs.add(fac.newReference("#xpointer(/)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            sha1, Collections.singletonList(env), null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // Reference 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        transforms.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            (Transform.ENVELOPED, (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
             (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        refs.add(fac.newReference("#xpointer(/)", sha1, transforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // Reference 12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        refs.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            (fac.newReference("#object-3", sha1, null, XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // Reference 13
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        Transform withComments = fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
             (TransformParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        refs.add(fac.newReference("#object-3", sha1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            Collections.singletonList(withComments), XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // Reference 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        refs.add(fac.newReference("#xpointer(id('object-3'))", sha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        // Reference 15
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        withComments = fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             (TransformParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        refs.add(fac.newReference("#xpointer(id('object-3'))", sha1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            Collections.singletonList(withComments), XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // Reference 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        refs.add(fac.newReference("#reference-2", sha1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Reference 17
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        refs.add(fac.newReference("#manifest-reference-1", sha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            null, "reference-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        // Reference 18
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        refs.add(fac.newReference("#reference-1", sha1, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            "reference-2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        SignedInfo si = fac.newSignedInfo(withoutComments, dsaSha1, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // create keyinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        XPathFilterParameterSpec xpf = new XPathFilterParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            "ancestor-or-self::dsig:X509Data",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            Collections.singletonMap("dsig", XMLSignature.XMLNS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        RetrievalMethod rm = kifac.newRetrievalMethod("#object-4",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            X509Data.TYPE, Collections.singletonList(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            (Transform.XPATH, xpf)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        KeyInfo ki = kifac.newKeyInfo(Collections.singletonList(rm), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // create objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        List<XMLStructure> objs = new ArrayList<XMLStructure>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // Object 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            (new DOMStructure(doc.createTextNode("I am the text."))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            "object-1", "text/plain", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // Object 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            (new DOMStructure(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            "object-2", "text/plain", Transform.BASE64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // Object 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        Element nc = doc.createElementNS(null, "NonCommentandus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        nc.appendChild(doc.createComment(" Commentandum "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            (new DOMStructure(nc)), "object-3", null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        // Manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        List<Reference> manRefs = new ArrayList<Reference>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        // Manifest Reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        manRefs.add(fac.newReference(STYLESHEET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            sha1, null, null, "manifest-reference-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // Manifest Reference 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        manRefs.add(fac.newReference("#reference-1", sha1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // Manifest Reference 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        List<Transform> manTrans = new ArrayList<Transform>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        String xslt = ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
          + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
          + "            xmlns='http://www.w3.org/TR/xhtml1/strict' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
          + "            exclude-result-prefixes='foo' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
          + "            version='1.0'>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
          + "  <xsl:output encoding='UTF-8' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
          + "           indent='no' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
          + "           method='xml' />\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
          + "  <xsl:template match='/'>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
          + "    <html>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
          + "   <head>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
          + "    <title>Notaries</title>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
          + "   </head>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
          + "   <body>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
          + "    <table>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
          + "      <xsl:for-each select='Notaries/Notary'>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
          + "           <tr>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
          + "           <th>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
          + "            <xsl:value-of select='@name' />\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
          + "           </th>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
          + "           </tr>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
          + "      </xsl:for-each>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
          + "    </table>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
          + "   </body>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
          + "    </html>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
          + "  </xsl:template>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
          + "</xsl:stylesheet>\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        Document docxslt = db.parse(new ByteArrayInputStream(xslt.getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        Node xslElem = docxslt.getDocumentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        manTrans.add(fac.newTransform(Transform.XSLT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            new XSLTTransformParameterSpec(new DOMStructure(xslElem))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        manTrans.add(fac.newTransform(CanonicalizationMethod.INCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        manRefs.add(fac.newReference("#notaries", sha1, manTrans, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            (fac.newManifest(manRefs, "manifest-1")), null, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // SignatureProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        Element sa = doc.createElementNS("urn:demo", "SignerAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        sa.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:demo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        Element ip = doc.createElementNS("urn:demo", "IP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        ip.appendChild(doc.createTextNode("192.168.21.138"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        sa.appendChild(ip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        SignatureProperty sp = fac.newSignatureProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            (Collections.singletonList(new DOMStructure(sa)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            "#signature", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        SignatureProperties sps = fac.newSignatureProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            (Collections.singletonList(sp), "signature-properties-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        objs.add(fac.newXMLObject(Collections.singletonList(sps), null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        // Object 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        List<Object> xds = new ArrayList<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        xds.add("CN=User");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        xds.add(kifac.newX509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            ("CN=User", new BigInteger("45ef2729", 16)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        xds.add(signingCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            (kifac.newX509Data(xds)), "object-4", null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        XMLSignature sig = fac.newXMLSignature(si, ki, objs, "signature", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        dbf.setNamespaceAware(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        dbf.setValidating(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        Document envDoc = dbf.newDocumentBuilder().parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            (new FileInputStream(ENVELOPE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        Element ys = (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            envDoc.getElementsByTagName("YoursSincerely").item(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        DOMSignContext dsc = new DOMSignContext(signingKey, ys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
//      StringWriter sw = new StringWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
//        dumpDocument(envDoc, sw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        NodeList nl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            envDoc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (nl.getLength() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            throw new Exception("Couldn't find signature Element");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        Element sigElement = (Element) nl.item(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            (new X509KeySelector(ks), sigElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        File f = new File(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            System.getProperty("dir.test.vector.baltimore") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            System.getProperty("file.separator") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            "merlin-xmldsig-twenty-three" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            System.getProperty("file.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        dvc.setBaseURI(f.toURI().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    private static void dumpDocument(Document doc, Writer w) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        TransformerFactory tf = TransformerFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        Transformer trans = tf.newTransformer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
//      trans.setOutputProperty(OutputKeys.INDENT, "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        trans.transform(new DOMSource(doc), new StreamResult(w));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    private static void test_create_signature_external
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        boolean b64) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        // create reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        Reference ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (b64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            ref = fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                (STYLESHEET_B64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                (fac.newTransform(Transform.BASE64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                 (TransformParameterSpec) null)), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            ref = fac.newReference(STYLESHEET, sha1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        SignedInfo si = fac.newSignedInfo(withoutComments, sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            Collections.singletonList(ref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        XMLSignature sig = fac.newXMLSignature(si, ki);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        DOMSignContext dsc = new DOMSignContext(signingKey, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        dsc.setURIDereferencer(httpUd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            (ks, doc.getDocumentElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        File f = new File(DATA_DIR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        dvc.setBaseURI(f.toURI().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        dvc.setURIDereferencer(httpUd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    private static void test_create_signature_enveloping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        (DigestMethod dm, SignatureMethod sm, KeyInfo ki, Key signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         KeySelector ks, boolean b64) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // create reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        Reference ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (b64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            ref = fac.newReference("#object", dm, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                (fac.newTransform(Transform.BASE64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                 (TransformParameterSpec) null)), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            ref = fac.newReference("#object", dm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        SignedInfo si = fac.newSignedInfo(withoutComments, sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            Collections.singletonList(ref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        // create Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        String text = b64 ? "c29tZSB0ZXh0" : "some text";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        XMLObject obj = fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            (new DOMStructure(doc.createTextNode(text))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            "object", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        XMLSignature sig = fac.newXMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            (si, ki, Collections.singletonList(obj), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        DOMSignContext dsc = new DOMSignContext(signingKey, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
//        dumpDocument(doc, new FileWriter("/tmp/foo.xml"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            (ks, doc.getDocumentElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    static void test_create_exc_signature() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        System.out.println("* Generating exc_signature.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        List<Reference> refs = new ArrayList<Reference>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // create reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
             Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                (fac.newTransform(CanonicalizationMethod.EXCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                 (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        // create reference 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        List<String> prefixList = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        prefixList.add("bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        prefixList.add("#default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
             Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, params)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // create reference 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
             Collections.singletonList(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                 (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // create reference 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        prefixList = new ArrayList<String>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        prefixList.add("bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        prefixList.add("#default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        params = new ExcC14NParameterSpec(prefixList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
             Collections.singletonList(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, params)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        SignedInfo si = fac.newSignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            fac.newCanonicalizationMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                (CanonicalizationMethod.EXCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                 (C14NMethodParameterSpec) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // create KeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        List<XMLStructure> kits = new ArrayList<XMLStructure>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        kits.add(kifac.newKeyValue(validatingKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        KeyInfo ki = kifac.newKeyInfo(kits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        // create Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        Element baz = doc.createElementNS("urn:bar", "bar:Baz");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        Comment com = doc.createComment(" comment ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        baz.appendChild(com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        XMLObject obj = fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            (new DOMStructure(baz)), "to-be-signed", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        XMLSignature sig = fac.newXMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            (si, ki, Collections.singletonList(obj), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        Element foo = doc.createElementNS("urn:foo", "Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        foo.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        foo.setAttributeNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            ("http://www.w3.org/2000/xmlns/", "xmlns:bar", "urn:bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        doc.appendChild(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        DOMSignContext dsc = new DOMSignContext(signingKey, foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        dsc.putNamespacePrefix(XMLSignature.XMLNS, "dsig");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
//      dumpDocument(doc, new FileWriter("/tmp/foo.xml"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            (new KeySelectors.KeyValueKeySelector(), foo.getLastChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    static void test_create_sign_spec() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        System.out.println("* Generating sign-spec.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        List<Reference> refs = new ArrayList<Reference>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        // create reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        List<XPathType> types = new ArrayList<XPathType>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        types.add(new XPathType(" //NotToBeSigned ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            XPathType.Filter.SUBTRACT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        types.add(new XPathType(" //ReallyToBeSigned ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            XPathType.Filter.UNION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        XPathFilter2ParameterSpec xp1 = new XPathFilter2ParameterSpec(types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            ("", fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
             Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // create reference 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        List<Transform> trans2 = new ArrayList<Transform>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        trans2.add(fac.newTransform(Transform.ENVELOPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            (Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                (new XPathType(" / ", XPathType.Filter.UNION)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        trans2.add(fac.newTransform(Transform.XPATH2, xp2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        refs.add(fac.newReference("#signature-value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            fac.newDigestMethod(DigestMethod.SHA1, null), trans2, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        SignedInfo si = fac.newSignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            fac.newCanonicalizationMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                (CanonicalizationMethod.INCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                 (C14NMethodParameterSpec) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // create KeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        List<XMLStructure> kits = new ArrayList<XMLStructure>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        kits.add(kifac.newKeyValue(validatingKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        List<Object> xds = new ArrayList<Object>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        xds.add("CN=User");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        xds.add(signingCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        kits.add(kifac.newX509Data(xds));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        KeyInfo ki = kifac.newKeyInfo(kits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        XMLSignature sig = fac.newXMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            (si, ki, null, null, "signature-value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        Element tbs1 = doc.createElementNS(null, "ToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        Comment tbs1Com = doc.createComment(" comment ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        Element tbs1Data = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        Element tbs1ntbs = doc.createElementNS(null, "NotToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        Element tbs1rtbs = doc.createElementNS(null, "ReallyToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        Comment tbs1rtbsCom = doc.createComment(" comment ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        Element tbs1rtbsData = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        tbs1rtbs.appendChild(tbs1rtbsCom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        tbs1rtbs.appendChild(tbs1rtbsData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        tbs1ntbs.appendChild(tbs1rtbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        tbs1.appendChild(tbs1Com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        tbs1.appendChild(tbs1Data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        tbs1.appendChild(tbs1ntbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        Element tbs2 = doc.createElementNS(null, "ToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        Element tbs2Data = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        Element tbs2ntbs = doc.createElementNS(null, "NotToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        Element tbs2ntbsData = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        tbs2ntbs.appendChild(tbs2ntbsData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        tbs2.appendChild(tbs2Data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        tbs2.appendChild(tbs2ntbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        Element document = doc.createElementNS(null, "Document");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        document.appendChild(tbs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        document.appendChild(tbs2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        doc.appendChild(document);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        DOMSignContext dsc = new DOMSignContext(signingKey, document);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
//      dumpDocument(doc, new FileWriter("/tmp/foo.xml"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            (new KeySelectors.KeyValueKeySelector(), document.getLastChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    private static final String DSA_Y =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        "070662842167565771936588335128634396171789331656318483584455493822" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        "400811200853331373030669235424928346190274044631949560438023934623" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        "71310375123430985057160";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    private static final String DSA_P =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        "013232376895198612407547930718267435757728527029623408872245156039" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        "757713029036368719146452186041204237350521785240337048752071462798" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        "273003935646236777459223";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    private static final String DSA_Q =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        "0857393771208094202104259627990318636601332086981";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    private static final String DSA_G =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        "054216440574364751416096484883257051280474283943804743768346673007" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        "661082626139005426812890807137245973106730741193551360857959820973" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        "90670890367185141189796";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    private static final String DSA_X =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        "0527140396812450214498055937934275626078768840117";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    private static final String RSA_MOD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        "010800185049102889923150759252557522305032794699952150943573164381" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        "936603255999071981574575044810461362008102247767482738822150129277" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        "490998033971789476107463";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    private static final String RSA_PRIV =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        "016116973584421969795445996229612671947635798429212816611707210835" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        "915586591340598683996088487065438751488342251960069575392056288063" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        "6800379454345804879553";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    private static final String RSA_PUB = "065537";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    private static final String RSA_1024_MOD = "098871307553789439961130765" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        "909423744508062468450669519128736624058048856940468016843888594585" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        "322862378444314635412341974900625010364163960238734457710620107530" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        "573945081856371709138380902553309075505688814637544923038853658690" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        "857672483016239697038853418682988686871489963827000080098971762923" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        "833614557257607521";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    private static final String RSA_1024_PRIV = "03682574144968491431483287" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        "297021581096848810374110568017963075809477047466189822987258068867" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        "704855380407747867998863645890602646601140183818953428006646987710" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        "237008997971129772408397621801631622129297063463868593083106979716" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        "204903524890556839550490384015324575598723478554854070823335021842" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        "210112348400928769";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    private static PublicKey getPublicKey(String algo) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        return getPublicKey(algo, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    private static PublicKey getPublicKey(String algo, int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        KeyFactory kf = KeyFactory.getInstance(algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        KeySpec kspec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (algo.equalsIgnoreCase("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                                         new BigInteger(DSA_P),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                                         new BigInteger(DSA_Q),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                                         new BigInteger(DSA_G));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        } else if (algo.equalsIgnoreCase("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            if (keysize == 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                                             new BigInteger(RSA_PUB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                                             new BigInteger(RSA_PUB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        } else throw new RuntimeException("Unsupported key algorithm " + algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        return kf.generatePublic(kspec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    private static PrivateKey getPrivateKey(String algo) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        return getPrivateKey(algo, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    private static PrivateKey getPrivateKey(String algo, int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        KeyFactory kf = KeyFactory.getInstance(algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        KeySpec kspec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        if (algo.equalsIgnoreCase("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            kspec = new DSAPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                (new BigInteger(DSA_X), new BigInteger(DSA_P),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                 new BigInteger(DSA_Q), new BigInteger(DSA_G));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        } else if (algo.equalsIgnoreCase("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (keysize == 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                kspec = new RSAPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                    (new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                                              new BigInteger(RSA_1024_PRIV));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        } else throw new RuntimeException("Unsupported key algorithm " + algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        return kf.generatePrivate(kspec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    private static SecretKey getSecretKey(final byte[] secret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        return new SecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            public String getFormat()   { return "RAW"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            public byte[] getEncoded()  { return secret; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            public String getAlgorithm(){ return "SECRET"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * This URIDereferencer returns locally cached copies of http content to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * avoid test failures due to network glitches, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    private static class HttpURIDereferencer implements URIDereferencer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        private URIDereferencer defaultUd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        HttpURIDereferencer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        public Data dereference(final URIReference ref, XMLCryptoContext ctx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        throws URIReferenceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            String uri = ref.getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    FileInputStream fis = new FileInputStream(new File
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                        (DATA_DIR, uri.substring(uri.lastIndexOf('/'))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    return new OctetStreamData(fis,ref.getURI(),ref.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                } catch (Exception e) { throw new URIReferenceException(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            // fallback on builtin deref
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            return defaultUd.dereference(ref, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
}