jdk/src/share/classes/sun/security/krb5/KrbCred.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.KeyUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.DerValue;
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 the KRB-CRED message that a client uses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * send its delegated credentials to a server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Supports delegation of one ticket only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class KrbCred {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private byte[] obuf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private KRBCred credMessg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Ticket ticket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private EncKrbCredPart encPart = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Credentials creds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private KerberosTime timeStamp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         // Used in InitialToken with null key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public KrbCred(Credentials tgt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                   Credentials serviceTicket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                   EncryptionKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        PrincipalName client = tgt.getClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        PrincipalName tgService = tgt.getServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        PrincipalName server = serviceTicket.getServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (!serviceTicket.getClient().equals(client))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new KrbException(Krb5.KRB_ERR_GENERIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                "Client principal does not match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // XXX Check Windows flag OK-TO-FORWARD-TO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // Invoke TGS-REQ to get a forwarded TGT for the peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        KDCOptions options = new KDCOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        options.set(KDCOptions.FORWARDED, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        options.set(KDCOptions.FORWARDABLE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        HostAddresses sAddrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // XXX Also NT_GSS_KRB5_PRINCIPAL can be a host based principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // GSSName.NT_HOSTBASED_SERVICE should display with KRB_NT_SRV_HST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (server.getNameType() == PrincipalName.KRB_NT_SRV_HST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            sAddrs=  new HostAddresses(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        KrbTgsReq tgsReq = new KrbTgsReq(options, tgt, tgService,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                         null, null, null, null, sAddrs, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        credMessg = createMessage(tgsReq.sendAndGetCreds(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        obuf = credMessg.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    KRBCred createMessage(Credentials delegatedCreds, EncryptionKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        EncryptionKey sessionKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            = delegatedCreds.getSessionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        PrincipalName princ = delegatedCreds.getClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Realm realm = princ.getRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        PrincipalName tgService = delegatedCreds.getServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Realm tgsRealm = tgService.getRealm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        KrbCredInfo credInfo = new KrbCredInfo(sessionKey, realm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                               princ, delegatedCreds.flags, delegatedCreds.authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                               delegatedCreds.startTime, delegatedCreds.endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                               delegatedCreds.renewTill, tgsRealm, tgService,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                               delegatedCreds.cAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        timeStamp = new KerberosTime(KerberosTime.NOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        KrbCredInfo[] credInfos = {credInfo};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        EncKrbCredPart encPart =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new EncKrbCredPart(credInfos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                               timeStamp, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        EncryptedData encEncPart = new EncryptedData(key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            encPart.asn1Encode(), KeyUsage.KU_ENC_KRB_CRED_PART);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Ticket[] tickets = {delegatedCreds.ticket};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        credMessg = new KRBCred(tickets, encEncPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return credMessg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         // Used in InitialToken, key always NULL_KEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public KrbCred(byte[] asn1Message, EncryptionKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        credMessg = new KRBCred(asn1Message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        ticket = credMessg.tickets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        byte[] temp = credMessg.encPart.decrypt(key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            KeyUsage.KU_ENC_KRB_CRED_PART);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        byte[] plainText = credMessg.encPart.reset(temp, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        DerValue encoding = new DerValue(plainText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        EncKrbCredPart encPart = new EncKrbCredPart(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        timeStamp = encPart.timeStamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        KrbCredInfo credInfo = encPart.ticketInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        EncryptionKey credInfoKey = credInfo.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Realm prealm = credInfo.prealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // XXX PrincipalName can store realm + principalname or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // just principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        PrincipalName pname = credInfo.pname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        pname.setRealm(prealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        TicketFlags flags = credInfo.flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        KerberosTime authtime = credInfo.authtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        KerberosTime starttime = credInfo.starttime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        KerberosTime endtime = credInfo.endtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        KerberosTime renewTill = credInfo.renewTill;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Realm srealm = credInfo.srealm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        PrincipalName sname = credInfo.sname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        sname.setRealm(srealm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        HostAddresses caddr = credInfo.caddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.out.println(">>>Delegated Creds have pname=" + pname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                               + " sname=" + sname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                               + " authtime=" + authtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                               + " starttime=" + starttime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                               + " endtime=" + endtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                               + "renewTill=" + renewTill);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        creds = new Credentials(ticket, pname, sname, credInfoKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                flags, authtime, starttime, endtime, renewTill, caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the delegated credentials from the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Credentials[] getDelegatedCreds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Credentials[] allCreds = {creds};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return allCreds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns the ASN.1 encoding that should be sent to the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public byte[] getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}