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