jdk/test/javax/xml/crypto/dsig/ValidationTests.java
author tbell
Thu, 23 Apr 2009 21:32:44 -0700
changeset 2636 3d0e25588136
parent 2 90ce3da70b43
child 3462 4477fb399895
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4635230
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 6365103
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @bug 6366054
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary Basic unit tests for validating XML Signatures with JSR 105
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *     X509KeySelector.java ValidationTests.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ValidationTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.xml.crypto.Data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.xml.crypto.KeySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.xml.crypto.OctetStreamData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.xml.crypto.URIDereferencer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.xml.crypto.URIReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.xml.crypto.URIReferenceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.xml.crypto.XMLCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.xml.crypto.dsig.XMLSignatureFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This is a testcase to validate all "merlin-xmldsig-twenty-three"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * testcases from Baltimore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class ValidationTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static SignatureValidator validator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static String DIR = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final static String DATA_DIR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        DIR + System.getProperty("file.separator") + "data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final static String KEYSTORE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        DATA_DIR + System.getProperty("file.separator") + "certs" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.getProperty("file.separator") + "xmldsig.jks";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final static String STYLESHEET =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "http://www.w3.org/TR/xml-stylesheet";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static String STYLESHEET_B64 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "http://www.w3.org/Signature/2002/04/xml-stylesheet.b64";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static String[] FILES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "signature-enveloped-dsa.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        "signature-enveloping-b64-dsa.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        "signature-enveloping-dsa.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "signature-enveloping-rsa.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "signature-enveloping-hmac-sha1.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "signature-enveloping-hmac-sha1-40.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "signature-external-dsa.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        "signature-external-b64-dsa.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "signature-retrievalmethod-rawx509crt.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        "signature-keyname.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "signature-x509-crt-crl.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        "signature-x509-crt.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        "signature-x509-is.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "signature-x509-ski.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "signature-x509-sn.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
//      "signature.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        "exc-signature.xml",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        "sign-spec.xml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static KeySelector skks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            skks =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            //should not occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private final static KeySelector SKKS = skks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final static KeySelector KVKS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        new KeySelectors.KeyValueKeySelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private final static KeySelector CKS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        new KeySelectors.CollectionKeySelector(new File(DATA_DIR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private final static KeySelector RXKS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        new KeySelectors.RawX509KeySelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final static KeySelector XKS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final static KeySelector[] KEY_SELECTORS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SKKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        SKKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        CKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        CKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        RXKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        RXKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        CKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        CKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        CKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
//        XKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        KVKS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        RXKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static URIDereferencer httpUd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        httpUd = new HttpURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        validator = new SignatureValidator(new File(DATA_DIR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        boolean atLeastOneFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        for (int i=0; i < FILES.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.out.println("Validating " + FILES[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (test_signature(FILES[i], KEY_SELECTORS[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                System.out.println("PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                System.out.println("FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                atLeastOneFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // test with reference caching enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        System.out.println("Validating sign-spec.xml with caching enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (test_signature("sign-spec.xml", RXKS, true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println("PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.out.println("FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            atLeastOneFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (atLeastOneFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                ("At least one signature did not validate as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static boolean test_signature(String file, KeySelector ks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return test_signature(file, ks, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static boolean test_signature(String file, KeySelector ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        boolean cache) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            KeyStore keystore = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            keystore.load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                (new FileInputStream(KEYSTORE), "changeit".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            ks = new X509KeySelector(keystore, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return validator.validate(file, ks, httpUd, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * This URIDereferencer returns locally cached copies of http content to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * avoid test failures due to network glitches, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static class HttpURIDereferencer implements URIDereferencer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        private URIDereferencer defaultUd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        HttpURIDereferencer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public Data dereference(final URIReference ref, XMLCryptoContext ctx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        throws URIReferenceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            String uri = ref.getURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    FileInputStream fis = new FileInputStream(new File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        (DATA_DIR, uri.substring(uri.lastIndexOf('/'))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    return new OctetStreamData(fis,ref.getURI(),ref.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                } catch (Exception e) { throw new URIReferenceException(e); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // fallback on builtin deref
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return defaultUd.dereference(ref, ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}