src/java.base/share/classes/java/security/AlgorithmParameters.java
author mullan
Wed, 13 Nov 2019 13:43:06 -0500
changeset 59059 27a266d5fb13
parent 58242 94bb65cb37d3
permissions -rw-r--r--
8214483: Remove algorithms that use MD5 or DES from security requirements Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
     2
 * Copyright (c) 1997, 2019, 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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.InvalidParameterSpecException;
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
    31
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class is used as an opaque representation of cryptographic parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    36
 * <p>An {@code AlgorithmParameters} object for managing the parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * for a particular algorithm can be obtained by
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    38
 * calling one of the {@code getInstance} factory methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (static methods that return instances of a given class).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    41
 * <p>Once an {@code AlgorithmParameters} object is obtained, it must be
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    42
 * initialized via a call to {@code init}, using an appropriate parameter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * specification or parameter encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>A transparent parameter specification is obtained from an
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    46
 * {@code AlgorithmParameters} object via a call to
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    47
 * {@code getParameterSpec}, and a byte encoding of the parameters is
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    48
 * obtained via a call to {@code getEncoded}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    50
 * <p> Every implementation of the Java platform is required to support the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    51
 * following standard {@code AlgorithmParameters} algorithms:
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    52
 * <ul>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    53
 * <li>{@code AES}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    54
 * <li>{@code DESede}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    55
 * <li>{@code DiffieHellman}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
    56
 * <li>{@code DSA}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    57
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    58
 * These algorithms are described in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
    59
 * "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    60
 * AlgorithmParameters section</a> of the
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
    61
 * Java Security Standard Algorithm Names Specification.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    62
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    63
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    64
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @see java.security.spec.AlgorithmParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see java.security.spec.DSAParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see KeyPairGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class AlgorithmParameters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private AlgorithmParametersSpi paramSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Has this object been initialized?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Creates an AlgorithmParameters object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param paramSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                  Provider provider, String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.paramSpi = paramSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns the name of the algorithm associated with this parameter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return the algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns a parameter object for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * A new AlgorithmParameters object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * AlgorithmParametersSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p> The returned parameter object must be initialized via a call to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   126
     * {@code init}, using an appropriate parameter specification or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * parameter encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   129
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   130
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   131
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   132
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   133
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   134
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   135
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   136
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param algorithm the name of the algorithm requested.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   138
     * See the AlgorithmParameters section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   139
     * "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   140
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   143
     * @return the new parameter object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   145
     * @throws NoSuchAlgorithmException if no {@code Provider} supports an
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   146
     *         {@code AlgorithmParametersSpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   147
     *         specified algorithm
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   148
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   149
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static AlgorithmParameters getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   155
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                             (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                           (Provider)objs[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                           algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch(NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new NoSuchAlgorithmException(algorithm + " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns a parameter object for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p> A new AlgorithmParameters object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * AlgorithmParametersSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p>The returned parameter object must be initialized via a call to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   179
     * {@code init}, using an appropriate parameter specification or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * parameter encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param algorithm the name of the algorithm requested.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   183
     * See the AlgorithmParameters section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   184
     * "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   185
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   190
     * @return the new parameter object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   192
     * @throws IllegalArgumentException if the provider name is {@code null}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   193
     *         or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   195
     * @throws NoSuchAlgorithmException if an {@code AlgorithmParametersSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   196
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   197
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   199
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   200
     *         registered in the security provider list
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   201
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   202
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static AlgorithmParameters getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                                  String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        throws NoSuchAlgorithmException, NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   210
        Objects.requireNonNull(algorithm, "null algorithm name");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   211
        if (provider == null || provider.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                         provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                       (Provider)objs[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                       algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns a parameter object for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p> A new AlgorithmParameters object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * AlgorithmParametersSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>The returned parameter object must be initialized via a call to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   229
     * {@code init}, using an appropriate parameter specification or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * parameter encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param algorithm the name of the algorithm requested.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   233
     * See the AlgorithmParameters section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   234
     * "{@docRoot}/../specs/security/standard-names.html#algorithmparameters-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 41826
diff changeset
   235
     * Java Security Standard Algorithm Names Specification</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   240
     * @return the new parameter object
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   241
     *
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   242
     * @throws IllegalArgumentException if the provider is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   244
     * @throws NoSuchAlgorithmException if an
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   245
     *         {@code AlgorithmParameterGeneratorSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   246
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   247
     *         from the specified {@code Provider} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   249
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public static AlgorithmParameters getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                                  Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        throws NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 37348
diff changeset
   259
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (provider == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                         provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                       (Provider)objs[1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                       algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns the provider of this parameter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the provider of this parameter object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Initializes this parameter object using the parameters
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   280
     * specified in {@code paramSpec}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param paramSpec the parameter specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
   284
     * @throws    InvalidParameterSpecException if the given parameter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * specification is inappropriate for the initialization of this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * object, or if this parameter object has already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public final void init(AlgorithmParameterSpec paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        throws InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (this.initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new InvalidParameterSpecException("already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        paramSpi.engineInit(paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        this.initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Imports the specified parameters and decodes them according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * primary decoding format for parameters. The primary decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * format for parameters is ASN.1, if an ASN.1 specification for this type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * of parameters exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param params the encoded parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
   305
     * @throws    IOException on decoding errors, or if this parameter object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * has already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public final void init(byte[] params) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (this.initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new IOException("already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        paramSpi.engineInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        this.initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   316
     * Imports the parameters from {@code params} and decodes them
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * according to the specified decoding scheme.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   318
     * If {@code format} is null, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * primary decoding format for parameters is used. The primary decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * format is ASN.1, if an ASN.1 specification for these parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param params the encoded parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param format the name of the decoding scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
   327
     * @throws    IOException on decoding errors, or if this parameter object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * has already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public final void init(byte[] params, String format) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (this.initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new IOException("already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        paramSpi.engineInit(params, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        this.initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Returns a (transparent) specification of this parameter object.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   339
     * {@code paramSpec} identifies the specification class in which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * the parameters should be returned. It could, for example, be
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   341
     * {@code DSAParameterSpec.class}, to indicate that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * parameters should be returned in an instance of the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   343
     * {@code DSAParameterSpec} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
19828
b4f91bc595fe 8024432: Fix doclint issues in java.security
juh
parents: 18579
diff changeset
   345
     * @param <T> the type of the parameter specification to be returrned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param paramSpec the specification class in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * the parameters should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return the parameter specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
   351
     * @throws    InvalidParameterSpecException if the requested parameter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * specification is inappropriate for this parameter object, or if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * parameter object has not been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public final <T extends AlgorithmParameterSpec>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        T getParameterSpec(Class<T> paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        throws InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (this.initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new InvalidParameterSpecException("not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return paramSpi.engineGetParameterSpec(paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Returns the parameters in their primary encoding format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * The primary encoding format for parameters is ASN.1, if an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * specification for this type of parameters exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return the parameters encoded using their primary encoding format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
   372
     * @throws    IOException on encoding errors, or if this parameter object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * has not been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public final byte[] getEncoded() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (this.initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new IOException("not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return paramSpi.engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Returns the parameters encoded in the specified scheme.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9035
diff changeset
   385
     * If {@code format} is null, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * primary encoding format for parameters is used. The primary encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * format is ASN.1, if an ASN.1 specification for these parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param format the name of the encoding format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return the parameters encoded using the specified encoding scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 53018
diff changeset
   394
     * @throws    IOException on encoding errors, or if this parameter object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * has not been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public final byte[] getEncoded(String format) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (this.initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new IOException("not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return paramSpi.engineGetEncoded(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Returns a formatted string describing the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return a formatted string describing the parameters, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * parameter object has not been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (this.initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return paramSpi.engineToString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
}