jdk/src/share/classes/sun/security/krb5/EncryptionKey.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 2 90ce3da70b43
child 4168 1a8d21bb898c
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
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
 * Portions Copyright 2000-2007 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.GeneralSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.krb5.internal.ktab.KeyTab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.krb5.internal.ccache.CCacheOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.spec.DESKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.crypto.spec.DESedeKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class encapsulates the concept of an EncryptionKey. An encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * key is defined in RFC 4120 as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * EncryptionKey   ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *         keytype         [0] Int32 -- actually encryption type --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *         keyvalue        [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * keytype
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     This field specifies the encryption type of the encryption key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     that follows in the keyvalue field.  Although its name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     "keytype", it actually specifies an encryption type.  Previously,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     multiple cryptosystems that performed encryption differently but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     were capable of using keys with the same characteristics were
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     permitted to share an assigned number to designate the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     key; this usage is now deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * keyvalue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     This field contains the key itself, encoded as an octet string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class EncryptionKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final EncryptionKey NULL_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        new EncryptionKey(new byte[] {}, EncryptedData.ETYPE_NULL, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private byte[] keyValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private Integer kvno; // not part of ASN1 encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public synchronized int getEType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public final Integer getKeyVersionNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return kvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Returns the raw key bytes, not in any ASN.1 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public final byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // This method cannot be called outside sun.security, hence no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // cloning. getEncoded() calls this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return keyValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public synchronized Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return new EncryptionKey(keyValue, keyType, kvno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Obtains the latest version of the secret key of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * the principal from a keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param princ the principal whose secret key is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param keytab the path to the keytab file. A value of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * will be accepted to indicate that the default path should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @returns the secret key or null if none was found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Replaced by acquireSecretKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static EncryptionKey acquireSecretKey(PrincipalName princ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                                 String keytab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (princ == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                "Cannot have null pricipal name to look in keytab.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        KeyTab ktab = KeyTab.getInstance(keytab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (ktab == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return ktab.readServiceKey(princ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Obtains all versions of the secret key of the principal from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * keytab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @Param princ the principal whose secret key is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param keytab the path to the keytab file. A value of null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * will be accepted to indicate that the default path should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @returns an array of secret keys or null if none were found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static EncryptionKey[] acquireSecretKeys(PrincipalName princ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                    String keytab)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (princ == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                "Cannot have null pricipal name to look in keytab.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // KeyTab getInstance(keytab) will call KeyTab.getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // if keytab is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        KeyTab ktab = KeyTab.getInstance(keytab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (ktab == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return ktab.readServiceKeys(princ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Generate a list of keys using the given principal and password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Construct a key for each configured etype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Caller is responsible for clearing password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Usually, when keyType is decoded from ASN.1 it will contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * value indicating what the algorithm to be used is. However, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * converting from a password to a key for the AS-EXCHANGE, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * keyType will not be available. Use builtin list of default etypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * as the default in that case. If default_tkt_enctypes was set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the libdefaults of krb5.conf, then use that sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         // Used in Krb5LoginModule
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static EncryptionKey[] acquireSecretKeys(char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String salt) throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return (acquireSecretKeys(password, salt, false, 0, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Generates a list of keys using the given principal, password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * and the pre-authentication values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static EncryptionKey[] acquireSecretKeys(char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String salt, boolean pa_exists, int pa_etype, byte[] pa_s2kparams)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int[] etypes = EType.getDefaults("default_tkt_enctypes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (etypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            etypes = EType.getBuiltInDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // set the preferred etype for preauth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if ((pa_exists) && (pa_etype != EncryptedData.ETYPE_NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                System.out.println("Pre-Authentication: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        "Set preferred etype = " + pa_etype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (EType.isSupported(pa_etype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // reset etypes to preferred value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                etypes = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                etypes[0] = pa_etype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        EncryptionKey[] encKeys = new EncryptionKey[etypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for (int i = 0; i < etypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (EType.isSupported(etypes[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                encKeys[i] = new EncryptionKey(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        stringToKey(password, salt, pa_s2kparams, etypes[i]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        etypes[i], null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    System.out.println("Encryption Type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        EType.toString(etypes[i]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        " is not supported/enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return encKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // Used in Krb5AcceptCredential, self
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public EncryptionKey(byte[] keyValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                         int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                         Integer kvno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (keyValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            this.keyValue = new byte[keyValue.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            System.arraycopy(keyValue, 0, this.keyValue, 0, keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new IllegalArgumentException("EncryptionKey: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                               "Key bytes cannot be null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        this.kvno = kvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Constructs an EncryptionKey by using the specified key type and key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * value.  It is used to recover the key when retrieving data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * credential cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     // Used in JSSE (KerberosWrapper), Credentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     // javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public EncryptionKey(int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                         byte[] keyValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        this(keyValue, keyType, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static byte[] stringToKey(char[] password, String salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byte[] s2kparams, int keyType) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        char[] slt = salt.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        char[] pwsalt = new char[password.length + slt.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        System.arraycopy(password, 0, pwsalt, 0, password.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        System.arraycopy(slt, 0, pwsalt, password.length, slt.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        Arrays.fill(slt, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            switch (keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                case EncryptedData.ETYPE_DES_CBC_CRC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                case EncryptedData.ETYPE_DES_CBC_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        return Des.string_to_key_bytes(pwsalt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        return Des3.stringToKey(pwsalt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                case EncryptedData.ETYPE_ARCFOUR_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        return ArcFourHmac.stringToKey(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        return Aes128.stringToKey(password, salt, s2kparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        return Aes256.stringToKey(password, salt, s2kparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        throw new IllegalArgumentException("encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        EType.toString(keyType) + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            Arrays.fill(pwsalt, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Used in javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public EncryptionKey(char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                         String salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                         String algorithm) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (algorithm == null || algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            keyType = EncryptedData.ETYPE_DES_CBC_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else if (algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            keyType = EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else if (algorithm.equalsIgnoreCase("AES128")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            keyType = EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else if (algorithm.equalsIgnoreCase("ArcFourHmac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            keyType = EncryptedData.ETYPE_ARCFOUR_HMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else if (algorithm.equalsIgnoreCase("AES256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            keyType = EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // validate if AES256 is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (!EType.isSupported(keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                throw new IllegalArgumentException("Algorithm " + algorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        " not enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new IllegalArgumentException("Algorithm " + algorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        keyValue = stringToKey(password, salt, null, keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        kvno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Generates a sub-sessionkey from a given session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     // Used in KrbApRep, KrbApReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    EncryptionKey(EncryptionKey key) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // generate random sub-session key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        keyValue = Confounder.bytes(key.keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        for (int i = 0; i < keyValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
          keyValue[i] ^= key.keyValue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        keyType = key.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // check for key parity and weak keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // check for DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                (keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // fix DES key parity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    keyValue = Des.set_parity(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // check for weak key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (DESKeySpec.isWeak(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // check for 3DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // fix 3DES key parity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    keyValue = Des3.parityFix(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // check for weak keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                byte[] oneKey = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                for (int i=0; i<keyValue.length; i+=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    System.arraycopy(keyValue, i, oneKey, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    if (DESKeySpec.isWeak(oneKey, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Constructs an instance of EncryptionKey type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         // Used in javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public EncryptionKey(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            keyType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            keyValue = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Returns the ASN.1 encoding of this EncryptionKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * EncryptionKey ::=   SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *                             keytype[0]    INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                             keyvalue[1]   OCTET STRING }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @return byte array of encoded EncryptionKey object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public synchronized byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        temp.putInteger(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                       (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        temp.putOctetString(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                       (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public synchronized void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (keyValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            for (int i = 0; i < keyValue.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                keyValue[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Parse (unmarshal) an Encryption key from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return an instance of EncryptionKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public static EncryptionKey parse(DerInputStream data, byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                      explicitTag, boolean optional) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                      Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                           explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return new EncryptionKey(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Writes key value in FCC format to a <code>CCacheOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public synchronized void writeKey(CCacheOutputStream cos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        cos.write16(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // we use KRB5_FCC_FVNO_3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        cos.write16(keyType); // key type is recorded twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        cos.write32(keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        for (int i = 0; i < keyValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            cos.write8(keyValue[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return new String("EncryptionKey: keyType=" + keyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                          + " kvno=" + kvno
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                          + " keyValue (hex dump)="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                          + (keyValue == null || keyValue.length == 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        " Empty Key" : '\n' + Krb5.hexDumper.encode(keyValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                             + '\n'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public static EncryptionKey findKey(int etype, EncryptionKey[] keys)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // check if encryption type is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (!EType.isSupported(etype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            throw new KrbException("Encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                EType.toString(etype) + " is not supported/enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int ktype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            ktype = keys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            if (EType.isSupported(ktype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                if (etype == ktype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    return keys[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // Key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // allow DES key to be used for the DES etypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if ((etype == EncryptedData.ETYPE_DES_CBC_CRC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            etype == EncryptedData.ETYPE_DES_CBC_MD5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                ktype = keys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                if (ktype == EncryptedData.ETYPE_DES_CBC_CRC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    ktype == EncryptedData.ETYPE_DES_CBC_MD5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    return new EncryptionKey(etype, keys[i].getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
}