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 public class ETypeInfoEntry implements Asn1Object
52 {
53
54 private static final Logger LOG = LoggerFactory.getLogger( ETypeInfoEntry.class );
55
56
57 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58
59
60 private EncryptionType etype;
61
62
63 private byte[] salt;
64
65
66 private int etypeTagLength;
67 private int saltTagLength;
68 private int etypeInfoEntrySeqLength;
69
70
71
72
73
74
75
76
77 public ETypeInfoEntry( EncryptionType etype, byte[] salt )
78 {
79 this.etype = etype;
80 this.salt = salt;
81 }
82
83
84
85
86
87 public ETypeInfoEntry()
88 {
89 }
90
91
92
93
94
95
96
97 public byte[] getSalt()
98 {
99 return salt;
100 }
101
102
103
104
105
106 public void setSalt( byte[] salt )
107 {
108 this.salt = salt;
109 }
110
111
112
113
114
115
116
117 public EncryptionType getEType()
118 {
119 return etype;
120 }
121
122
123
124
125
126 public void setEType( EncryptionType etype )
127 {
128 this.etype = etype;
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public int computeLength()
155 {
156
157 int etypeLength = BerValue.getNbBytes( etype.getValue() );
158 etypeTagLength = 1 + TLV.getNbBytes( etypeLength ) + etypeLength;
159 etypeInfoEntrySeqLength = 1 + TLV.getNbBytes( etypeTagLength ) + etypeTagLength;
160
161
162 if ( salt != null )
163 {
164 saltTagLength = 1 + TLV.getNbBytes( salt.length ) + salt.length;
165 etypeInfoEntrySeqLength += 1 + TLV.getNbBytes( saltTagLength ) + saltTagLength;
166 }
167
168 return 1 + TLV.getNbBytes( etypeInfoEntrySeqLength ) + etypeInfoEntrySeqLength;
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
188 {
189 if ( buffer == null )
190 {
191 throw new EncoderException( I18n.err( I18n.ERR_148 ) );
192 }
193
194 try
195 {
196
197 buffer.put( UniversalTag.SEQUENCE.getValue() );
198 buffer.put( TLV.getBytes( etypeInfoEntrySeqLength ) );
199
200
201 buffer.put( ( byte ) KerberosConstants.ETYPE_INFO_ENTRY_ETYPE_TAG );
202 buffer.put( TLV.getBytes( etypeTagLength ) );
203 BerValue.encode( buffer, etype.getValue() );
204
205
206 if ( salt != null )
207 {
208 buffer.put( ( byte ) KerberosConstants.ETYPE_INFO_ENTRY_SALT_TAG );
209 buffer.put( TLV.getBytes( saltTagLength ) );
210 BerValue.encode( buffer, salt );
211 }
212 }
213 catch ( BufferOverflowException boe )
214 {
215 LOG.error( I18n.err( I18n.ERR_145, 1 + TLV.getNbBytes( etypeInfoEntrySeqLength )
216 + etypeInfoEntrySeqLength, buffer.capacity() ) );
217 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
218 }
219
220 if ( IS_DEBUG )
221 {
222 LOG.debug( "ETYPE-INFO-ENTRY encoding : {}", Strings.dumpBytes( buffer.array() ) );
223 LOG.debug( "ETYPE-INFO-ENTRY initial value : {}", this );
224 }
225
226 return buffer;
227 }
228
229
230
231
232
233 public String toString()
234 {
235 StringBuilder sb = new StringBuilder();
236
237 sb.append( "ETYPE-INFO-ENTRY : {\n" );
238 sb.append( " etype: " ).append( etype ).append( '\n' );
239
240 if ( salt != null )
241 {
242 sb.append( " salt: " ).append( Strings.dumpBytes( salt ) ).append( '\n' );
243 }
244
245 sb.append( "}\n" );
246
247 return sb.toString();
248 }
249 }