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