jdk/src/share/classes/sun/security/krb5/KrbApRep.java
author xuelei
Sat, 24 Jul 2010 22:59:41 +0800
changeset 6122 16fa7ed7ff1b
parent 5975 076cd013e5e4
child 18536 092411ced388
permissions -rw-r--r--
6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID Reviewed-by: mullan, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2010, 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
/*
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 sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
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 KRB-AP-REP sent from the service to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class KrbApRep {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private byte[] obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private byte[] ibuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private EncAPRepPart encPart; // although in plain text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private APRep apRepMessg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Constructs a KRB-AP-REP to send to a client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     // Used in AcceptSecContextToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public KrbApRep(KrbApReq incomingReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    boolean useSeqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        boolean useSubKey) throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        EncryptionKey subKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            (useSubKey?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
             new EncryptionKey(incomingReq.getCreds().getSessionKey()):null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        SeqNumber seqNum = new LocalSeqNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        init(incomingReq, subKey, seqNum);
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
     * Constructs a KRB-AP-REQ from the bytes received from a service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     // Used in AcceptSecContextToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public KrbApRep(byte[] message, Credentials tgtCreds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    KrbApReq outgoingReq) throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this(message, tgtCreds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        authenticate(outgoingReq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private void init(KrbApReq apReq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
              EncryptionKey subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        SeqNumber seqNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        createMessage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                      apReq.getCreds().key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                      apReq.getCtime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                      apReq.cusec(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                      subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                      seqNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        obuf = apRepMessg.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs a KrbApRep object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param msg a byte array of reply message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param tgs_creds client's credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private KrbApRep(byte[] msg, Credentials tgs_creds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this(new DerValue(msg), tgs_creds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Constructs a KrbApRep object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param msg a byte array of reply message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param tgs_creds client's credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception KrbException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private KrbApRep(DerValue encoding, Credentials tgs_creds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        APRep rep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            rep = new APRep(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (Asn1Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            rep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            KRBError err = new KRBError(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            String errStr = err.getErrorString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            String eText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (errStr.charAt(errStr.length() - 1) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                eText = errStr.substring(0, errStr.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                eText = errStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            KrbException ke = new KrbException(err.getErrorCode(), eText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        byte[] temp = rep.encPart.decrypt(tgs_creds.key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            KeyUsage.KU_ENC_AP_REP_PART);
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   133
        byte[] enc_ap_rep_part = rep.encPart.reset(temp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        encoding = new DerValue(enc_ap_rep_part);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        encPart = new EncAPRepPart(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private void authenticate(KrbApReq apReq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (encPart.ctime.getSeconds() != apReq.getCtime().getSeconds() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            encPart.cusec != apReq.getCtime().getMicroSeconds())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new KrbApErrException(Krb5.KRB_AP_ERR_MUT_FAIL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the optional subkey stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * this message. Returns null if none is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public EncryptionKey getSubKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // XXX Can encPart be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return encPart.getSubKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the optional sequence number stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * this message. Returns null if none is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public Integer getSeqNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // XXX Can encPart be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return encPart.getSeqNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns the ASN.1 encoding that should be sent to the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public byte[] getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private void createMessage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                               EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                               KerberosTime ctime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                               int cusec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                               EncryptionKey subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                               SeqNumber seqNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws Asn1Exception, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
               KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Integer seqno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (seqNumber != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            seqno = new Integer(seqNumber.current());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        encPart = new EncAPRepPart(ctime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                   cusec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                   subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                   seqno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        byte[] encPartEncoding = encPart.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        EncryptedData encEncPart = new EncryptedData(key, encPartEncoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            KeyUsage.KU_ENC_AP_REP_PART);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        apRepMessg = new APRep(encEncPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}