src/java.base/share/classes/sun/security/provider/MoreDrbgParameters.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
child 57950 4612a3cfb927
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     1
/*
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     4
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    10
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    15
 * accompanied this code).
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    16
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    20
 *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    23
 * questions.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    24
 */
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    25
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    26
package sun.security.provider;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    27
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    28
import java.io.IOException;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    29
import java.io.Serializable;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    30
import java.security.DrbgParameters;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    31
import java.security.SecureRandomParameters;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    32
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    33
/**
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    34
 * Exported and non-exported parameters that can be used by DRBGs.
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    35
 */
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    36
public class MoreDrbgParameters implements SecureRandomParameters, Serializable {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    37
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    38
    private static final long serialVersionUID = 9L;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    39
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    40
    final transient EntropySource es;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    41
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    42
    final String mech;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    43
    final String algorithm;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    44
    final boolean usedf;
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    45
    final int strength;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    46
    final DrbgParameters.Capability capability;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    47
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    48
    // The following 2 fields will be reassigned in readObject and
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    49
    // thus cannot be final
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    50
    byte[] nonce;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    51
    byte[] personalizationString;
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    52
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    53
    /**
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    54
     * Creates a new {@code MoreDrbgParameters} object.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    55
     *
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    56
     * @param es the {@link EntropySource} to use. If set to {@code null},
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    57
     *           a default entropy source will be used.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    58
     * @param mech mech name. If set to {@code null}, the one in
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    59
     *             securerandom.drbg.config is used. This argument is ignored
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    60
     *             when passing to HashDrbg/HmacDrbg/CtrDrbg.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    61
     * @param algorithm the requested algorithm to use. If set to {@code null},
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    62
     *                  the algorithm will be decided by strength.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    63
     * @param nonce the nonce to use. If set to {@code null},
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    64
     *              a nonce will be assigned.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    65
     * @param usedf whether a derivation function should be used
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    66
     * @param config a {@link DrbgParameters.Instantiation} object
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    67
     */
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    68
    public MoreDrbgParameters(EntropySource es, String mech,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    69
                              String algorithm, byte[] nonce, boolean usedf,
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    70
                              DrbgParameters.Instantiation config) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    71
        this.mech = mech;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    72
        this.algorithm = algorithm;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    73
        this.es = es;
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    74
        this.nonce = (nonce == null) ? null : nonce.clone();
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    75
        this.usedf = usedf;
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    76
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    77
        this.strength = config.getStrength();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    78
        this.capability = config.getCapability();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    79
        this.personalizationString = config.getPersonalizationString();
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    80
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    81
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    82
    @Override
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
    83
    public String toString() {
38853
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    84
        return mech + "," + algorithm + "," + usedf + "," + strength
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    85
                + "," + capability + "," + personalizationString;
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    86
    }
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    87
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    88
    private void readObject(java.io.ObjectInputStream s)
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    89
            throws IOException, ClassNotFoundException {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    90
        s.defaultReadObject();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    91
        if (nonce != null) {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    92
            nonce = nonce.clone();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    93
        }
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    94
        if (personalizationString != null) {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    95
            personalizationString = personalizationString.clone();
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    96
        }
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    97
        if (capability == null) {
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    98
            throw new IllegalArgumentException("Input data is corrupted");
971a7101da5b 8157308: Make AbstractDrbg non-Serializable
weijun
parents: 37796
diff changeset
    99
        }
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   100
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents:
diff changeset
   101
}