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.util.*; |
| 35 | import java.util.Vector; |
| 36 | import java.io.IOException; |
| 37 | import java.math.BigInteger; |
| 38 | |
| 39 | /** |
| 40 | * Implements the ASN.1 EncAPRepPart type. |
| 41 | * |
| 42 | * <xmp> |
| 43 | * EncAPRepPart ::= [APPLICATION 27] SEQUENCE { |
| 44 | * ctime [0] KerberosTime, |
| 45 | * cusec [1] Microseconds, |
| 46 | * subkey [2] EncryptionKey OPTIONAL, |
| 47 | * seq-number [3] UInt32 OPTIONAL |
| 48 | * } |
| 49 | * </xmp> |
| 50 | * |
| 51 | * <p> |
| 52 | * This definition reflects the Network Working Group RFC 4120 |
| 53 | * specification available at |
| 54 | * <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
| 55 | * http://www.ietf.org/rfc/rfc4120.txt</a>. |
| 56 | */ |
| 57 | public class EncAPRepPart { |
| 58 | public KerberosTime ctime; |
| 59 | public int cusec; |
| 60 | EncryptionKey subKey; //optional |
| 61 | Integer seqNumber; //optional |
| 62 | |
| 63 | public EncAPRepPart( |
| 64 | KerberosTime new_ctime, |
| 65 | int new_cusec, |
| 66 | EncryptionKey new_subKey, |
| 67 | Integer new_seqNumber |
| 68 | ) { |
| 69 | ctime = new_ctime; |
| 70 | cusec = new_cusec; |
| 71 | subKey = new_subKey; |
| 72 | seqNumber = new_seqNumber; |
| 73 | } |
| 74 | |
| 75 | public EncAPRepPart(byte[] data) |
| 76 | throws Asn1Exception, IOException { |
| 77 | init(new DerValue(data)); |
| 78 | } |
| 79 | |
| 80 | public EncAPRepPart(DerValue encoding) |
| 81 | throws Asn1Exception, IOException { |
| 82 | init(encoding); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Initializes an EncaPRepPart object. |
| 87 | * @param encoding a single DER-encoded value. |
| 88 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 89 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 90 | */ |
| 91 | private void init(DerValue encoding) throws Asn1Exception, IOException { |
| 92 | DerValue der, subDer; |
| 93 | if (((encoding.getTag() & (byte)0x1F) != (byte)0x1B) |
| 94 | || (encoding.isApplication() != true) |
| 95 | || (encoding.isConstructed() != true)) |
| 96 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 97 | der = encoding.getData().getDerValue(); |
| 98 | if (der.getTag() != DerValue.tag_Sequence) |
| 99 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 100 | ctime = KerberosTime.parse(der.getData(), (byte)0x00, true); |
| 101 | subDer = der.getData().getDerValue(); |
| 102 | if ((subDer.getTag() & (byte)0x1F) == (byte)0x01) { |
| 103 | cusec = subDer.getData().getBigInteger().intValue(); |
| 104 | } |
| 105 | else |
| 106 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 107 | if (der.getData().available() > 0) { |
| 108 | subKey = EncryptionKey.parse(der.getData(), (byte)0x02, true); |
| 109 | } |
| 110 | else { |
| 111 | subKey = null; |
| 112 | seqNumber = null; |
| 113 | } |
| 114 | if (der.getData().available() > 0) { |
| 115 | subDer = der.getData().getDerValue(); |
| 116 | if ((subDer.getTag() & 0x1F) != 0x03) { |
| 117 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 118 | } |
| 119 | seqNumber = new Integer(subDer.getData().getBigInteger().intValue()); |
| 120 | } |
| 121 | else seqNumber = null; |
| 122 | if (der.getData().available() > 0) |
| 123 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Encodes an EncAPRepPart object. |
| 128 | * @return byte array of encoded EncAPRepPart object. |
| 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 | */ |
| 132 | public byte[] asn1Encode() throws Asn1Exception, IOException{ |
| 133 | Vector<DerValue> v = new Vector<DerValue> (); |
| 134 | DerOutputStream temp = new DerOutputStream(); |
| 135 | v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), ctime.asn1Encode())); |
| 136 | temp.putInteger(BigInteger.valueOf(cusec)); |
| 137 | v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp.toByteArray())); |
| 138 | if (subKey != null) |
| 139 | v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), subKey.asn1Encode())); |
| 140 | if (seqNumber != null) { |
| 141 | temp = new DerOutputStream(); |
| 142 | // encode as an unsigned integer (UInt32) |
| 143 | temp.putInteger(BigInteger.valueOf(seqNumber.longValue())); |
| 144 | v.addElement(new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp.toByteArray())); |
| 145 | } |
| 146 | DerValue der[] = new DerValue[v.size()]; |
| 147 | v.copyInto(der); |
| 148 | temp = new DerOutputStream(); |
| 149 | temp.putSequence(der); |
| 150 | DerOutputStream out = new DerOutputStream(); |
| 151 | out.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x1B), temp); |
| 152 | return out.toByteArray(); |
| 153 | } |
| 154 | |
| 155 | public final EncryptionKey getSubKey() { |
| 156 | return subKey; |
| 157 | } |
| 158 | |
| 159 | public final Integer getSeqNumber() { |
| 160 | return seqNumber; |
| 161 | } |
| 162 | |
| 163 | } |