jdk/test/javax/crypto/CryptoPermission/RC2PermCheck.java
author darcy
Wed, 29 Apr 2015 10:25:53 -0700
changeset 30046 cf2c86e1819e
parent 5506 202f599c92aa
permissions -rw-r--r--
8078334: Mark regression tests using randomness Reviewed-by: xuelei, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4892365
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Ensure the crypto permission check on cipher algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * with restricted parameter values are correctly enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Valerie Peng
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5506
diff changeset
    30
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class RC2PermCheck {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Provider p = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.out.println("Testing provider " + p.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (Cipher.getMaxAllowedKeyLength("DES") == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            // skip this test for unlimited jurisdiction policy files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            System.out.println("Skip this test due to unlimited version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // Currently, RC2 is the only algorithm whose parameter values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // are restricted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        String algo = "RC2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Cipher c = Cipher.getInstance(algo + "/CBC/PKCS5Padding", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        SecretKeySpec key = new SecretKeySpec(new byte[16], "RC2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        SecureRandom srand = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int numOfTests = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        boolean result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // test set#1: init with no parameter supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i = 0; i < numOfTests; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    c.init(Cipher.ENCRYPT_MODE, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    c.init(Cipher.ENCRYPT_MODE, key, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    c.init(Cipher.ENCRYPT_MODE, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                           (AlgorithmParameters) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    c.init(Cipher.ENCRYPT_MODE, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                           (AlgorithmParameters) null, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    c.init(Cipher.ENCRYPT_MODE, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                           (AlgorithmParameterSpec) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    c.init(Cipher.ENCRYPT_MODE, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                           (AlgorithmParameterSpec) null, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                System.out.println("Test#1." + i + " failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // test set#2: init with parameter within limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        RC2ParameterSpec paramSpec = new RC2ParameterSpec(128, new byte[8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        AlgorithmParameters param = AlgorithmParameters.getInstance(algo, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        param.init(paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        numOfTests = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < numOfTests; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    c.init(Cipher.ENCRYPT_MODE, key, paramSpec, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    c.init(Cipher.ENCRYPT_MODE, key, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    c.init(Cipher.ENCRYPT_MODE, key, param, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.out.println("Test#2." + i + " failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // test set#3: init with parameter over limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        paramSpec = new RC2ParameterSpec(256, new byte[8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        param = AlgorithmParameters.getInstance(algo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        param.init(paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i = 0; i < numOfTests; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    System.out.println("Test#3." + i + " failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    c.init(Cipher.ENCRYPT_MODE, key, paramSpec, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    System.out.println("Test#3." + i + " failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    c.init(Cipher.ENCRYPT_MODE, key, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    System.out.println("Test#3." + i + " failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    c.init(Cipher.ENCRYPT_MODE, key, param, srand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    System.out.println("Test#3." + i + " failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // expected exception thrown; proceed to next test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            System.out.println("All tests passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new Exception("One or more test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}