src/java.base/share/classes/sun/security/provider/KeyProtector.java
author coffeys
Fri, 03 Aug 2018 14:14:59 +0100
changeset 51293 53c3b460503c
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
8208583: Better management of internal KeyStore buffers Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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
     */
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 47216
diff changeset
   123
    public KeyProtector(byte[] passwordBytes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 47216
diff changeset
   126
        if (passwordBytes == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
           throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        md = MessageDigest.getInstance(DIGEST_ALG);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 47216
diff changeset
   130
        this.passwdBytes = passwordBytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Protects the given plaintext key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public byte[] protect(Key key) throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int encrKeyOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IllegalArgumentException("plaintext key can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (!"PKCS#8".equalsIgnoreCase(key.getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new KeyStoreException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                "Cannot get key bytes, not PKCS#8 encoded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        byte[] plainKey = key.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (plainKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new KeyStoreException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                "Cannot get key bytes, encoding not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        numRounds = plainKey.length / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if ((plainKey.length % DIGEST_LEN) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // Create a random salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SecureRandom random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        random.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Set up the byte array which will be XORed with "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        byte[] xorKey = new byte[plainKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // XOR "plainKey" with "xorKey", and store the result in "tmpKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        byte[] tmpKey = new byte[plainKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (i = 0; i < tmpKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            tmpKey[i] = (byte)(plainKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // Store salt and "tmpKey" in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        byte[] encrKey = new byte[salt.length + tmpKey.length + DIGEST_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        System.arraycopy(salt, 0, encrKey, encrKeyOffset, salt.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        encrKeyOffset += salt.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        System.arraycopy(tmpKey, 0, encrKey, encrKeyOffset, tmpKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        encrKeyOffset += tmpKey.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Append digest(password, plainKey) as an integrity check to "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        System.arraycopy(digest, 0, encrKey, encrKeyOffset, digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // wrap the protected private key in a PKCS#8-style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // EncryptedPrivateKeyInfo, and returns its encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        AlgorithmId encrAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            encrAlg = new AlgorithmId(new ObjectIdentifier(KEY_PROTECTOR_OID));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new KeyStoreException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Recovers the plaintext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * using the password provided at construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public Key recover(EncryptedPrivateKeyInfo encrInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int encrKeyLen; // the length of the encrpyted key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        // do we support the algorithm?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        AlgorithmId encrAlg = encrInfo.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (!(encrAlg.getOID().toString().equals(KEY_PROTECTOR_OID))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new UnrecoverableKeyException("Unsupported key protection "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                                + "algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte[] protectedKey = encrInfo.getEncryptedData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * Get the salt associated with this key (the first SALT_LEN bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * <code>protectedKey</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        numRounds = encrKeyLen / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if ((encrKeyLen % DIGEST_LEN) != 0) numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Get the encrypted key portion and store it in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        byte[] encrKey = new byte[encrKeyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Set up the byte array which will be XORed with "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        byte[] xorKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // XOR "encrKey" with "xorKey", and store the result in "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        byte[] plainKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        for (i = 0; i < plainKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * Check the integrity of the recovered key by concatenating it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * the password, digesting the concatenation, and comparing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * result of the digest operation with the digest provided at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * of <code>protectedKey</code>. If the two digest values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * different, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        Arrays.fill(passwdBytes, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        for (i = 0; i < digest.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                throw new UnrecoverableKeyException("Cannot recover key");
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
        // The parseKey() method of PKCS8Key parses the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // algorithm and instantiates the appropriate key factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // which in turn parses the key material.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return PKCS8Key.parseKey(new DerValue(plainKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}