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 | * (C) Copyright IBM Corp. 1999 All Rights Reserved. |
| 27 | * Copyright 1997 The Open Group Research Institute. All rights reserved. |
| 28 | */ |
| 29 | |
| 30 | package sun.security.krb5.internal; |
| 31 | |
| 32 | import sun.security.util.*; |
| 33 | import sun.security.krb5.Asn1Exception; |
| 34 | import java.util.Vector; |
| 35 | import java.io.IOException; |
| 36 | import java.math.BigInteger; |
| 37 | |
| 38 | /** |
| 39 | * Implements the ASN.1 EncKrbPrivPart type. |
| 40 | * |
| 41 | * <xmp> |
| 42 | * EncKrbPrivPart ::= [APPLICATION 28] SEQUENCE { |
| 43 | * user-data [0] OCTET STRING, |
| 44 | * timestamp [1] KerberosTime OPTIONAL, |
| 45 | * usec [2] Microseconds OPTIONAL, |
| 46 | * seq-number [3] UInt32 OPTIONAL, |
| 47 | * s-address [4] HostAddress -- sender's addr --, |
| 48 | * r-address [5] HostAddress OPTIONAL -- recip's addr |
| 49 | * } |
| 50 | * </xmp> |
| 51 | * |
| 52 | * <p> |
| 53 | * This definition reflects the Network Working Group RFC 4120 |
| 54 | * specification available at |
| 55 | * <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
| 56 | * http://www.ietf.org/rfc/rfc4120.txt</a>. |
| 57 | */ |
| 58 | |
| 59 | public class EncKrbPrivPart { |
| 60 | public byte[] userData = null; |
| 61 | public KerberosTime timestamp; //optional |
| 62 | public Integer usec; //optional |
| 63 | public Integer seqNumber; //optional |
| 64 | public HostAddress sAddress; //optional |
| 65 | public HostAddress rAddress; //optional |
| 66 | |
| 67 | public EncKrbPrivPart( |
| 68 | byte[] new_userData, |
| 69 | KerberosTime new_timestamp, |
| 70 | Integer new_usec, |
| 71 | Integer new_seqNumber, |
| 72 | HostAddress new_sAddress, |
| 73 | HostAddress new_rAddress |
| 74 | ) { |
| 75 | if (new_userData != null) { |
| 76 | userData = new_userData.clone(); |
| 77 | } |
| 78 | timestamp = new_timestamp; |
| 79 | usec = new_usec; |
| 80 | seqNumber = new_seqNumber; |
| 81 | sAddress = new_sAddress; |
| 82 | rAddress = new_rAddress; |
| 83 | } |
| 84 | |
| 85 | public EncKrbPrivPart(byte[] data) throws Asn1Exception, IOException { |
| 86 | init(new DerValue(data)); |
| 87 | } |
| 88 | |
| 89 | public EncKrbPrivPart(DerValue encoding) throws Asn1Exception, IOException { |
| 90 | init(encoding); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Initializes an EncKrbPrivPart object. |
| 95 | * @param encoding a single DER-encoded value. |
| 96 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 97 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 98 | */ |
| 99 | private void init(DerValue encoding) throws Asn1Exception, IOException { |
| 100 | DerValue der, subDer; |
| 101 | if (((encoding.getTag() & (byte)0x1F) != (byte)0x1C) |
| 102 | || (encoding.isApplication() != true) |
| 103 | || (encoding.isConstructed() != true)) |
| 104 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 105 | der = encoding.getData().getDerValue(); |
| 106 | if (der.getTag() != DerValue.tag_Sequence) |
| 107 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 108 | subDer = der.getData().getDerValue(); |
| 109 | if ((subDer.getTag() & (byte)0x1F) == (byte)0x00) { |
| 110 | userData = subDer.getData().getOctetString(); |
| 111 | } |
| 112 | else |
| 113 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 114 | timestamp = KerberosTime.parse(der.getData(), (byte)0x01, true); |
| 115 | if ((der.getData().peekByte() & 0x1F) == 0x02) { |
| 116 | subDer = der.getData().getDerValue(); |
| 117 | usec = new Integer(subDer.getData().getBigInteger().intValue()); |
| 118 | } |
| 119 | else usec = null; |
| 120 | if ((der.getData().peekByte() & 0x1F) == 0x03 ) { |
| 121 | subDer = der.getData().getDerValue(); |
| 122 | seqNumber = new Integer(subDer.getData().getBigInteger().intValue()); |
| 123 | } |
| 124 | else seqNumber = null; |
| 125 | sAddress = HostAddress.parse(der.getData(), (byte)0x04, false); |
| 126 | if (der.getData().available() > 0) { |
| 127 | rAddress = HostAddress.parse(der.getData(), (byte)0x05, true); |
| 128 | } |
| 129 | if (der.getData().available() > 0) |
| 130 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Encodes an EncKrbPrivPart object. |
| 135 | * @return byte array of encoded EncKrbPrivPart object. |
| 136 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 137 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 138 | */ |
| 139 | public byte[] asn1Encode() throws Asn1Exception, IOException { |
| 140 | DerOutputStream temp = new DerOutputStream(); |
| 141 | DerOutputStream bytes = new DerOutputStream(); |
| 142 | |
| 143 | temp.putOctetString(userData); |
| 144 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp); |
| 145 | if (timestamp != null) |
| 146 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), timestamp.asn1Encode()); |
| 147 | if (usec != null) { |
| 148 | temp = new DerOutputStream(); |
| 149 | temp.putInteger(BigInteger.valueOf(usec.intValue())); |
| 150 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), temp); |
| 151 | } |
| 152 | if (seqNumber != null) { |
| 153 | temp = new DerOutputStream(); |
| 154 | // encode as an unsigned integer (UInt32) |
| 155 | temp.putInteger(BigInteger.valueOf(seqNumber.longValue())); |
| 156 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp); |
| 157 | } |
| 158 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), sAddress.asn1Encode()); |
| 159 | if (rAddress != null) { |
| 160 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), rAddress.asn1Encode()); |
| 161 | } |
| 162 | temp = new DerOutputStream(); |
| 163 | temp.write(DerValue.tag_Sequence, bytes); |
| 164 | bytes = new DerOutputStream(); |
| 165 | bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x1C), temp); |
| 166 | return bytes.toByteArray(); |
| 167 | } |
| 168 | } |