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.AbstractAsn1Object;
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 extends AbstractAsn1Object
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 @Override
89 public int computeLength()
90 {
91 newPasswdLen = 1 + TLV.getNbBytes( newPasswd.length ) + newPasswd.length;
92
93 seqLen = 1 + TLV.getNbBytes( newPasswdLen ) + newPasswdLen;
94
95 if ( targName != null )
96 {
97 targNameLen = targName.computeLength();
98 seqLen += 1 + TLV.getNbBytes( targNameLen ) + targNameLen;
99 }
100
101 if ( targRealm != null )
102 {
103 targRealmLen = Strings.getBytesUtf8( targRealm ).length;
104 targRealmLen = 1 + TLV.getNbBytes( targRealmLen ) + targRealmLen;
105 seqLen += 1 + TLV.getNbBytes( targRealmLen ) + targRealmLen;
106 }
107
108 return 1 + TLV.getNbBytes( seqLen ) + seqLen;
109 }
110
111
112 @Override
113 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
114 {
115 if ( buffer == null )
116 {
117 buffer = ByteBuffer.allocate( computeLength() );
118 }
119
120
121 buffer.put( UniversalTag.SEQUENCE.getValue() );
122 buffer.put( BerValue.getBytes( seqLen ) );
123
124
125 buffer.put( ( byte ) KerberosConstants.CHNGPWD_NEWPWD_TAG );
126 buffer.put( BerValue.getBytes( newPasswdLen ) );
127 BerValue.encode( buffer, newPasswd );
128
129 if ( targName != null )
130 {
131 buffer.put( ( byte ) KerberosConstants.CHNGPWD_TARGNAME_TAG );
132 buffer.put( BerValue.getBytes( targNameLen ) );
133
134 targName.encode( buffer );
135 }
136
137 if ( targRealm != null )
138 {
139 buffer.put( ( byte ) KerberosConstants.CHNGPWD_TARGREALM_TAG );
140 buffer.put( BerValue.getBytes( targRealmLen ) );
141 buffer.put( UniversalTag.GENERAL_STRING.getValue() );
142 buffer.put( BerValue.getBytes( targRealmLen - 2 ) );
143 buffer.put( Strings.getBytesUtf8( targRealm ) );
144 }
145
146 return buffer;
147 }
148
149
150 public byte[] getNewPasswd()
151 {
152 return newPasswd;
153 }
154
155
156 public void setNewPasswd( byte[] newPasswd )
157 {
158 this.newPasswd = newPasswd;
159 }
160
161
162 public PrincipalName getTargName()
163 {
164 return targName;
165 }
166
167
168 public void setTargName( PrincipalName targName )
169 {
170 this.targName = targName;
171 }
172
173
174 public String getTargRealm()
175 {
176 return targRealm;
177 }
178
179
180 public void setTargRealm( String targRealm )
181 {
182 this.targRealm = targRealm;
183 }
184
185
186
187
188
189 public String toString()
190 {
191 StringBuilder sb = new StringBuilder();
192
193 sb.append( "ChangePasswdData : \n" );
194
195 sb.append( " newPasswd : " ).append( newPasswd ).append( '\n' );
196 sb.append( " targName : " ).append( targName ).append( '\n' );
197 sb.append( " targRealm : " ).append( targRealm ).append( '\n' );
198
199 return sb.toString();
200 }
201 }