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.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   * 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 extends AbstractAsn1Object
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       *  +--> 0xA0 L2 newPasswd tag
77       *  |     |
78       *  |     +--> 0x04 L2-1 newPasswd (Octet string)
79       *  |
80       *  +--> 0xA1 L3 targName tag
81       *  |     |
82       *  |     +--> 0x30 L3-1 targName (PrincipalName)
83       *  |
84       *  +--> 0xA2 L4 targRealm tag
85       *        |
86       *        +--> 0x1B L4-1 targRealm (KerberosString)
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         // ChangePasswdData
121         buffer.put( UniversalTag.SEQUENCE.getValue() );
122         buffer.put( BerValue.getBytes( seqLen ) );
123 
124         // newpasswd
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      * @see Object#toString()
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 }