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