ValidationTests.java 8.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24 25
 */

/**
 * @test
26
 * @bug 4635230 6365103 6366054 6824440 7131084 8046724 8079693
D
duke 已提交
27 28 29
 * @summary Basic unit tests for validating XML Signatures with JSR 105
 * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
 *     X509KeySelector.java ValidationTests.java
M
mullan 已提交
30
 * @run main/othervm ValidationTests
D
duke 已提交
31 32 33 34 35 36 37
 * @author Sean Mullan
 */
import java.io.File;
import java.io.FileInputStream;
import java.security.*;
import javax.xml.crypto.Data;
import javax.xml.crypto.KeySelector;
38
import javax.xml.crypto.MarshalException;
D
duke 已提交
39 40 41 42 43
import javax.xml.crypto.OctetStreamData;
import javax.xml.crypto.URIDereferencer;
import javax.xml.crypto.URIReference;
import javax.xml.crypto.URIReferenceException;
import javax.xml.crypto.XMLCryptoContext;
M
mullan 已提交
44
import javax.xml.crypto.dsig.XMLSignatureException;
D
duke 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
import javax.xml.crypto.dsig.XMLSignatureFactory;

public class ValidationTests {

    private static SignatureValidator validator;
    private final static String DIR = System.getProperty("test.src", ".");
    private final static String DATA_DIR =
        DIR + System.getProperty("file.separator") + "data";
    private final static String KEYSTORE =
        DATA_DIR + System.getProperty("file.separator") + "certs" +
        System.getProperty("file.separator") + "xmldsig.jks";
    private final static String STYLESHEET =
        "http://www.w3.org/TR/xml-stylesheet";
    private final static String STYLESHEET_B64 =
        "http://www.w3.org/Signature/2002/04/xml-stylesheet.b64";

61 62 63
    static class Test {
        String file;
        KeySelector ks;
64 65 66
        Class exception;

        Test(String file, KeySelector ks, Class exception) {
67 68
            this.file = file;
            this.ks = ks;
69 70 71 72 73 74
            this.exception = exception;
        }

        // XMLSignatureException is expected by default
        Test(String file, KeySelector ks) {
            this(file, ks, XMLSignatureException.class);
75 76
        }
    }
D
duke 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    static KeySelector skks;
    static {
        try {
            skks =
                new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"));
        } catch (Exception e) {
            //should not occur
        }
    }
    private final static KeySelector SKKS = skks;
    private final static KeySelector KVKS =
        new KeySelectors.KeyValueKeySelector();
    private final static KeySelector CKS =
        new KeySelectors.CollectionKeySelector(new File(DATA_DIR));
    private final static KeySelector RXKS =
        new KeySelectors.RawX509KeySelector();
    private final static KeySelector XKS = null;
    private static URIDereferencer httpUd = null;

97 98 99 100 101
    private final static Test[] VALID_TESTS = {
        new Test("signature-enveloped-dsa.xml", KVKS),
        new Test("signature-enveloping-b64-dsa.xml", KVKS),
        new Test("signature-enveloping-dsa.xml", KVKS),
        new Test("signature-enveloping-rsa.xml", KVKS),
102
        new Test("signature-enveloping-p256-sha1.xml", KVKS),
103 104
        new Test("signature-enveloping-p384-sha1.xml", KVKS),
        new Test("signature-enveloping-p521-sha1.xml", KVKS),
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        new Test("signature-enveloping-hmac-sha1.xml", SKKS),
        new Test("signature-external-dsa.xml", KVKS),
        new Test("signature-external-b64-dsa.xml", KVKS),
        new Test("signature-retrievalmethod-rawx509crt.xml", CKS),
        new Test("signature-keyname.xml", CKS),
        new Test("signature-x509-crt-crl.xml", RXKS),
        new Test("signature-x509-crt.xml", RXKS),
        new Test("signature-x509-is.xml", CKS),
        new Test("signature-x509-ski.xml", CKS),
        new Test("signature-x509-sn.xml", CKS),
        new Test("signature.xml", XKS),
        new Test("exc-signature.xml", KVKS),
        new Test("sign-spec.xml", RXKS),
        new Test("xmldsig-xfilter2.xml", KVKS)
    };

