src/java.security.jgss/share/classes/sun/security/jgss/krb5/AcceptSecContextToken.java
author weijun
Tue, 14 Aug 2018 22:39:34 +0800
changeset 51398 3c389a284345
parent 47216 71c04702a3d5
permissions -rw-r--r--
8209416: Refactoring GetPropertyAction calls in security libs Reviewed-by: xuelei, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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;
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    31
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    32
import sun.security.action.GetBooleanAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.krb5.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
class AcceptSecContextToken extends InitialToken {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private KrbApRep apRep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * Creates an AcceptSecContextToken for the context acceptor to send to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * the context initiator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public AcceptSecContextToken(Krb5Context context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                 KrbApReq apReq)
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    45
        throws KrbException, IOException, GSSException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
51398
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    47
        boolean useSubkey = GetBooleanAction
3c389a284345 8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents: 47216
diff changeset
    48
                .privilegedGetProperty("sun.security.krb5.acceptor.subkey");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        boolean useSequenceNumber = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    52
        EncryptionKey subKey = null;
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    53
        if (useSubkey) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    54
            subKey = new EncryptionKey(apReq.getCreds().getSessionKey());
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    55
            context.setKey(Krb5Context.ACCEPTOR_SUBKEY, subKey);
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    56
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents: 14342
diff changeset
    57
        apRep = new KrbApRep(apReq, useSequenceNumber, subKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        context.resetMySequenceNumber(apRep.getSeqNumber().intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * Note: The acceptor side context key was set when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * InitSecContextToken was received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates an AcceptSecContextToken at the context initiator's side
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * using the bytes received from  the acceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public AcceptSecContextToken(Krb5Context context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                 Credentials serviceCreds, KrbApReq apReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                 InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws IOException, GSSException, KrbException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int tokenId = ((is.read()<<8) | is.read());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (tokenId != Krb5Token.AP_REP_ID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new GSSException(GSSException.DEFECTIVE_TOKEN, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                   "AP_REP token id does not match!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        byte[] apRepBytes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            new sun.security.util.DerValue(is).toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        KrbApRep apRep = new KrbApRep(apRepBytes, serviceCreds, apReq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * Allow the context acceptor to set a subkey if desired, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * though our context acceptor will not do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        EncryptionKey subKey = apRep.getSubKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (subKey != null) {
10697
ecee258b7d87 7077646: gssapi wrap for CFX per-message tokens always set FLAG_ACCEPTOR_SUBKEY
weijun
parents: 5506
diff changeset
    93
            context.setKey(Krb5Context.ACCEPTOR_SUBKEY, subKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.out.println("\n\nSub-Session key from AP-REP is: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                               getHexBytes(subKey.getBytes()) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Integer apRepSeqNumber = apRep.getSeqNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int peerSeqNumber = (apRepSeqNumber != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                             apRepSeqNumber.intValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                             0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        context.resetPeerSequenceNumber(peerSeqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public final byte[] encode() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        byte[] apRepBytes = apRep.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        byte[] retVal = new byte[2 + apRepBytes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        writeInt(Krb5Token.AP_REP_ID, retVal, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.arraycopy(apRepBytes, 0, retVal, 2, apRepBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}