jdk/src/share/classes/sun/security/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 1997-2006 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 sun.security.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.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.pkcs.PKCS8Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.pkcs.EncryptedPrivateKeyInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This is an implementation of a Sun proprietary, exportable algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * intended for use when protecting (or recovering the cleartext version of)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * sensitive keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This algorithm is not intended as a general purpose cipher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This is how the algorithm works for key protection:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * p - user password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * s - random salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * X - xor key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * P - to-be-protected key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Y - protected key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * R - what gets stored in the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Step 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Take the user's password, append a random salt (of fixed size) to it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * and hash it: d1 = digest(p, s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Store d1 in X.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Step 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Take the user's password, append the digest result from the previous step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * and hash it: dn = digest(p, dn-1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Store dn in X (append it to the previously stored digests).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Repeat this step until the length of X matches the length of the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * P.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Step 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * XOR X and P, and store the result in Y: Y = X XOR P.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Step 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Store s, Y, and digest(p, P) in the result buffer R:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * R = s + Y + digest(p, P), where "+" denotes concatenation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * (NOTE: digest(p, P) is stored in the result buffer, so that when the key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * recovered, we can check if the recovered key indeed matches the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * key.) R is stored in the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * The protected key is recovered as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Step1 and Step2 are the same as above, except that the salt is not randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * generated, but taken from the result R of step 4 (the first length(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Step 3 (XOR operation) yields the plaintext key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Then concatenate the password with the recovered key, and compare with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * last length(digest(p, P)) bytes of R. If they match, the recovered key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * indeed the same key as the original key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @see java.security.KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @see JavaKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @see KeyTool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
final class KeyProtector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int SALT_LEN = 20; // the salt length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final String DIGEST_ALG = "SHA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final int DIGEST_LEN = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // defined by JavaSoft
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    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
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // The password used for protecting/recovering keys passed through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // key protector. We store it as a byte array, so that we can digest it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private byte[] passwdBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Creates an instance of this class, and initializes it with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>The password is expected to be in printable ASCII.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Normal rules for good password selection apply: at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * seven characters, mixed case, with punctuation encouraged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Phrases or words which are easily guessed, for example by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * being found in dictionaries, are bad.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public KeyProtector(char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
           throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        md = MessageDigest.getInstance(DIGEST_ALG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Convert password to byte array, so that it can be digested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        passwdBytes = new byte[password.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (i=0, j=0; i<password.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            passwdBytes[j++] = (byte)(password[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            passwdBytes[j++] = (byte)password[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Ensures that the password bytes of this key protector are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * set to zero when there are no more references to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (passwdBytes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Protects the given plaintext key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public byte[] protect(Key key) throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int encrKeyOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new IllegalArgumentException("plaintext key can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (!"PKCS#8".equalsIgnoreCase(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new KeyStoreException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                "Cannot get key bytes, not PKCS#8 encoded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        byte[] plainKey = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (plainKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new KeyStoreException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                "Cannot get key bytes, encoding not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        numRounds = plainKey.length / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if ((plainKey.length % DIGEST_LEN) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Create a random salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        SecureRandom random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        random.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // Set up the byte array which will be XORed with "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        byte[] xorKey = new byte[plainKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // XOR "plainKey" with "xorKey", and store the result in "tmpKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        byte[] tmpKey = new byte[plainKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        for (i = 0; i < tmpKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            tmpKey[i] = (byte)(plainKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // Store salt and "tmpKey" in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        byte[] encrKey = new byte[salt.length + tmpKey.length + DIGEST_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        System.arraycopy(salt, 0, encrKey, encrKeyOffset, salt.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        encrKeyOffset += salt.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        System.arraycopy(tmpKey, 0, encrKey, encrKeyOffset, tmpKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        encrKeyOffset += tmpKey.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Append digest(password, plainKey) as an integrity check to "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        System.arraycopy(digest, 0, encrKey, encrKeyOffset, digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // wrap the protected private key in a PKCS#8-style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // EncryptedPrivateKeyInfo, and returns its encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        AlgorithmId encrAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            encrAlg = new AlgorithmId(new ObjectIdentifier(KEY_PROTECTOR_OID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new KeyStoreException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Recovers the plaintext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * using the password provided at construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public Key recover(EncryptedPrivateKeyInfo encrInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        throws UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int encrKeyLen; // the length of the encrpyted key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // do we support the algorithm?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        AlgorithmId encrAlg = encrInfo.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (!(encrAlg.getOID().toString().equals(KEY_PROTECTOR_OID))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new UnrecoverableKeyException("Unsupported key protection "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                                + "algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        byte[] protectedKey = encrInfo.getEncryptedData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * Get the salt associated with this key (the first SALT_LEN bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * <code>protectedKey</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        numRounds = encrKeyLen / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if ((encrKeyLen % DIGEST_LEN) != 0) numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Get the encrypted key portion and store it in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        byte[] encrKey = new byte[encrKeyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // Set up the byte array which will be XORed with "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        byte[] xorKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // XOR "encrKey" with "xorKey", and store the result in "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        byte[] plainKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        for (i = 0; i < plainKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * Check the integrity of the recovered key by concatenating it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * the password, digesting the concatenation, and comparing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * result of the digest operation with the digest provided at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * of <code>protectedKey</code>. If the two digest values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * different, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        for (i = 0; i < digest.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                throw new UnrecoverableKeyException("Cannot recover key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // The parseKey() method of PKCS8Key parses the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // algorithm and instantiates the appropriate key factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // which in turn parses the key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            return PKCS8Key.parseKey(new DerValue(plainKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}