src/java.security.jgss/share/classes/sun/security/jgss/krb5/InitSecContextToken.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 26629 jdk/src/java.security.jgss/share/classes/sun/security/jgss/krb5/InitSecContextToken.java@3b9ed8175488
child 50187 261d0ac3b09d
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15006
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 3483
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: 3483
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: 3483
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3483
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3483
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
package sun.security.jgss.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.InetAddress;
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
    33
import sun.security.krb5.internal.AuthorizationData;
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
    34
import sun.security.krb5.internal.KerberosTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class InitSecContextToken extends InitialToken {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private KrbApReq apReq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * For the context initiator to call. It constructs a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * InitSecContextToken to send over to the peer containing the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * flags and the AP-REQ. It also updates the context with the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * sequence number and shared context key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * (When mutual auth is enabled the peer has an opportunity to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * renegotiate the session key in the followup AcceptSecContextToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * that it sends.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    InitSecContextToken(Krb5Context context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                               Credentials tgt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                               Credentials serviceTicket)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        throws KrbException, IOException, GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        boolean mutualRequired = context.getMutualAuthState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        boolean useSubkey = true; // MIT Impl will crash if this is not set!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        boolean useSequenceNumber = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        OverloadedChecksum gssChecksum =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            new OverloadedChecksum(context, tgt, serviceTicket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Checksum checksum = gssChecksum.getChecksum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
    63
        context.setTktFlags(serviceTicket.getFlags());
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
    64
        context.setAuthTime(
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
    65
                new KerberosTime(serviceTicket.getAuthTime()).toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        apReq = new KrbApReq(serviceTicket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                             mutualRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                             useSubkey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                             useSequenceNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                             checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        context.resetMySequenceNumber(apReq.getSeqNumber().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        EncryptionKey subKey = apReq.getSubKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (subKey != null)
10697
ecee258b7d87 7077646: gssapi wrap for CFX per-message tokens always set FLAG_ACCEPTOR_SUBKEY
weijun
parents: 9249
diff changeset
    76
            context.setKey(Krb5Context.INITIATOR_SUBKEY, subKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        else
10697
ecee258b7d87 7077646: gssapi wrap for CFX per-message tokens always set FLAG_ACCEPTOR_SUBKEY
weijun
parents: 9249
diff changeset
    78
            context.setKey(Krb5Context.SESSION_KEY, serviceTicket.getSessionKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!mutualRequired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            context.resetPeerSequenceNumber(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * For the context acceptor to call. It reads the bytes out of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * InputStream and constructs an InitSecContextToken with them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 10697
diff changeset
    88
    InitSecContextToken(Krb5Context context, Krb5AcceptCredential cred,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                               InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        throws IOException, GSSException, KrbException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int tokenId = ((is.read()<<8) | is.read());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (tokenId != Krb5Token.AP_REQ_ID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                   "AP_REQ token id does not match!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // XXX Modify KrbApReq cons to take an InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        byte[] apReqBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            new sun.security.util.DerValue(is).toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        //debug("=====ApReqBytes: [" + getHexBytes(apReqBytes) + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        InetAddress addr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (context.getChannelBinding() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            addr = context.getChannelBinding().getInitiatorAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 10697
diff changeset
   107
        apReq = new KrbApReq(apReqBytes, cred, addr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        //debug("\nReceived AP-REQ and authenticated it.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   110
        EncryptionKey sessionKey = apReq.getCreds().getSessionKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
          System.out.println("\n\nSession key from service ticket is: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
          getHexBytes(sessionKey.getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        EncryptionKey subKey = apReq.getSubKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (subKey != null) {
10697
ecee258b7d87 7077646: gssapi wrap for CFX per-message tokens always set FLAG_ACCEPTOR_SUBKEY
weijun
parents: 9249
diff changeset
   119
            context.setKey(Krb5Context.INITIATOR_SUBKEY, subKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
              System.out.println("Sub-Session key from authenticator is: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
              getHexBytes(subKey.getBytes()) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else {
10697
ecee258b7d87 7077646: gssapi wrap for CFX per-message tokens always set FLAG_ACCEPTOR_SUBKEY
weijun
parents: 9249
diff changeset
   125
            context.setKey(Krb5Context.SESSION_KEY, sessionKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            //System.out.println("Sub-Session Key Missing in Authenticator.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
9249
7aa2fc5371c5 7030180: AES 128/256 decrypt exception
weijun
parents: 5506
diff changeset
   129
        OverloadedChecksum gssChecksum = new OverloadedChecksum(
7aa2fc5371c5 7030180: AES 128/256 decrypt exception
weijun
parents: 5506
diff changeset
   130
                context, apReq.getChecksum(), sessionKey, subKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        gssChecksum.setContextFlags(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Credentials delegCred = gssChecksum.getDelegatedCreds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (delegCred != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Krb5CredElement credElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                Krb5InitCredential.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                   (Krb5NameElement)context.getSrcName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                   delegCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            context.setDelegCred(credElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Integer apReqSeqNumber = apReq.getSeqNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int peerSeqNumber = (apReqSeqNumber != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                             apReqSeqNumber.intValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                             0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        context.resetPeerSequenceNumber(peerSeqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (!context.getMutualAuthState())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // Use the same sequence number as the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // (Behaviour exhibited by the Windows SSPI server)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            context.resetMySequenceNumber(peerSeqNumber);
3483
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
   150
        context.setAuthTime(
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
   151
                new KerberosTime(apReq.getCreds().getAuthTime()).toString());
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
   152
        context.setTktFlags(apReq.getCreds().getFlags());
a16fce1820ef 6821190: more InquireType values for ExtendedGSSContext
weijun
parents: 715
diff changeset
   153
        AuthorizationData ad = apReq.getCreds().getAuthzData();
26629
3b9ed8175488 8042900: Allow com.sun.security.jgss to be in different module than org.ietf.jgss
weijun
parents: 25859
diff changeset
   154
        context.setAuthzData(ad);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public final KrbApReq getKrbApReq() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return apReq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public final byte[] encode() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        byte[] apReqBytes = apReq.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        byte[] retVal = new byte[2 + apReqBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        writeInt(Krb5Token.AP_REQ_ID, retVal, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        System.arraycopy(apReqBytes, 0, retVal, 2, apReqBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        //      System.out.println("GSS-Token with AP_REQ is:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        //      System.out.println(getHexBytes(retVal));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}