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.security.MessageDigest;
26 import java.util.Arrays;
27
28 import org.apache.directory.api.asn1.Asn1Object;
29 import org.apache.directory.api.asn1.EncoderException;
30 import org.apache.directory.api.asn1.ber.tlv.BerValue;
31 import org.apache.directory.api.asn1.ber.tlv.TLV;
32 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
33 import org.apache.directory.api.util.Strings;
34 import org.apache.directory.server.i18n.I18n;
35 import org.apache.directory.shared.kerberos.KerberosConstants;
36 import org.apache.directory.shared.kerberos.codec.types.EncryptionType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class EncryptionKey implements Asn1Object
55 {
56
57 private static final Logger log = LoggerFactory.getLogger( EncryptionKey.class );
58
59
60 private static final boolean IS_DEBUG = log.isDebugEnabled();
61
62
63 private EncryptionType keyType;
64
65
66 private byte[] keyValue;
67
68
69 private int keyVersion;
70
71
72 private int keyTypeLength;
73 private int keyValueLength;
74 private int encryptionKeyLength;
75
76
77
78
79
80 public EncryptionKey()
81 {
82 }
83
84
85
86
87
88
89
90
91 public EncryptionKey( EncryptionType keyType, byte[] keyValue )
92 {
93 this.keyType = keyType;
94 this.keyValue = keyValue;
95 }
96
97
98
99
100
101
102
103
104
105 public EncryptionKey( EncryptionType keyType, byte[] keyValue, int keyVersion )
106 {
107 this.keyType = keyType;
108 this.keyValue = keyValue;
109 this.keyVersion = keyVersion;
110 }
111
112
113
114
115
116 public synchronized void destroy()
117 {
118 if ( keyValue != null )
119 {
120 Arrays.fill( keyValue, ( byte ) 0x00 );
121 }
122 }
123
124
125
126
127
128
129
130 public EncryptionType getKeyType()
131 {
132 return keyType;
133 }
134
135
136
137
138
139
140 public void setKeyType( EncryptionType keyType )
141 {
142 this.keyType = keyType;
143 }
144
145
146
147
148
149
150
151 public byte[] getKeyValue()
152 {
153 return keyValue;
154 }
155
156
157
158
159
160
161
162 public int getKeyVersion()
163 {
164 return keyVersion;
165 }
166
167
168
169
170
171
172 public void setKeyVersion( int keyVersion )
173 {
174 this.keyVersion = keyVersion;
175 }
176
177
178
179
180
181
182 public void setKeyValue( byte[] keyValue )
183 {
184 this.keyValue = keyValue;
185 }
186
187
188
189
190
191 @Override
192 public int hashCode()
193 {
194 int hash = 37;
195 hash = hash * 17 + keyType.hashCode();
196 hash = hash * 17 + Arrays.hashCode( keyValue );
197
198 return hash;
199 }
200
201
202
203
204
205 @Override
206 public boolean equals( Object o )
207 {
208 if ( this == o )
209 {
210 return true;
211 }
212
213 if ( !( o instanceof EncryptionKey ) )
214 {
215 return false;
216 }
217
218 EncryptionKey../../../../org/apache/directory/shared/kerberos/components/EncryptionKey.html#EncryptionKey">EncryptionKey that = ( EncryptionKey ) o;
219 return ( this.keyType == that.keyType ) && ( MessageDigest.isEqual( this.keyValue, that.keyValue ) );
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 public int computeLength()
246 {
247
248 keyTypeLength = 1 + 1 + BerValue.getNbBytes( keyType.getValue() );
249 encryptionKeyLength = 1 + TLV.getNbBytes( keyTypeLength ) + keyTypeLength;
250
251
252 if ( keyValue == null )
253 {
254 keyValueLength = 1 + 1;
255 }
256 else
257 {
258 keyValueLength = 1 + TLV.getNbBytes( keyValue.length ) + keyValue.length;
259 }
260
261 encryptionKeyLength += 1 + TLV.getNbBytes( keyValueLength ) + keyValueLength;
262
263
264 return 1 + BerValue.getNbBytes( encryptionKeyLength ) + encryptionKeyLength;
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
284 {
285 if ( buffer == null )
286 {
287 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
288 }
289
290 try
291 {
292
293 buffer.put( UniversalTag.SEQUENCE.getValue() );
294 buffer.put( TLV.getBytes( encryptionKeyLength ) );
295
296
297 buffer.put( ( byte ) KerberosConstants.ENCRYPTION_KEY_TYPE_TAG );
298 buffer.put( TLV.getBytes( keyTypeLength ) );
299 BerValue.encode( buffer, keyType.getValue() );
300
301
302 buffer.put( ( byte ) KerberosConstants.ENCRYPTION_KEY_VALUE_TAG );
303 buffer.put( TLV.getBytes( keyValueLength ) );
304 BerValue.encode( buffer, keyValue );
305 }
306 catch ( BufferOverflowException boe )
307 {
308 log.error( I18n.err( I18n.ERR_142, 1 + TLV.getNbBytes( encryptionKeyLength )
309 + encryptionKeyLength, buffer.capacity() ) );
310 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
311 }
312
313 if ( IS_DEBUG )
314 {
315 log.debug( "EncryptionKey encoding : {}", Strings.dumpBytes( buffer.array() ) );
316 log.debug( "EncryptionKey initial value : {}", this );
317 }
318
319 return buffer;
320 }
321
322
323
324
325
326 public String toString()
327 {
328 return keyType.toString() + " (" + keyType.getValue() + ")";
329 }
330 }