jdk/src/share/classes/sun/security/krb5/internal/KRBCred.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 73 cf334423502b
permissions -rw-r--r--
Initial load
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
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
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
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.security.krb5.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.krb5.EncryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.RealmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Implements the ASN.1 Authenticator type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * KRB-CRED     ::= [APPLICATION 22] SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      pvno            [0] INTEGER (5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      msg-type        [1] INTEGER (22),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      tickets         [2] SEQUENCE OF Ticket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      enc-part        [3] EncryptedData -- EncKrbCredPart
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class KRBCred {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public Ticket[] tickets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public EncryptedData encPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        private int pvno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        private int msgType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public KRBCred(Ticket[] new_tickets, EncryptedData new_encPart) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                pvno = Krb5.PVNO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                msgType = Krb5.KRB_CRED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (new_tickets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    tickets = new Ticket[new_tickets.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    for (int i = 0; i < new_tickets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        if (new_tickets[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                            throw new IOException("Cannot create a KRBCred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                            tickets[i] = (Ticket)new_tickets[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                encPart = new_encPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public KRBCred(byte[] data) throws Asn1Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                RealmException, KrbApErrException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                init(new DerValue(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public KRBCred(DerValue encoding) throws Asn1Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                RealmException, KrbApErrException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                init(encoding);
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
         * Initializes an KRBCred object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * @exception KrbApErrException if the value read from the DER-encoded data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         *  stream does not match the pre-defined value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * @exception RealmException if an error occurs while parsing a Realm object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        private void init(DerValue encoding) throws Asn1Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                RealmException, KrbApErrException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (((encoding.getTag() & (byte)0x1F) != (byte)0x16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        || (encoding.isApplication() != true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        || (encoding.isConstructed() != true))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        DerValue der, subDer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (der.getTag() != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if ((subDer.getTag() & 0x1F) == 0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        pvno = subDer.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        if (pvno != Krb5.PVNO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if ((subDer.getTag() & 0x1F) == 0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        msgType = subDer.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (msgType != Krb5.KRB_CRED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if ((subDer.getTag() & 0x1F) == 0x02) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        DerValue subsubDer = subDer.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (subsubDer.getTag() != DerValue.tag_SequenceOf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Vector<Ticket> v = new Vector<Ticket> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            while (subsubDer.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                v.addElement(new Ticket(subsubDer.getData().getDerValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (v.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                tickets = new Ticket[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                v.copyInto(tickets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                encPart = EncryptedData.parse(der.getData(), (byte)0x03, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (der.getData().available() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * Encodes an KRBCred object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * @return the data of encoded EncAPRepPart object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        DerOutputStream temp, bytes, out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        temp.putInteger(BigInteger.valueOf(pvno));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                temp.putInteger(BigInteger.valueOf(msgType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                for (int i = 0; i < tickets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        temp.write(tickets[i].asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        bytes.write(DerValue.tag_SequenceOf, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                out.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), encPart.asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        bytes.write(DerValue.tag_Sequence, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                out.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x16), bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}