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.server.kerberos.changepwd.messages;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.EncoderException;
26  import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswdErrorType;
27  import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswordException;
28  import org.apache.directory.shared.kerberos.codec.KerberosDecoder;
29  import org.apache.directory.shared.kerberos.exceptions.KerberosException;
30  import org.apache.directory.shared.kerberos.messages.ApReq;
31  import org.apache.directory.shared.kerberos.messages.KrbPriv;
32  
33  
34  /**
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class ChangePasswordRequest extends AbstractPasswordMessage
38  {
39      private ApReq authHeader;
40      private KrbPriv privateMessage;
41  
42      private short authHeaderLen;
43      private short privateMessageLen;
44      private short messageLength;
45  
46  
47      public ChangePasswordRequest( ApReq authHeader, KrbPriv privateMessage )
48      {
49          this( PVNO, authHeader, privateMessage );
50      }
51  
52  
53      /**
54       * Creates a new instance of ChangePasswordRequest.
55       *
56       * @param versionNumber The version number
57       * @param authHeader The authentication header
58       * @param privateMessage The private part
59       */
60      public ChangePasswordRequest( short versionNumber, ApReq authHeader, KrbPriv privateMessage )
61      {
62          super( versionNumber );
63  
64          this.authHeader = authHeader;
65          this.privateMessage = privateMessage;
66      }
67  
68  
69      /**
70       * Returns the {@link ApReq}.
71       *
72       * @return The {@link ApReq}.
73       */
74      public ApReq getAuthHeader()
75      {
76          return authHeader;
77      }
78  
79  
80      /**
81       * Returns the {@link KrbPriv}.
82       *
83       * @return The {@link KrbPriv}.
84       */
85      public KrbPriv getPrivateMessage()
86      {
87          return privateMessage;
88      }
89  
90  
91      @Override
92      public short computeLength()
93      {
94          authHeaderLen = ( short ) authHeader.computeLength();
95          privateMessageLen = ( short ) privateMessage.computeLength();
96  
97          messageLength = ( short ) ( HEADER_LENGTH + authHeaderLen + privateMessageLen );
98  
99          return messageLength;
100     }
101 
102 
103     @Override
104     public ByteBuffer encode( ByteBuffer buf ) throws EncoderException
105     {
106         buf.putShort( messageLength );
107         buf.putShort( getVersionNumber() );
108 
109         // Build application request bytes
110         buf.putShort( authHeaderLen );
111         authHeader.encode( buf );
112 
113         privateMessage.encode( buf );
114 
115         return buf;
116     }
117 
118 
119     /**
120      * Decodes a {@link ByteBuffer} into a {@link ChangePasswordRequest}.
121      *
122      * @param buf
123      * @return The {@link ChangePasswordRequest}.
124      * @throws ChangePasswordException If the decoding failed
125      */
126     public static ChangePasswordRequest decode( ByteBuffer buf ) throws ChangePasswordException
127     {
128         try
129         {
130             buf.getShort(); // message length
131 
132             short pvno = buf.getShort();
133 
134             short authHeaderLength = buf.getShort();
135 
136             byte[] undecodedAuthHeader = new byte[authHeaderLength];
137             buf.get( undecodedAuthHeader, 0, authHeaderLength );
138 
139             ApReq authHeader = KerberosDecoder.decodeApReq( undecodedAuthHeader );
140 
141             byte[] encodedPrivate = new byte[buf.remaining()];
142             buf.get( encodedPrivate, 0, buf.remaining() );
143 
144             KrbPriv privMessage = KerberosDecoder.decodeKrbPriv( encodedPrivate );
145 
146             return new ChangePasswordRequest( pvno, authHeader, privMessage );
147         }
148         catch ( KerberosException e )
149         {
150             throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_MALFORMED, e );
151         }
152     }
153 
154 }