jdk/src/share/classes/sun/security/krb5/EncryptionKey.java
author weijun
Mon, 17 Dec 2012 12:18:46 +0800
changeset 14895 5ad2d7032375
parent 10432 ef33e56c55a9
child 18168 f47169155ea0
permissions -rw-r--r--
7197159: accept different kvno if there no match Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
     2
 * Copyright (c) 2000, 2011, 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,
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 7183
diff changeset
   141
                                                    String keytab) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (princ == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                "Cannot have null pricipal name to look in keytab.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // KeyTab getInstance(keytab) will call KeyTab.getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // if keytab is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        KeyTab ktab = KeyTab.getInstance(keytab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return ktab.readServiceKeys(princ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   154
     * Obtains a key for a given etype of a principal with possible new salt
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   155
     * and s2kparams
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   156
     * @param cname NOT null
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   157
     * @param password NOT null
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   158
     * @param etype
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   159
     * @param snp can be NULL
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   160
     * @returns never null
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   161
     */
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   162
    public static EncryptionKey acquireSecretKey(PrincipalName cname,
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   163
            char[] password, int etype, PAData.SaltAndParams snp)
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   164
            throws KrbException {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   165
        String salt;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   166
        byte[] s2kparams;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   167
        if (snp != null) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   168
            salt = snp.salt != null ? snp.salt : cname.getSalt();
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   169
            s2kparams = snp.params;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   170
        } else {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   171
            salt = cname.getSalt();
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   172
            s2kparams = null;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   173
        }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   174
        return acquireSecretKey(password, salt, etype, s2kparams);
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   175
    }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   176
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   177
    /**
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   178
     * Obtains a key for a given etype with salt and optional s2kparams
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   179
     * @param password NOT null
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   180
     * @param salt NOT null
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   181
     * @param etype
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   182
     * @param s2kparams can be NULL
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 9499
diff changeset
   183
     * @returns never null
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   184
     */
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   185
    public static EncryptionKey acquireSecretKey(char[] password,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   186
            String salt, int etype, byte[] s2kparams)
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   187
            throws KrbException {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   188
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   189
        return new EncryptionKey(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   190
                        stringToKey(password, salt, s2kparams, etype),
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   191
                        etype, null);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   192
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   193
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   194
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Generate a list of keys using the given principal and password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Construct a key for each configured etype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Caller is responsible for clearing password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Usually, when keyType is decoded from ASN.1 it will contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * value indicating what the algorithm to be used is. However, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * converting from a password to a key for the AS-EXCHANGE, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * keyType will not be available. Use builtin list of default etypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * as the default in that case. If default_tkt_enctypes was set in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * the libdefaults of krb5.conf, then use that sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public static EncryptionKey[] acquireSecretKeys(char[] password,
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   208
            String salt) throws KrbException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int[] etypes = EType.getDefaults("default_tkt_enctypes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (etypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            etypes = EType.getBuiltInDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        EncryptionKey[] encKeys = new EncryptionKey[etypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        for (int i = 0; i < etypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (EType.isSupported(etypes[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                encKeys[i] = new EncryptionKey(
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5802
diff changeset
   219
                        stringToKey(password, salt, null, etypes[i]),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        etypes[i], null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    System.out.println("Encryption Type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        EType.toString(etypes[i]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        " is not supported/enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return encKeys;
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 Krb5AcceptCredential, self
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public EncryptionKey(byte[] keyValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                         int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                         Integer kvno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (keyValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            this.keyValue = new byte[keyValue.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            System.arraycopy(keyValue, 0, this.keyValue, 0, keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new IllegalArgumentException("EncryptionKey: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                               "Key bytes cannot be null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        this.kvno = kvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Constructs an EncryptionKey by using the specified key type and key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * value.  It is used to recover the key when retrieving data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * credential cache file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     // Used in JSSE (KerberosWrapper), Credentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     // javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public EncryptionKey(int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                         byte[] keyValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        this(keyValue, keyType, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static byte[] stringToKey(char[] password, String salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        byte[] s2kparams, int keyType) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        char[] slt = salt.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        char[] pwsalt = new char[password.length + slt.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.arraycopy(password, 0, pwsalt, 0, password.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        System.arraycopy(slt, 0, pwsalt, password.length, slt.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Arrays.fill(slt, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            switch (keyType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                case EncryptedData.ETYPE_DES_CBC_CRC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                case EncryptedData.ETYPE_DES_CBC_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        return Des.string_to_key_bytes(pwsalt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        return Des3.stringToKey(pwsalt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                case EncryptedData.ETYPE_ARCFOUR_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        return ArcFourHmac.stringToKey(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        return Aes128.stringToKey(password, salt, s2kparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        return Aes256.stringToKey(password, salt, s2kparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        throw new IllegalArgumentException("encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        EType.toString(keyType) + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            Arrays.fill(pwsalt, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // Used in javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public EncryptionKey(char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                         String salt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                         String algorithm) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (algorithm == null || algorithm.equalsIgnoreCase("DES")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            keyType = EncryptedData.ETYPE_DES_CBC_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else if (algorithm.equalsIgnoreCase("DESede")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            keyType = EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else if (algorithm.equalsIgnoreCase("AES128")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            keyType = EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else if (algorithm.equalsIgnoreCase("ArcFourHmac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            keyType = EncryptedData.ETYPE_ARCFOUR_HMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else if (algorithm.equalsIgnoreCase("AES256")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            keyType = EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // validate if AES256 is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (!EType.isSupported(keyType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                throw new IllegalArgumentException("Algorithm " + algorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        " not enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new IllegalArgumentException("Algorithm " + algorithm +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        keyValue = stringToKey(password, salt, null, keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        kvno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Generates a sub-sessionkey from a given session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     // Used in KrbApRep, KrbApReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    EncryptionKey(EncryptionKey key) throws KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // generate random sub-session key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        keyValue = Confounder.bytes(key.keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        for (int i = 0; i < keyValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
          keyValue[i] ^= key.keyValue[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        keyType = key.keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // check for key parity and weak keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // check for DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                (keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                // fix DES key parity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    keyValue = Des.set_parity(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // check for weak key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (DESKeySpec.isWeak(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            // check for 3DES key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // fix 3DES key parity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    keyValue = Des3.parityFix(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                // check for weak keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                byte[] oneKey = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                for (int i=0; i<keyValue.length; i+=8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    System.arraycopy(keyValue, i, oneKey, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    if (DESKeySpec.isWeak(oneKey, 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            KrbCryptoException ke = new KrbCryptoException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Constructs an instance of EncryptionKey type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         // Used in javax.security.auth.kerberos.KeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public EncryptionKey(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            keyType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            keyValue = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Returns the ASN.1 encoding of this EncryptionKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * EncryptionKey ::=   SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *                             keytype[0]    INTEGER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *                             keyvalue[1]   OCTET STRING }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @return byte array of encoded EncryptionKey object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public synchronized byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        temp.putInteger(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                       (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        temp.putOctetString(keyValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                       (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public synchronized void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (keyValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            for (int i = 0; i < keyValue.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                keyValue[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Parse (unmarshal) an Encryption key from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param optional indicate if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @exception IOException if an I/O error occurs while reading encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @return an instance of EncryptionKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public static EncryptionKey parse(DerInputStream data, byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                      explicitTag, boolean optional) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                      Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                           explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return new EncryptionKey(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Writes key value in FCC format to a <code>CCacheOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public synchronized void writeKey(CCacheOutputStream cos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        cos.write16(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // we use KRB5_FCC_FVNO_3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        cos.write16(keyType); // key type is recorded twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        cos.write32(keyValue.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        for (int i = 0; i < keyValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            cos.write8(keyValue[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return new String("EncryptionKey: keyType=" + keyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                          + " kvno=" + kvno
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                          + " keyValue (hex dump)="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                          + (keyValue == null || keyValue.length == 0 ?
5457
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 4532
diff changeset
   513
                        " Empty Key" : '\n'
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 4532
diff changeset
   514
                        + Krb5.hexDumper.encodeBuffer(keyValue)
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 4532
diff changeset
   515
                        + '\n'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   518
    /**
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   519
     * Find a key with given etype
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   520
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public static EncryptionKey findKey(int etype, EncryptionKey[] keys)
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   522
            throws KrbException {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   523
        return findKey(etype, null, keys);
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
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   526
    /**
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   527
     * Determines if a kvno matches another kvno. Used in the method
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   528
     * findKey(type, kvno, keys). Always returns true if either input
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   529
     * 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
   530
     *
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   531
     * 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
   532
     * in javax.security.auth.kerberos.KerberosKey. Therefore, the info
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   533
     * 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
   534
     * class and KerberosKey.
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   535
     */
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   536
    private static boolean versionMatches(Integer v1, Integer v2) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   537
        if (v1 == null || v1 == 0 || v2 == null || v2 == 0) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   538
            return true;
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   539
        }
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   540
        return v1.equals(v2);
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   541
    }
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   542
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   543
    /**
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   544
     * Find a key with given etype and kvno
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   545
     * @param kvno if null, return any (first?) key
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   546
     */
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   547
    public static EncryptionKey findKey(int etype, Integer kvno, EncryptionKey[] keys)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // check if encryption type is supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (!EType.isSupported(etype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            throw new KrbException("Encryption type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                EType.toString(etype) + " is not supported/enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        int ktype;
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   557
        boolean etypeFound = false;
14895
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   558
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   559
        // When no matched kvno is found, returns tke key of the same
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   560
        // etype with the highest kvno
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   561
        int kvno_found = 0;
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   562
        EncryptionKey key_found = null;
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   563
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            ktype = keys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (EType.isSupported(ktype)) {
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   567
                Integer kv = keys[i].getKeyVersionNumber();
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   568
                if (etype == ktype) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   569
                    etypeFound = true;
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   570
                    if (versionMatches(kvno, kv)) {
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   571
                        return keys[i];
14895
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   572
                    } else if (kv > kvno_found) {
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   573
                        // kv is not null
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   574
                        key_found = keys[i];
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   575
                        kvno_found = kv;
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   576
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   580
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // Key not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        // allow DES key to be used for the DES etypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if ((etype == EncryptedData.ETYPE_DES_CBC_CRC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            etype == EncryptedData.ETYPE_DES_CBC_MD5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                ktype = keys[i].getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                if (ktype == EncryptedData.ETYPE_DES_CBC_CRC ||
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   588
                        ktype == EncryptedData.ETYPE_DES_CBC_MD5) {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   589
                    Integer kv = keys[i].getKeyVersionNumber();
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   590
                    etypeFound = true;
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   591
                    if (versionMatches(kvno, kv)) {
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   592
                        return new EncryptionKey(etype, keys[i].getBytes());
14895
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   593
                    } else if (kv > kvno_found) {
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   594
                        key_found = new EncryptionKey(etype, keys[i].getBytes());
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   595
                        kvno_found = kv;
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 2
diff changeset
   596
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   600
        if (etypeFound) {
14895
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   601
            return key_found;
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   602
            // For compatibility, will not fail here.
5ad2d7032375 7197159: accept different kvno if there no match
weijun
parents: 10432
diff changeset
   603
            //throw new KrbException(Krb5.KRB_AP_ERR_BADKEYVER);
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4168
diff changeset
   604
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
}