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