1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.shared.kerberos.components;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25 import java.util.Arrays;
26
27 import org.apache.directory.api.asn1.AbstractAsn1Object;
28 import org.apache.directory.api.asn1.EncoderException;
29 import org.apache.directory.api.asn1.ber.tlv.BerValue;
30 import org.apache.directory.api.asn1.ber.tlv.TLV;
31 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
32 import org.apache.directory.api.util.Strings;
33 import org.apache.directory.server.i18n.I18n;
34 import org.apache.directory.shared.kerberos.KerberosConstants;
35 import org.apache.directory.shared.kerberos.codec.types.TransitedEncodingType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class TransitedEncoding extends AbstractAsn1Object
54 {
55
56 private static final Logger log = LoggerFactory.getLogger( TransitedEncoding.class );
57
58
59 private static final boolean IS_DEBUG = log.isDebugEnabled();
60
61
62
63
64
65
66 private TransitedEncodingType trType;
67
68
69 private byte[] contents;
70
71
72 private int trTypeLength;
73 private int contentsLength;
74 private int transitedEncodingLength;
75
76
77
78
79
80 public TransitedEncoding()
81 {
82 trType = TransitedEncodingType.NULL;
83 contents = Strings.EMPTY_BYTES;
84 }
85
86
87
88
89
90
91
92 public byte[] getContents()
93 {
94 return contents;
95 }
96
97
98
99
100
101
102 public void setContents( byte[] contents )
103 {
104 this.contents = contents;
105 }
106
107
108
109
110
111
112
113 public TransitedEncodingType getTrType()
114 {
115 return trType;
116 }
117
118
119
120
121
122
123 public void setTrType( TransitedEncodingType trType )
124 {
125 this.trType = trType;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public int computeLength()
153 {
154
155 trTypeLength = 1 + 1 + BerValue.getNbBytes( trType.getValue() );
156 transitedEncodingLength = 1 + TLV.getNbBytes( trTypeLength ) + trTypeLength;
157
158
159 if ( contents == null )
160 {
161 contentsLength = 1 + 1;
162 }
163 else
164 {
165 contentsLength = 1 + TLV.getNbBytes( contents.length ) + contents.length;
166 }
167
168 transitedEncodingLength += 1 + TLV.getNbBytes( contentsLength ) + contentsLength;
169
170
171 int transitedEncodingSeqLength = 1 + BerValue.getNbBytes( transitedEncodingLength ) + transitedEncodingLength;
172
173 return transitedEncodingSeqLength;
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
193 {
194 if ( buffer == null )
195 {
196 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
197 }
198
199 try
200 {
201
202 buffer.put( UniversalTag.SEQUENCE.getValue() );
203 buffer.put( TLV.getBytes( transitedEncodingLength ) );
204
205
206 buffer.put( ( byte ) KerberosConstants.TRANSITED_ENCODING_TR_TYPE_TAG );
207 buffer.put( TLV.getBytes( trTypeLength ) );
208 BerValue.encode( buffer, trType.getValue() );
209
210
211 buffer.put( ( byte ) KerberosConstants.TRANSITED_ENCODING_CONTENTS_TAG );
212 buffer.put( TLV.getBytes( contentsLength ) );
213 BerValue.encode( buffer, contents );
214 }
215 catch ( BufferOverflowException boe )
216 {
217 log.error( I18n.err( I18n.ERR_147, 1 + TLV.getNbBytes( transitedEncodingLength )
218 + transitedEncodingLength, buffer.capacity() ) );
219 throw new EncoderException( I18n.err( I18n.ERR_138 ) );
220 }
221
222 if ( IS_DEBUG )
223 {
224 log.debug( "TransitedEncoding encoding : {}", Strings.dumpBytes( buffer.array() ) );
225 log.debug( "TransitedEncoding initial value : {}", toString() );
226 }
227
228 return buffer;
229 }
230
231
232
233
234
235 @Override
236 public int hashCode()
237 {
238 final int prime = 31;
239 int result = 1;
240 result = prime * result + Arrays.hashCode( contents );
241 result = prime * result + ( ( trType == null ) ? 0 : trType.hashCode() );
242 return result;
243 }
244
245
246
247
248
249 @Override
250 public boolean equals( Object obj )
251 {
252 if ( this == obj )
253 {
254 return true;
255 }
256
257 if ( obj == null )
258 {
259 return false;
260 }
261
262 TransitedEncoding other = ( TransitedEncoding ) obj;
263
264 if ( !Arrays.equals( contents, other.contents ) )
265 {
266 return false;
267 }
268
269 if ( trType != other.trType )
270 {
271 return false;
272 }
273
274 return true;
275 }
276
277
278
279
280
281 public String toString()
282 {
283 StringBuilder sb = new StringBuilder();
284
285 sb.append( "TransitedEncoding : {\n" );
286 sb.append( " tr-type: " ).append( trType ).append( '\n' );
287
288 sb.append( " contents: " ).append( Strings.dumpBytes( contents ) ).append( "\n}\n" );
289
290 return sb.toString();
291 }
292 }