jdk/src/share/classes/com/sun/crypto/provider/KeyProtector.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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 1998-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 java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream.GetField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.crypto.CipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.crypto.NoSuchPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.crypto.IllegalBlockSizeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.crypto.SealedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * This class implements a protection mechanism for private keys. In JCE, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * use a stronger protection mechanism than in the JDK, because we can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the <code>Cipher</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Private keys are protected using the JCE mechanism, and are recovered using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * either the JDK or JCE mechanism, depending on how the key has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * protected. This allows us to parse Sun's keystore implementation that ships
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * with JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @see JceKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
final class KeyProtector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // defined by SunSoft (SKI project)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final String PBE_WITH_MD5_AND_DES3_CBC_OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            = "1.3.6.1.4.1.42.2.19.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // JavaSoft proprietary key-protection algorithm (used to protect private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // keys in the keystore implementation that comes with JDK 1.2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    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
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final int SALT_LEN = 20; // the salt length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final int DIGEST_LEN = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // the password used for protecting/recovering keys passed through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // key protector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final Provider PROV = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    KeyProtector(char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
           throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.password = password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Protects the given cleartext private key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    byte[] protect(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // create a random salt (8 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        byte[] salt = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        SunJCE.RANDOM.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // create PBE parameters from salt and iteration count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // encrypt private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        PBEWithMD5AndTripleDESCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        cipher = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        cipher.engineInit(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        byte[] plain = (byte[])key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        byte[] encrKey = cipher.engineDoFinal(plain, 0, plain.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // wrap encrypted private key in EncryptedPrivateKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // (as defined in PKCS#8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        AlgorithmParameters pbeParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            AlgorithmParameters.getInstance("PBE", PROV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        pbeParams.init(pbeSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        AlgorithmId encrAlg = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            (new ObjectIdentifier(PBE_WITH_MD5_AND_DES3_CBC_OID), pbeParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Recovers the cleartext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * using the password provided at construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    Key recover(EncryptedPrivateKeyInfo encrInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        throws UnrecoverableKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        byte[] plain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            String encrAlg = encrInfo.getAlgorithm().getOID().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (!encrAlg.equals(PBE_WITH_MD5_AND_DES3_CBC_OID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                && !encrAlg.equals(KEY_PROTECTOR_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                throw new UnrecoverableKeyException("Unsupported encryption "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                                    + "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
            if (encrAlg.equals(KEY_PROTECTOR_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // JDK 1.2 style recovery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                plain = recover(encrInfo.getEncryptedData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                byte[] encodedParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    encrInfo.getAlgorithm().getEncodedParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // parse the PBE parameters into the corresponding spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                AlgorithmParameters pbeParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    AlgorithmParameters.getInstance("PBE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                pbeParams.init(encodedParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                PBEParameterSpec pbeSpec = (PBEParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    pbeParams.getParameterSpec(PBEParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                SecretKey sKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                // decrypt private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                PBEWithMD5AndTripleDESCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                cipher = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                cipher.engineInit(Cipher.DECRYPT_MODE, sKey, pbeSpec, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                plain=cipher.engineDoFinal(encrInfo.getEncryptedData(), 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                           encrInfo.getEncryptedData().length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // determine the private-key algorithm, and parse private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // using the appropriate key factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            String oidName = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                (new PrivateKeyInfo(plain).getAlgorithm().getOID()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            KeyFactory kFac = KeyFactory.getInstance(oidName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return kFac.generatePrivate(new PKCS8EncodedKeySpec(plain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // Note: this catch needed to be here because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // later catch of GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new UnrecoverableKeyException(gse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Recovers the cleartext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * using the password provided at construction time. This method implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * the recovery algorithm used by Sun's keystore implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private byte[] recover(byte[] protectedKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws UnrecoverableKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int encrKeyLen; // the length of the encrpyted key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // Get the salt associated with this key (the first SALT_LEN bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // <code>protectedKey</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        numRounds = encrKeyLen / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if ((encrKeyLen % DIGEST_LEN) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // Get the encrypted key portion and store it in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        byte[] encrKey = new byte[encrKeyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Set up the byte array which will be XORed with "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        byte[] xorKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // Convert password to byte array, so that it can be digested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        byte[] passwdBytes = new byte[password.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (i=0, j=0; i<password.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            passwdBytes[j++] = (byte)(password[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            passwdBytes[j++] = (byte)password[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // XOR "encrKey" with "xorKey", and store the result in "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        byte[] plainKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        for (i = 0; i < plainKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // Check the integrity of the recovered key by concatenating it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // the password, digesting the concatenation, and comparing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // result of the digest operation with the digest provided at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // of <code>protectedKey</code>. If the two digest values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // different, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        java.util.Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (i = 0; i < digest.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                throw new UnrecoverableKeyException("Cannot recover key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return plainKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Seals the given cleartext key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * construction time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    SealedObject seal(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // create a random salt (8 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        byte[] salt = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        SunJCE.RANDOM.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // create PBE parameters from salt and iteration count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        SecretKey sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // seal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        PBEWithMD5AndTripleDESCipher cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        cipherSpi = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        cipher = new CipherForKeyProtector(cipherSpi, PROV,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                           "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return new SealedObjectForKeyProtector(key, cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Unseals the sealed key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    Key unseal(SealedObject so)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        throws NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            SecretKey skey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            SealedObjectForKeyProtector soForKeyProtector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (!(so instanceof SealedObjectForKeyProtector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                soForKeyProtector = new SealedObjectForKeyProtector(so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                soForKeyProtector = (SealedObjectForKeyProtector)so;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            AlgorithmParameters params = soForKeyProtector.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                throw new UnrecoverableKeyException("Cannot get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                    "algorithm parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            PBEWithMD5AndTripleDESCipher cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            cipherSpi = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Cipher cipher = new CipherForKeyProtector(cipherSpi, PROV,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                                   "PBEWithMD5AndTripleDES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            cipher.init(Cipher.DECRYPT_MODE, skey, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return (Key)soForKeyProtector.getObject(cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // Note: this catch needed to be here because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            // later catch of GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new UnrecoverableKeyException(cnfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new UnrecoverableKeyException(gse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
final class CipherForKeyProtector extends javax.crypto.Cipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Creates a Cipher object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param cipherSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param transformation the transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    protected CipherForKeyProtector(CipherSpi cipherSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                    Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                    String transformation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        super(cipherSpi, provider, transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
final class SealedObjectForKeyProtector extends javax.crypto.SealedObject {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    static final long serialVersionUID = -3650226485480866989L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    SealedObjectForKeyProtector(Serializable object, Cipher c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        throws IOException, IllegalBlockSizeException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        super(object, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    SealedObjectForKeyProtector(SealedObject so) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        super(so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    AlgorithmParameters getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        AlgorithmParameters params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (super.encodedParams != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                params = AlgorithmParameters.getInstance("PBE", "SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                params.init(super.encodedParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } catch (NoSuchProviderException nspe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                // eat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                //eat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                //eat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
}