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.checksum.ChecksumType;
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 public class Checksum extends AbstractAsn1Object
51 {
52
53 private static final Logger log = LoggerFactory.getLogger( Checksum.class );
54
55
56 private static final boolean IS_DEBUG = log.isDebugEnabled();
57
58
59 private ChecksumType cksumtype;
60
61
62 private byte[] checksum;
63
64
65 private transient int checksumTypeLength;
66 private transient int checksumBytesLength;
67 private transient int checksumLength;
68
69
70
71
72
73 public Checksum()
74 {
75 }
76
77
78
79
80
81
82
83
84 public Checksum( ChecksumType cksumtype, byte[] checksum )
85 {
86 this.cksumtype = cksumtype;
87 this.checksum = checksum;
88 }
89
90
91
92
93
94 public boolean equals( Object o )
95 {
96 if ( this == o )
97 {
98 return true;
99 }
100
101 if ( !( o instanceof Checksum ) )
102 {
103 return false;
104 }
105
106 Checksum that = ( Checksum ) o;
107
108 return ( cksumtype == that.cksumtype ) && ( Arrays.equals( checksum, that.checksum ) );
109 }
110
111
112
113
114
115
116
117 public byte[] getChecksumValue()
118 {
119 return checksum;
120 }
121
122
123
124
125
126
127
128 public void setChecksumValue( byte[] checksum )
129 {
130 this.checksum = checksum;
131 }
132
133
134
135
136
137
138
139 public ChecksumType getChecksumType()
140 {
141 return cksumtype;
142 }
143
144
145
146
147
148
149
150 public void setChecksumType( ChecksumType cksumType )
151 {
152 this.cksumtype = cksumType;
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 public int computeLength()
178 {
179
180 checksumTypeLength = 1 + 1 + Value.getNbBytes( cksumtype.getOrdinal() );
181 checksumLength = 1 + TLV.getNbBytes( checksumTypeLength ) + checksumTypeLength;
182
183
184 if ( checksum == null )
185 {
186 checksumBytesLength = 1 + 1;
187 }
188 else
189 {
190 checksumBytesLength = 1 + TLV.getNbBytes( checksum.length ) + checksum.length;
191 }
192
193 checksumLength += 1 + TLV.getNbBytes( checksumBytesLength ) + checksumBytesLength;
194
195
196 int checksumSeqLength = 1 + Value.getNbBytes( checksumLength ) + checksumLength;
197
198 return checksumSeqLength;
199
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
219 {
220 if ( buffer == null )
221 {
222 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
223 }
224
225 try
226 {
227
228 buffer.put( UniversalTag.SEQUENCE_TAG );
229 buffer.put( TLV.getBytes( checksumLength ) );
230
231
232 buffer.put( ( byte ) 0xA0 );
233 buffer.put( TLV.getBytes( checksumTypeLength ) );
234 Value.encode( buffer, cksumtype.getOrdinal() );
235
236
237 buffer.put( ( byte ) 0xA1 );
238 buffer.put( TLV.getBytes( checksumBytesLength ) );
239 Value.encode( buffer, checksum );
240 }
241 catch ( BufferOverflowException boe )
242 {
243 log.error( "Cannot encode the Checksum object, the PDU size is {} when only {} bytes has been allocated", 1
244 + TLV.getNbBytes( checksumLength ) + checksumLength, buffer.capacity() );
245 throw new EncoderException( "The PDU buffer size is too small !" );
246 }
247
248 if ( IS_DEBUG )
249 {
250 log.debug( "Checksum encoding : {}", StringTools.dumpBytes( buffer.array() ) );
251 log.debug( "Checksum initial value : {}", toString() );
252 }
253
254 return buffer;
255 }
256
257
258
259
260 public String toString()
261 {
262 return toString( "" );
263 }
264
265
266
267
268
269 public String toString( String tabs )
270 {
271 StringBuilder sb = new StringBuilder();
272
273 sb.append( tabs ).append( "Checksum : {\n" );
274 sb.append( tabs ).append( " cksumtype: " ).append( cksumtype ).append( '\n' );
275
276 if ( checksum != null )
277 {
278 sb.append( tabs + " checksum:" ).append( StringTools.dumpBytes( checksum ) ).append( '\n' );
279 }
280
281 sb.append( tabs + "}\n" );
282
283 return sb.toString();
284 }
285 }