    private final static Test[] INVALID_TESTS = {
        new Test("signature-enveloping-hmac-sha1-40.xml", SKKS),
        new Test("signature-enveloping-hmac-sha1-trunclen-0-attack.xml", SKKS),
124 125 126 127 128 129 130 131 132 133 134
        new Test("signature-enveloping-hmac-sha1-trunclen-8-attack.xml", SKKS),
        new Test("signature-extra-text-in-signed-info.xml", SKKS,
                MarshalException.class),
        new Test("signature-wrong-canonicalization-method-algorithm.xml", SKKS,
                MarshalException.class),
        new Test("signature-wrong-transform-algorithm.xml", SKKS,
                MarshalException.class),
        new Test("signature-no-reference-uri.xml", SKKS),
        new Test("signature-wrong-signature-method-algorithm.xml", SKKS,
                MarshalException.class),
        new Test("signature-wrong-tag-names.xml", SKKS, MarshalException.class)
135 136
    };

D
duke 已提交
137 138 139 140 141 142
    public static void main(String args[]) throws Exception {
        httpUd = new HttpURIDereferencer();

        validator = new SignatureValidator(new File(DATA_DIR));

        boolean atLeastOneFailed = false;
143 144 145
        for (Test test : VALID_TESTS) {
            System.out.println("Validating " + test.file);
            if (test_signature(test)) {
D
duke 已提交
146 147 148 149 150 151 152 153
                System.out.println("PASSED");
            } else {
                System.out.println("FAILED");
                atLeastOneFailed = true;
            }
        }
        // test with reference caching enabled
        System.out.println("Validating sign-spec.xml with caching enabled");
154
        if (test_signature(new Test("sign-spec.xml", RXKS), true)) {
D
duke 已提交
155 156 157 158 159 160
            System.out.println("PASSED");
        } else {
            System.out.println("FAILED");
            atLeastOneFailed = true;
        }

161 162 163 164 165 166
        for (Test test : INVALID_TESTS) {
            System.out.println("Validating " + test.file);
            try {
                test_signature(test);
                System.out.println("FAILED");
                atLeastOneFailed = true;
167 168 169 170 171 172 173 174
            } catch (Exception e) {
                System.out.println("Exception: " + e);
                if (e.getClass() != test.exception) {
                    System.out.println("FAILED: unexpected exception");
                    atLeastOneFailed = true;
                } else {
                    System.out.println("PASSED");
                }
175
            }
M
mullan 已提交
176 177
        }

D
duke 已提交
178 179 180 181 182 183
        if (atLeastOneFailed) {
            throw new Exception
                ("At least one signature did not validate as expected");
        }
    }

184 185
    public static boolean test_signature(Test test) throws Exception {
        return test_signature(test, false);
D
duke 已提交
186 187
    }

188 189 190 191
    public static boolean test_signature(Test test, boolean cache)
        throws Exception
    {
        if (test.ks == null) {
D
duke 已提交
192
            KeyStore keystore = KeyStore.getInstance("JKS");
193 194 195 196
            try (FileInputStream fis = new FileInputStream(KEYSTORE)) {
                keystore.load(fis, "changeit".toCharArray());
                test.ks = new X509KeySelector(keystore, false);
            }
D
duke 已提交
197
        }
198
        return validator.validate(test.file, test.ks, httpUd, cache);
D
duke 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    }

    /**
     * This URIDereferencer returns locally cached copies of http content to
     * avoid test failures due to network glitches, etc.
     */
    private static class HttpURIDereferencer implements URIDereferencer {
        private URIDereferencer defaultUd;

        HttpURIDereferencer() {
            defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer();
        }

        public Data dereference(final URIReference ref, XMLCryptoContext ctx)
        throws URIReferenceException {
            String uri = ref.getURI();
            if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) {
                try {
                    FileInputStream fis = new FileInputStream(new File
                        (DATA_DIR, uri.substring(uri.lastIndexOf('/'))));
                    return new OctetStreamData(fis,ref.getURI(),ref.getType());
                } catch (Exception e) { throw new URIReferenceException(e); }
            }

            // fallback on builtin deref
            return defaultUd.dereference(ref, ctx);
        }
    }
}