J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.krb5.internal; |
| 27 | |
| 28 | import sun.security.util.*; |
| 29 | import sun.security.krb5.Asn1Exception; |
| 30 | import java.io.IOException; |
| 31 | |
| 32 | /** |
| 33 | * Implements the ASN.1 ETYPE-INFO-ENTRY type. |
| 34 | * |
| 35 | * ETYPE-INFO2-ENTRY ::= SEQUENCE { |
| 36 | * etype [0] Int32, |
| 37 | * salt [1] KerberosString OPTIONAL, |
| 38 | * s2kparams [2] OCTET STRING OPTIONAL |
| 39 | * } |
| 40 | * |
| 41 | * @author Seema Malkani |
| 42 | */ |
| 43 | |
| 44 | public class ETypeInfo2 { |
| 45 | |
| 46 | private int etype; |
| 47 | private String saltStr = null; |
| 48 | private byte[] s2kparams = null; |
| 49 | |
| 50 | private static final byte TAG_TYPE = 0; |
| 51 | private static final byte TAG_VALUE1 = 1; |
| 52 | private static final byte TAG_VALUE2 = 2; |
| 53 | |
| 54 | private ETypeInfo2() { |
| 55 | } |
| 56 | |
| 57 | public ETypeInfo2(int etype, byte[] salt, byte[] s2kparams) { |
| 58 | this.etype = etype; |
| 59 | if (salt != null) { |
| 60 | this.saltStr = new String(salt); |
| 61 | } |
| 62 | if (s2kparams != null) { |
| 63 | this.s2kparams = s2kparams.clone(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public Object clone() { |
| 68 | ETypeInfo2 etypeInfo2 = new ETypeInfo2(); |
| 69 | etypeInfo2.etype = etype; |
| 70 | etypeInfo2.saltStr = saltStr; |
| 71 | if (s2kparams != null) { |
| 72 | etypeInfo2.s2kparams = new byte[s2kparams.length]; |
| 73 | System.arraycopy(s2kparams, 0, etypeInfo2.s2kparams, |
| 74 | 0, s2kparams.length); |
| 75 | } |
| 76 | return etypeInfo2; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Constructs a ETypeInfo2 object. |
| 81 | * @param encoding a DER-encoded data. |
| 82 | * @exception Asn1Exception if an error occurs while decoding an |
| 83 | * ASN1 encoded data. |
| 84 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 85 | */ |
| 86 | public ETypeInfo2(DerValue encoding) throws Asn1Exception, IOException { |
| 87 | DerValue der = null; |
| 88 | |
| 89 | if (encoding.getTag() != DerValue.tag_Sequence) { |
| 90 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 91 | } |
| 92 | |
| 93 | // etype |
| 94 | der = encoding.getData().getDerValue(); |
| 95 | if ((der.getTag() & 0x1F) == 0x00) { |
| 96 | this.etype = der.getData().getBigInteger().intValue(); |
| 97 | } |
| 98 | else |
| 99 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 100 | |
| 101 | // salt |
| 102 | if (encoding.getData().available() > 0) { |
| 103 | der = encoding.getData().getDerValue(); |
| 104 | if ((der.getTag() & 0x1F) == 0x01) { |
| 105 | this.saltStr = der.getData().getGeneralString(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // s2kparams |
| 110 | if (encoding.getData().available() > 0) { |
| 111 | der = encoding.getData().getDerValue(); |
| 112 | if ((der.getTag() & 0x1F) == 0x02) { |
| 113 | this.s2kparams = der.getData().getOctetString(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (encoding.getData().available() > 0) |
| 118 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Encodes this object to an OutputStream. |
| 123 | * |
| 124 | * @return byte array of the encoded data. |
| 125 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 126 | * @exception Asn1Exception on encoding errors. |
| 127 | */ |
| 128 | public byte[] asn1Encode() throws Asn1Exception, IOException { |
| 129 | |
| 130 | DerOutputStream bytes = new DerOutputStream(); |
| 131 | DerOutputStream temp = new DerOutputStream(); |
| 132 | |
| 133 | temp.putInteger(etype); |
| 134 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
| 135 | TAG_TYPE), temp); |
| 136 | |
| 137 | if (saltStr != null) { |
| 138 | temp = new DerOutputStream(); |
| 139 | temp.putGeneralString(saltStr); |
| 140 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
| 141 | TAG_VALUE1), temp); |
| 142 | } |
| 143 | if (s2kparams != null) { |
| 144 | temp = new DerOutputStream(); |
| 145 | temp.putOctetString(s2kparams); |
| 146 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, |
| 147 | TAG_VALUE2), temp); |
| 148 | } |
| 149 | |
| 150 | temp = new DerOutputStream(); |
| 151 | temp.write(DerValue.tag_Sequence, bytes); |
| 152 | return temp.toByteArray(); |
| 153 | } |
| 154 | |
| 155 | // accessor methods |
| 156 | public int getEType() { |
| 157 | return etype; |
| 158 | } |
| 159 | |
| 160 | public byte[] getSalt() { |
| 161 | return ((saltStr == null) ? null : saltStr.getBytes()); |
| 162 | } |
| 163 | |
| 164 | public byte[] getParams() { |
| 165 | return ((s2kparams == null) ? null : s2kparams.clone()); |
| 166 | } |
| 167 | |
| 168 | } |