jdk/src/share/classes/sun/security/krb5/KrbSafe.java
author weijun
Thu, 24 Jun 2010 14:26:35 +0800
changeset 5975 076cd013e5e4
parent 5506 202f599c92aa
child 13247 74902cfeb9c6
permissions -rw-r--r--
6946669: SSL/Krb5 should not call EncryptedData.reset(data, false) Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2004, 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.EncryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.crypto.*;
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
class KrbSafe extends KrbAppMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private byte[] obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private byte[] userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public KrbSafe(byte[] userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                   Credentials creds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                   EncryptionKey subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                   KerberosTime timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                   SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                   HostAddress saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                   HostAddress raddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                   )  throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        EncryptionKey reqKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (subKey != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            reqKey = subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            reqKey = creds.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        obuf = mk_safe(userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                       reqKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                       timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                       seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                       saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                       raddr
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
    public KrbSafe(byte[] msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                   Credentials creds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                   EncryptionKey subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                   SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                   HostAddress saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                   HostAddress raddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                   boolean timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                   boolean seqNumberRequired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                   )  throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        KRBSafe krb_safe = new KRBSafe(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        EncryptionKey reqKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (subKey != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            reqKey = subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            reqKey = creds.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        userData = rd_safe(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                           krb_safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                           reqKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                           seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                           saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           raddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                           timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                           seqNumberRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                           creds.client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           creds.client.getRealm()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                           );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public byte[] getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private  byte[] mk_safe(byte[] userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            KerberosTime timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            HostAddress sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            HostAddress rAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            ) throws Asn1Exception, IOException, KdcErrException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                Integer usec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                Integer seqno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                if (timestamp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                usec = new Integer(timestamp.getMicroSeconds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                if (seqNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                    seqno = new Integer(seqNumber.current());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                    seqNumber.step();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                KRBSafeBody krb_safeBody =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                new KRBSafeBody(userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                                timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                                usec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                seqno,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                                rAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                byte[] temp = krb_safeBody.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                Checksum cksum = new Checksum(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                    Checksum.SAFECKSUMTYPE_DEFAULT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                    temp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                    key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                    KeyUsage.KU_KRB_SAFE_CKSUM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                KRBSafe krb_safe = new KRBSafe(krb_safeBody, cksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                temp = krb_safe.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                return krb_safe.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private byte[] rd_safe(KRBSafe krb_safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                           EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                           SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                           HostAddress sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                           HostAddress rAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                           boolean timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                           boolean seqNumberRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                           PrincipalName cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                           Realm crealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                           ) throws Asn1Exception, KdcErrException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                           KrbApErrException, IOException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                               byte[] temp = krb_safe.safeBody.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                               if (!krb_safe.cksum.verifyKeyedChecksum(temp, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                   KeyUsage.KU_KRB_SAFE_CKSUM)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                       throw new KrbApErrException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                           Krb5.KRB_AP_ERR_MODIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                               check(krb_safe.safeBody.timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                     krb_safe.safeBody.usec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                     krb_safe.safeBody.seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                     krb_safe.safeBody.sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                     krb_safe.safeBody.rAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                     seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                     sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                     rAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                     timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                     seqNumberRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                     cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                     crealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                               return krb_safe.safeBody.userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}