1 /*
   2  * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4953554
  27  * @summary Ensure either IllegalAlgorithmParameterException or
  28  * InvalidKeyException is thrown instead of SecurityException when
  29  * crypto permssion checks failed.
  30  * @author Valerie Peng
  31  * @key randomness
  32  */
  33 
  34 import java.io.*;
  35 import java.util.*;
  36 
  37 import java.security.*;
  38 import java.security.spec.*;
  39 
  40 import javax.crypto.*;
  41 import javax.crypto.spec.*;
  42 
  43 public class AllPermCheck {
  44 
  45     private static String SYM_ALGOS[] = {
  46         "AES", "Blowfish", "RC2", "ARCFOUR"
  47     };
  48 
  49     public static void runTest(Cipher c, Key key) throws Exception {
  50         SecureRandom sr = new SecureRandom();
  51 
  52         for (int i = 0; i < 6; i++) {
  53             try {
  54                 switch (i) {
  55                 case 0:
  56                     c.init(Cipher.ENCRYPT_MODE, key);
  57                     break;
  58                 case 1:
  59                     c.init(Cipher.ENCRYPT_MODE, key, sr);
  60                     break;
  61                 case 2:
  62                     c.init(Cipher.ENCRYPT_MODE, key,
  63                            (AlgorithmParameters)null);
  64                     break;
  65                 case 3:
  66                     c.init(Cipher.ENCRYPT_MODE, key,
  67                            (AlgorithmParameters)null, sr);
  68                     break;
  69                 case 4:
  70                     c.init(Cipher.ENCRYPT_MODE, key,
  71                            (AlgorithmParameterSpec)null);
  72                     break;
  73                 case 5:
  74                     c.init(Cipher.ENCRYPT_MODE, key,
  75                            (AlgorithmParameterSpec)null, sr);
  76                     break;
  77                 }
  78                 throw new Exception("...#" + i + " should throw IKE for " +
  79                                     key.getEncoded().length + "-byte keys");
  80             } catch (InvalidKeyException ike) {
  81                 System.out.println("...#" + i + " expected IKE thrown");
  82             }
  83         }
  84     }
  85 
  86     public static void main(String[] args) throws Exception {
  87         Provider p = Security.getProvider("SunJCE");
  88         System.out.println("Testing provider " + p.getName() + "...");
  89         if (Cipher.getMaxAllowedKeyLength("DES") == Integer.MAX_VALUE) {
  90             // skip this test for unlimited jurisdiction policy files
  91             System.out.println("Skip this test due to unlimited version");
  92             return;
  93         }
  94         for (int i = 0; i < SYM_ALGOS.length; i++) {
  95             String algo = SYM_ALGOS[i];
  96             Cipher c = Cipher.getInstance(algo, p);
  97             int keyLength = Cipher.getMaxAllowedKeyLength(algo);
  98             SecretKey key = new SecretKeySpec(new byte[keyLength/8 + 8], algo);
  99             System.out.println("Testing " + algo + " Cipher");
 100             runTest(c, key);
 101         }
 102         System.out.println("All tests passed!");
 103     }
 104 }