jdk/src/share/classes/sun/security/krb5/KrbPriv.java
author xuelei
Sat, 24 Jul 2010 22:59:41 +0800
changeset 6122 16fa7ed7ff1b
parent 5975 076cd013e5e4
child 13247 74902cfeb9c6
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.*;
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
/** XXX This class does not appear to be used. **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class KrbPriv extends KrbAppMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private byte[] obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private byte[] userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private KrbPriv(byte[] userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                   Credentials creds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                   EncryptionKey subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                   KerberosTime timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                   SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                   HostAddress saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                   HostAddress raddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                   )  throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        EncryptionKey reqKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (subKey != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            reqKey = subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            reqKey = creds.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        obuf = mk_priv(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                       userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                       reqKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                       timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                       seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                       saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                       raddr
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                       );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private KrbPriv(byte[] msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                   Credentials creds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                   EncryptionKey subKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                   SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                   HostAddress saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                   HostAddress raddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                   boolean timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                   boolean seqNumberRequired
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                   )  throws KrbException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        KRBPriv krb_priv = new KRBPriv(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        EncryptionKey reqKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (subKey != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            reqKey = subKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            reqKey = creds.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        userData = rd_priv(krb_priv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                           reqKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                           seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                           saddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                           raddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                           seqNumberRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                           creds.client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                           creds.client.getRealm()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public byte[] getMessage() throws KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return obuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private byte[] mk_priv(byte[] userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                           EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                           KerberosTime timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                           SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                           HostAddress sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                           HostAddress rAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                           ) throws Asn1Exception, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                           KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                               Integer usec = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                               Integer seqno = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                               if (timestamp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                               usec = new Integer(timestamp.getMicroSeconds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                               if (seqNumber != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                   seqno = new Integer(seqNumber.current());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                   seqNumber.step();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                               EncKrbPrivPart unenc_encKrbPrivPart =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                               new EncKrbPrivPart(userData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                  timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                                  usec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                                  seqno,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                  sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                  rAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                                  );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                               byte[] temp = unenc_encKrbPrivPart.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                               EncryptedData encKrbPrivPart =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                               new EncryptedData(key, temp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                   KeyUsage.KU_ENC_KRB_PRIV_PART);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                               KRBPriv krb_priv = new KRBPriv(encKrbPrivPart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                               temp = krb_priv.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                               return krb_priv.asn1Encode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private byte[] rd_priv(KRBPriv krb_priv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                           EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                           SeqNumber seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                           HostAddress sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                           HostAddress rAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                           boolean timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                           boolean seqNumberRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                           PrincipalName cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                           Realm crealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                           ) throws Asn1Exception, KdcErrException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                           KrbApErrException, IOException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                               byte[] bytes = krb_priv.encPart.decrypt(key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                   KeyUsage.KU_ENC_KRB_PRIV_PART);
5975
076cd013e5e4 6946669: SSL/Krb5 should not call EncryptedData.reset(data, false)
weijun
parents: 5506
diff changeset
   161
                               byte[] temp = krb_priv.encPart.reset(bytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                               DerValue ref = new DerValue(temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                               EncKrbPrivPart enc_part = new EncKrbPrivPart(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                               check(enc_part.timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                     enc_part.usec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                     enc_part.seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                     enc_part.sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                     enc_part.rAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                     seqNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                     sAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                     rAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                     timestampRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                     seqNumberRequired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                     cname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                     crealm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                               return enc_part.userData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}