View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.shared.kerberos.components;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.Asn1Object;
27  import org.apache.directory.api.asn1.EncoderException;
28  import org.apache.directory.api.asn1.ber.tlv.BerValue;
29  import org.apache.directory.api.asn1.ber.tlv.TLV;
30  import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
31  import org.apache.directory.api.util.Strings;
32  import org.apache.directory.server.i18n.I18n;
33  import org.apache.directory.shared.kerberos.KerberosConstants;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * The AdAndOr structure is used to store a AD-AND-OR associated to a type.
40   * 
41   * The ASN.1 grammar is :
42   * <pre>
43   * AD-AND-OR               ::= SEQUENCE {
44   *         condition-count [0] Int32,
45   *         elements        [1] &lt;AuthorizationData&gt;
46   * }
47   * </pre>
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   */
50  public class AdAndOr implements Asn1Object
51  {
52      /** The logger */
53      private static final Logger LOG = LoggerFactory.getLogger( AdAndOr.class );
54  
55      /** Speedup for logs */
56      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
57  
58      /** The condition-count */
59      private int conditionCount;
60  
61      /** The elements */
62      private AuthorizationData elements;
63  
64      // Storage for computed lengths
65      private int conditionCountTagLength;
66      private int elementsTagLength;
67      private int adAndOrSeqLength;
68  
69  
70      /**
71       * Creates a new instance of AdAndOr
72       */
73      public AdAndOr()
74      {
75      }
76  
77  
78      /**
79       * @return the conditionCount
80       */
81      public int getConditionCount()
82      {
83          return conditionCount;
84      }
85  
86  
87      /**
88       * @param conditionCount the conditionCount to set
89       */
90      public void setConditionCount( int conditionCount )
91      {
92          this.conditionCount = conditionCount;
93      }
94  
95  
96      /**
97       * @return the elements
98       */
99      public AuthorizationData getElements()
100     {
101         return elements;
102     }
103 
104 
105     /**
106      * @param elements the elements to set
107      */
108     public void setElements( AuthorizationData elements )
109     {
110         this.elements = elements;
111     }
112 
113 
114     /**
115      * Compute the AD-AND-OR length
116      * <pre>
117      * 0x30 L1 AD-AND-OR sequence
118      *  |
119      *  +--&gt; 0xA1 L2 condition count tag
120      *  |     |
121      *  |     +--&gt; 0x02 L2-1 condition count (int)
122      *  |
123      *  +--&gt; 0xA2 L3 elements tag
124      *        |
125      *        +--&gt; 0x30 L3-1 elements (AuthorizationData)
126      * </pre>
127      */
128     @Override
129     public int computeLength()
130     {
131         // Compute the condition count length
132         int conditionCountLength = BerValue.getNbBytes( conditionCount );
133         conditionCountTagLength = 1 + TLV.getNbBytes( conditionCountLength ) + conditionCountLength;
134         adAndOrSeqLength = 1 + TLV.getNbBytes( conditionCountTagLength ) + conditionCountTagLength;
135 
136         // Compute the elements length
137         elementsTagLength = elements.computeLength();
138         adAndOrSeqLength += 1 + TLV.getNbBytes( elementsTagLength ) + elementsTagLength;
139 
140         // Compute the whole sequence length
141         return 1 + TLV.getNbBytes( adAndOrSeqLength ) + adAndOrSeqLength;
142     }
143 
144 
145     /**
146      * Encode the AD-AND-OR message to a PDU.
147      * 
148      * @param buffer The buffer where to put the PDU. It should have been allocated
149      * before, with the right size.
150      * @return The constructed PDU.
151      */
152     @Override
153     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
154     {
155         if ( buffer == null )
156         {
157             throw new EncoderException( I18n.err( I18n.ERR_148 ) );
158         }
159 
160         try
161         {
162             // The AD-AND-OR SEQ OF Tag
163             buffer.put( UniversalTag.SEQUENCE.getValue() );
164             buffer.put( TLV.getBytes( adAndOrSeqLength ) );
165 
166             // the condition-count
167             buffer.put( ( byte ) KerberosConstants.AD_AND_OR_CONDITION_COUNT_TAG );
168             buffer.put( ( byte ) conditionCountTagLength );
169             BerValue.encode( buffer, conditionCount );
170 
171             // the elements
172             buffer.put( ( byte ) KerberosConstants.AD_AND_OR_ELEMENTS_TAG );
173             buffer.put( ( byte ) elementsTagLength );
174 
175             elements.encode( buffer );
176         }
177         catch ( BufferOverflowException boe )
178         {
179             LOG.error( I18n.err( I18n.ERR_139, 1 + TLV.getNbBytes( adAndOrSeqLength )
180                 + adAndOrSeqLength, buffer.capacity() ) );
181             throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
182         }
183 
184         if ( IS_DEBUG )
185         {
186             LOG.debug( "AD-AND-OR encoding : {}", Strings.dumpBytes( buffer.array() ) );
187             LOG.debug( "AD-AND-OR initial value : {}", this );
188         }
189 
190         return buffer;
191     }
192 
193 
194     /**
195      * @see Object#toString()
196      */
197     public String toString()
198     {
199         return toString( "" );
200     }
201 
202 
203     /**
204      * @see Object#toString()
205      */
206     public String toString( String tabs )
207     {
208         StringBuilder sb = new StringBuilder();
209 
210         sb.append( tabs ).append( "AD-AND-OR : {\n" );
211         sb.append( tabs ).append( "    condition-count: " ).append( conditionCount ).append( '\n' );
212         sb.append( tabs + "    elements:" ).append( elements ).append( '\n' );
213         sb.append( tabs + "}\n" );
214 
215         return sb.toString();
216     }
217 }