jdk/test/javax/xml/crypto/dsig/GenerationTests.java
author juh
Fri, 09 Jan 2015 11:58:34 -0800
changeset 28308 5fdc6e6c0b97
parent 27747 3a271dc8b758
child 30690 f6d8413a278c
permissions -rw-r--r--
8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4506
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4506
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4506
diff changeset
    21
 * questions.
2
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
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
    26
 * @bug 4635230 6283345 6303830 6824440 6867348 7094155 8038184 8038349 8046949
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    27
 *      8046724
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Basic unit tests for generating XML Signatures with JSR 105
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *     X509KeySelector.java GenerationTests.java
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10888
diff changeset
    31
 * @run main/othervm GenerationTests
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.spec.DSAPrivateKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.spec.DSAPublicKeySpec;
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    49
import java.security.spec.ECField;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    50
import java.security.spec.ECFieldFp;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    51
import java.security.spec.ECParameterSpec;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    52
import java.security.spec.ECPoint;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    53
import java.security.spec.ECPrivateKeySpec;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    54
import java.security.spec.ECPublicKeySpec;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    55
import java.security.spec.EllipticCurve;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.security.spec.RSAPrivateKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.security.spec.RSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.xml.XMLConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.xml.parsers.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import org.w3c.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.xml.crypto.Data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.xml.crypto.KeySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.xml.crypto.OctetStreamData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.xml.crypto.URIDereferencer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.xml.crypto.URIReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import javax.xml.crypto.URIReferenceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.xml.crypto.XMLCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.xml.crypto.XMLStructure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.xml.crypto.dom.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import javax.xml.crypto.dsig.dom.DOMSignContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import javax.xml.crypto.dsig.dom.DOMValidateContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import javax.xml.crypto.dsig.keyinfo.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import javax.xml.crypto.dsig.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import javax.xml.transform.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.xml.transform.dom.DOMSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import javax.xml.transform.stream.StreamResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Test that recreates merlin-xmldsig-twenty-three test vectors but with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * different keys and X.509 data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class GenerationTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static XMLSignatureFactory fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static KeyInfoFactory kifac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static DocumentBuilder db;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static CanonicalizationMethod withoutComments;
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
    91
    private static SignatureMethod dsaSha1, dsaSha256, rsaSha1,
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    92
                                   rsaSha256, rsaSha384, rsaSha512,
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    93
                                   ecdsaSha1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static DigestMethod sha1, sha256, sha384, sha512;
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
    95
    private static KeyInfo dsa1024, dsa2048, rsa, rsa1024, p256ki;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static KeySelector kvks = new KeySelectors.KeyValueKeySelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static KeySelector sks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static Key signingKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static PublicKey validatingKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static Certificate signingCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    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
   103
//    private final static String DIR = ".";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private final static String DATA_DIR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        DIR + System.getProperty("file.separator") + "data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private final static String KEYSTORE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        DATA_DIR + System.getProperty("file.separator") + "certs" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.getProperty("file.separator") + "test.jks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private final static String CRL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        DATA_DIR + System.getProperty("file.separator") + "certs" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.getProperty("file.separator") + "crl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private final static String ENVELOPE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        DATA_DIR + System.getProperty("file.separator") + "envelope.xml";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static URIDereferencer httpUd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private final static String STYLESHEET =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        "http://www.w3.org/TR/xml-stylesheet";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private final static String STYLESHEET_B64 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        "http://www.w3.org/Signature/2002/04/xml-stylesheet.b64";
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   119
    private final static String DSA_SHA256 =
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   120
        "http://www.w3.org/2009/xmldsig11#dsa-sha256";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        setup();
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   124
        test_create_signature_enveloped_dsa(1024);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   125
        test_create_signature_enveloped_dsa(2048);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        test_create_signature_enveloping_b64_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        test_create_signature_enveloping_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        test_create_signature_enveloping_hmac_sha1_40();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        test_create_signature_enveloping_hmac_sha256();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        test_create_signature_enveloping_hmac_sha384();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        test_create_signature_enveloping_hmac_sha512();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        test_create_signature_enveloping_rsa();
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   133
        test_create_signature_enveloping_p256_sha1();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        test_create_signature_external_b64_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        test_create_signature_external_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        test_create_signature_keyname();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        test_create_signature_retrievalmethod_rawx509crt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        test_create_signature_x509_crt_crl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        test_create_signature_x509_crt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        test_create_signature_x509_is();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        test_create_signature_x509_ski();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        test_create_signature_x509_sn();
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   143
        test_create_signature();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        test_create_exc_signature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        test_create_sign_spec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        test_create_signature_enveloping_sha256_dsa();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        test_create_signature_enveloping_sha384_rsa_sha256();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        test_create_signature_enveloping_sha512_rsa_sha384();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        test_create_signature_enveloping_sha512_rsa_sha512();
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   150
        test_create_signature_reference_dependency();
