jdk/src/java.base/share/classes/java/security/KeyRep.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 18579 jdk/src/share/classes/java/security/KeyRep.java@b678846778ad
child 29492 a4bf9a570035
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <a href="../../../platform/serialization/spec/security.html">
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final long serialVersionUID = -4757683898830641853L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Key type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static enum Type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        /** Type for secret keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        SECRET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        /** Type for public keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        /** Type for private keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        PRIVATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final String PKCS8 = "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final String X509 = "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final String RAW = "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private Type type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The Key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The Key encoding format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private String format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The encoded Key bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Construct the alternate Key class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param type either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param algorithm the algorithm returned from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   119
     *          {@code Key.getAlgorithm()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param format the encoding format returned from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   121
     *          {@code Key.getFormat()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param encoded the encoded bytes returned from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   123
     *          {@code Key.getEncoded()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @exception NullPointerException
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   126
     *          if type is {@code null},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   127
     *          if algorithm is {@code null},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   128
     *          if format is {@code null},
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 14342
diff changeset
   129
     *          or if encoded is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public KeyRep(Type type, String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                String format, byte[] encoded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (type == null || algorithm == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            format == null || encoded == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new NullPointerException("invalid null input(s)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.algorithm = algorithm;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   141
        this.format = format.toUpperCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.encoded = encoded.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Resolve the Key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p> This method supports three Type/format combinations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <li> Type.SECRET/"RAW" - returns a SecretKeySpec object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * constructed using encoded key bytes and algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <li> Type.PUBLIC/"X.509" - gets a KeyFactory instance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * the key algorithm, constructs an X509EncodedKeySpec with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * encoded key bytes, and generates a public key from the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <li> Type.PRIVATE/"PKCS#8" - gets a KeyFactory instance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * the key algorithm, constructs a PKCS8EncodedKeySpec with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * encoded key bytes, and generates a private key from the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the resolved Key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception ObjectStreamException if the Type/format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  combination is unrecognized, if the algorithm, key format, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  encoded key bytes are unrecognized/invalid, of if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *  resolution of the key fails for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (type == Type.SECRET && RAW.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return new SecretKeySpec(encoded, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else if (type == Type.PUBLIC && X509.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                KeyFactory f = KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                return f.generatePublic(new X509EncodedKeySpec(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } else if (type == Type.PRIVATE && PKCS8.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                KeyFactory f = KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return f.generatePrivate(new PKCS8EncodedKeySpec(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        ("unrecognized type/format combination: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        type + "/" + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } catch (NotSerializableException nse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            NotSerializableException nse = new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                        ("java.security.Key: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                        "[" + type + "] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                        "[" + algorithm + "] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                        "[" + format + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            nse.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}