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
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.apache.directory.shared.kerberos.codec.types.PaDataType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class PaData implements Asn1Object
51 {
52
53 private static final Logger log = LoggerFactory.getLogger( PaData.class );
54
55
56 private static final boolean IS_DEBUG = log.isDebugEnabled();
57
58
59 private PaDataType paDataType;
60
61
62 private byte[] paDataValue;
63
64
65 private int paDataTypeTagLength;
66 private int paDataValueTagLength;
67 private int preAuthenticationDataSeqLength;
68
69
70
71
72
73 public PaData()
74 {
75 }
76
77
78
79
80
81
82
83
84 public PaData( PaDataType paDataType, byte[] paDataValue )
85 {
86 this.paDataType = paDataType;
87 this.paDataValue = paDataValue;
88 }
89
90
91
92
93
94
95
96 public PaDataType getPaDataType()
97 {
98 return paDataType;
99 }
100
101
102
103
104
105
106
107 public void setPaDataType( int paDataType )
108 {
109 this.paDataType = PaDataType.getTypeByValue( paDataType );
110 }
111
112
113
114
115
116
117
118 public void setPaDataType( PaDataType paDataType )
119 {
120 this.paDataType = paDataType;
121 }
122
123
124
125
126
127
128
129 public byte[] getPaDataValue()
130 {
131 return paDataValue;
132 }
133
134
135
136
137
138
139
140 public void setPaDataValue( byte[] paDataValue )
141 {
142 this.paDataValue = paDataValue;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public int computeLength()
169 {
170
171 int paDataTypeLength = BerValue.getNbBytes( paDataType.getValue() );
172 paDataTypeTagLength = 1 + TLV.getNbBytes( paDataTypeLength ) + paDataTypeLength;
173 preAuthenticationDataSeqLength = 1 + TLV.getNbBytes( paDataTypeTagLength ) + paDataTypeTagLength;
174
175
176 if ( paDataValue == null )
177 {
178 paDataValueTagLength = 1 + 1;
179 }
180 else
181 {
182 paDataValueTagLength = 1 + TLV.getNbBytes( paDataValue.length ) + paDataValue.length;
183 }
184
185
186 preAuthenticationDataSeqLength += 1 + TLV.getNbBytes( paDataValueTagLength ) + paDataValueTagLength;
187
188 return 1 + TLV.getNbBytes( preAuthenticationDataSeqLength ) + preAuthenticationDataSeqLength;
189
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
209 {
210 if ( buffer == null )
211 {
212 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
213 }
214
215 try
216 {
217
218 buffer.put( UniversalTag.SEQUENCE.getValue() );
219 buffer.put( TLV.getBytes( preAuthenticationDataSeqLength ) );
220
221
222 buffer.put( ( byte ) KerberosConstants.PADATA_TYPE_TAG );
223 buffer.put( TLV.getBytes( paDataTypeTagLength ) );
224 BerValue.encode( buffer, paDataType.getValue() );
225
226
227 buffer.put( ( byte ) KerberosConstants.PADATA_VALUE_TAG );
228 buffer.put( TLV.getBytes( paDataValueTagLength ) );
229 BerValue.encode( buffer, paDataValue );
230 }
231 catch ( BufferOverflowException boe )
232 {
233 log.error( I18n.err( I18n.ERR_145, 1 + TLV.getNbBytes( preAuthenticationDataSeqLength )
234 + preAuthenticationDataSeqLength, buffer.capacity() ) );
235 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
236 }
237
238 if ( IS_DEBUG )
239 {
240 log.debug( "PreAuthenticationData encoding : {}", Strings.dumpBytes( buffer.array() ) );
241 log.debug( "PreAuthenticationData initial value : {}", this );
242 }
243
244 return buffer;
245 }
246
247
248
249
250
251 public String toString()
252 {
253 return toString( "" );
254 }
255
256
257
258
259
260 public String toString( String tabs )
261 {
262 StringBuilder sb = new StringBuilder();
263
264 sb.append( tabs ).append( "PreAuthenticationData : \n" );
265 sb.append( tabs ).append( " padata-type: " ).append( paDataType ).append( '\n' );
266
267 if ( paDataValue != null )
268 {
269 sb.append( tabs + " padata-value:" ).append( Strings.dumpBytes( paDataValue ) ).append( '\n' );
270 }
271
272 return sb.toString();
273 }
274 }