src/java.base/share/classes/com/sun/crypto/provider/PBEKey.java
author coffeys
Fri, 03 Aug 2018 14:14:59 +0100
changeset 51293 53c3b460503c
parent 48560 46e99460e8c9
child 51504 c9a3e3cac9c7
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: 48560
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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
48560
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    28
import java.lang.ref.Reference;
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 25859
diff changeset
    29
import java.security.MessageDigest;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.KeyRep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.InvalidKeySpecException;
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    32
import java.util.Arrays;
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14342
diff changeset
    33
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.PBEKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
46097
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    37
import jdk.internal.ref.CleanerFactory;
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class represents a PBE key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
final class PBEKey implements SecretKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final long serialVersionUID = -2234768909660948176L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Creates a PBE key from a given PBE key specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
46097
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    56
     * @param keytype the given PBE key specification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    58
    PBEKey(PBEKeySpec keySpec, String keytype, boolean useCleaner)
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    59
            throws InvalidKeySpecException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        char[] passwd = keySpec.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (passwd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            // Should allow an empty password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            passwd = new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10336
diff changeset
    65
        // Accept "\0" to signify "zero-length password with no terminator".
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10336
diff changeset
    66
        if (!(passwd.length == 1 && passwd[0] == 0)) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10336
diff changeset
    67
            for (int i=0; i<passwd.length; i++) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10336
diff changeset
    68
                if ((passwd[i] < '\u0020') || (passwd[i] > '\u007E')) {
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10336
diff changeset
    69
                    throw new InvalidKeySpecException("Password is not ASCII");
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10336
diff changeset
    70
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.key = new byte[passwd.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (int i=0; i<passwd.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            this.key[i] = (byte) (passwd[i] & 0x7f);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    76
        Arrays.fill(passwd, ' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        type = keytype;
46097
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    78
22316369c9b0 8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents: 44534
diff changeset
    79
        // Use the cleaner to zero the key when no longer referenced
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    80
        if (useCleaner) {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    81
            final byte[] k = this.key;
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    82
            CleanerFactory.cleaner().register(this,
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    83
                () -> Arrays.fill(k, (byte) 0x00));
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
    84
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public byte[] getEncoded() {
48560
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    88
        // The key is zeroized by finalize()
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    89
        // The reachability fence ensures finalize() isn't called early
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    90
        byte[] result = key.clone();
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    91
        Reference.reachabilityFence(this);
46e99460e8c9 8172525: Improve key keying case
apetcher
parents: 47216
diff changeset
    92
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 1; i < this.key.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            retval += this.key[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 14342
diff changeset
   112
        return(retval ^= getAlgorithm().toLowerCase(Locale.ENGLISH).hashCode());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (!(obj instanceof SecretKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        SecretKey that = (SecretKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (!(that.getAlgorithm().equalsIgnoreCase(type)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        byte[] thatEncoded = that.getEncoded();
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 25859
diff changeset
   128
        boolean ret = MessageDigest.isEqual(this.key, thatEncoded);
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   129
        Arrays.fill(thatEncoded, (byte)0x00);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
51293
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   134
     * Clears the internal copy of the key.
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   135
     *
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   136
     */
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   137
    @Override
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   138
    public void destroy() {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   139
        if (key != null) {
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   140
            Arrays.fill(key, (byte) 0x00);
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   141
            key = null;
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   142
        }
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   143
    }
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   144
53c3b460503c 8208583: Better management of internal KeyStore buffers
coffeys
parents: 48560
diff changeset
   145
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * readObject is called to restore the state of this key from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         throws java.io.IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        s.defaultReadObject();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   153
        key = key.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Replace the PBE key to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the standard KeyRep object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws java.io.ObjectStreamException if a new object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * this PBE key could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return new KeyRep(KeyRep.Type.SECRET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}