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.KerberosTime;
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 PaEncTsEnc implements Asn1Object
52 {
53
54 private static final Logger log = LoggerFactory.getLogger( PaEncTsEnc.class );
55
56
57 private static final boolean IS_DEBUG = log.isDebugEnabled();
58
59
60 private KerberosTime patimestamp;
61
62
63 private Integer pausec;
64
65
66 private int paTimestampLength;
67 private int paUsecLength;
68 private int paEncTsEncLength;
69
70
71
72
73
74 public PaEncTsEnc()
75 {
76 }
77
78
79
80
81
82 public PaEncTsEnc( KerberosTime paTimestamp, int pausec )
83 {
84 this.patimestamp = paTimestamp;
85 this.pausec = pausec;
86 }
87
88
89
90
91
92
93
94 public KerberosTime getPaTimestamp()
95 {
96 return patimestamp;
97 }
98
99
100
101
102
103
104
105 public void setPaTimestamp( KerberosTime patimestamp )
106 {
107 this.patimestamp = patimestamp;
108 }
109
110
111
112
113
114 public int getPausec()
115 {
116 if ( pausec == null )
117 {
118 return -1;
119 }
120
121 return pausec;
122 }
123
124
125
126
127
128 public void setPausec( int pausec )
129 {
130 this.pausec = pausec;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public int computeLength()
152 {
153
154 paTimestampLength = 0x11;
155
156 paEncTsEncLength = 1 + TLV.getNbBytes( paTimestampLength ) + paTimestampLength;
157
158
159 if ( pausec != null )
160 {
161 int pausecLength = BerValue.getNbBytes( pausec );
162 paUsecLength = 1 + TLV.getNbBytes( pausecLength ) + pausecLength;
163 paEncTsEncLength += 1 + TLV.getNbBytes( paUsecLength ) + paUsecLength;
164 }
165
166
167 return 1 + TLV.getNbBytes( paEncTsEncLength ) + paEncTsEncLength;
168 }
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( paEncTsEncLength ) );
199
200
201 buffer.put( ( byte ) KerberosConstants.PA_ENC_TS_ENC_PA_TIMESTAMP_TAG );
202 buffer.put( ( byte ) 0x11 );
203
204 buffer.put( UniversalTag.GENERALIZED_TIME.getValue() );
205 buffer.put( ( byte ) 0x0F );
206 buffer.put( patimestamp.getBytes() );
207
208
209 if ( pausec != null )
210 {
211 buffer.put( ( byte ) KerberosConstants.PA_ENC_TS_ENC_PA_USEC_TAG );
212 buffer.put( TLV.getBytes( paUsecLength ) );
213 BerValue.encode( buffer, pausec );
214 }
215 }
216 catch ( BufferOverflowException boe )
217 {
218 log.error( I18n.err( I18n.ERR_140, 1 + TLV.getNbBytes( paEncTsEncLength ) + paEncTsEncLength,
219 buffer.capacity() ) );
220 throw new EncoderException( I18n.err( I18n.ERR_138 ), boe );
221 }
222
223 if ( IS_DEBUG )
224 {
225 log.debug( "Checksum encoding : {}", Strings.dumpBytes( buffer.array() ) );
226 log.debug( "Checksum initial value : {}", this );
227 }
228
229 return buffer;
230 }
231
232
233
234
235
236 public String toString()
237 {
238 return toString( "" );
239 }
240
241
242
243
244
245 public String toString( String tabs )
246 {
247 StringBuilder sb = new StringBuilder();
248
249 sb.append( tabs ).append( "PA-ENC-TS-ENC : {\n" );
250 sb.append( tabs ).append( " patimestamp : " ).append( patimestamp ).append( '\n' );
251
252 if ( pausec != null )
253 {
254 sb.append( tabs + " pausec :" ).append( pausec ).append( '\n' );
255 }
256
257 sb.append( tabs + "}\n" );
258
259 return sb.toString();
260 }
261 }