jdk/src/share/classes/sun/security/krb5/internal/AuthorizationData.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 73 cf334423502b
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package sun.security.krb5.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.internal.ccache.CCacheOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * In RFC4120, the ASN.1 AuthorizationData is defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * AuthorizationData            ::= SEQUENCE OF SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *              ad-type         [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *              ad-data         [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Here, two classes are used to implement it and they can be represented as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * AuthorizationData ::= SEQUENCE OF AuthorizationDataEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * AuthorizationDataEntry ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *              ad-type[0]  Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *              ad-data[1]  OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class AuthorizationData implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        private AuthorizationDataEntry[] entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        private AuthorizationData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public AuthorizationData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                AuthorizationDataEntry[] new_entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        ) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (new_entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                   entry = new AuthorizationDataEntry[new_entries.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                   for (int i = 0; i < new_entries.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        if (new_entries[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                           throw new IOException("Cannot create an AuthorizationData");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                           entry[i] = (AuthorizationDataEntry)new_entries[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public AuthorizationData(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                AuthorizationDataEntry new_entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                entry = new AuthorizationDataEntry[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                entry[0] = new_entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                AuthorizationData new_authorizationData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        new AuthorizationData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        new_authorizationData.entry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                new AuthorizationDataEntry[entry.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        for (int i = 0; i < entry.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                new_authorizationData.entry[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                        (AuthorizationDataEntry)entry[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                return new_authorizationData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * Constructs a new <code>AuthorizationData,</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * @param der a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public AuthorizationData(DerValue der) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                Vector<AuthorizationDataEntry> v =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    new Vector<AuthorizationDataEntry> ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (der.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                while (der.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        v.addElement(new AuthorizationDataEntry(der.getData().getDerValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (v.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        entry = new AuthorizationDataEntry[v.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        v.copyInto(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * Encodes an <code>AuthorizationData</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * @return byte array of encoded <code>AuthorizationData</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                DerValue der[] = new DerValue[entry.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                for (int i = 0; i < entry.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        der[i] = new DerValue(entry[i].asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                bytes.putSequence(der);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                return bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Parse (unmarshal) an <code>AuthorizationData</code> object from a DER input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * This form of parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param data the Der input stream value, which contains one or more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return an instance of AuthorizationData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public static AuthorizationData parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if ((optional) && (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        return new AuthorizationData(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * Writes <code>AuthorizationData</code> data fields to a output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public void writeAuth(CCacheOutputStream cos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                for (int i = 0; i < entry.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        entry[i].writeEntry(cos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String retVal = "AuthorizationData:\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (int i = 0; i < entry.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            retVal += entry[i].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}