src/java.base/share/classes/java/security/KeyRep.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 57950 4612a3cfb927
child 58679 9c3209ff7550
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 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.*;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
    29
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.X509EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.SecretKeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Standardized representation for serialized Key objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Note that a serialized Key may contain sensitive information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * which should not be exposed in untrusted environments.  See the
45138
ddcafe0d0ea3 8180082: Broken javadoc links
rriggs
parents: 29492
diff changeset
    45
 * <a href="{@docRoot}/../specs/serialization/security.html">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Security Appendix</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of the Serialization Specification for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see KeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see javax.crypto.spec.SecretKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see java.security.spec.X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see java.security.spec.PKCS8EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class KeyRep implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    60
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final long serialVersionUID = -4757683898830641853L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Key type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static enum Type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        /** Type for secret keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        SECRET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        /** Type for public keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /** Type for private keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        PRIVATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final String PKCS8 = "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final String X509 = "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final String RAW = "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Type type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The Key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The Key encoding format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private String format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The encoded Key bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Construct the alternate Key class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param type either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param algorithm the algorithm returned from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   118
     *          {@code Key.getAlgorithm()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param format the encoding format returned from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   120
     *          {@code Key.getFormat()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param encoded the encoded bytes returned from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   122
     *          {@code Key.getEncoded()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   124
     * @throws    NullPointerException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   125
     *          if type is {@code null},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   126
     *          if algorithm is {@code null},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   127
     *          if format is {@code null},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   128
     *          or if encoded is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public KeyRep(Type type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                String format, byte[] encoded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (type == null || algorithm == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            format == null || encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new NullPointerException("invalid null input(s)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.algorithm = algorithm;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   140
        this.format = format.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.encoded = encoded.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Resolve the Key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p> This method supports three Type/format combinations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <li> Type.SECRET/"RAW" - returns a SecretKeySpec object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * constructed using encoded key bytes and algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <li> Type.PUBLIC/"X.509" - gets a KeyFactory instance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the key algorithm, constructs an X509EncodedKeySpec with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * encoded key bytes, and generates a public key from the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <li> Type.PRIVATE/"PKCS#8" - gets a KeyFactory instance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the key algorithm, constructs a PKCS8EncodedKeySpec with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * encoded key bytes, and generates a private key from the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the resolved Key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57950
diff changeset
   161
     * @throws    ObjectStreamException if the Type/format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  combination is unrecognized, if the algorithm, key format, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  encoded key bytes are unrecognized/invalid, of if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  resolution of the key fails for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
   166
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (type == Type.SECRET && RAW.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return new SecretKeySpec(encoded, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else if (type == Type.PUBLIC && X509.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                KeyFactory f = KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return f.generatePublic(new X509EncodedKeySpec(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else if (type == Type.PRIVATE && PKCS8.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                KeyFactory f = KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return f.generatePrivate(new PKCS8EncodedKeySpec(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        ("unrecognized type/format combination: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        type + "/" + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (NotSerializableException nse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            NotSerializableException nse = new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                        ("java.security.Key: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                        "[" + type + "] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                        "[" + algorithm + "] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                        "[" + format + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            nse.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}