1 /*
   2  * Copyright (c) 2005, 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 6231216
  27  * @summary Verify key wrapping (of extractable keys) works for RSA/PKCS1
  28  * @author Andreas Sterbenz
  29  * @library ..
  30  * @key randomness
  31  */
  32 
  33 import java.io.*;
  34 import java.util.*;
  35 
  36 import java.security.*;
  37 
  38 import javax.crypto.*;
  39 import javax.crypto.spec.*;
  40 
  41 public class KeyWrap extends PKCS11Test {
  42 
  43     public void main(Provider p) throws Exception {
  44         try {
  45             Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
  46         } catch (GeneralSecurityException e) {
  47             System.out.println("Not supported by provider, skipping");
  48             return;
  49         }
  50         KeyPair kp;
  51         try {
  52             KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
  53             kpg.initialize(512);
  54             kp = kpg.generateKeyPair();
  55         } catch (Exception e) {
  56             try {
  57                 System.out.println("Could not generate KeyPair on provider " + p + ", trying migration");
  58                 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  59                 kpg.initialize(512);
  60                 kp = kpg.generateKeyPair();
  61                 KeyFactory kf = KeyFactory.getInstance("RSA", p);
  62                 PublicKey pub = (PublicKey)kf.translateKey(kp.getPublic());
  63                 PrivateKey priv = (PrivateKey)kf.translateKey(kp.getPrivate());
  64                 kp = new KeyPair(pub, priv);
  65             } catch (Exception ee) {
  66                 ee.printStackTrace();
  67                 System.out.println("Provider does not support RSA, skipping");
  68                 return;
  69             }
  70         }
  71         System.out.println(kp);
  72         Random r = new Random();
  73         byte[] b = new byte[16];
  74         r.nextBytes(b);
  75         String alg = "AES";
  76         SecretKey key = new SecretKeySpec(b, alg);
  77 
  78         Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
  79 //      Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  80         c.init(Cipher.WRAP_MODE, kp.getPublic());
  81         byte[] wrapped = c.wrap(key);
  82         System.out.println("wrapped: " + wrapped.length);
  83 
  84         c.init(Cipher.UNWRAP_MODE, kp.getPrivate());
  85         Key unwrapped = c.unwrap(wrapped, alg, Cipher.SECRET_KEY);
  86         System.out.println("unwrapped: " + unwrapped);
  87 
  88         boolean eq = key.equals(unwrapped);
  89         System.out.println(eq);
  90         if (eq == false) {
  91             throw new Exception("Unwrapped key does not match original key");
  92         }
  93     }
  94 
  95     public static void main(String[] args) throws Exception {
  96         main(new KeyWrap());
  97     }
  98 
  99 }