1 /*
   2  * Copyright (c) 2010, 2016, 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 6687725
  27  * @summary Test internal PKCS5Padding impl with various error conditions.
  28  * @author Valerie Peng
  29  * @library ..
  30  * @modules jdk.crypto.pkcs11
  31  * @run main/othervm TestPKCS5PaddingError
  32  * @run main/othervm TestPKCS5PaddingError sm
  33  */
  34 
  35 import java.security.AlgorithmParameters;
  36 import java.security.NoSuchAlgorithmException;
  37 import java.security.Provider;
  38 import javax.crypto.BadPaddingException;
  39 import javax.crypto.Cipher;
  40 import javax.crypto.IllegalBlockSizeException;
  41 import javax.crypto.KeyGenerator;
  42 import javax.crypto.SecretKey;
  43 
  44 public class TestPKCS5PaddingError extends PKCS11Test {
  45     private static class CI { // class for holding Cipher Information
  46         String transformation;
  47         String keyAlgo;
  48 
  49         CI(String transformation, String keyAlgo) {
  50             this.transformation = transformation;
  51             this.keyAlgo = keyAlgo;
  52         }
  53     }
  54 
  55     private static final CI[] TEST_LIST = {
  56         // algorithms which use the native padding impl
  57         new CI("DES/CBC/PKCS5Padding", "DES"),
  58         new CI("DESede/CBC/PKCS5Padding", "DESede"),
  59         new CI("AES/CBC/PKCS5Padding", "AES"),
  60         // algorithms which use SunPKCS11's own padding impl
  61         new CI("DES/ECB/PKCS5Padding", "DES"),
  62         new CI("DESede/ECB/PKCS5Padding", "DESede"),
  63         new CI("AES/ECB/PKCS5Padding", "AES"),
  64     };
  65 
  66     private static StringBuffer debugBuf = new StringBuffer();
  67 
  68     @Override
  69     public void main(Provider p) throws Exception {
  70         try {
  71             byte[] plainText = new byte[200];
  72 
  73             for (int i = 0; i < TEST_LIST.length; i++) {
  74                 CI currTest = TEST_LIST[i];
  75                 System.out.println("===" + currTest.transformation + "===");
  76                 try {
  77                     KeyGenerator kg =
  78                             KeyGenerator.getInstance(currTest.keyAlgo, p);
  79                     SecretKey key = kg.generateKey();
  80                     Cipher c1 = Cipher.getInstance(currTest.transformation,
  81                                                    "SunJCE");
  82                     c1.init(Cipher.ENCRYPT_MODE, key);
  83                     byte[] cipherText = c1.doFinal(plainText);
  84                     AlgorithmParameters params = c1.getParameters();
  85                     Cipher c2 = Cipher.getInstance(currTest.transformation, p);
  86                     c2.init(Cipher.DECRYPT_MODE, key, params);
  87 
  88                     // 1st test: wrong output length
  89                     // NOTE: Skip NSS since it reports CKR_DEVICE_ERROR when
  90                     // the data passed to its EncryptUpdate/DecryptUpdate is
  91                     // not multiple of blocks
  92                     if (!p.getName().equals("SunPKCS11-NSS")) {
  93                         try {
  94                             System.out.println("Testing with wrong cipherText length");
  95                             c2.doFinal(cipherText, 0, cipherText.length - 2);
  96                         } catch (IllegalBlockSizeException ibe) {
  97                             // expected
  98                         } catch (Exception ex) {
  99                             System.out.println("Error: Unexpected Ex " + ex);
 100                             ex.printStackTrace();
 101                         }
 102                     }
 103                     // 2nd test: wrong padding value
 104                     try {
 105                         System.out.println("Testing with wrong padding bytes");
 106                         cipherText[cipherText.length - 1]++;
 107                         c2.doFinal(cipherText);
 108                     } catch (BadPaddingException bpe) {
 109                         // expected
 110                     } catch (Exception ex) {
 111                         System.out.println("Error: Unexpected Ex " + ex);
 112                         ex.printStackTrace();
 113                     }
 114                     System.out.println("DONE");
 115                 } catch (NoSuchAlgorithmException nsae) {
 116                     System.out.println("Skipping unsupported algorithm: " +
 117                             nsae);
 118                 }
 119             }
 120         } catch (Exception ex) {
 121             // print out debug info when exception is encountered
 122             if (debugBuf != null) {
 123                 System.out.println(debugBuf.toString());
 124                 debugBuf = new StringBuffer();
 125             }
 126             throw ex;
 127         }
 128     }
 129 
 130     public static void main(String[] args) throws Exception {
 131         main(new TestPKCS5PaddingError(), args);
 132     }
 133 }