jdk/src/share/classes/java/security/KeyRep.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.X509EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.SecretKeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Standardized representation for serialized Key objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Note that a serialized Key may contain sensitive information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * which should not be exposed in untrusted environments.  See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <a href="../../../platform/serialization/spec/security.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Security Appendix</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of the Serialization Specification for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see KeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see javax.crypto.spec.SecretKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see java.security.spec.X509EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see java.security.spec.PKCS8EncodedKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class KeyRep implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final long serialVersionUID = -4757683898830641853L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Key type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static enum Type {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        /** Type for secret keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        SECRET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /** Type for public keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /** Type for private keys. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        PRIVATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final String PKCS8 = "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final String X509 = "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final String RAW = "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private Type type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * The Key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The Key encoding format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private String format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The encoded Key bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private byte[] encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Construct the alternate Key class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *          <code>Key.getAlgorithm()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param format the encoding format returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          <code>Key.getFormat()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param encoded the encoded bytes returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *          <code>Key.getEncoded()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *          if type is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *          if algorithm is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *          if format is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *          or if encoded is <code>null</code>
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.format = format.toUpperCase();
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
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the resolved Key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception ObjectStreamException if the Type/format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  combination is unrecognized, if the algorithm, key format, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  encoded key bytes are unrecognized/invalid, of if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  resolution of the key fails for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (type == Type.SECRET && RAW.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return new SecretKeySpec(encoded, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } else if (type == Type.PUBLIC && X509.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                KeyFactory f = KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return f.generatePublic(new X509EncodedKeySpec(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if (type == Type.PRIVATE && PKCS8.equals(format)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                KeyFactory f = KeyFactory.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return f.generatePrivate(new PKCS8EncodedKeySpec(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throw new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        ("unrecognized type/format combination: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        type + "/" + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (NotSerializableException nse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            NotSerializableException nse = new NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                        ("java.security.Key: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                        "[" + type + "] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                        "[" + algorithm + "] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                        "[" + format + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            nse.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw nse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}