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