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