jdk/src/share/classes/com/sun/crypto/provider/DESKeyFactory.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.crypto.SecretKeyFactorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.spec.DESKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class implements the DES key factory of the Sun provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public final class DESKeyFactory extends SecretKeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Verify the SunJCE provider in the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @exception SecurityException if fails to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * its own integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public DESKeyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (!SunJCE.verifySelfIntegrity(this.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            throw new SecurityException("The SunJCE provider may have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                        "been tampered.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Generates a <code>SecretKey</code> object from the provided key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * specification (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param keySpec the specification (key material) of the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @return the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected SecretKey engineGenerateSecret(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        DESKey desKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (!(keySpec instanceof DESKeySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                DESKeySpec desKeySpec = (DESKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                desKey = new DESKey(desKeySpec.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return desKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns a specification (key material) of the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected KeySpec engineGetKeySpec(SecretKey key, Class keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if ((key instanceof SecretKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                && (key.getAlgorithm().equalsIgnoreCase("DES"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                && (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // Check if requested key spec is amongst the valid ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if ((keySpec != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    DESKeySpec.class.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    return new DESKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    ("Inappropriate key format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new InvalidKeySpecException("Secret key has wrong size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Translates a <code>SecretKey</code> object, whose provider may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * unknown or potentially untrusted, into a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>SecretKey</code> object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception InvalidKeyException if the given key cannot be processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected SecretKey engineTranslateKey(SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if ((key != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                (key.getAlgorithm().equalsIgnoreCase("DES")) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (key instanceof com.sun.crypto.provider.DESKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                DESKeySpec desKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    = (DESKeySpec)engineGetKeySpec(key, DESKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                return engineGenerateSecret(desKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    ("Inappropriate key format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new InvalidKeyException("Cannot translate key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}