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.util.*; |
| 34 | import sun.security.krb5.Asn1Exception; |
| 35 | import java.util.Vector; |
| 36 | import java.io.IOException; |
| 37 | import sun.security.krb5.internal.ccache.CCacheOutputStream; |
| 38 | |
| 39 | /** |
| 40 | * In RFC4120, the ASN.1 AuthorizationData is defined as: |
| 41 | * |
| 42 | * AuthorizationData ::= SEQUENCE OF SEQUENCE { |
| 43 | * ad-type [0] Int32, |
| 44 | * ad-data [1] OCTET STRING |
| 45 | * } |
| 46 | * |
| 47 | * Here, two classes are used to implement it and they can be represented as follows: |
| 48 | * |
| 49 | * AuthorizationData ::= SEQUENCE OF AuthorizationDataEntry |
| 50 | * AuthorizationDataEntry ::= SEQUENCE { |
| 51 | * ad-type[0] Int32, |
| 52 | * ad-data[1] OCTET STRING |
| 53 | * } |
| 54 | */ |
| 55 | public class AuthorizationData implements Cloneable { |
| 56 | private AuthorizationDataEntry[] entry = null; |
| 57 | |
| 58 | private AuthorizationData() { |
| 59 | } |
| 60 | |
| 61 | public AuthorizationData( |
| 62 | AuthorizationDataEntry[] new_entries |
| 63 | ) throws IOException { |
| 64 | if (new_entries != null) { |
| 65 | entry = new AuthorizationDataEntry[new_entries.length]; |
| 66 | for (int i = 0; i < new_entries.length; i++) { |
| 67 | if (new_entries[i] == null) { |
| 68 | throw new IOException("Cannot create an AuthorizationData"); |
| 69 | } else { |
| 70 | entry[i] = (AuthorizationDataEntry)new_entries[i].clone(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public AuthorizationData( |
| 77 | AuthorizationDataEntry new_entry |
| 78 | ) { |
| 79 | entry = new AuthorizationDataEntry[1]; |
| 80 | entry[0] = new_entry; |
| 81 | } |
| 82 | |
| 83 | public Object clone() { |
| 84 | AuthorizationData new_authorizationData = |
| 85 | new AuthorizationData(); |
| 86 | if (entry != null) { |
| 87 | new_authorizationData.entry = |
| 88 | new AuthorizationDataEntry[entry.length]; |
| 89 | for (int i = 0; i < entry.length; i++) |
| 90 | new_authorizationData.entry[i] = |
| 91 | (AuthorizationDataEntry)entry[i].clone(); |
| 92 | } |
| 93 | return new_authorizationData; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Constructs a new <code>AuthorizationData,</code> instance. |
| 98 | * @param der a single DER-encoded value. |
| 99 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 100 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 101 | */ |
| 102 | public AuthorizationData(DerValue der) throws Asn1Exception, IOException { |
| 103 | Vector<AuthorizationDataEntry> v = |
| 104 | new Vector<AuthorizationDataEntry> (); |
| 105 | if (der.getTag() != DerValue.tag_Sequence) { |
| 106 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 107 | } |
| 108 | while (der.getData().available() > 0) { |
| 109 | v.addElement(new AuthorizationDataEntry(der.getData().getDerValue())); |
| 110 | } |
| 111 | if (v.size() > 0) { |
| 112 | entry = new AuthorizationDataEntry[v.size()]; |
| 113 | v.copyInto(entry); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Encodes an <code>AuthorizationData</code> object. |
| 119 | * @return byte array of encoded <code>AuthorizationData</code> object. |
| 120 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 121 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 122 | */ |
| 123 | public byte[] asn1Encode() throws Asn1Exception, IOException { |
| 124 | DerOutputStream bytes = new DerOutputStream(); |
| 125 | DerValue der[] = new DerValue[entry.length]; |
| 126 | for (int i = 0; i < entry.length; i++) { |
| 127 | der[i] = new DerValue(entry[i].asn1Encode()); |
| 128 | } |
| 129 | bytes.putSequence(der); |
| 130 | return bytes.toByteArray(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Parse (unmarshal) an <code>AuthorizationData</code> object from a DER input stream. |
| 135 | * This form of parsing might be used when expanding a value which is part of |
| 136 | * a constructed sequence and uses explicitly tagged type. |
| 137 | * |
| 138 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 139 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 140 | * @param data the Der input stream value, which contains one or more marshaled value. |
| 141 | * @param explicitTag tag number. |
| 142 | * @param optional indicates if this data field is optional |
| 143 | * @return an instance of AuthorizationData. |
| 144 | * |
| 145 | */ |
| 146 | public static AuthorizationData parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException{ |
| 147 | if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) { |
| 148 | return null; |
| 149 | } |
| 150 | DerValue der = data.getDerValue(); |
| 151 | if (explicitTag != (der.getTag() & (byte)0x1F)) { |
| 152 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 153 | } |
| 154 | else { |
| 155 | DerValue subDer = der.getData().getDerValue(); |
| 156 | return new AuthorizationData(subDer); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Writes <code>AuthorizationData</code> data fields to a output stream. |
| 162 | * |
| 163 | * @param cos a <code>CCacheOutputStream</code> to be written to. |
| 164 | * @exception IOException if an I/O exception occurs. |
| 165 | */ |
| 166 | public void writeAuth(CCacheOutputStream cos) throws IOException { |
| 167 | for (int i = 0; i < entry.length; i++) { |
| 168 | entry[i].writeEntry(cos); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | public String toString() { |
| 173 | String retVal = "AuthorizationData:\n"; |
| 174 | for (int i = 0; i < entry.length; i++) { |
| 175 | retVal += entry[i].toString(); |
| 176 | } |
| 177 | return retVal; |
| 178 | } |
| 179 | } |