View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * Change password data structure
37   * 
38   * ChangePasswdData ::=  SEQUENCE {
39   *       newpasswd[0]   OCTET STRING,
40   *       targname[1]    PrincipalName OPTIONAL,
41   *       targrealm[2]   Realm OPTIONAL
42   *     }
43   *     
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class ChangePasswdData implements Asn1Object
47  {
48  
49      /** the new password */
50      private byte[] newPasswd;
51  
52      /** principal name of the client */
53      private PrincipalName targName;
54  
55      /** name of client's realm */
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       * Compute the ChangePasswdData length
71       * <pre>
72       * ChangePasswdData :
73       *
74       * 0x30 L1 ChangePasswdData sequence
75       *  |
76       *  +--&gt; 0xA0 L2 newPasswd tag
77       *  |     |
78       *  |     +--&gt; 0x04 L2-1 newPasswd (Octet string)
79       *  |
80       *  +--&gt; 0xA1 L3 targName tag
81       *  |     |
82       *  |     +--&gt; 0x30 L3-1 targName (PrincipalName)
83       *  |
84       *  +--&gt; 0xA2 L4 targRealm tag
85       *        |
86       *        +--&gt; 0x1B L4-1 targRealm (KerberosString)
87       * </pre>
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         // ChangePasswdData
122         buffer.put( UniversalTag.SEQUENCE.getValue() );
123         buffer.put( BerValue.getBytes( seqLen ) );
124 
125         // newpasswd
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      * @see Object#toString()
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 }