jdk/src/share/classes/sun/security/krb5/KrbTgsReq.java
author weijun
Thu, 24 Jun 2010 14:26:35 +0800
changeset 5975 076cd013e5e4
parent 5506 202f599c92aa
child 7183 d8ccc1c73358
permissions -rw-r--r--
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false) Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2000, 2008, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.EncryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.internal.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class encapsulates a Kerberos TGS-REQ that is sent from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * client to the KDC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class KrbTgsReq extends KrbKdcReq {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private PrincipalName princName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private PrincipalName servName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private TGSReq tgsReqMessg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private KerberosTime ctime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Ticket secondTicket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean useSubkey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    EncryptionKey tgsReqKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int defaultTimeout = 30*1000; // 30 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     // Used in CredentialsUtil
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public KrbTgsReq(Credentials asCreds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                     PrincipalName sname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this(new KDCOptions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            asCreds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            null, // KerberosTime from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            null, // KerberosTime till
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            null, // KerberosTime rtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            null, // eTypes, // null, // int[] eTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            null, // HostAddresses addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            null, // AuthorizationData authorizationData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            null, // Ticket[] additionalTickets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            null); // EncryptionKey subSessionKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    78
    // Called by Credentials, KrbCred
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    79
    KrbTgsReq(
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    80
            KDCOptions options,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    81
            Credentials asCreds,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    82
            PrincipalName sname,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    83
            KerberosTime from,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    84
            KerberosTime till,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    85
            KerberosTime rtime,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    86
            int[] eTypes,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    87
            HostAddresses addresses,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    88
            AuthorizationData authorizationData,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    89
            Ticket[] additionalTickets,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    90
            EncryptionKey subKey) throws KrbException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    92
        princName = asCreds.client;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    93
        servName = sname;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    94
        ctime = new KerberosTime(KerberosTime.NOW);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    97
        // check if they are valid arguments. The optional fields
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    98
        // should be  consistent with settings in KDCOptions.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
    99
        if (options.get(KDCOptions.FORWARDABLE) &&
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   100
                (!(asCreds.flags.get(Krb5.TKT_OPTS_FORWARDABLE)))) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   101
            throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   102
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   103
        if (options.get(KDCOptions.FORWARDED)) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   104
            if (!(asCreds.flags.get(KDCOptions.FORWARDABLE)))
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   105
                throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   106
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   107
        if (options.get(KDCOptions.PROXIABLE) &&
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   108
                (!(asCreds.flags.get(Krb5.TKT_OPTS_PROXIABLE)))) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   109
            throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   110
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   111
        if (options.get(KDCOptions.PROXY)) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   112
            if (!(asCreds.flags.get(KDCOptions.PROXIABLE)))
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   113
                throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   114
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   115
        if (options.get(KDCOptions.ALLOW_POSTDATE) &&
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   116
                (!(asCreds.flags.get(Krb5.TKT_OPTS_MAY_POSTDATE)))) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   117
            throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   118
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   119
        if (options.get(KDCOptions.RENEWABLE) &&
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   120
                (!(asCreds.flags.get(Krb5.TKT_OPTS_RENEWABLE)))) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   121
            throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   122
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   124
        if (options.get(KDCOptions.POSTDATED)) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   125
            if (!(asCreds.flags.get(KDCOptions.POSTDATED)))
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   126
                throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   127
        } else {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   128
            if (from != null)  from = null;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   129
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   130
        if (options.get(KDCOptions.RENEWABLE)) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   131
            if (!(asCreds.flags.get(KDCOptions.RENEWABLE)))
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   132
                throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   133
        } else {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   134
            if (rtime != null)  rtime = null;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   135
        }
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   136
        if (options.get(KDCOptions.ENC_TKT_IN_SKEY)) {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   137
            if (additionalTickets == null)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   138
                throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   139
            // in TGS_REQ there could be more than one additional
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   140
            // tickets,  but in file-based credential cache,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   141
            // there is only one additional ticket field.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   142
                secondTicket = additionalTickets[0];
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   143
        } else {
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   144
            if (additionalTickets != null)
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   145
                additionalTickets = null;
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   146
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   148
        tgsReqMessg = createRequest(
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   149
                options,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   150
                asCreds.ticket,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   151
                asCreds.key,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   152
                ctime,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   153
                princName,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   154
                princName.getRealm(),
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   155
                servName,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   156
                from,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   157
                till,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   158
                rtime,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   159
                eTypes,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   160
                addresses,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   161
                authorizationData,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   162
                additionalTickets,
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   163
                subKey);
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   164
        obuf = tgsReqMessg.asn1Encode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   166
        // XXX We need to revisit this to see if can't move it
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   167
        // up such that FORWARDED flag set in the options
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   168
        // is included in the marshaled request.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   169
        /*
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   170
         * If this is based on a forwarded ticket, record that in the
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   171
         * options, because the returned TgsRep will contain the
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   172
         * FORWARDED flag set.
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   173
         */
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   174
        if (asCreds.flags.get(KDCOptions.FORWARDED))
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   175
            options.set(KDCOptions.FORWARDED, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
73
cf334423502b 6641312: Fix krb5 codes indentation problems
weijun
parents: 2
diff changeset
   178
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Sends a TGS request to the realm of the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public String send() throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String realmStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (servName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            realmStr = servName.getRealmString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return (send(realmStr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public KrbTgsRep getReply()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return new KrbTgsRep(ibuf, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Sends the request, waits for a reply, and returns the Credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Used in Credentials, KrbCred, and internal/CredentialsUtil.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public Credentials sendAndGetCreds() throws IOException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        KrbTgsRep tgs_rep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        String kdc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            kdc = send();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            tgs_rep = getReply();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } catch (KrbException ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (ke.returnCode() == Krb5.KRB_ERR_RESPONSE_TOO_BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                // set useTCP and retry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                send(servName.getRealmString(), kdc, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                tgs_rep = getReply();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return tgs_rep.getCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    KerberosTime getCtime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return ctime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private TGSReq createRequest(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                         KDCOptions kdc_options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                         Ticket ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                         EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                         KerberosTime ctime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                         PrincipalName cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                         Realm crealm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                         PrincipalName sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                         KerberosTime from,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                         KerberosTime till,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                         KerberosTime rtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                         int[] eTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                         HostAddresses addresses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                         AuthorizationData authorizationData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                         Ticket[] additionalTickets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                         EncryptionKey subKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throws Asn1Exception, IOException, KdcErrException, KrbApErrException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
               UnknownHostException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        KerberosTime req_till = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (till == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            req_till = new KerberosTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            req_till = till;
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
         * RFC 4120, Section 5.4.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * For KRB_TGS_REP, the ciphertext is encrypted in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * sub-session key from the Authenticator, or if absent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * the session key from the ticket-granting ticket used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * in the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * To support this, use tgsReqKey to remember which key to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        tgsReqKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int[] req_eTypes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (eTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            req_eTypes = EType.getDefaults("default_tgs_enctypes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (req_eTypes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                throw new KrbCryptoException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            "No supported encryption types listed in default_tgs_enctypes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            req_eTypes = eTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        EncryptionKey reqKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        EncryptedData encAuthorizationData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (authorizationData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            byte[] ad = authorizationData.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (subKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                reqKey = subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                tgsReqKey = subKey;    // Key to use to decrypt reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                useSubkey = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                encAuthorizationData = new EncryptedData(reqKey, ad,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    KeyUsage.KU_TGS_REQ_AUTH_DATA_SUBKEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                encAuthorizationData = new EncryptedData(key, ad,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    KeyUsage.KU_TGS_REQ_AUTH_DATA_SESSKEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        KDCReqBody reqBody = new KDCReqBody(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                            kdc_options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                            cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                            // crealm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                            sname.getRealm(), // TO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                            sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                            from,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                            req_till,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                            rtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                            Nonce.value(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                            req_eTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                            addresses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                            encAuthorizationData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                            additionalTickets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        byte[] temp = reqBody.asn1Encode(Krb5.KRB_TGS_REQ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // if the checksum type is one of the keyed checksum types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // use session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Checksum cksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        switch (Checksum.CKSUMTYPE_DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        case Checksum.CKSUMTYPE_RSA_MD4_DES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        case Checksum.CKSUMTYPE_DES_MAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        case Checksum.CKSUMTYPE_DES_MAC_K:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        case Checksum.CKSUMTYPE_RSA_MD4_DES_K:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        case Checksum.CKSUMTYPE_RSA_MD5_DES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        case Checksum.CKSUMTYPE_HMAC_SHA1_DES3_KD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        case Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES128:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES256:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            cksum = new Checksum(Checksum.CKSUMTYPE_DEFAULT, temp, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                KeyUsage.KU_PA_TGS_REQ_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        case Checksum.CKSUMTYPE_CRC32:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        case Checksum.CKSUMTYPE_RSA_MD4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        case Checksum.CKSUMTYPE_RSA_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            cksum = new Checksum(Checksum.CKSUMTYPE_DEFAULT, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // Usage will be KeyUsage.KU_PA_TGS_REQ_AUTHENTICATOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        byte[] tgs_ap_req = new KrbApReq(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                         new APOptions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                         ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                         key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                         crealm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                         cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                         cksum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                         ctime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                         reqKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                         null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                         null).getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        PAData[] tgsPAData = new PAData[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        tgsPAData[0] = new PAData(Krb5.PA_TGS_REQ, tgs_ap_req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return new TGSReq(tgsPAData, reqBody);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    TGSReq getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return tgsReqMessg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    Ticket getSecondTicket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return secondTicket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private static void debug(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        //      System.err.println(">>> KrbTgsReq: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    boolean usedSubkey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return useSubkey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}