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.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * 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.util;
32 
33 import java.io.IOException;
34 import java.util.Arrays;
35 import sun.security.krb5.internal.Krb5;
36 import sun.security.util.BitArray;
37 import sun.security.util.DerOutputStream;
38 
39 /**
40  * A wrapper class around sun.security.util.BitArray, so that KDCOptions,
41  * TicketFlags and ApOptions in krb5 classes can utilize some functions
42  * in BitArray classes.
43  *
44  * The data type is defined in RFC 4120 as:
45  *
46  * 5.2.8.  KerberosFlags
47  *
48  *  For several message types, a specific constrained bit string type,
49  *  KerberosFlags, is used.
50  *
51  *  KerberosFlags   ::= BIT STRING (SIZE (32..MAX))
52  *                      -- minimum number of bits shall be sent,
53  *                      -- but no fewer than 32
54  *
55  * @author Yanni Zhang
56  */
57 public class KerberosFlags {
58     BitArray bits;
59 
60     // This constant is used by child classes.
61     protected static final int BITS_PER_UNIT = 8;
62 
KerberosFlags(int length)63     public KerberosFlags(int length) throws IllegalArgumentException {
64         bits = new BitArray(length);
65     }
66 
KerberosFlags(int length, byte[] a)67     public KerberosFlags(int length, byte[] a) throws IllegalArgumentException {
68         bits = new BitArray(length, a);
69         if (length != Krb5.KRB_FLAGS_MAX+1) {
70             bits = new BitArray(Arrays.copyOf(bits.toBooleanArray(), Krb5.KRB_FLAGS_MAX+1));
71         }
72     }
73 
KerberosFlags(boolean[] bools)74     public KerberosFlags(boolean[] bools) {
75         bits = new BitArray((bools.length==Krb5.KRB_FLAGS_MAX+1)?
76             bools:
77             Arrays.copyOf(bools, Krb5.KRB_FLAGS_MAX+1));
78     }
79 
set(int index, boolean value)80     public void set(int index, boolean value) {
81         bits.set(index, value);
82     }
83 
get(int index)84     public boolean get(int index) {
85         return bits.get(index);
86     }
87 
toBooleanArray()88     public boolean[] toBooleanArray() {
89         return bits.toBooleanArray();
90     }
91 
92     /**
93      * Writes the encoded data.
94      *
95      * @exception IOException if an I/O error occurs while reading encoded data.
96      * @return an byte array of encoded KDCOptions.
97      */
asn1Encode()98     public byte[] asn1Encode() throws IOException {
99         DerOutputStream out = new DerOutputStream();
100         out.putUnalignedBitString(bits);
101         return out.toByteArray();
102     }
103 
toString()104     public String toString() {
105         return bits.toString();
106     }
107 }
108