jdk/test/javax/crypto/CryptoPermissions/TestUnlimited.java
author wetmore
Mon, 05 Dec 2016 17:04:02 -0800
changeset 42365 5e640c2994d6
parent 40565 3ac0ba151e70
permissions -rw-r--r--
8170157: Enable unlimited cryptographic policy by default in OracleJDK 8169335: Add a crypto policy fallback in case Security Property 'crypto.policy' does not exist Reviewed-by: erikj, ihse, weijun, xuelei, coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     1
/*
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     4
 *
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    10
 *
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    15
 * accompanied this code).
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    16
 *
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    20
 *
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    23
 * questions.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    24
 */
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    25
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    26
/**
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    27
 * @test
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    28
 * @bug 8061842
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    29
 * @summary Package jurisdiction policy files as something other than JAR
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    30
 * @run main/othervm TestUnlimited use_default default
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    31
 * @run main/othervm TestUnlimited "" exception
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    32
 * @run main/othervm TestUnlimited limited limited
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    33
 * @run main/othervm TestUnlimited unlimited unlimited
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    34
 * @run main/othervm TestUnlimited unlimited/ unlimited
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    35
 * @run main/othervm TestUnlimited NosuchDir exception
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    36
 * @run main/othervm TestUnlimited . exception
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    37
 * @run main/othervm TestUnlimited /tmp/unlimited exception
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    38
 * @run main/othervm TestUnlimited ../policy/unlimited exception
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    39
 * @run main/othervm TestUnlimited ./unlimited exception
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    40
 * @run main/othervm TestUnlimited /unlimited exception
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    41
 */
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    42
import javax.crypto.*;
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    43
import java.security.Security;
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    44
import java.nio.file.*;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    45
import java.util.stream.*;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    46
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    47
public class TestUnlimited {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    48
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    49
    private enum Result {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    50
        UNLIMITED,
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    51
        LIMITED,
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    52
        EXCEPTION,
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    53
        UNKNOWN
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    54
    };
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    55
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    56
    /*
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    57
     * Grab the default policy entry from java.security.
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    58
     *
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    59
     * If the input java.security file is malformed
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    60
     * (missing crypto.policy, attribute/no value, etc), throw
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    61
     * exception.  split() might throw AIOOB which
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    62
     * is ok behavior.
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    63
     */
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    64
    private static String getDefaultPolicy() throws Exception {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    65
        String javaHome = System.getProperty("java.home");
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    66
        Path path = Paths.get(javaHome, "conf", "security", "java.security");
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    67
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    68
        try (Stream<String> lines = Files.lines(path)) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    69
            return lines.filter(x -> x.startsWith("crypto.policy="))
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    70
                    .findFirst().orElseThrow(
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    71
                            () -> new Exception("Missing crypto.policy"))
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    72
                    .split("=")[1].trim();
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    73
        }
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    74
    }
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    75
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    76
    public static void main(String[] args) throws Exception {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    77
        /*
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    78
         * Override the Security property to allow for unlimited policy.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    79
         * Would need appropriate permissions if Security Manager were
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    80
         * active.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    81
         */
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    82
        if (args.length != 2) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    83
            throw new Exception("Two args required");
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    84
        }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    85
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    86
        String testStr = args[0];
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    87
        String expectedStr = args[1];
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    88
        if (testStr.equals("use_default")) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    89
            expectedStr = getDefaultPolicy();
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    90
        }
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    91
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    92
        Result expected = Result.UNKNOWN;  // avoid NPE warnings
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    93
        Result result;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
    94
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    95
        switch (expectedStr) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    96
        case "unlimited":
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    97
            expected = Result.UNLIMITED;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    98
            break;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
    99
        case "limited":
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   100
            expected = Result.LIMITED;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   101
            break;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   102
        case "exception":
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   103
            expected = Result.EXCEPTION;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   104
            break;
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   105
        default:
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   106
            throw new Exception("Unexpected argument");
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   107
        }
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   108
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   109
        System.out.println("Testing: " + testStr);
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   110
        if (testStr.equals("\"\"")) {
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   111
            Security.setProperty("crypto.policy", "");
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   112
        } else {
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   113
            // skip default case.
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   114
            if (!testStr.equals("use_default")) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   115
                Security.setProperty("crypto.policy", testStr);
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   116
            }
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   117
        }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   118
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   119
        /*
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   120
         * Use the AES as the test Cipher
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   121
         * If there is an error initializing, we will never get past here.
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   122
         */
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   123
        try {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   124
            int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   125
            System.out.println("max AES key len:" + maxKeyLen);
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   126
            if (maxKeyLen > 128) {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   127
                System.out.println("Unlimited policy is active");
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   128
                result = Result.UNLIMITED;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   129
            } else {
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   130
                System.out.println("Unlimited policy is NOT active");
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   131
                result = Result.LIMITED;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   132
            }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   133
        } catch (Throwable e) {
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   134
            //ExceptionInInitializerError's
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   135
            result = Result.EXCEPTION;
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   136
        }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   137
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   138
        System.out.println(
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   139
                "Expected:\t" + expected + "\nResult:\t\t" + result);
42365
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   140
        if (!expected.equals(result)) {
5e640c2994d6 8170157: Enable unlimited cryptographic policy by default in OracleJDK
wetmore
parents: 40565
diff changeset
   141
            throw new Exception("Didn't match");
40565
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   142
        }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   143
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   144
        System.out.println("DONE!");
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   145
    }
3ac0ba151e70 8061842: Package jurisdiction policy files as something other than JAR
wetmore
parents:
diff changeset
   146
}