jdk/src/share/classes/sun/security/krb5/EncryptionKey.java
author weijun
Thu, 24 Jun 2010 14:26:35 +0800
changeset 5975 076cd013e5e4
parent 5802 ea99d72d3c19
child 7183 d8ccc1c73358
permissions -rw-r--r--
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false) Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
diff changeset
     2
 * Copyright (c) 2000, 2010, 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: 5457
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: 5457
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: 5457
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
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
/*
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
        EncryptionKey[] encKeys = new EncryptionKey[etypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        for (int i = 0; i < etypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (EType.isSupported(etypes[i])) {
5802
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   194
                byte[] s2kparams = (pa_exists && etypes[i] == pa_etype)
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   195
                        ? pa_s2kparams : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                encKeys[i] = new EncryptionKey(
5802
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   197
                        stringToKey(password, salt, s2kparams, etypes[i]),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        etypes[i], null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    System.out.println("Encryption Type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        EType.toString(etypes[i]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        " is not supported/enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return encKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // Used in Krb5AcceptCredential, self
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public EncryptionKey(byte[] keyValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                         int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                         Integer kvno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (keyValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            this.keyValue = new byte[keyValue.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            System.arraycopy(keyValue, 0, this.keyValue, 0, keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IllegalArgumentException("EncryptionKey: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                               "Key bytes cannot be null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.kvno = kvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Constructs an EncryptionKey by using the specified key type and key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * value.  It is used to recover the key when retrieving data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * credential cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     // Used in JSSE (KerberosWrapper), Credentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     // javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public EncryptionKey(int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                         byte[] keyValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this(keyValue, keyType, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static byte[] stringToKey(char[] password, String salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        byte[] s2kparams, int keyType) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        char[] slt = salt.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        char[] pwsalt = new char[password.length + slt.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        System.arraycopy(password, 0, pwsalt, 0, password.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        System.arraycopy(slt, 0, pwsalt, password.length, slt.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Arrays.fill(slt, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            switch (keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                case EncryptedData.ETYPE_DES_CBC_CRC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                case EncryptedData.ETYPE_DES_CBC_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        return Des.string_to_key_bytes(pwsalt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        return Des3.stringToKey(pwsalt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                case EncryptedData.ETYPE_ARCFOUR_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        return ArcFourHmac.stringToKey(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        return Aes128.stringToKey(password, salt, s2kparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        return Aes256.stringToKey(password, salt, s2kparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        throw new IllegalArgumentException("encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        EType.toString(keyType) + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            Arrays.fill(pwsalt, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    // Used in javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public EncryptionKey(char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                         String salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                         String algorithm) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (algorithm == null || algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            keyType = EncryptedData.ETYPE_DES_CBC_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else if (algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            keyType = EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else if (algorithm.equalsIgnoreCase("AES128")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            keyType = EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else if (algorithm.equalsIgnoreCase("ArcFourHmac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            keyType = EncryptedData.ETYPE_ARCFOUR_HMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else if (algorithm.equalsIgnoreCase("AES256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            keyType = EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // validate if AES256 is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (!EType.isSupported(keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                throw new IllegalArgumentException("Algorithm " + algorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        " not enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new IllegalArgumentException("Algorithm " + algorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        keyValue = stringToKey(password, salt, null, keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        kvno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Generates a sub-sessionkey from a given session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     // Used in KrbApRep, KrbApReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    EncryptionKey(EncryptionKey key) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // generate random sub-session key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        keyValue = Confounder.bytes(key.keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        for (int i = 0; i < keyValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
          keyValue[i] ^= key.keyValue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        keyType = key.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // check for key parity and weak keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // check for DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                (keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // fix DES key parity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    keyValue = Des.set_parity(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // check for weak key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (DESKeySpec.isWeak(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // check for 3DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                // fix 3DES key parity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    keyValue = Des3.parityFix(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // check for weak keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                byte[] oneKey = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                for (int i=0; i<keyValue.length; i+=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    System.arraycopy(keyValue, i, oneKey, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    if (DESKeySpec.isWeak(oneKey, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Constructs an instance of EncryptionKey type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         // Used in javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public EncryptionKey(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            keyType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            keyValue = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns the ASN.1 encoding of this EncryptionKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * EncryptionKey ::=   SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *                             keytype[0]    INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *                             keyvalue[1]   OCTET STRING }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @return byte array of encoded EncryptionKey object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public synchronized byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        temp.putInteger(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                       (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        temp.putOctetString(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                       (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public synchronized void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (keyValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            for (int i = 0; i < keyValue.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                keyValue[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Parse (unmarshal) an Encryption key from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return an instance of EncryptionKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public static EncryptionKey parse(DerInputStream data, byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                      explicitTag, boolean optional) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                      Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                           explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return new EncryptionKey(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Writes key value in FCC format to a <code>CCacheOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public synchronized void writeKey(CCacheOutputStream cos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        cos.write16(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // we use KRB5_FCC_FVNO_3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        cos.write16(keyType); // key type is recorded twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        cos.write32(keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        for (int i = 0; i < keyValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            cos.write8(keyValue[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return new String("EncryptionKey: keyType=" + keyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                          + " kvno=" + kvno
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                          + " keyValue (hex dump)="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                          + (keyValue == null || keyValue.length == 0 ?
5457
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 4532
diff changeset
   491
                        " Empty Key" : '\n'
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 4532
diff changeset
   492
                        + Krb5.hexDumper.encodeBuffer(keyValue)
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 4532
diff changeset
   493
                        + '\n'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   496
    /**
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   497
     * Find a key with given etype
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   498
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public static EncryptionKey findKey(int etype, EncryptionKey[] keys)
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   500
            throws KrbException {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   501
        return findKey(etype, null, keys);
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   502
    }
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   503
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   504
    /**
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   505
     * Determines if a kvno matches another kvno. Used in the method
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   506
     * findKey(type, kvno, keys). Always returns true if either input
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   507
     * is null or zero, in case any side does not have kvno info available.
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   508
     *
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   509
     * Note: zero is included because N/A is not a legal value for kvno
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   510
     * in javax.security.auth.kerberos.KerberosKey. Therefore, the info
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   511
     * that the kvno is N/A might be lost when converting between this
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   512
     * class and KerberosKey.
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   513
     */
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   514
    private static boolean versionMatches(Integer v1, Integer v2) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   515
        if (v1 == null || v1 == 0 || v2 == null || v2 == 0) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   516
            return true;
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   517
        }
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   518
        return v1.equals(v2);
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   519
    }
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   520
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   521
    /**
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   522
     * Find a key with given etype and kvno
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   523
     * @param kvno if null, return any (first?) key
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   524
     */
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   525
    public static EncryptionKey findKey(int etype, Integer kvno, EncryptionKey[] keys)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // check if encryption type is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (!EType.isSupported(etype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            throw new KrbException("Encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                EType.toString(etype) + " is not supported/enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int ktype;
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   535
        boolean etypeFound = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            ktype = keys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (EType.isSupported(ktype)) {
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   539
                Integer kv = keys[i].getKeyVersionNumber();
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   540
                if (etype == ktype) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   541
                    etypeFound = true;
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   542
                    if (versionMatches(kvno, kv)) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   543
                        return keys[i];
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   544
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   548
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // Key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // allow DES key to be used for the DES etypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if ((etype == EncryptedData.ETYPE_DES_CBC_CRC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            etype == EncryptedData.ETYPE_DES_CBC_MD5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                ktype = keys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                if (ktype == EncryptedData.ETYPE_DES_CBC_CRC ||
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   556
                        ktype == EncryptedData.ETYPE_DES_CBC_MD5) {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   557
                    Integer kv = keys[i].getKeyVersionNumber();
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   558
                    etypeFound = true;
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   559
                    if (versionMatches(kvno, kv)) {
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   560
                        return new EncryptionKey(etype, keys[i].getBytes());
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   561
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   565
        if (etypeFound) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   566
            throw new KrbException(Krb5.KRB_AP_ERR_BADKEYVER);
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   567
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
}