10888
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   151
        test_create_signature_with_attr_in_no_namespace();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   152
        test_create_signature_with_empty_id();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static void setup() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        fac = XMLSignatureFactory.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        kifac = fac.getKeyInfoFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        dbf.setNamespaceAware(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        db = dbf.newDocumentBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // get key & self-signed certificate from keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        FileInputStream fis = new FileInputStream(KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        ks.load(fis, "changeit".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        signingKey = ks.getKey("user", "changeit".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        signingCert = ks.getCertificate("user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        validatingKey = signingCert.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // create common objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        withoutComments = fac.newCanonicalizationMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        dsaSha1 = fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null);
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   174
        dsaSha256 = fac.newSignatureMethod(DSA_SHA256, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        sha1 = fac.newDigestMethod(DigestMethod.SHA1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        sha256 = fac.newDigestMethod(DigestMethod.SHA256, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        sha384 = fac.newDigestMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            ("http://www.w3.org/2001/04/xmldsig-more#sha384", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        sha512 = fac.newDigestMethod(DigestMethod.SHA512, null);
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   180
        dsa1024 = kifac.newKeyInfo(Collections.singletonList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            (kifac.newKeyValue(validatingKey)));
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   182
        dsa2048 = kifac.newKeyInfo(Collections.singletonList
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   183
            (kifac.newKeyValue(getPublicKey("DSA", 2048))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        rsa = kifac.newKeyInfo(Collections.singletonList
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   185
            (kifac.newKeyValue(getPublicKey("RSA", 512))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        rsa1024 = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            (kifac.newKeyValue(getPublicKey("RSA", 1024))));
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   188
        p256ki = kifac.newKeyInfo(Collections.singletonList
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   189
            (kifac.newKeyValue(getECPublicKey())));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        rsaSha1 = fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        rsaSha256 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        rsaSha384 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        rsaSha512 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", null);
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   197
        ecdsaSha1 = fac.newSignatureMethod
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   198
            ("http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha1", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        sks = new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        httpUd = new HttpURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   204
    static void test_create_signature_enveloped_dsa(int size) throws Exception {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   205
        System.out.println("* Generating signature-enveloped-dsa-"
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   206
                           + size + ".xml");
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   207
        SignatureMethod sm = null;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   208
        KeyInfo ki = null;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   209
        Key privKey;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   210
        if (size == 1024) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   211
            sm = dsaSha1;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   212
            ki = dsa1024;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   213
            privKey = signingKey;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   214
        } else if (size == 2048) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   215
            sm = dsaSha256;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   216
            ki = dsa2048;
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   217
            privKey = getPrivateKey("DSA", 2048);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   218
        } else throw new RuntimeException("unsupported keysize:" + size);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   219
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SignedInfo si = fac.newSignedInfo
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   222
            (withoutComments, sm, Collections.singletonList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                (fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    ("", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        (fac.newTransform(Transform.ENVELOPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                            (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                 null, null)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // create XMLSignature
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   230
        XMLSignature sig = fac.newXMLSignature(si, ki);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Element envelope = doc.createElementNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            ("http://example.org/envelope", "Envelope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        envelope.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            "xmlns", "http://example.org/envelope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        doc.appendChild(envelope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   239
        DOMSignContext dsc = new DOMSignContext(privKey, envelope);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        sig.sign(dsc);
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   242
//        StringWriter sw = new StringWriter();
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   243
//        dumpDocument(doc, sw);
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   244
//        System.out.println(sw.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            (kvks, envelope.getFirstChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static void test_create_signature_enveloping_b64_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        System.out.println("* Generating signature-enveloping-b64-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        test_create_signature_enveloping
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   264
            (sha1, dsaSha1, dsa1024, signingKey, kvks, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    static void test_create_signature_enveloping_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        System.out.println("* Generating signature-enveloping-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        test_create_signature_enveloping
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   271
            (sha1, dsaSha1, dsa1024, signingKey, kvks, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    static void test_create_signature_enveloping_sha256_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        System.out.println("* Generating signature-enveloping-sha256-dsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        test_create_signature_enveloping
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   278
            (sha256, dsaSha1, dsa1024, signingKey, kvks, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    static void test_create_signature_enveloping_hmac_sha1_40()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        System.out.println("* Generating signature-enveloping-hmac-sha1-40.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        SignatureMethod hmacSha1 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            (SignatureMethod.HMAC_SHA1, new HMACParameterSpec(40));
3462
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   287
        try {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   288
            test_create_signature_enveloping(sha1, hmacSha1, null,
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   289
                getSecretKey("secret".getBytes("ASCII")), sks, false);
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   290
        } catch (Exception e) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   291
            if (!(e instanceof XMLSignatureException)) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   292
                throw e;
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   293
            }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents: 1639
diff changeset
   294
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    static void test_create_signature_enveloping_hmac_sha256()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        System.out.println("* Generating signature-enveloping-hmac-sha256.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        SignatureMethod hmacSha256 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        test_create_signature_enveloping(sha1, hmacSha256, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    static void test_create_signature_enveloping_hmac_sha384()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        System.out.println("* Generating signature-enveloping-hmac-sha384.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        SignatureMethod hmacSha384 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        test_create_signature_enveloping(sha1, hmacSha384, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static void test_create_signature_enveloping_hmac_sha512()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        System.out.println("* Generating signature-enveloping-hmac-sha512.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        SignatureMethod hmacSha512 = fac.newSignatureMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        test_create_signature_enveloping(sha1, hmacSha512, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            getSecretKey("secret".getBytes("ASCII")), sks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static void test_create_signature_enveloping_rsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        System.out.println("* Generating signature-enveloping-rsa.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        test_create_signature_enveloping(sha1, rsaSha1, rsa,
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   331
            getPrivateKey("RSA", 512), kvks, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    static void test_create_signature_enveloping_sha384_rsa_sha256()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        System.out.println("* Generating signature-enveloping-sha384-rsa_sha256.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        test_create_signature_enveloping(sha384, rsaSha256, rsa,
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   339
            getPrivateKey("RSA", 512), kvks, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    static void test_create_signature_enveloping_sha512_rsa_sha384()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        System.out.println("* Generating signature-enveloping-sha512-rsa_sha384.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        test_create_signature_enveloping(sha512, rsaSha384, rsa1024,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            getPrivateKey("RSA", 1024), kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static void test_create_signature_enveloping_sha512_rsa_sha512()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        System.out.println("* Generating signature-enveloping-sha512-rsa_sha512.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        test_create_signature_enveloping(sha512, rsaSha512, rsa1024,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            getPrivateKey("RSA", 1024), kvks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   359
    static void test_create_signature_enveloping_p256_sha1() throws Exception {
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   360
        System.out.println("* Generating signature-enveloping-p256-sha1.xml");
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   361
        test_create_signature_enveloping(sha1, ecdsaSha1, p256ki,
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   362
            getECPrivateKey(), kvks, false);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   363
        System.out.println();
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   364
    }
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
   365
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    static void test_create_signature_external_b64_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        System.out.println("* Generating signature-external-b64-dsa.xml");
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   368
        test_create_signature_external(dsaSha1, dsa1024, signingKey, kvks, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    static void test_create_signature_external_dsa() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        System.out.println("* Generating signature-external-dsa.xml");
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   374
        test_create_signature_external(dsaSha1, dsa1024, signingKey, kvks, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    static void test_create_signature_keyname() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        System.out.println("* Generating signature-keyname.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        KeyInfo kn = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            (kifac.newKeyName("user")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        test_create_signature_external(dsaSha1, kn, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    static void test_create_signature_retrievalmethod_rawx509crt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            "* Generating signature-retrievalmethod-rawx509crt.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        KeyInfo rm = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            (kifac.newRetrievalMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            ("certs/user.crt", X509Data.RAW_X509_CERTIFICATE_TYPE, null)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        test_create_signature_external(dsaSha1, rm, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            new X509KeySelector(ks), 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_crt_crl() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        System.out.println("* Generating signature-x509-crt-crl.xml");
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   401
        List<Object> xds = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        xds.add(signingCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        FileInputStream fis = new FileInputStream(CRL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        X509CRL crl = (X509CRL) cf.generateCRL(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        xds.add(crl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        KeyInfo crt_crl = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            (kifac.newX509Data(xds)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        test_create_signature_external(dsaSha1, crt_crl, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static void test_create_signature_x509_crt() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        System.out.println("* Generating signature-x509-crt.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        KeyInfo crt = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            (kifac.newX509Data(Collections.singletonList(signingCert))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        test_create_signature_external(dsaSha1, crt, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    static void test_create_signature_x509_is() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        System.out.println("* Generating signature-x509-is.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        KeyInfo is = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            (kifac.newX509Data(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            (kifac.newX509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            ("CN=User", new BigInteger("45ef2729", 16))))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        test_create_signature_external(dsaSha1, is, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    static void test_create_signature_x509_ski() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        System.out.println("* Generating signature-x509-ski.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        KeyInfo ski = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            (kifac.newX509Data(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            ("keyid".getBytes("ASCII")))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        test_create_signature_external(dsaSha1, ski, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            KeySelector.singletonKeySelector(validatingKey), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    static void test_create_signature_x509_sn() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        System.out.println("* Generating signature-x509-sn.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        KeyInfo sn = kifac.newKeyInfo(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            (kifac.newX509Data(Collections.singletonList("CN=User"))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        test_create_signature_external(dsaSha1, sn, signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            new X509KeySelector(ks), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   458
    static void test_create_signature_reference_dependency() throws Exception {
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   459
        System.out.println("* Generating signature-reference-dependency.xml");
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   460
        // create references
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   461
        List<Reference> refs = Collections.singletonList
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   462
            (fac.newReference("#object-1", sha1));
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   463
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   464
        // create SignedInfo
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   465
        SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs);
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   466
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   467
        // create objects
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   468
        List<XMLObject> objs = new ArrayList<>();
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   469
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   470
        // Object 1
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   471
        List<Reference> manRefs = Collections.singletonList
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   472
            (fac.newReference("#object-2", sha1));
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   473
        objs.add(fac.newXMLObject(Collections.singletonList
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   474
            (fac.newManifest(manRefs, "manifest-1")), "object-1", null, null));
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   475
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   476
        // Object 2
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   477
        Document doc = db.newDocument();
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   478
        Element nc = doc.createElementNS(null, "NonCommentandus");
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   479
        nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "");
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   480
        nc.appendChild(doc.createComment(" Commentandum "));
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   481
        objs.add(fac.newXMLObject(Collections.singletonList
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   482
            (new DOMStructure(nc)), "object-2", null, null));
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   483
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   484
        // create XMLSignature
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   485
        XMLSignature sig = fac.newXMLSignature(si, rsa, objs, "signature", null);
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   486
        DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc);
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   487
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   488
        sig.sign(dsc);
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   489
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   490
//      dumpDocument(doc, new PrintWriter(System.out));
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   491
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   492
        DOMValidateContext dvc = new DOMValidateContext
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   493
            (kvks, doc.getDocumentElement());
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   494
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   495
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   496
        if (sig.equals(sig2) == false) {
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   497
            throw new Exception
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   498
                ("Unmarshalled signature is not equal to generated signature");
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   499
        }
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   500
        if (sig2.validate(dvc) == false) {
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   501
            throw new Exception("Validation of generated signature failed");
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   502
        }
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   503
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   504
        System.out.println();
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   505
    }
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   506
10888
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   507
    static void test_create_signature_with_attr_in_no_namespace()
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   508
        throws Exception
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   509
    {
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   510
        System.out.println
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   511
            ("* Generating signature-with-attr-in-no-namespace.xml");
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   512
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   513
        // create references
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   514
        List<Reference> refs = Collections.singletonList
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   515
            (fac.newReference("#unknown", sha1));
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   516
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   517
        // create SignedInfo
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   518
        SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs);
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   519
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   520
        // create object-1
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   521
        Document doc = db.newDocument();
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   522
        Element nc = doc.createElementNS(null, "NonCommentandus");
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   523
        // add attribute with no namespace
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   524
        nc.setAttribute("Id", "unknown");
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   525
        XMLObject obj = fac.newXMLObject(Collections.singletonList
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   526
            (new DOMStructure(nc)), "object-1", null, null);
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   527
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   528
        // create XMLSignature
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   529
        XMLSignature sig = fac.newXMLSignature(si, rsa,
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   530
                                               Collections.singletonList(obj),
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   531
                                               "signature", null);
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   532
        DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10888
diff changeset
   533
        dsc.setIdAttributeNS(nc, null, "Id");
10888
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   534
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   535
        sig.sign(dsc);
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   536
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   537
//      dumpDocument(doc, new PrintWriter(System.out));
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   538
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   539
        DOMValidateContext dvc = new DOMValidateContext
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   540
            (kvks, doc.getDocumentElement());
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10888
diff changeset
   541
        dvc.setIdAttributeNS(nc, null, "Id");
10888
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   542
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   543
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   544
        if (sig.equals(sig2) == false) {
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   545
            throw new Exception
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   546
                ("Unmarshalled signature is not equal to generated signature");
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   547
        }
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   548
        if (sig2.validate(dvc) == false) {
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   549
            throw new Exception("Validation of generated signature failed");
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   550
        }
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   551
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   552
        System.out.println();
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   553
    }
1aa4710eb4f6 7094155: JSR105 code throws javax.xml.crypto.URIReferenceException when running into Java 7 VM
mullan
parents: 5506
diff changeset
   554
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   555
    static void test_create_signature_with_empty_id() throws Exception {
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   556
        System.out.println("* Generating signature-with-empty-id.xml");
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   557
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   558
        // create references
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   559
        List<Reference> refs = Collections.singletonList
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   560
            (fac.newReference("#", sha1));
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   561
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   562
        // create SignedInfo
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   563
        SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs);
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   564
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   565
        // create object with empty id
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   566
        Document doc = db.newDocument();
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   567
        XMLObject obj = fac.newXMLObject(Collections.singletonList
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   568
            (new DOMStructure(doc.createTextNode("I am the text."))),
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   569
            "", "text/plain", null);
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   570
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   571
        // create XMLSignature
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   572
        XMLSignature sig = fac.newXMLSignature(si, rsa,
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   573
                                               Collections.singletonList(obj),
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   574
                                               "signature", null);
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
   575
        DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA", 512), doc);
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   576
        sig.sign(dsc);
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   577
    }
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18240
diff changeset
   578
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    static void test_create_signature() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        System.out.println("* Generating signature.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        // create references
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   583
        List<Reference> refs = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // Reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        refs.add(fac.newReference(STYLESHEET, sha1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // Reference 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            (STYLESHEET_B64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            (fac.newTransform(Transform.BASE64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                (TransformParameterSpec) null)), null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // Reference 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        refs.add(fac.newReference("#object-1", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            (fac.newTransform(Transform.XPATH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            new XPathFilterParameterSpec("self::text()"))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        // Reference 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        String expr = "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
          + " ancestor-or-self::dsig:SignedInfo                  " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
          + "  and                                               " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
          + " count(ancestor-or-self::dsig:Reference |           " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
          + "      here()/ancestor::dsig:Reference[1]) >         " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
          + " count(ancestor-or-self::dsig:Reference)            " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
          + "  or                                                " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
          + " count(ancestor-or-self::node() |                   " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
          + "      id('notaries')) =                             " + "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
          + " count(ancestor-or-self::node())                    " + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        XPathFilterParameterSpec xfp = new XPathFilterParameterSpec(expr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            Collections.singletonMap("dsig", XMLSignature.XMLNS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        refs.add(fac.newReference("", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            (fac.newTransform(Transform.XPATH, xfp)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // Reference 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        refs.add(fac.newReference("#object-2", sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            (fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                (Transform.BASE64, (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // Reference 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            ("#manifest-1", sha1, null, Manifest.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // Reference 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        refs.add(fac.newReference("#signature-properties-1", sha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            SignatureProperties.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        // Reference 8
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   634
        List<Transform> transforms = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            (Transform.ENVELOPED, (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        refs.add(fac.newReference("", sha1, transforms, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        // Reference 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        refs.add(fac.newReference("", sha1, transforms, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        // Reference 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        Transform env = fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            (Transform.ENVELOPED, (TransformParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        refs.add(fac.newReference("#xpointer(/)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            sha1, Collections.singletonList(env), null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        // Reference 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        transforms.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            (Transform.ENVELOPED, (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        transforms.add(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
             (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        refs.add(fac.newReference("#xpointer(/)", sha1, transforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // Reference 12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        refs.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            (fac.newReference("#object-3", sha1, null, XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        // Reference 13
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        Transform withComments = fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
             (TransformParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        refs.add(fac.newReference("#object-3", sha1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            Collections.singletonList(withComments), XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // Reference 14
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        refs.add(fac.newReference("#xpointer(id('object-3'))", sha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // Reference 15
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        withComments = fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            (CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
             (TransformParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        refs.add(fac.newReference("#xpointer(id('object-3'))", sha1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            Collections.singletonList(withComments), XMLObject.TYPE, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // Reference 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        refs.add(fac.newReference("#reference-2", sha1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // Reference 17
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        refs.add(fac.newReference("#manifest-reference-1", sha1, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            null, "reference-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // Reference 18
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        refs.add(fac.newReference("#reference-1", sha1, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            "reference-2"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        SignedInfo si = fac.newSignedInfo(withoutComments, dsaSha1, refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        // create keyinfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        XPathFilterParameterSpec xpf = new XPathFilterParameterSpec(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            "ancestor-or-self::dsig:X509Data",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            Collections.singletonMap("dsig", XMLSignature.XMLNS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        RetrievalMethod rm = kifac.newRetrievalMethod("#object-4",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            X509Data.TYPE, Collections.singletonList(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            (Transform.XPATH, xpf)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        KeyInfo ki = kifac.newKeyInfo(Collections.singletonList(rm), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // create objects
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   709
        List<XMLObject> objs = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        // Object 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            (new DOMStructure(doc.createTextNode("I am the text."))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            "object-1", "text/plain", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        // Object 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            (new DOMStructure(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            "object-2", "text/plain", Transform.BASE64));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // Object 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        Element nc = doc.createElementNS(null, "NonCommentandus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        nc.appendChild(doc.createComment(" Commentandum "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            (new DOMStructure(nc)), "object-3", null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        // Manifest
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   729
        List<Reference> manRefs = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // Manifest Reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        manRefs.add(fac.newReference(STYLESHEET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            sha1, null, null, "manifest-reference-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        // Manifest Reference 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        manRefs.add(fac.newReference("#reference-1", sha1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        // Manifest Reference 3
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   739
        List<Transform> manTrans = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        String xslt = ""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
          + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
          + "            xmlns='http://www.w3.org/TR/xhtml1/strict' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
          + "            exclude-result-prefixes='foo' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
          + "            version='1.0'>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
          + "  <xsl:output encoding='UTF-8' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
          + "           indent='no' \n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
          + "           method='xml' />\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
          + "  <xsl:template match='/'>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
          + "    <html>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
          + "   <head>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
          + "    <title>Notaries</title>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
          + "   </head>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
          + "   <body>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
          + "    <table>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
          + "      <xsl:for-each select='Notaries/Notary'>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
          + "           <tr>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
          + "           <th>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
          + "            <xsl:value-of select='@name' />\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
          + "           </th>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
          + "           </tr>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
          + "      </xsl:for-each>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
          + "    </table>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
          + "   </body>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
          + "    </html>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
          + "  </xsl:template>\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
          + "</xsl:stylesheet>\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        Document docxslt = db.parse(new ByteArrayInputStream(xslt.getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        Node xslElem = docxslt.getDocumentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        manTrans.add(fac.newTransform(Transform.XSLT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            new XSLTTransformParameterSpec(new DOMStructure(xslElem))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        manTrans.add(fac.newTransform(CanonicalizationMethod.INCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        manRefs.add(fac.newReference("#notaries", sha1, manTrans, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            (fac.newManifest(manRefs, "manifest-1")), null, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // SignatureProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        Element sa = doc.createElementNS("urn:demo", "SignerAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        sa.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:demo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        Element ip = doc.createElementNS("urn:demo", "IP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        ip.appendChild(doc.createTextNode("192.168.21.138"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        sa.appendChild(ip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        SignatureProperty sp = fac.newSignatureProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            (Collections.singletonList(new DOMStructure(sa)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            "#signature", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        SignatureProperties sps = fac.newSignatureProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            (Collections.singletonList(sp), "signature-properties-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        objs.add(fac.newXMLObject(Collections.singletonList(sps), 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
        // Object 4
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   794
        List<Object> xds = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        xds.add("CN=User");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        xds.add(kifac.newX509IssuerSerial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            ("CN=User", new BigInteger("45ef2729", 16)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        xds.add(signingCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        objs.add(fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            (kifac.newX509Data(xds)), "object-4", null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        XMLSignature sig = fac.newXMLSignature(si, ki, objs, "signature", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        dbf.setNamespaceAware(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        dbf.setValidating(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        Document envDoc = dbf.newDocumentBuilder().parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            (new FileInputStream(ENVELOPE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        Element ys = (Element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            envDoc.getElementsByTagName("YoursSincerely").item(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        DOMSignContext dsc = new DOMSignContext(signingKey, ys);
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   814
        dsc.setURIDereferencer(httpUd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
//      StringWriter sw = new StringWriter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
//        dumpDocument(envDoc, sw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        NodeList nl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            envDoc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (nl.getLength() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            throw new Exception("Couldn't find signature Element");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        Element sigElement = (Element) nl.item(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            (new X509KeySelector(ks), sigElement);
4506
402b248b41de 6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents: 3462
diff changeset
   830
        dvc.setURIDereferencer(httpUd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        File f = new File(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            System.getProperty("dir.test.vector.baltimore") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            System.getProperty("file.separator") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            "merlin-xmldsig-twenty-three" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            System.getProperty("file.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        dvc.setBaseURI(f.toURI().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    private static void dumpDocument(Document doc, Writer w) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        TransformerFactory tf = TransformerFactory.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        Transformer trans = tf.newTransformer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
//      trans.setOutputProperty(OutputKeys.INDENT, "yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        trans.transform(new DOMSource(doc), new StreamResult(w));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    private static void test_create_signature_external
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        (SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        boolean b64) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        // create reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        Reference ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        if (b64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            ref = fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                (STYLESHEET_B64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                sha1, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                (fac.newTransform(Transform.BASE64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                 (TransformParameterSpec) null)), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            ref = fac.newReference(STYLESHEET, sha1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        SignedInfo si = fac.newSignedInfo(withoutComments, sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            Collections.singletonList(ref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        XMLSignature sig = fac.newXMLSignature(si, ki);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        DOMSignContext dsc = new DOMSignContext(signingKey, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        dsc.setURIDereferencer(httpUd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            (ks, doc.getDocumentElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        File f = new File(DATA_DIR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        dvc.setBaseURI(f.toURI().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        dvc.setURIDereferencer(httpUd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    private static void test_create_signature_enveloping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        (DigestMethod dm, SignatureMethod sm, KeyInfo ki, Key signingKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         KeySelector ks, boolean b64) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // create reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        Reference ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (b64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            ref = fac.newReference("#object", dm, Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                (fac.newTransform(Transform.BASE64,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                 (TransformParameterSpec) null)), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            ref = fac.newReference("#object", dm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        SignedInfo si = fac.newSignedInfo(withoutComments, sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            Collections.singletonList(ref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        // create Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        String text = b64 ? "c29tZSB0ZXh0" : "some text";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        XMLObject obj = fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            (new DOMStructure(doc.createTextNode(text))),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            "object", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        XMLSignature sig = fac.newXMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            (si, ki, Collections.singletonList(obj), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        DOMSignContext dsc = new DOMSignContext(signingKey, doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
//        dumpDocument(doc, new FileWriter("/tmp/foo.xml"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            (ks, doc.getDocumentElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    static void test_create_exc_signature() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        System.out.println("* Generating exc_signature.xml");
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   954
        List<Reference> refs = new ArrayList<>(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        // create reference 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
             Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                (fac.newTransform(CanonicalizationMethod.EXCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                 (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // create reference 2
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   966
        List<String> prefixList = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        prefixList.add("bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        prefixList.add("#default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
             Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, params)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // create reference 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
             Collections.singletonList(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                 (TransformParameterSpec) null)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        // create reference 4
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
   987
        prefixList = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        prefixList.add("bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        prefixList.add("#default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        params = new ExcC14NParameterSpec(prefixList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            ("#xpointer(id('to-be-signed'))",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
             fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
             Collections.singletonList(fac.newTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, params)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        SignedInfo si = fac.newSignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            fac.newCanonicalizationMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                (CanonicalizationMethod.EXCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                 (C14NMethodParameterSpec) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        // create KeyInfo
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
  1006
        List<XMLStructure> kits = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        kits.add(kifac.newKeyValue(validatingKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        KeyInfo ki = kifac.newKeyInfo(kits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        // create Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        Element baz = doc.createElementNS("urn:bar", "bar:Baz");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        Comment com = doc.createComment(" comment ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        baz.appendChild(com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        XMLObject obj = fac.newXMLObject(Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            (new DOMStructure(baz)), "to-be-signed", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        XMLSignature sig = fac.newXMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            (si, ki, Collections.singletonList(obj), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        Element foo = doc.createElementNS("urn:foo", "Foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        foo.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        foo.setAttributeNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            ("http://www.w3.org/2000/xmlns/", "xmlns:bar", "urn:bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        doc.appendChild(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        DOMSignContext dsc = new DOMSignContext(signingKey, foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        dsc.putNamespacePrefix(XMLSignature.XMLNS, "dsig");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
//      dumpDocument(doc, new FileWriter("/tmp/foo.xml"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            (new KeySelectors.KeyValueKeySelector(), foo.getLastChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    static void test_create_sign_spec() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        System.out.println("* Generating sign-spec.xml");
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
  1051
        List<Reference> refs = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        // create reference 1
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
  1054
        List<XPathType> types = new ArrayList<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        types.add(new XPathType(" //NotToBeSigned ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            XPathType.Filter.SUBTRACT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        types.add(new XPathType(" //ReallyToBeSigned ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            XPathType.Filter.UNION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        XPathFilter2ParameterSpec xp1 = new XPathFilter2ParameterSpec(types);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        refs.add(fac.newReference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            ("", fac.newDigestMethod(DigestMethod.SHA1, null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
             Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
             null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        // create reference 2
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
  1067
        List<Transform> trans2 = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        trans2.add(fac.newTransform(Transform.ENVELOPED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            (TransformParameterSpec) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            (Collections.singletonList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                (new XPathType(" / ", XPathType.Filter.UNION)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        trans2.add(fac.newTransform(Transform.XPATH2, xp2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        refs.add(fac.newReference("#signature-value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            fac.newDigestMethod(DigestMethod.SHA1, null), trans2, null, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        // create SignedInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        SignedInfo si = fac.newSignedInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            fac.newCanonicalizationMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                (CanonicalizationMethod.INCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                 (C14NMethodParameterSpec) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        // create KeyInfo
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
  1085
        List<XMLStructure> kits = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        kits.add(kifac.newKeyValue(validatingKey));
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 24251
diff changeset
  1087
        List<Object> xds = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        xds.add("CN=User");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        xds.add(signingCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        kits.add(kifac.newX509Data(xds));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        KeyInfo ki = kifac.newKeyInfo(kits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        // create XMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        XMLSignature sig = fac.newXMLSignature
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            (si, ki, null, null, "signature-value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        Document doc = db.newDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        Element tbs1 = doc.createElementNS(null, "ToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        Comment tbs1Com = doc.createComment(" comment ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        Element tbs1Data = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        Element tbs1ntbs = doc.createElementNS(null, "NotToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        Element tbs1rtbs = doc.createElementNS(null, "ReallyToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        Comment tbs1rtbsCom = doc.createComment(" comment ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        Element tbs1rtbsData = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        tbs1rtbs.appendChild(tbs1rtbsCom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        tbs1rtbs.appendChild(tbs1rtbsData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        tbs1ntbs.appendChild(tbs1rtbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        tbs1.appendChild(tbs1Com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        tbs1.appendChild(tbs1Data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        tbs1.appendChild(tbs1ntbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        Element tbs2 = doc.createElementNS(null, "ToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        Element tbs2Data = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        Element tbs2ntbs = doc.createElementNS(null, "NotToBeSigned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        Element tbs2ntbsData = doc.createElementNS(null, "Data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        tbs2ntbs.appendChild(tbs2ntbsData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        tbs2.appendChild(tbs2Data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        tbs2.appendChild(tbs2ntbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        Element document = doc.createElementNS(null, "Document");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        document.appendChild(tbs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        document.appendChild(tbs2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        doc.appendChild(document);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        DOMSignContext dsc = new DOMSignContext(signingKey, document);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        sig.sign(dsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
//      dumpDocument(doc, new FileWriter("/tmp/foo.xml"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        DOMValidateContext dvc = new DOMValidateContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            (new KeySelectors.KeyValueKeySelector(), document.getLastChild());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if (sig.equals(sig2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                ("Unmarshalled signature is not equal to generated signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if (sig2.validate(dvc) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            throw new Exception("Validation of generated signature failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    private static final String DSA_Y =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        "070662842167565771936588335128634396171789331656318483584455493822" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        "400811200853331373030669235424928346190274044631949560438023934623" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        "71310375123430985057160";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    private static final String DSA_P =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        "013232376895198612407547930718267435757728527029623408872245156039" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        "757713029036368719146452186041204237350521785240337048752071462798" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        "273003935646236777459223";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    private static final String DSA_Q =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        "0857393771208094202104259627990318636601332086981";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    private static final String DSA_G =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        "054216440574364751416096484883257051280474283943804743768346673007" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        "661082626139005426812890807137245973106730741193551360857959820973" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        "90670890367185141189796";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    private static final String DSA_X =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        "0527140396812450214498055937934275626078768840117";
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1161
    private static final String DSA_2048_Y =
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1162
        "15119007057343785981993995134621348945077524760182795513668325877793414638620983617627033248732235626178802906346261435991040697338468329634416089753032362617771631199351767336660070462291411472735835843440140283101463231807789628656218830720378705090795271104661936237385140354825159080766174663596286149653433914842868551355716015585570827642835307073681358328172009941968323702291677280809277843998510864653406122348712345584706761165794179850728091522094227603562280855104749858249588234915206290448353957550635709520273178475097150818955098638774564910092913714625772708285992586894795017709678223469405896699928";
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1163
    private static final String DSA_2048_P =
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1164
        "18111848663142005571178770624881214696591339256823507023544605891411707081617152319519180201250440615163700426054396403795303435564101919053459832890139496933938670005799610981765220283775567361483662648340339405220348871308593627647076689407931875483406244310337925809427432681864623551598136302441690546585427193224254314088256212718983105131138772434658820375111735710449331518776858786793875865418124429269409118756812841019074631004956409706877081612616347900606555802111224022921017725537417047242635829949739109274666495826205002104010355456981211025738812433088757102520562459649777989718122219159982614304359";
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1165
    private static final String DSA_2048_Q =
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1166
        "19689526866605154788513693571065914024068069442724893395618704484701";
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1167
    private static final String DSA_2048_G =
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1168
        "2859278237642201956931085611015389087970918161297522023542900348087718063098423976428252369340967506010054236052095950169272612831491902295835660747775572934757474194739347115870723217560530672532404847508798651915566434553729839971841903983916294692452760249019857108409189016993380919900231322610083060784269299257074905043636029708121288037909739559605347853174853410208334242027740275688698461842637641566056165699733710043802697192696426360843173620679214131951400148855611740858610821913573088059404459364892373027492936037789337011875710759208498486908611261954026964574111219599568903257472567764789616958430";
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1169
    private static final String DSA_2048_X =
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1170
        "14562787764977288900757387442281559936279834964901963465277698843172";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    private static final String RSA_MOD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        "010800185049102889923150759252557522305032794699952150943573164381" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        "936603255999071981574575044810461362008102247767482738822150129277" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        "490998033971789476107463";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    private static final String RSA_PRIV =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        "016116973584421969795445996229612671947635798429212816611707210835" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        "915586591340598683996088487065438751488342251960069575392056288063" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        "6800379454345804879553";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    private static final String RSA_PUB = "065537";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    private static final String RSA_1024_MOD = "098871307553789439961130765" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        "909423744508062468450669519128736624058048856940468016843888594585" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        "322862378444314635412341974900625010364163960238734457710620107530" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        "573945081856371709138380902553309075505688814637544923038853658690" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        "857672483016239697038853418682988686871489963827000080098971762923" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        "833614557257607521";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    private static final String RSA_1024_PRIV = "03682574144968491431483287" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        "297021581096848810374110568017963075809477047466189822987258068867" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        "704855380407747867998863645890602646601140183818953428006646987710" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        "237008997971129772408397621801631622129297063463868593083106979716" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        "204903524890556839550490384015324575598723478554854070823335021842" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        "210112348400928769";
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1192
    private static final String EC_X =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1193
        "335863644451761614592446380116804721648611739647823420286081723541" +
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1194
        "6166183710";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1195
    private static final String EC_Y =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1196
        "951559601159729477487064127150143688502130342917782252098602422796" +
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1197
        "95457910701";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1198
    private static final String EC_S =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1199
        "425976209773168452211813225517384419928639977904006759709292218082" +
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1200
        "7440083936";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1201
    private static final ECParameterSpec EC_PARAMS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1203
    static {
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1204
        final String ec_sfield, ec_a, ec_b, ec_gx, ec_gy, ec_n;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1205
        ec_sfield =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1206
            "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1207
        ec_a =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1208
            "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1209
        ec_b =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1210
            "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1211
        ec_gx =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1212
            "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1213
        ec_gy =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1214
            "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1215
        ec_n =
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1216
            "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551";
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1217
        final int ec_h = 1;
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1218
        final ECField ec_field = new ECFieldFp(bigInt(ec_sfield));
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1219
        final EllipticCurve ec_curve = new EllipticCurve(ec_field,
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1220
                                                bigInt(ec_a), bigInt(ec_b));
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1221
        final ECPoint ec_g = new ECPoint(bigInt(ec_gx), bigInt(ec_gy));
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1222
        EC_PARAMS = new ECParameterSpec(ec_curve, ec_g, bigInt(ec_n), ec_h);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1223
    }
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1224
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1225
    private static BigInteger bigInt(String s) {
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1226
        return new BigInteger(s, 16);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1227
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    private static PublicKey getPublicKey(String algo, int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        KeyFactory kf = KeyFactory.getInstance(algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        KeySpec kspec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (algo.equalsIgnoreCase("DSA")) {
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1233
            if (keysize == 1024) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1234
                kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1235
                                             new BigInteger(DSA_P),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1236
                                             new BigInteger(DSA_Q),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1237
                                             new BigInteger(DSA_G));
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1238
            } else if (keysize == 2048) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1239
                kspec = new DSAPublicKeySpec(new BigInteger(DSA_2048_Y),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1240
                                             new BigInteger(DSA_2048_P),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1241
                                             new BigInteger(DSA_2048_Q),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1242
                                             new BigInteger(DSA_2048_G));
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1243
            } else throw new RuntimeException("Unsupported keysize:" + keysize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        } else if (algo.equalsIgnoreCase("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            if (keysize == 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                                             new BigInteger(RSA_PUB));
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1248
            } else if (keysize == 1024) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                                             new BigInteger(RSA_PUB));
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1251
            } else throw new RuntimeException("Unsupported keysize:" + keysize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        } else throw new RuntimeException("Unsupported key algorithm " + algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        return kf.generatePublic(kspec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1256
    private static PublicKey getECPublicKey() throws Exception {
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1257
        KeyFactory kf = KeyFactory.getInstance("EC");
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1258
        KeySpec kspec = new ECPublicKeySpec(new ECPoint(new BigInteger(EC_X),
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1259
                                                        new BigInteger(EC_Y)),
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1260
                                            EC_PARAMS);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1261
        return kf.generatePublic(kspec);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1262
    }
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1263
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    private static PrivateKey getPrivateKey(String algo, int keysize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        KeyFactory kf = KeyFactory.getInstance(algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        KeySpec kspec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        if (algo.equalsIgnoreCase("DSA")) {
24251
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1269
            if (keysize == 1024) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1270
                kspec = new DSAPrivateKeySpec
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1271
                    (new BigInteger(DSA_X), new BigInteger(DSA_P),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1272
                     new BigInteger(DSA_Q), new BigInteger(DSA_G));
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1273
            } else if (keysize == 2048) {
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1274
                kspec = new DSAPrivateKeySpec
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1275
                    (new BigInteger(DSA_2048_X), new BigInteger(DSA_2048_P),
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1276
                     new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
da7dc40edb67 8038349: Signing XML with DSA throws Exception when key is larger than 1024 bits
mullan
parents: 23930
diff changeset
  1277
            } else throw new RuntimeException("Unsupported keysize:" + keysize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        } else if (algo.equalsIgnoreCase("RSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            if (keysize == 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                kspec = new RSAPrivateKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                    (new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                              new BigInteger(RSA_1024_PRIV));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        } else throw new RuntimeException("Unsupported key algorithm " + algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        return kf.generatePrivate(kspec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
28308
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1290
    private static PrivateKey getECPrivateKey() throws Exception {
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1291
        KeyFactory kf = KeyFactory.getInstance("EC");
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1292
        KeySpec kspec = new ECPrivateKeySpec(new BigInteger(EC_S), EC_PARAMS);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1293
        return kf.generatePrivate(kspec);
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1294
    }
5fdc6e6c0b97 8046724: XML Signature ECKeyValue elements cannot be marshalled or unmarshalled
juh
parents: 27747
diff changeset
  1295
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    private static SecretKey getSecretKey(final byte[] secret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        return new SecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            public String getFormat()   { return "RAW"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            public byte[] getEncoded()  { return secret; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            public String getAlgorithm(){ return "SECRET"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * This URIDereferencer returns locally cached copies of http content to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * avoid test failures due to network glitches, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    private static class HttpURIDereferencer implements URIDereferencer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        private URIDereferencer defaultUd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        HttpURIDereferencer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        public Data dereference(final URIReference ref, XMLCryptoContext ctx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        throws URIReferenceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            String uri = ref.getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    FileInputStream fis = new FileInputStream(new File
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                        (DATA_DIR, uri.substring(uri.lastIndexOf('/'))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    return new OctetStreamData(fis,ref.getURI(),ref.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                } catch (Exception e) { throw new URIReferenceException(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            // fallback on builtin deref
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            return defaultUd.dereference(ref, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
}