jdk/src/share/classes/com/sun/crypto/provider/PBEKeyFactory.java
author valeriep
Tue, 08 May 2012 17:57:48 -0700
changeset 12685 8a448b5b9006
parent 10336 0bb1999251f8
child 14405 e7fff80005c1
permissions -rw-r--r--
4963723: Implement SHA-224 Summary: Add support for SHA-224, SHA224withRSA, SHA224withECDSA, HmacSHA224 and OAEPwithSHA-224AndMGF1Padding. Reviewed-by: vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
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 java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.SecretKeyFactorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.spec.PBEKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements a key factory for PBE keys according to PKCS#5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * meaning that the password must consist of printable ASCII characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (values 32 to 126 decimal inclusive) and only the low order 8 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of each password character are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
abstract class PBEKeyFactory extends SecretKeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static HashSet<String> validTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
3353
ddbd63234844 6647452: Remove obfuscation, framework and provider self-verification checking
wetmore
parents: 2
diff changeset
    51
     * Simple constructor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private PBEKeyFactory(String keytype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        type = keytype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        validTypes = new HashSet<String>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        validTypes.add("PBEWithMD5AndDES".toUpperCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        validTypes.add("PBEWithSHA1AndDESede".toUpperCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        validTypes.add("PBEWithSHA1AndRC2_40".toUpperCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // Proprietary algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        validTypes.add("PBEWithMD5AndTripleDES".toUpperCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final class PBEWithMD5AndDES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public PBEWithMD5AndDES()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            super("PBEWithMD5AndDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final class PBEWithSHA1AndDESede
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public PBEWithSHA1AndDESede()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            super("PBEWithSHA1AndDESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static final class PBEWithSHA1AndRC2_40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public PBEWithSHA1AndRC2_40()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            super("PBEWithSHA1AndRC2_40");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Private proprietary algorithm for supporting JCEKS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final class PBEWithMD5AndTripleDES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            extends PBEKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public PBEWithMD5AndTripleDES()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            super("PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Generates a <code>SecretKey</code> object from the provided key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * specification (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param keySpec the specification (key material) of the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected SecretKey engineGenerateSecret(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!(keySpec instanceof PBEKeySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new InvalidKeySpecException("Invalid key spec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return new PBEKey((PBEKeySpec)keySpec, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns a specification (key material) of the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   134
    protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if ((key instanceof SecretKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            && (validTypes.contains(key.getAlgorithm().toUpperCase()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            && (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // Check if requested key spec is amongst the valid ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if ((keySpecCl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                && PBEKeySpec.class.isAssignableFrom(keySpecCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                byte[] passwdBytes = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                char[] passwdChars = new char[passwdBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                for (int i=0; i<passwdChars.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                PBEKeySpec ret = new PBEKeySpec(passwdChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // password char[] was cloned in PBEKeySpec constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // so we can zero it out here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                java.util.Arrays.fill(passwdChars, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                java.util.Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new InvalidKeySpecException("Invalid key spec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new InvalidKeySpecException("Invalid key "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                              + "format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Translates a <code>SecretKey</code> object, whose provider may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * unknown or potentially untrusted, into a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <code>SecretKey</code> object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception InvalidKeyException if the given key cannot be processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected SecretKey engineTranslateKey(SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if ((key != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                (validTypes.contains(key.getAlgorithm().toUpperCase())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                (key.getFormat().equalsIgnoreCase("RAW"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (key instanceof com.sun.crypto.provider.PBEKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    (key, PBEKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                return engineGenerateSecret(pbeKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                throw new InvalidKeyException("Invalid key format/algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (InvalidKeySpecException ikse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new InvalidKeyException("Cannot translate key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                          + ikse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}