1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.messages.value;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.directory.server.kerberos.shared.messages.value.types.PaDataType;
27 import org.apache.directory.shared.asn1.AbstractAsn1Object;
28 import org.apache.directory.shared.asn1.ber.tlv.TLV;
29 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.shared.asn1.ber.tlv.Value;
31 import org.apache.directory.shared.asn1.codec.EncoderException;
32 import org.apache.directory.shared.ldap.util.StringTools;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class PaData extends AbstractAsn1Object
50 {
51
52 private static final Logger log = LoggerFactory.getLogger( PaData.class );
53
54
55 private static final boolean IS_DEBUG = log.isDebugEnabled();
56
57
58 private PaDataType paDataType;
59
60
61 private byte[] paDataValue;
62
63
64 private transient int paDataTypeTagLength;
65 private transient int paDataValueTagLength;
66 private transient int preAuthenticationDataSeqLength;
67
68
69
70
71
72 public PaData()
73 {
74 }
75
76
77
78
79
80
81
82
83 public PaData( PaDataType paDataType, byte[] paDataValue )
84 {
85 this.paDataType = paDataType;
86 this.paDataValue = paDataValue;
87 }
88
89
90
91
92
93
94
95 public PaDataType getPaDataType()
96 {
97 return paDataType;
98 }
99
100
101
102
103
104
105
106 public void setPaDataType( int paDataType )
107 {
108 this.paDataType = PaDataType.getTypeByOrdinal( paDataType );
109 }
110
111
112
113
114
115
116
117 public void setPaDataType( PaDataType paDataType )
118 {
119 this.paDataType = paDataType;
120 }
121
122
123
124
125
126
127
128 public byte[] getPaDataValue()
129 {
130 return paDataValue;
131 }
132
133
134
135
136
137
138
139 public void setPaDataValue( byte[] paDataValue )
140 {
141 this.paDataValue = paDataValue;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public int computeLength()
167 {
168
169 int paDataTypeLength = Value.getNbBytes( paDataType.getOrdinal() );
170 paDataTypeTagLength = 1 + TLV.getNbBytes( paDataTypeLength ) + paDataTypeLength;
171 preAuthenticationDataSeqLength = 1 + TLV.getNbBytes( paDataTypeTagLength ) + paDataTypeTagLength;
172
173
174 if ( paDataValue == null )
175 {
176 paDataValueTagLength = 1 + 1;
177 }
178 else
179 {
180 paDataValueTagLength = 1 + TLV.getNbBytes( paDataValue.length ) + paDataValue.length;
181 }
182
183
184 preAuthenticationDataSeqLength += 1 + TLV.getNbBytes( paDataValueTagLength ) + paDataValueTagLength;
185
186 return 1 + TLV.getNbBytes( preAuthenticationDataSeqLength ) + preAuthenticationDataSeqLength;
187
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
207 {
208 if ( buffer == null )
209 {
210 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
211 }
212
213 try
214 {
215
216 buffer.put( UniversalTag.SEQUENCE_TAG );
217 buffer.put( TLV.getBytes( preAuthenticationDataSeqLength ) );
218
219
220 buffer.put( ( byte ) 0xA1 );
221 buffer.put( TLV.getBytes( paDataTypeTagLength ) );
222 Value.encode( buffer, paDataType.getOrdinal() );
223
224
225 buffer.put( ( byte ) 0xA2 );
226 buffer.put( TLV.getBytes( paDataValueTagLength ) );
227 Value.encode( buffer, paDataValue );
228 }
229 catch ( BufferOverflowException boe )
230 {
231 log.error( "Cannot encode the PreAuthenticationData object, the PDU size is {} when only {} bytes has been allocated", 1
232 + TLV.getNbBytes( preAuthenticationDataSeqLength ) + preAuthenticationDataSeqLength, buffer.capacity() );
233 throw new EncoderException( "The PDU buffer size is too small !" );
234 }
235
236 if ( IS_DEBUG )
237 {
238 log.debug( "PreAuthenticationData encoding : {}", StringTools.dumpBytes( buffer.array() ) );
239 log.debug( "PreAuthenticationData initial value : {}", toString() );
240 }
241
242 return buffer;
243 }
244
245
246
247
248 public String toString()
249 {
250 return toString( "" );
251 }
252
253
254
255
256
257 public String toString( String tabs )
258 {
259 StringBuilder sb = new StringBuilder();
260
261 sb.append( tabs ).append( "PreAuthenticationData : {\n" );
262 sb.append( tabs ).append( " padata-type: " ).append( paDataType ).append( '\n' );
263
264 if ( paDataValue != null )
265 {
266 sb.append( tabs + " padata-value:" ).append( StringTools.dumpBytes( paDataValue ) ).append( '\n' );
267 }
268
269 sb.append( tabs + "}\n" );
270
271 return sb.toString();
272 }
273 }