Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]
    1   /*
    2    * Copyright (c) 1997, 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 javax.crypto.SecretKey;
   29   import javax.crypto.SecretKeyFactorySpi;
   30   import javax.crypto.spec.DESKeySpec;
   31   import java.security.InvalidKeyException;
   32   import java.security.spec.KeySpec;
   33   import java.security.spec.InvalidKeySpecException;
   34   
   35   /**
   36    * This class implements the DES key factory of the Sun provider.
   37    *
   38    * @author Jan Luehe
   39    *
   40    */
   41   
   42   public final class DESKeyFactory extends SecretKeyFactorySpi {
   43   
   44       /**
   45        * Empty constructor
   46        */
   47       public DESKeyFactory() {
   48       }
   49   
   50       /**
   51        * Generates a <code>SecretKey</code> object from the provided key
   52        * specification (key material).
   53        *
   54        * @param keySpec the specification (key material) of the secret key
   55        *
   56        * @return the secret key
   57        *
   58        * @exception InvalidKeySpecException if the given key specification
   59        * is inappropriate for this key factory to produce a public key.
   60        */
   61       protected SecretKey engineGenerateSecret(KeySpec keySpec)
   62           throws InvalidKeySpecException {
   63           DESKey desKey = null;
   64   
   65           try {
   66               if (!(keySpec instanceof DESKeySpec)) {
   67                   throw new InvalidKeySpecException
   68                       ("Inappropriate key specification");
   69               }
   70               else {
   71                   DESKeySpec desKeySpec = (DESKeySpec)keySpec;
   72                   desKey = new DESKey(desKeySpec.getKey());
   73               }
   74           } catch (InvalidKeyException e) {
   75           }
   76           return desKey;
   77       }
   78   
   79       /**
   80        * Returns a specification (key material) of the given key
   81        * in the requested format.
   82        *
   83        * @param key the key
   84        *
   85        * @param keySpec the requested format in which the key material shall be
   86        * returned
   87        *
   88        * @return the underlying key specification (key material) in the
   89        * requested format
   90        *
   91        * @exception InvalidKeySpecException if the requested key specification is
   92        * inappropriate for the given key, or the given key cannot be processed
   93        * (e.g., the given key has an unrecognized algorithm or format).
   94        */
   95       protected KeySpec engineGetKeySpec(SecretKey key, Class keySpec)
   96           throws InvalidKeySpecException {
   97   
   98           try {
   99   
  100               if ((key instanceof SecretKey)
  101                   && (key.getAlgorithm().equalsIgnoreCase("DES"))
  102                   && (key.getFormat().equalsIgnoreCase("RAW"))) {
  103   
  104                   // Check if requested key spec is amongst the valid ones
  105                   if ((keySpec != null) &&
  106                       DESKeySpec.class.isAssignableFrom(keySpec)) {
  107                       return new DESKeySpec(key.getEncoded());
  108   
  109                   } else {
  110                       throw new InvalidKeySpecException
  111                           ("Inappropriate key specification");
  112                   }
  113   
  114               } else {
  115                   throw new InvalidKeySpecException
  116                       ("Inappropriate key format/algorithm");
  117               }
  118   
  119           } catch (InvalidKeyException e) {
  120               throw new InvalidKeySpecException("Secret key has wrong size");
  121           }
  122       }
  123   
  124       /**
  125        * Translates a <code>SecretKey</code> object, whose provider may be
  126        * unknown or potentially untrusted, into a corresponding
  127        * <code>SecretKey</code> object of this key factory.
  128        *
  129        * @param key the key whose provider is unknown or untrusted
  130        *
  131        * @return the translated key
  132        *
  133        * @exception InvalidKeyException if the given key cannot be processed by
  134        * this key factory.
  135        */
  136       protected SecretKey engineTranslateKey(SecretKey key)
  137           throws InvalidKeyException {
  138   
  139           try {
  140   
  141               if ((key != null) &&
  142                   (key.getAlgorithm().equalsIgnoreCase("DES")) &&
  143                   (key.getFormat().equalsIgnoreCase("RAW"))) {
  144   
  145                   // Check if key originates from this factory
  146                   if (key instanceof com.sun.crypto.provider.DESKey) {
  147                       return key;
  148                   }
  149                   // Convert key to spec
  150                   DESKeySpec desKeySpec
  151                       = (DESKeySpec)engineGetKeySpec(key, DESKeySpec.class);
  152                   // Create key from spec, and return it
  153                   return engineGenerateSecret(desKeySpec);
  154   
  155               } else {
  156                   throw new InvalidKeyException
  157                       ("Inappropriate key format/algorithm");
  158               }
  159   
  160           } catch (InvalidKeySpecException e) {
  161               throw new InvalidKeyException("Cannot translate key");
  162           }
  163       }
  164   }

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