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