J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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. Sun designates this |
| 7 | * particular file as subject to the "Classpath" exception as provided |
| 8 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any 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 | |
| 31 | package sun.security.krb5.internal; |
| 32 | |
| 33 | import sun.security.krb5.*; |
| 34 | import sun.security.krb5.EncryptionKey; |
| 35 | import sun.security.util.*; |
| 36 | import java.util.Vector; |
| 37 | import java.io.IOException; |
| 38 | import java.math.BigInteger; |
| 39 | /** |
| 40 | * Implements the ASN.1 EncKDCRepPart type. |
| 41 | * |
| 42 | * <xmp> |
| 43 | * EncKDCRepPart ::= SEQUENCE { |
| 44 | * key [0] EncryptionKey, |
| 45 | * last-req [1] LastReq, |
| 46 | * nonce [2] UInt32, |
| 47 | * key-expiration [3] KerberosTime OPTIONAL, |
| 48 | * flags [4] TicketFlags, |
| 49 | * authtime [5] KerberosTime, |
| 50 | * starttime [6] KerberosTime OPTIONAL, |
| 51 | * endtime [7] KerberosTime, |
| 52 | * renew-till [8] KerberosTime OPTIONAL, |
| 53 | * srealm [9] Realm, |
| 54 | * sname [10] PrincipalName, |
| 55 | * caddr [11] HostAddresses OPTIONAL |
| 56 | * } |
| 57 | * </xmp> |
| 58 | * |
| 59 | * <p> |
| 60 | * This definition reflects the Network Working Group RFC 4120 |
| 61 | * specification available at |
| 62 | * <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
| 63 | * http://www.ietf.org/rfc/rfc4120.txt</a>. |
| 64 | */ |
| 65 | public class EncKDCRepPart { |
| 66 | public EncryptionKey key; |
| 67 | public LastReq lastReq; |
| 68 | public int nonce; |
| 69 | public KerberosTime keyExpiration; //optional |
| 70 | public TicketFlags flags; |
| 71 | public KerberosTime authtime; |
| 72 | public KerberosTime starttime; //optional |
| 73 | public KerberosTime endtime; |
| 74 | public KerberosTime renewTill; //optional |
| 75 | public Realm srealm; |
| 76 | public PrincipalName sname; |
| 77 | public HostAddresses caddr; //optional |
| 78 | public int msgType; //not included in sequence |
| 79 | |
| 80 | public EncKDCRepPart( |
| 81 | EncryptionKey new_key, |
| 82 | LastReq new_lastReq, |
| 83 | int new_nonce, |
| 84 | KerberosTime new_keyExpiration, |
| 85 | TicketFlags new_flags, |
| 86 | KerberosTime new_authtime, |
| 87 | KerberosTime new_starttime, |
| 88 | KerberosTime new_endtime, |
| 89 | KerberosTime new_renewTill, |
| 90 | Realm new_srealm, |
| 91 | PrincipalName new_sname, |
| 92 | HostAddresses new_caddr, |
| 93 | int new_msgType |
| 94 | ) { |
| 95 | key = new_key; |
| 96 | lastReq = new_lastReq; |
| 97 | nonce = new_nonce; |
| 98 | keyExpiration = new_keyExpiration; |
| 99 | flags = new_flags; |
| 100 | authtime = new_authtime; |
| 101 | starttime = new_starttime; |
| 102 | endtime = new_endtime; |
| 103 | renewTill = new_renewTill; |
| 104 | srealm = new_srealm; |
| 105 | sname = new_sname; |
| 106 | caddr = new_caddr; |
| 107 | msgType = new_msgType; |
| 108 | } |
| 109 | |
| 110 | public EncKDCRepPart() { |
| 111 | } |
| 112 | |
| 113 | public EncKDCRepPart(byte[] data, int rep_type) |
| 114 | throws Asn1Exception, IOException, RealmException{ |
| 115 | init(new DerValue(data), rep_type); |
| 116 | } |
| 117 | |
| 118 | public EncKDCRepPart(DerValue encoding, int rep_type) |
| 119 | throws Asn1Exception, IOException, RealmException |
| 120 | { |
| 121 | init(encoding, rep_type); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Initializes an EncKDCRepPart object. |
| 126 | * |
| 127 | * @param encoding a single DER-encoded value. |
| 128 | * @param rep_type type of the encrypted reply message. |
| 129 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 130 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 131 | * @exception RealmException if an error occurs while decoding an Realm object. |
| 132 | */ |
| 133 | protected void init(DerValue encoding, int rep_type) |
| 134 | throws Asn1Exception, IOException, RealmException |
| 135 | { |
| 136 | DerValue der, subDer; |
| 137 | //implementations return the incorrect tag value, so |
| 138 | //we don't use the above line; instead we use the following |
| 139 | msgType = (encoding.getTag() & (byte)0x1F); |
| 140 | if (msgType != Krb5.KRB_ENC_AS_REP_PART && |
| 141 | msgType != Krb5.KRB_ENC_TGS_REP_PART) |
| 142 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 143 | der = encoding.getData().getDerValue(); |
| 144 | if (der.getTag() != DerValue.tag_Sequence) |
| 145 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 146 | key = EncryptionKey.parse(der.getData(), (byte)0x00, false); |
| 147 | lastReq = LastReq.parse(der.getData(), (byte)0x01, false); |
| 148 | subDer = der.getData().getDerValue(); |
| 149 | if ((subDer.getTag() & (byte)0x1F) == (byte)0x02) |
| 150 | nonce = subDer.getData().getBigInteger().intValue(); |
| 151 | else throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 152 | keyExpiration = KerberosTime.parse(der.getData(), (byte)0x03, true); |
| 153 | flags = TicketFlags.parse(der.getData(), (byte)0x04, false); |
| 154 | authtime = KerberosTime.parse(der.getData(), (byte)0x05, false); |
| 155 | starttime = KerberosTime.parse(der.getData(), (byte)0x06, true); |
| 156 | endtime = KerberosTime.parse(der.getData(), (byte)0x07, false); |
| 157 | renewTill = KerberosTime.parse(der.getData(), (byte)0x08, true); |
| 158 | srealm = Realm.parse(der.getData(), (byte)0x09, false); |
| 159 | sname = PrincipalName.parse(der.getData(), (byte)0x0A, false); |
| 160 | if (der.getData().available() > 0) |
| 161 | caddr = HostAddresses.parse(der.getData(), (byte)0x0B, true); |
| 162 | if (der.getData().available() > 0) |
| 163 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Encodes an EncKDCRepPart object. |
| 168 | * @param rep_type type of encrypted reply message. |
| 169 | * @return byte array of encoded EncKDCRepPart object. |
| 170 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 171 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 172 | */ |
| 173 | public byte[] asn1Encode(int rep_type) throws Asn1Exception, |
| 174 | IOException { |
| 175 | DerOutputStream temp = new DerOutputStream(); |
| 176 | DerOutputStream bytes = new DerOutputStream(); |
| 177 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), key.asn1Encode()); |
| 178 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), lastReq.asn1Encode()); |
| 179 | temp.putInteger(BigInteger.valueOf(nonce)); |
| 180 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), temp); |
| 181 | |
| 182 | if (keyExpiration != null) |
| 183 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), keyExpiration.asn1Encode()); |
| 184 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), flags.asn1Encode()); |
| 185 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), authtime.asn1Encode()); |
| 186 | if (starttime != null) |
| 187 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x06), starttime.asn1Encode()); |
| 188 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x07), endtime.asn1Encode()); |
| 189 | if (renewTill != null) |
| 190 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x08), renewTill.asn1Encode()); |
| 191 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x09), srealm.asn1Encode()); |
| 192 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x0A), sname.asn1Encode()); |
| 193 | if (caddr != null) |
| 194 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x0B), caddr.asn1Encode()); |
| 195 | //should use the rep_type to build the encoding |
| 196 | //but other implementations do not; it is ignored and |
| 197 | //the cached msgType is used instead |
| 198 | temp = new DerOutputStream(); |
| 199 | temp.write(DerValue.tag_Sequence, bytes); |
| 200 | bytes = new DerOutputStream(); |
| 201 | bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)msgType), temp); |
| 202 | return bytes.toByteArray(); |
| 203 | } |
| 204 | |
| 205 | } |