Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]
    1   /*
    2    * Copyright (c) 2005, 2009, 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.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package com.sun.crypto.provider;
   27   
   28   import java.io;
   29   import java.security.InvalidKeyException;
   30   import java.security.spec.KeySpec;
   31   import java.security.spec.InvalidKeySpecException;
   32   import javax.crypto.SecretKey;
   33   import javax.crypto.SecretKeyFactorySpi;
   34   import javax.crypto.spec.PBEKeySpec;
   35   import javax.crypto.spec.SecretKeySpec;
   36   
   37   /**
   38    * This class implements a key factory for PBE keys derived using
   39    * PBKDF2 with HmacSHA1 psuedo random function(PRF) as defined in
   40    * PKCS#5 v2.0.
   41    *
   42    * @author Valerie Peng
   43    *
   44    */
   45   public final class PBKDF2HmacSHA1Factory extends SecretKeyFactorySpi {
   46   
   47       /**
   48        * Empty constructor
   49        */
   50       public PBKDF2HmacSHA1Factory() {
   51       }
   52   
   53       /**
   54        * Generates a <code>SecretKey</code> object from the provided key
   55        * specification (key material).
   56        *
   57        * @param keySpec the specification (key material) of the secret key
   58        *
   59        * @return the secret key
   60        *
   61        * @exception InvalidKeySpecException if the given key specification
   62        * is inappropriate for this key factory to produce a public key.
   63        */
   64       protected SecretKey engineGenerateSecret(KeySpec keySpec)
   65           throws InvalidKeySpecException
   66       {
   67           if (!(keySpec instanceof PBEKeySpec)) {
   68               throw new InvalidKeySpecException("Invalid key spec");
   69           }
   70           PBEKeySpec ks = (PBEKeySpec) keySpec;
   71           return new PBKDF2KeyImpl(ks, "HmacSHA1");
   72       }
   73   
   74       /**
   75        * Returns a specification (key material) of the given key
   76        * in the requested format.
   77        *
   78        * @param key the key
   79        *
   80        * @param keySpec the requested format in which the key material shall be
   81        * returned
   82        *
   83        * @return the underlying key specification (key material) in the
   84        * requested format
   85        *
   86        * @exception InvalidKeySpecException if the requested key
   87        * specification is inappropriate for the given key, or the
   88        * given key cannot be processed (e.g., the given key has an
   89        * unrecognized algorithm or format).
   90        */
   91       protected KeySpec engineGetKeySpec(SecretKey key, Class keySpecCl)
   92           throws InvalidKeySpecException {
   93           if (key instanceof javax.crypto.interfaces.PBEKey) {
   94               // Check if requested key spec is amongst the valid ones
   95               if ((keySpecCl != null)
   96                   && PBEKeySpec.class.isAssignableFrom(keySpecCl)) {
   97                   javax.crypto.interfaces.PBEKey pKey =
   98                       (javax.crypto.interfaces.PBEKey) key;
   99                   return new PBEKeySpec
  100                       (pKey.getPassword(), pKey.getSalt(),
  101                        pKey.getIterationCount(), pKey.getEncoded().length*8);
  102               } else {
  103                   throw new InvalidKeySpecException("Invalid key spec");
  104               }
  105           } else {
  106               throw new InvalidKeySpecException("Invalid key " +
  107                                                  "format/algorithm");
  108           }
  109       }
  110   
  111       /**
  112        * Translates a <code>SecretKey</code> object, whose provider may be
  113        * unknown or potentially untrusted, into a corresponding
  114        * <code>SecretKey</code> object of this key factory.
  115        *
  116        * @param key the key whose provider is unknown or untrusted
  117        *
  118        * @return the translated key
  119        *
  120        * @exception InvalidKeyException if the given key cannot be processed by
  121        * this key factory.
  122        */
  123       protected SecretKey engineTranslateKey(SecretKey key)
  124           throws InvalidKeyException {
  125           if ((key != null) &&
  126               (key.getAlgorithm().equalsIgnoreCase("PBKDF2WithHmacSHA1")) &&
  127               (key.getFormat().equalsIgnoreCase("RAW"))) {
  128   
  129               // Check if key originates from this factory
  130               if (key instanceof com.sun.crypto.provider.PBKDF2KeyImpl) {
  131                   return key;
  132               }
  133               // Check if key implements the PBEKey
  134               if (key instanceof javax.crypto.interfaces.PBEKey) {
  135                   javax.crypto.interfaces.PBEKey pKey =
  136                       (javax.crypto.interfaces.PBEKey) key;
  137                   try {
  138                       PBEKeySpec spec =
  139                           new PBEKeySpec(pKey.getPassword(),
  140                                          pKey.getSalt(),
  141                                          pKey.getIterationCount(),
  142                                          pKey.getEncoded().length*8);
  143                       return new PBKDF2KeyImpl(spec, "HmacSHA1");
  144                   } catch (InvalidKeySpecException re) {
  145                       InvalidKeyException ike = new InvalidKeyException
  146                           ("Invalid key component(s)");
  147                       ike.initCause(re);
  148                       throw ike;
  149                   }
  150               }
  151           }
  152           throw new InvalidKeyException("Invalid key format/algorithm");
  153       }
  154   }

Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]