jdk/src/share/classes/com/sun/crypto/provider/PBKDF2KeyImpl.java
author vinnie
Thu, 11 Apr 2013 17:57:08 +0100
changeset 16909 78a1749a43e2
parent 10336 0bb1999251f8
child 16910 d0ed88d92afe
permissions -rw-r--r--
7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider Reviewed-by: vinnie, wetmore Contributed-by: Tony Scarpino <anthony.scarpino@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 10336
diff changeset
     2
 * Copyright (c) 2005, 2013, 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: 4348
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: 4348
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: 4348
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4348
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
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
    28
import java.io.ObjectStreamException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.KeyRep;
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.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.Mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.spec.PBEKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class represents a PBE key derived using PBKDF2 defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * in PKCS#5 v2.0. meaning that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 1) the password must consist of characters which will be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *    to bytes using UTF-8 character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 2) salt, iteration count, and to be derived key length are supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
final class PBKDF2KeyImpl implements javax.crypto.interfaces.PBEKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static final long serialVersionUID = -2234868909660948157L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private char[] passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte[] salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int iterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Mac prf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static byte[] getPasswordBytes(char[] passwd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Charset utf8 = Charset.forName("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        CharBuffer cb = CharBuffer.wrap(passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ByteBuffer bb = utf8.encode(cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int len = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byte[] passwdBytes = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        bb.get(passwdBytes, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return passwdBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Creates a PBE key from a given PBE key specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param key the given PBE key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    PBKDF2KeyImpl(PBEKeySpec keySpec, String prfAlgo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        char[] passwd = keySpec.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (passwd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // Should allow an empty password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            this.passwd = new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            this.passwd = passwd.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Convert the password from char[] to byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        byte[] passwdBytes = getPasswordBytes(this.passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this.salt = keySpec.getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (salt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new InvalidKeySpecException("Salt not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.iterCount = keySpec.getIterationCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (iterCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InvalidKeySpecException("Iteration count not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } else if (iterCount < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new InvalidKeySpecException("Iteration count is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int keyLength = keySpec.getKeyLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (keyLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new InvalidKeySpecException("Key length not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else if (keyLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new InvalidKeySpecException("Key length is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
16909
78a1749a43e2 7171982: Cipher getParameters() throws RuntimeException: Cannot find SunJCE provider
vinnie
parents: 10336
diff changeset
   108
            this.prf = Mac.getInstance(prfAlgo, SunJCE.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // not gonna happen; re-throw just in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            InvalidKeySpecException ike = new InvalidKeySpecException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ike.initCause(nsae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   118
    private static byte[] deriveKey(final Mac prf, final byte[] password,
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   119
            byte[] salt, int iterCount, int keyLengthInBit) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int keyLength = keyLengthInBit/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        byte[] key = new byte[keyLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            int hlen = prf.getMacLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            int intL = (keyLength + hlen - 1)/hlen; // ceiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int intR = keyLength - (intL - 1)*hlen; // residue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            byte[] ui = new byte[hlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            byte[] ti = new byte[hlen];
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   128
            // SecretKeySpec cannot be used, since password can be empty here.
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   129
            SecretKey macKey = new SecretKey() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   130
                private static final long serialVersionUID = 7874493593505141603L;
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   131
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   132
                public String getAlgorithm() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   133
                    return prf.getAlgorithm();
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   134
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   135
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   136
                public String getFormat() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   137
                    return "RAW";
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   138
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   139
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   140
                public byte[] getEncoded() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   141
                    return password;
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   142
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   143
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   144
                public int hashCode() {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   145
                    return Arrays.hashCode(password) * 41 +
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   146
                            prf.getAlgorithm().toLowerCase().hashCode();
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   147
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   148
                @Override
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   149
                public boolean equals(Object obj) {
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   150
                    if (this == obj) return true;
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   151
                    if (this.getClass() != obj.getClass()) return false;
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   152
                    SecretKey sk = (SecretKey)obj;
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   153
                    return prf.getAlgorithm().equalsIgnoreCase(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   154
                        sk.getAlgorithm()) &&
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   155
                        Arrays.equals(password, sk.getEncoded());
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   156
                }
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   157
            };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            prf.init(macKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            byte[] ibytes = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            for (int i = 1; i <= intL; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                prf.update(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                ibytes[3] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                ibytes[2] = (byte) ((i >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ibytes[1] = (byte) ((i >> 16) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                ibytes[0] = (byte) ((i >> 24) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                prf.update(ibytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                prf.doFinal(ui, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                System.arraycopy(ui, 0, ti, 0, ui.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                for (int j = 2; j <= iterCount; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    prf.update(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    prf.doFinal(ui, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    // XOR the intermediate Ui's together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    for (int k = 0; k < ui.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        ti[k] ^= ui[k];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (i == intL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    System.arraycopy(ti, 0, key, (i-1)*hlen, intR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    System.arraycopy(ti, 0, key, (i-1)*hlen, hlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (GeneralSecurityException gse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new RuntimeException("Error deriving PBKDF2 keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public byte[] getEncoded() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   192
        return key.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return "PBKDF2With" + prf.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public int getIterationCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return iterCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public char[] getPassword() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   204
        return passwd.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public byte[] getSalt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return salt.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int i = 1; i < this.key.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            retval += this.key[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return(retval ^= getAlgorithm().toLowerCase().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (!(obj instanceof SecretKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        SecretKey that = (SecretKey) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (!(that.getAlgorithm().equalsIgnoreCase(getAlgorithm())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (!(that.getFormat().equalsIgnoreCase("RAW")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        byte[] thatEncoded = that.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        boolean ret = Arrays.equals(key, that.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        java.util.Arrays.fill(thatEncoded, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Replace the PBE key to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return the standard KeyRep object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @throws ObjectStreamException if a new object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * this PBE key could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
4348
5b1eb97d243a 6879540: enable empty password for kerberos 5
weijun
parents: 715
diff changeset
   254
    private Object writeReplace() throws ObjectStreamException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return new KeyRep(KeyRep.Type.SECRET, getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                              getFormat(), getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Ensures that the password bytes of this key are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * erased when there are no more references to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (this.passwd != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   266
                java.util.Arrays.fill(this.passwd, '0');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                this.passwd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (this.key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                java.util.Arrays.fill(this.key, (byte)0x00);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                this.key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}