src/java.base/share/classes/com/sun/crypto/provider/KeyProtector.java
author weijun
Fri, 12 Jan 2018 08:06:24 +0800
changeset 49783 977c6dd636bd
parent 47417 5984d1c9d03d
child 51293 53c3b460503c
permissions -rw-r--r--
8189997: Enhance keystore mechanisms 8194259: keytool error: java.io.IOException: Invalid secret key format Reviewed-by: mullan, valeriep, rriggs, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47417
diff changeset
     2
 * Copyright (c) 1998, 2018, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AlgorithmParameters;
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
    38
import java.security.spec.InvalidParameterSpecException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.CipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.crypto.SealedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class implements a protection mechanism for private keys. In JCE, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * use a stronger protection mechanism than in the JDK, because we can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the <code>Cipher</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Private keys are protected using the JCE mechanism, and are recovered using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * either the JDK or JCE mechanism, depending on how the key has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * protected. This allows us to parse Sun's keystore implementation that ships
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * with JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see JceKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
final class KeyProtector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // defined by SunSoft (SKI project)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final String PBE_WITH_MD5_AND_DES3_CBC_OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            = "1.3.6.1.4.1.42.2.19.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // JavaSoft proprietary key-protection algorithm (used to protect private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // keys in the keystore implementation that comes with JDK 1.2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final String KEY_PROTECTOR_OID = "1.3.6.1.4.1.42.2.17.1.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
    74
    private static final int MAX_ITERATION_COUNT = 5000000;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
    75
    private static final int ITERATION_COUNT = 200000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int SALT_LEN = 20; // the salt length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int DIGEST_LEN = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // the password used for protecting/recovering keys passed through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // key protector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    KeyProtector(char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
           throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.password = password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Protects the given cleartext private key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    byte[] protect(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // create a random salt (8 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        byte[] salt = new byte[8];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14317
diff changeset
    99
        SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // create PBE parameters from salt and iteration count
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   102
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // encrypt private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        PBEWithMD5AndTripleDESCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        cipher = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        cipher.engineInit(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   113
        byte[] plain = key.getEncoded();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        byte[] encrKey = cipher.engineDoFinal(plain, 0, plain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // wrap encrypted private key in EncryptedPrivateKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // (as defined in PKCS#8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        AlgorithmParameters pbeParams =
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   119
            AlgorithmParameters.getInstance("PBE", SunJCE.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        pbeParams.init(pbeSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        AlgorithmId encrAlg = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            (new ObjectIdentifier(PBE_WITH_MD5_AND_DES3_CBC_OID), pbeParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Recovers the cleartext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * using the password provided at construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    Key recover(EncryptedPrivateKeyInfo encrInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws UnrecoverableKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        byte[] plain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            String encrAlg = encrInfo.getAlgorithm().getOID().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (!encrAlg.equals(PBE_WITH_MD5_AND_DES3_CBC_OID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                && !encrAlg.equals(KEY_PROTECTOR_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new UnrecoverableKeyException("Unsupported encryption "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                    + "algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (encrAlg.equals(KEY_PROTECTOR_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // JDK 1.2 style recovery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                plain = recover(encrInfo.getEncryptedData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                byte[] encodedParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    encrInfo.getAlgorithm().getEncodedParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // parse the PBE parameters into the corresponding spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                AlgorithmParameters pbeParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    AlgorithmParameters.getInstance("PBE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                pbeParams.init(encodedParams);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   155
                PBEParameterSpec pbeSpec =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   156
                        pbeParams.getParameterSpec(PBEParameterSpec.class);
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   157
                if (pbeSpec.getIterationCount() > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   158
                    throw new IOException("PBE iteration count too large");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   159
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                SecretKey sKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                // decrypt private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                PBEWithMD5AndTripleDESCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                cipher = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                cipher.engineInit(Cipher.DECRYPT_MODE, sKey, pbeSpec, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                plain=cipher.engineDoFinal(encrInfo.getEncryptedData(), 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                           encrInfo.getEncryptedData().length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // determine the private-key algorithm, and parse private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // using the appropriate key factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            String oidName = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                (new PrivateKeyInfo(plain).getAlgorithm().getOID()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            KeyFactory kFac = KeyFactory.getInstance(oidName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return kFac.generatePrivate(new PKCS8EncodedKeySpec(plain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // Note: this catch needed to be here because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // later catch of GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new UnrecoverableKeyException(gse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Recovers the cleartext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * using the password provided at construction time. This method implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the recovery algorithm used by Sun's keystore implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private byte[] recover(byte[] protectedKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throws UnrecoverableKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int encrKeyLen; // the length of the encrpyted key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Get the salt associated with this key (the first SALT_LEN bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // <code>protectedKey</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        numRounds = encrKeyLen / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if ((encrKeyLen % DIGEST_LEN) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // Get the encrypted key portion and store it in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        byte[] encrKey = new byte[encrKeyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Set up the byte array which will be XORed with "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        byte[] xorKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Convert password to byte array, so that it can be digested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        byte[] passwdBytes = new byte[password.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (i=0, j=0; i<password.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            passwdBytes[j++] = (byte)(password[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            passwdBytes[j++] = (byte)password[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // XOR "encrKey" with "xorKey", and store the result in "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        byte[] plainKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        for (i = 0; i < plainKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // Check the integrity of the recovered key by concatenating it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // the password, digesting the concatenation, and comparing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // result of the digest operation with the digest provided at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // of <code>protectedKey</code>. If the two digest values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // different, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        java.util.Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        for (i = 0; i < digest.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                throw new UnrecoverableKeyException("Cannot recover key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return plainKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Seals the given cleartext key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * construction time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    SealedObject seal(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // create a random salt (8 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        byte[] salt = new byte[8];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14317
diff changeset
   287
        SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // create PBE parameters from salt and iteration count
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   290
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // seal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        PBEWithMD5AndTripleDESCipher cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        cipherSpi = new PBEWithMD5AndTripleDESCipher();
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   302
        cipher = new CipherForKeyProtector(cipherSpi, SunJCE.getInstance(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                           "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return new SealedObjectForKeyProtector(key, cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Unseals the sealed key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    Key unseal(SealedObject so)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        throws NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            SecretKey skey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            SealedObjectForKeyProtector soForKeyProtector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (!(so instanceof SealedObjectForKeyProtector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                soForKeyProtector = new SealedObjectForKeyProtector(so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                soForKeyProtector = (SealedObjectForKeyProtector)so;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            AlgorithmParameters params = soForKeyProtector.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                throw new UnrecoverableKeyException("Cannot get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                    "algorithm parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   331
            PBEParameterSpec pbeSpec;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   332
            try {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   333
                pbeSpec = params.getParameterSpec(PBEParameterSpec.class);
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   334
            } catch (InvalidParameterSpecException ipse) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   335
                throw new IOException("Invalid PBE algorithm parameters");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   336
            }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   337
            if (pbeSpec.getIterationCount() > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   338
                throw new IOException("PBE iteration count too large");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   339
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            PBEWithMD5AndTripleDESCipher cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            cipherSpi = new PBEWithMD5AndTripleDESCipher();
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   342
            Cipher cipher = new CipherForKeyProtector(cipherSpi,
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   343
                                                      SunJCE.getInstance(),
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   344
                                                      "PBEWithMD5AndTripleDES");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            cipher.init(Cipher.DECRYPT_MODE, skey, params);
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47417
diff changeset
   346
            return soForKeyProtector.getKey(cipher);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // Note: this catch needed to be here because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // later catch of GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new UnrecoverableKeyException(cnfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new UnrecoverableKeyException(gse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
final class CipherForKeyProtector extends javax.crypto.Cipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Creates a Cipher object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param cipherSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param transformation the transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    protected CipherForKeyProtector(CipherSpi cipherSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                    Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                    String transformation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        super(cipherSpi, provider, transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
}