jdk/src/share/classes/sun/security/krb5/KrbAsRep.java
author weijun
Fri, 12 Nov 2010 21:33:14 +0800
changeset 7183 d8ccc1c73358
parent 5975 076cd013e5e4
child 8166 13423c0952ad
permissions -rw-r--r--
6960894: Better AS-REQ creation and processing Reviewed-by: valeriep
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.krb5.internal.crypto.EType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    39
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class encapsulates a AS-REP message that the KDC sends to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    45
class KrbAsRep extends KrbKdcRep {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    47
    private ASRep rep;  // The AS-REP message
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    48
    private Credentials creds;  // The Credentials provide by the AS-REP
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    49
                                // message, created by initiator after calling
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    50
                                // the decrypt() method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    54
    KrbAsRep(byte[] ibuf) throws
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    55
            KrbException, Asn1Exception, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        DerValue encoding = new DerValue(ibuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            rep = new ASRep(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } catch (Asn1Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            rep = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            KRBError err = new KRBError(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            String errStr = err.getErrorString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            String eText = null; // pick up text sent by the server (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (errStr != null && errStr.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                if (errStr.charAt(errStr.length() - 1) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    eText = errStr.substring(0, errStr.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    eText = errStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            KrbException ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (eText == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                // no text sent from server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                ke = new KrbException(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    System.out.println("KRBError received: " + eText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                // override default text with server text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                ke = new KrbException(err, eText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            ke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    85
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    87
    // KrbAsReqBuilder need to read back the PA for key generation
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    88
    PAData[] getPA() {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    89
        return rep.pAData;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    90
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    91
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    92
    /**
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    93
     * Called by KrbAsReqBuilder to resolve a AS-REP message using keys.
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    94
     * @param keys user provided keys, not null
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    95
     * @param asReq the original AS-REQ sent, used to validate AS-REP
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    96
     */
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    97
    void decryptUsingKeys(EncryptionKey[] keys, KrbAsReq asReq)
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    98
            throws KrbException, Asn1Exception, IOException {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
    99
        EncryptionKey dkey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int encPartKeyType = rep.encPart.getEType();
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   101
        Integer encPartKvno = rep.encPart.kvno;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   102
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   103
            dkey = EncryptionKey.findKey(encPartKeyType, encPartKvno, keys);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   104
        } catch (KrbException ke) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   105
            if (ke.returnCode() == Krb5.KRB_AP_ERR_BADKEYVER) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   106
                // Fallback to no kvno. In some cases, keytab is generated
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   107
                // not by sysadmin but Java's ktab command
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   108
                dkey = EncryptionKey.findKey(encPartKeyType, keys);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   109
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   110
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (dkey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new KrbException(Krb5.API_INVALID_ARG,
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   113
                "Cannot find key for type/kvno to decrypt AS REP - " +
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   114
                EType.toString(encPartKeyType) + "/" + encPartKvno);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   116
        decrypt(dkey, asReq);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   117
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   119
    /**
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   120
     * Called by KrbAsReqBuilder to resolve a AS-REP message using a password.
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   121
     * @param password user provided password. not null
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   122
     * @param asReq the original AS-REQ sent, used to validate AS-REP
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   123
     * @param cname the user principal name, used to provide salt
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   124
     */
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   125
    void decryptUsingPassword(char[] password,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   126
            KrbAsReq asReq, PrincipalName cname)
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   127
            throws KrbException, Asn1Exception, IOException {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   128
        int encPartKeyType = rep.encPart.getEType();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   129
        PAData.SaltAndParams snp =
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   130
                PAData.getSaltAndParams(encPartKeyType, rep.pAData);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   131
        EncryptionKey dkey = null;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   132
        dkey = EncryptionKey.acquireSecretKey(password,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   133
                snp.salt == null ? cname.getSalt() : snp.salt,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   134
                encPartKeyType,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   135
                snp.params);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   136
        decrypt(dkey, asReq);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   137
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   138
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   139
    /**
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   140
     * Decrypts encrypted content inside AS-REP. Called by initiator.
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   141
     * @param dkey the decryption key to use
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   142
     * @param asReq the original AS-REQ sent, used to validate AS-REP
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   143
     */
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   144
    private void decrypt(EncryptionKey dkey, KrbAsReq asReq)
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   145
            throws KrbException, Asn1Exception, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        byte[] enc_as_rep_bytes = rep.encPart.decrypt(dkey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            KeyUsage.KU_ENC_AS_REP_PART);
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   148
        byte[] enc_as_rep_part = rep.encPart.reset(enc_as_rep_bytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   150
        DerValue encoding = new DerValue(enc_as_rep_part);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        EncASRepPart enc_part = new EncASRepPart(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        rep.ticket.sname.setRealm(rep.ticket.realm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        rep.encKDCRepPart = enc_part;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   155
        ASReq req = asReq.getMessage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        check(req, rep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        creds = new Credentials(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                rep.ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                req.reqBody.cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                rep.ticket.sname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                enc_part.key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                enc_part.flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                enc_part.authtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                enc_part.starttime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                enc_part.endtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                enc_part.renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                enc_part.caddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.out.println(">>> KrbAsRep cons in KrbAsReq.getReply " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                               req.reqBody.cname.getNameString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   175
    Credentials getCreds() {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   176
        return Objects.nonNull(creds, "Creds not available yet.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 5975
diff changeset
   179
    sun.security.krb5.internal.ccache.Credentials getCCreds() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return new sun.security.krb5.internal.ccache.Credentials(rep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}