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.messages;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.Asn1Object;
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.util.Strings;
31 import org.apache.directory.shared.kerberos.KerberosConstants;
32 import org.apache.directory.shared.kerberos.components.PrincipalName;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class ChangePasswdData implements Asn1Object
47 {
48
49
50 private byte[] newPasswd;
51
52
53 private PrincipalName targName;
54
55
56 private String targRealm;
57
58 private int newPasswdLen;
59 private int targNameLen;
60 private int targRealmLen;
61 private int seqLen;
62
63
64 public ChangePasswdData()
65 {
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 @Override
90 public int computeLength()
91 {
92 newPasswdLen = 1 + TLV.getNbBytes( newPasswd.length ) + newPasswd.length;
93
94 seqLen = 1 + TLV.getNbBytes( newPasswdLen ) + newPasswdLen;
95
96 if ( targName != null )
97 {
98 targNameLen = targName.computeLength();
99 seqLen += 1 + TLV.getNbBytes( targNameLen ) + targNameLen;
100 }
101
102 if ( targRealm != null )
103 {
104 targRealmLen = Strings.getBytesUtf8( targRealm ).length;
105 targRealmLen = 1 + TLV.getNbBytes( targRealmLen ) + targRealmLen;
106 seqLen += 1 + TLV.getNbBytes( targRealmLen ) + targRealmLen;
107 }
108
109 return 1 + TLV.getNbBytes( seqLen ) + seqLen;
110 }
111
112
113 @Override
114 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
115 {
116 if ( buffer == null )
117 {
118 buffer = ByteBuffer.allocate( computeLength() );
119 }
120
121
122 buffer.put( UniversalTag.SEQUENCE.getValue() );
123 buffer.put( BerValue.getBytes( seqLen ) );
124
125
126 buffer.put( ( byte ) KerberosConstants.CHNGPWD_NEWPWD_TAG );
127 buffer.put( BerValue.getBytes( newPasswdLen ) );
128 BerValue.encode( buffer, newPasswd );
129
130 if ( targName != null )
131 {
132 buffer.put( ( byte ) KerberosConstants.CHNGPWD_TARGNAME_TAG );
133 buffer.put( BerValue.getBytes( targNameLen ) );
134
135 targName.encode( buffer );
136 }
137
138 if ( targRealm != null )
139 {
140 buffer.put( ( byte ) KerberosConstants.CHNGPWD_TARGREALM_TAG );
141 buffer.put( BerValue.getBytes( targRealmLen ) );
142 buffer.put( UniversalTag.GENERAL_STRING.getValue() );
143 buffer.put( BerValue.getBytes( targRealmLen - 2 ) );
144 buffer.put( Strings.getBytesUtf8( targRealm ) );
145 }
146
147 return buffer;
148 }
149
150
151 public byte[] getNewPasswd()
152 {
153 return newPasswd;
154 }
155
156
157 public void setNewPasswd( byte[] newPasswd )
158 {
159 this.newPasswd = newPasswd;
160 }
161
162
163 public PrincipalName getTargName()
164 {
165 return targName;
166 }
167
168
169 public void setTargName( PrincipalName targName )
170 {
171 this.targName = targName;
172 }
173
174
175 public String getTargRealm()
176 {
177 return targRealm;
178 }
179
180
181 public void setTargRealm( String targRealm )
182 {
183 this.targRealm = targRealm;
184 }
185
186
187
188
189
190 public String toString()
191 {
192 StringBuilder sb = new StringBuilder();
193
194 sb.append( "ChangePasswdData : \n" );
195
196 sb.append( " newPasswd : " ).append( Strings.utf8ToString( newPasswd ) ).append( '\n' );
197 sb.append( " targName : " ).append( targName ).append( '\n' );
198 sb.append( " targRealm : " ).append( targRealm ).append( '\n' );
199
200 return sb.toString();
201 }
202 }