KrbCredInfo.java revision 12318:bee34b1dcbf1
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25/*
26 *
27 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
28 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
29 */
30
31package sun.security.krb5.internal;
32
33import sun.security.krb5.*;
34import sun.security.util.*;
35import java.util.Vector;
36import java.io.IOException;
37
38/**
39 * Implements the ASN.1 KrbCredInfo type.
40 *
41 * <xmp>
42 * KrbCredInfo  ::= SEQUENCE {
43 *      key             [0] EncryptionKey,
44 *      prealm          [1] Realm OPTIONAL,
45 *      pname           [2] PrincipalName OPTIONAL,
46 *      flags           [3] TicketFlags OPTIONAL,
47 *      authtime        [4] KerberosTime OPTIONAL,
48 *      starttime       [5] KerberosTime OPTIONAL,
49 *      endtime         [6] KerberosTime OPTIONAL,
50 *      renew-till      [7] KerberosTime OPTIONAL,
51 *      srealm          [8] Realm OPTIONAL,
52 *      sname           [9] PrincipalName OPTIONAL,
53 *      caddr           [10] HostAddresses OPTIONAL
54 * }
55 * </xmp>
56 *
57 * <p>
58 * This definition reflects the Network Working Group RFC 4120
59 * specification available at
60 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
61 * http://www.ietf.org/rfc/rfc4120.txt</a>.
62 */
63
64public class KrbCredInfo {
65    public EncryptionKey key;
66    public PrincipalName pname; //optional
67    public TicketFlags flags; //optional
68    public KerberosTime authtime; //optional
69    public KerberosTime starttime; //optional
70    public KerberosTime endtime; //optional
71    public KerberosTime renewTill; //optional
72    public PrincipalName sname; //optional
73    public HostAddresses caddr; //optional
74
75    private KrbCredInfo() {
76    }
77
78    public KrbCredInfo(
79                       EncryptionKey new_key,
80                       PrincipalName new_pname,
81                       TicketFlags new_flags,
82                       KerberosTime new_authtime,
83                       KerberosTime new_starttime,
84                       KerberosTime new_endtime,
85                       KerberosTime new_renewTill,
86                       PrincipalName new_sname,
87                       HostAddresses new_caddr
88                           ) {
89        key = new_key;
90        pname = new_pname;
91        flags = new_flags;
92        authtime = new_authtime;
93        starttime = new_starttime;
94        endtime = new_endtime;
95        renewTill = new_renewTill;
96        sname = new_sname;
97        caddr = new_caddr;
98    }
99
100    /**
101     * Constructs a KrbCredInfo object.
102     * @param encoding a Der-encoded data.
103     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
104     * @exception IOException if an I/O error occurs while reading encoded data.
105     * @exception RealmException if an error occurs while parsing a Realm object.
106     */
107    public KrbCredInfo(DerValue encoding)
108            throws Asn1Exception, IOException, RealmException{
109        if (encoding.getTag() != DerValue.tag_Sequence) {
110            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
111        }
112        pname = null;
113        flags = null;
114        authtime = null;
115        starttime = null;
116        endtime = null;
117        renewTill = null;
118        sname = null;
119        caddr = null;
120        key = EncryptionKey.parse(encoding.getData(), (byte)0x00, false);
121        Realm prealm = null, srealm = null;
122        if (encoding.getData().available() > 0)
123            prealm = Realm.parse(encoding.getData(), (byte)0x01, true);
124        if (encoding.getData().available() > 0)
125            pname = PrincipalName.parse(encoding.getData(), (byte)0x02, true, prealm);
126        if (encoding.getData().available() > 0)
127            flags = TicketFlags.parse(encoding.getData(), (byte)0x03, true);
128        if (encoding.getData().available() > 0)
129            authtime = KerberosTime.parse(encoding.getData(), (byte)0x04, true);
130        if (encoding.getData().available() > 0)
131            starttime = KerberosTime.parse(encoding.getData(), (byte)0x05, true);
132        if (encoding.getData().available() > 0)
133            endtime = KerberosTime.parse(encoding.getData(), (byte)0x06, true);
134        if (encoding.getData().available() > 0)
135            renewTill = KerberosTime.parse(encoding.getData(), (byte)0x07, true);
136        if (encoding.getData().available() > 0)
137            srealm = Realm.parse(encoding.getData(), (byte)0x08, true);
138        if (encoding.getData().available() > 0)
139            sname = PrincipalName.parse(encoding.getData(), (byte)0x09, true, srealm);
140        if (encoding.getData().available() > 0)
141            caddr = HostAddresses.parse(encoding.getData(), (byte)0x0A, true);
142        if (encoding.getData().available() > 0)
143            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
144    }
145
146    /**
147     * Encodes an KrbCredInfo object.
148     * @return the byte array of encoded KrbCredInfo object.
149     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
150     * @exception IOException if an I/O error occurs while reading encoded data.
151     */
152    public byte[] asn1Encode() throws Asn1Exception, IOException {
153        Vector<DerValue> v = new Vector<>();
154        v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), key.asn1Encode()));
155        if (pname != null) {
156            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), pname.getRealm().asn1Encode()));
157            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), pname.asn1Encode()));
158        }
159        if (flags != null)
160            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), flags.asn1Encode()));
161        if (authtime != null)
162            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), authtime.asn1Encode()));
163        if (starttime != null)
164            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), starttime.asn1Encode()));
165        if (endtime != null)
166            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x06), endtime.asn1Encode()));
167        if (renewTill != null)
168            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x07), renewTill.asn1Encode()));
169        if (sname != null) {
170            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x08), sname.getRealm().asn1Encode()));
171            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x09), sname.asn1Encode()));
172        }
173        if (caddr != null)
174            v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x0A), caddr.asn1Encode()));
175        DerValue[] der = new DerValue[v.size()];
176        v.copyInto(der);
177        DerOutputStream out = new DerOutputStream();
178        out.putSequence(der);
179        return out.toByteArray();
180    }
181
182    public Object clone() {
183        KrbCredInfo kcred = new KrbCredInfo();
184        kcred.key = (EncryptionKey)key.clone();
185        // optional fields
186        if (pname != null)
187            kcred.pname = (PrincipalName)pname.clone();
188        if (flags != null)
189            kcred.flags = (TicketFlags)flags.clone();
190        kcred.authtime = authtime;
191        kcred.starttime = starttime;
192        kcred.endtime = endtime;
193        kcred.renewTill = renewTill;
194        if (sname != null)
195            kcred.sname = (PrincipalName)sname.clone();
196        if (caddr != null)
197            kcred.caddr = (HostAddresses)caddr.clone();
198        return kcred;
199    }
200
201}
202