1 /*
   2  * Copyright (c) 2014, 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 8029849
  27  * @summary Make sure signing via encrypt and verifying via decrypt are not
  28  * supported by OracleUcrypto provider.
  29  * @author Anthony Scarpino
  30  */
  31 
  32 import java.util.Random;
  33 import java.security.KeyPairGenerator;
  34 import java.security.KeyPair;
  35 import javax.crypto.Cipher;
  36 import java.security.InvalidKeyException;
  37 import java.security.NoSuchAlgorithmException;
  38 import java.security.Provider;
  39 
  40 public class CipherSignNotSupported extends UcryptoTest {
  41 
  42     public static void main(String[] args) throws Exception {
  43         main(new CipherSignNotSupported(), null);
  44     }
  45 
  46     public void doTest(Provider p) throws Exception {
  47         Cipher c = null;
  48         Random random = new Random();
  49         byte[] pt = new byte[117];
  50         byte[] ct = new byte[200];
  51         random.nextBytes(pt);
  52 
  53         try {
  54             c = Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
  55         } catch (NoSuchAlgorithmException e) {
  56             if (System.getProperty("os.version").compareTo("5.10") == 0) {
  57                 System.out.println("RSA not supported in S10");
  58                 return;
  59             }
  60             throw e;
  61         }
  62 
  63         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  64         kpg.initialize(1024);
  65         KeyPair kp = kpg.generateKeyPair();
  66 
  67         // Encryption
  68         c.init(Cipher.ENCRYPT_MODE, kp.getPublic());
  69         ct = c.doFinal(pt);
  70         // Decryption
  71         c.init(Cipher.DECRYPT_MODE, kp.getPrivate());
  72         c.doFinal(ct);
  73         // Sign
  74         try {
  75             c.init(Cipher.ENCRYPT_MODE, kp.getPrivate());
  76             ct = c.doFinal(pt);
  77             throw new RuntimeException("Encrypt operation should have failed.");
  78         } catch (InvalidKeyException e) {
  79             if (e.getMessage().compareTo("RSAPublicKey required for " +
  80                     "encryption") != 0) {
  81                 System.out.println("Wrong exception thrown.");
  82                 throw e;
  83             }
  84         }
  85         // Verify
  86         try {
  87             c.init(Cipher.DECRYPT_MODE, kp.getPublic());
  88             c.doFinal(ct);
  89             throw new RuntimeException("Decrypt operation should have failed.");
  90         } catch (InvalidKeyException e) {
  91             if (e.getMessage().compareTo("RSAPrivateCrtKey required for " +
  92                     "decryption") != 0) {
  93                 System.out.println("Wrong exception thrown.");
  94                 throw e;
  95             }
  96         }
  97 
  98         System.out.println("Pass");
  99     }
 100 }