src/java.base/share/classes/com/sun/crypto/provider/KeyProtector.java
author mbalao
Tue, 12 Nov 2019 00:30:55 -0300
changeset 59158 438337c846fb
parent 51504 c9a3e3cac9c7
permissions -rw-r--r--
8233404: System property to set the number of PBE iterations in JCEKS keystores Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47417
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AlgorithmParameters;
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
    38
import java.security.spec.InvalidParameterSpecException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.PKCS8EncodedKeySpec;
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
    40
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.crypto.CipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.crypto.SealedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.crypto.spec.*;
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
    47
import javax.security.auth.DestroyFailedException;
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.util.ObjectIdentifier;
59158
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    51
import sun.security.util.SecurityProperties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This class implements a protection mechanism for private keys. In JCE, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * use a stronger protection mechanism than in the JDK, because we can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the <code>Cipher</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Private keys are protected using the JCE mechanism, and are recovered using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * either the JDK or JCE mechanism, depending on how the key has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * protected. This allows us to parse Sun's keystore implementation that ships
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * with JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see JceKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
final class KeyProtector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // defined by SunSoft (SKI project)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final String PBE_WITH_MD5_AND_DES3_CBC_OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            = "1.3.6.1.4.1.42.2.19.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // JavaSoft proprietary key-protection algorithm (used to protect private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // keys in the keystore implementation that comes with JDK 1.2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    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
    77
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
    78
    private static final int MAX_ITERATION_COUNT = 5000000;
59158
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    79
    private static final int MIN_ITERATION_COUNT = 10000;
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    80
    private static final int DEFAULT_ITERATION_COUNT = 200000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final int SALT_LEN = 20; // the salt length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final int DIGEST_LEN = 20;
59158
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    83
    private static final int ITERATION_COUNT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // the password used for protecting/recovering keys passed through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // key protector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
59158
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    89
    /**
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    90
     * {@systemProperty jdk.jceks.iterationCount} property indicating the
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    91
     * number of iterations for password-based encryption (PBE) in JCEKS
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    92
     * keystores. Values in the range 10000 to 5000000 are considered valid.
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    93
     * If the value is out of this range, or is not a number, or is
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    94
     * unspecified; a default of 200000 is used.
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    95
     */
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    96
    static {
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    97
        int iterationCount = DEFAULT_ITERATION_COUNT;
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    98
        String ic = SecurityProperties.privilegedGetOverridable(
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
    99
                "jdk.jceks.iterationCount");
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   100
        if (ic != null && !ic.isEmpty()) {
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   101
            try {
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   102
                iterationCount = Integer.parseInt(ic);
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   103
                if (iterationCount < MIN_ITERATION_COUNT ||
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   104
                        iterationCount > MAX_ITERATION_COUNT) {
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   105
                    iterationCount = DEFAULT_ITERATION_COUNT;
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   106
                }
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   107
            } catch (NumberFormatException e) {}
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   108
        }
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   109
        ITERATION_COUNT = iterationCount;
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   110
    }
