src/java.base/share/classes/java/security/spec/EncodedKeySpec.java
author naoto
Tue, 15 Oct 2019 09:25:59 -0700
changeset 58603 2312d1a04c49
parent 47216 71c04702a3d5
permissions -rw-r--r--
8212749: DecimalFormat.setGroupingSize(int) allows setting negative grouping size 8231984: Clarify semantics of DecimalFormat.getGroupingSize(0) Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 35302
diff changeset
     2
 * Copyright (c) 1997, 2017, 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.spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class represents a public or private key in encoded format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see java.security.Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @see java.security.KeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see KeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see PKCS8EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class EncodedKeySpec implements KeySpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private byte[] encodedKey;
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    46
    private String algorithmName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    49
     * Creates a new {@code EncodedKeySpec} with the given encoded key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param encodedKey the encoded key. The contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * array are copied to protect against subsequent modification.
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    53
     * @throws NullPointerException if {@code encodedKey}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public EncodedKeySpec(byte[] encodedKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.encodedKey = encodedKey.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    61
     * Creates a new {@code EncodedKeySpec} with the given encoded key.
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    62
     * This constructor is useful when subsequent callers of the
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    63
     * {@code EncodedKeySpec} object might not know the algorithm
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    64
     * of the key.
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    65
     *
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    66
     * @param encodedKey the encoded key. The contents of the
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    67
     * array are copied to protect against subsequent modification.
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    68
     * @param algorithm the algorithm name of the encoded key
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    69
     * See the KeyFactory section in the <a href=
45118
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 35302
diff changeset
    70
     * "{@docRoot}/../specs/security/standard-names.html#keyfactory-algorithms">
e4258d800b54 8178278: Move Standard Algorithm Names document to specs directory
ihse
parents: 35302
diff changeset
    71
     * Java Security Standard Algorithm Names Specification</a>
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    72
     * for information about standard algorithm names.
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    73
     * @throws NullPointerException if {@code encodedKey}
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    74
     * or {@code algorithm} is null.
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    75
     * @throws IllegalArgumentException if {@code algorithm} is
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    76
     * the empty string {@code ""}
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 26716
diff changeset
    77
     * @since 9
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    78
     */
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    79
    protected EncodedKeySpec(byte[] encodedKey, String algorithm) {
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    80
        if (algorithm == null) {
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    81
            throw new NullPointerException("algorithm name may not be null");
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    82
        }
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    83
        if (algorithm.isEmpty()) {
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    84
            throw new IllegalArgumentException("algorithm name "
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    85
                                             + "may not be empty");
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    86
        }
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    87
        this.encodedKey = encodedKey.clone();
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    88
        this.algorithmName = algorithm;
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    89
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    90
    }
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    91
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    92
    /**
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    93
     * Returns the name of the algorithm of the encoded key.
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    94
     *
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    95
     * @return the name of the algorithm, or null if not specified
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 26716
diff changeset
    96
     * @since 9
26716
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    97
     */
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    98
    public String getAlgorithm() {
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
    99
        return algorithmName;
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
   100
    }
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
   101
189794fe8ba2 8047223: Add algorithm parameter to EncodedKeySpec class and its two subclasses
juh
parents: 25859
diff changeset
   102
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Returns the encoded key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return the encoded key. Returns a new array each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return this.encodedKey.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns the name of the encoding format associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * key specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>If the opaque representation of a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * (see {@link java.security.Key Key}) can be transformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * (see {@link java.security.KeyFactory KeyFactory})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * into this key specification (or a subclass of it),
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 5506
diff changeset
   120
     * {@code getFormat} called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * on the opaque key returns the same value as the
18552
005e115dc6ee 8017326: Cleanup of the javadoc <code> tag in java.security.spec
juh
parents: 5506
diff changeset
   122
     * {@code getFormat} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * of this key specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return a string representation of the encoding format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public abstract String getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}