001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.server.kerberos.changepwd.messages; 021 022 023import java.nio.ByteBuffer; 024 025import org.apache.directory.api.asn1.EncoderException; 026import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswdErrorType; 027import org.apache.directory.server.kerberos.changepwd.exceptions.ChangePasswordException; 028import org.apache.directory.shared.kerberos.codec.KerberosDecoder; 029import org.apache.directory.shared.kerberos.exceptions.KerberosException; 030import org.apache.directory.shared.kerberos.messages.ApReq; 031import org.apache.directory.shared.kerberos.messages.KrbPriv; 032 033 034/** 035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 036 */ 037public class ChangePasswordRequest extends AbstractPasswordMessage 038{ 039 private ApReq authHeader; 040 private KrbPriv privateMessage; 041 042 private short authHeaderLen; 043 private short privateMessageLen; 044 private short messageLength; 045 046 047 public ChangePasswordRequest( ApReq authHeader, KrbPriv privateMessage ) 048 { 049 this( PVNO, authHeader, privateMessage ); 050 } 051 052 053 /** 054 * Creates a new instance of ChangePasswordRequest. 055 * 056 * @param versionNumber The version number 057 * @param authHeader The authentication header 058 * @param privateMessage The private part 059 */ 060 public ChangePasswordRequest( short versionNumber, ApReq authHeader, KrbPriv privateMessage ) 061 { 062 super( versionNumber ); 063 064 this.authHeader = authHeader; 065 this.privateMessage = privateMessage; 066 } 067 068 069 /** 070 * Returns the {@link ApReq}. 071 * 072 * @return The {@link ApReq}. 073 */ 074 public ApReq getAuthHeader() 075 { 076 return authHeader; 077 } 078 079 080 /** 081 * Returns the {@link KrbPriv}. 082 * 083 * @return The {@link KrbPriv}. 084 */ 085 public KrbPriv getPrivateMessage() 086 { 087 return privateMessage; 088 } 089 090 091 @Override 092 public short computeLength() 093 { 094 authHeaderLen = ( short ) authHeader.computeLength(); 095 privateMessageLen = ( short ) privateMessage.computeLength(); 096 097 messageLength = ( short ) ( HEADER_LENGTH + authHeaderLen + privateMessageLen ); 098 099 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}