438337c846fb 8233404: System property to set the number of PBE iterations in JCEKS keystores
mbalao
parents: 51504
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    KeyProtector(char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
           throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.password = password;
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
     * Protects the given cleartext private key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    byte[] protect(PrivateKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // create a random salt (8 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        byte[] salt = new byte[8];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14317
diff changeset
   128
        SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // create PBE parameters from salt and iteration count
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   131
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   135
        SecretKey sKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        PBEWithMD5AndTripleDESCipher cipher;
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   137
        try {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   138
            sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES", false);
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   139
            // encrypt private key
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   140
            cipher = new PBEWithMD5AndTripleDESCipher();
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   141
            cipher.engineInit(Cipher.ENCRYPT_MODE, sKey, pbeSpec, null);
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   142
        } finally {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   143
            pbeKeySpec.clearPassword();
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   144
            if (sKey != null) sKey.destroy();
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   145
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   146
        byte[] plain = key.getEncoded();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        byte[] encrKey = cipher.engineDoFinal(plain, 0, plain.length);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   148
        Arrays.fill(plain, (byte) 0x00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // wrap encrypted private key in EncryptedPrivateKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // (as defined in PKCS#8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        AlgorithmParameters pbeParams =
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   153
            AlgorithmParameters.getInstance("PBE", SunJCE.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        pbeParams.init(pbeSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        AlgorithmId encrAlg = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            (new ObjectIdentifier(PBE_WITH_MD5_AND_DES3_CBC_OID), pbeParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return new EncryptedPrivateKeyInfo(encrAlg,encrKey).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Recovers the cleartext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * using the password provided at construction time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    Key recover(EncryptedPrivateKeyInfo encrInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        throws UnrecoverableKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    {
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   168
        byte[] plain = null;
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   169
        SecretKey sKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            String encrAlg = encrInfo.getAlgorithm().getOID().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (!encrAlg.equals(PBE_WITH_MD5_AND_DES3_CBC_OID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                && !encrAlg.equals(KEY_PROTECTOR_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new UnrecoverableKeyException("Unsupported encryption "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                    + "algorithm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (encrAlg.equals(KEY_PROTECTOR_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                // JDK 1.2 style recovery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                plain = recover(encrInfo.getEncryptedData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                byte[] encodedParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    encrInfo.getAlgorithm().getEncodedParams();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // parse the PBE parameters into the corresponding spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                AlgorithmParameters pbeParams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    AlgorithmParameters.getInstance("PBE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                pbeParams.init(encodedParams);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   189
                PBEParameterSpec pbeSpec =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   190
                        pbeParams.getParameterSpec(PBEParameterSpec.class);
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   191
                if (pbeSpec.getIterationCount() > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   192
                    throw new IOException("PBE iteration count too large");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   193
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   197
                sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES", false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                // decrypt private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                PBEWithMD5AndTripleDESCipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                cipher = new PBEWithMD5AndTripleDESCipher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                cipher.engineInit(Cipher.DECRYPT_MODE, sKey, pbeSpec, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                plain=cipher.engineDoFinal(encrInfo.getEncryptedData(), 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                           encrInfo.getEncryptedData().length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // determine the private-key algorithm, and parse private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // using the appropriate key factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            String oidName = new AlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                (new PrivateKeyInfo(plain).getAlgorithm().getOID()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            KeyFactory kFac = KeyFactory.getInstance(oidName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return kFac.generatePrivate(new PKCS8EncodedKeySpec(plain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // Note: this catch needed to be here because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // later catch of GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throw new UnrecoverableKeyException(gse.getMessage());
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   222
        } finally {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   223
            if (plain != null) Arrays.fill(plain, (byte) 0x00);
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   224
            if (sKey != null) {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   225
                try {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   226
                    sKey.destroy();
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   227
                } catch (DestroyFailedException e) {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   228
                    //shouldn't happen
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   229
                }
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   230
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Recovers the cleartext version of the given key (in protected format),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * using the password provided at construction time. This method implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * the recovery algorithm used by Sun's keystore implementation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * JDK 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private byte[] recover(byte[] protectedKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        throws UnrecoverableKeyException, NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int xorOffset; // offset in xorKey where next digest will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int encrKeyLen; // the length of the encrpyted key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // Get the salt associated with this key (the first SALT_LEN bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // <code>protectedKey</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        System.arraycopy(protectedKey, 0, salt, 0, SALT_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // Determine the number of digest rounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        encrKeyLen = protectedKey.length - SALT_LEN - DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        numRounds = encrKeyLen / DIGEST_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if ((encrKeyLen % DIGEST_LEN) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            numRounds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Get the encrypted key portion and store it in "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        byte[] encrKey = new byte[encrKeyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        System.arraycopy(protectedKey, SALT_LEN, encrKey, 0, encrKeyLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // Set up the byte array which will be XORed with "encrKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        byte[] xorKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Convert password to byte array, so that it can be digested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        byte[] passwdBytes = new byte[password.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (i=0, j=0; i<password.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            passwdBytes[j++] = (byte)(password[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            passwdBytes[j++] = (byte)password[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // Compute the digests, and store them in "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        for (i = 0, xorOffset = 0, digest = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
             i < numRounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
             i++, xorOffset += DIGEST_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            md.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // Copy the digest into "xorKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (i < numRounds - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                 digest.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                System.arraycopy(digest, 0, xorKey, xorOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                 xorKey.length - xorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // XOR "encrKey" with "xorKey", and store the result in "plainKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        byte[] plainKey = new byte[encrKey.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (i = 0; i < plainKey.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            plainKey[i] = (byte)(encrKey[i] ^ xorKey[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // Check the integrity of the recovered key by concatenating it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // the password, digesting the concatenation, and comparing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // result of the digest operation with the digest provided at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // of <code>protectedKey</code>. If the two digest values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // different, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        md.update(passwdBytes);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   306
        Arrays.fill(passwdBytes, (byte)0x00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        passwdBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        md.update(plainKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        for (i = 0; i < digest.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (digest[i] != protectedKey[SALT_LEN + encrKeyLen + i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                throw new UnrecoverableKeyException("Cannot recover key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return plainKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Seals the given cleartext key, using the password provided at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * construction time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    SealedObject seal(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // create a random salt (8 bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        byte[] salt = new byte[8];
15010
ec6b49ce42b1 8004044: Lazily instantiate SunJCE.RANDOM
valeriep
parents: 14317
diff changeset
   328
        SunJCE.getRandom().nextBytes(salt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // create PBE parameters from salt and iteration count
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   331
        PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   335
        SecretKey sKey = null;
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   336
        Cipher cipher;
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   337
        try {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   338
            sKey = new PBEKey(pbeKeySpec, "PBEWithMD5AndTripleDES", false);
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   339
            pbeKeySpec.clearPassword();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   341
            // seal key
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   342
            PBEWithMD5AndTripleDESCipher cipherSpi;
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   343
            cipherSpi = new PBEWithMD5AndTripleDESCipher();
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   344
            cipher = new CipherForKeyProtector(cipherSpi, SunJCE.getInstance(),
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   345
                                               "PBEWithMD5AndTripleDES");
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 51293
diff changeset
   346
            cipher.init(Cipher.ENCRYPT_MODE, sKey, pbeSpec);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   347
        } finally {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   348
            if (sKey != null) sKey.destroy();
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   349
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return new SealedObjectForKeyProtector(key, cipher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Unseals the sealed key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    Key unseal(SealedObject so)
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   357
        throws NoSuchAlgorithmException, UnrecoverableKeyException {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   358
        SecretKey sKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // create PBE key from password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            PBEKeySpec pbeKeySpec = new PBEKeySpec(this.password);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   362
            sKey = new PBEKey(pbeKeySpec,
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   363
                    "PBEWithMD5AndTripleDES", false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            pbeKeySpec.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            SealedObjectForKeyProtector soForKeyProtector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (!(so instanceof SealedObjectForKeyProtector)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                soForKeyProtector = new SealedObjectForKeyProtector(so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                soForKeyProtector = (SealedObjectForKeyProtector)so;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            AlgorithmParameters params = soForKeyProtector.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (params == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                throw new UnrecoverableKeyException("Cannot get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                                    "algorithm parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   377
            PBEParameterSpec pbeSpec;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   378
            try {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   379
                pbeSpec = params.getParameterSpec(PBEParameterSpec.class);
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   380
            } catch (InvalidParameterSpecException ipse) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   381
                throw new IOException("Invalid PBE algorithm parameters");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   382
            }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   383
            if (pbeSpec.getIterationCount() > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   384
                throw new IOException("PBE iteration count too large");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   385
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            PBEWithMD5AndTripleDESCipher cipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            cipherSpi = new PBEWithMD5AndTripleDESCipher();
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   388
            Cipher cipher = new CipherForKeyProtector(cipherSpi,
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   389
                                                      SunJCE.getInstance(),
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 15010
diff changeset
   390
                                                      "PBEWithMD5AndTripleDES");
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   391
            cipher.init(Cipher.DECRYPT_MODE, sKey, params);
49783
977c6dd636bd 8189997: Enhance keystore mechanisms
weijun
parents: 47417
diff changeset
   392
            return soForKeyProtector.getKey(cipher);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            // Note: this catch needed to be here because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // later catch of GeneralSecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new UnrecoverableKeyException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new UnrecoverableKeyException(cnfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new UnrecoverableKeyException(gse.getMessage());
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   403
        } finally {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   404
            if (sKey != null) {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   405
                try {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   406
                    sKey.destroy();
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   407
                } catch (DestroyFailedException e) {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   408
                    //shouldn't happen
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   409
                }
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 49783
diff changeset
   410
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
final class CipherForKeyProtector extends javax.crypto.Cipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Creates a Cipher object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param cipherSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param transformation the transformation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    protected CipherForKeyProtector(CipherSpi cipherSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                    Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                    String transformation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        super(cipherSpi, provider, transformation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}