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.ApRep; 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 ChangePasswordReply extends AbstractPasswordMessage 038{ 039 private ApRep applicationReply; 040 private KrbPriv privateMessage; 041 042 private short applicationReplyLen; 043 private short privateMessageLen; 044 private short messageLength; 045 046 047 public ChangePasswordReply( ApRep applicationReply, KrbPriv privateMessage ) 048 { 049 this( PVNO, applicationReply, privateMessage ); 050 } 051 052 053 /** 054 * Creates a new instance of ChangePasswordReply. 055 * 056 * @param versionNumber The version number 057 * @param applicationReply The application reply 058 * @param privateMessage The private part 059 */ 060 public ChangePasswordReply( short versionNumber, ApRep applicationReply, KrbPriv privateMessage ) 061 { 062 super( versionNumber ); 063 064 this.applicationReply = applicationReply; 065 this.privateMessage = privateMessage; 066 } 067 068 069 /** 070 * Returns the {@link ApRep} instance. 071 * 072 * @return The {@link ApRep} instance. 073 */ 074 public ApRep getApplicationReply() 075 { 076 return applicationReply; 077 } 078 079 080 /** 081 * Returns the {@link KrbPriv} instance. 082 * 083 * @return The {@link KrbPriv} instance. 084 */ 085 public KrbPriv getPrivateMessage() 086 { 087 return privateMessage; 088 } 089 090 091 @Override 092 public short computeLength() 093 { 094 applicationReplyLen = ( short ) applicationReply.computeLength(); 095 privateMessageLen = ( short ) privateMessage.computeLength(); 096 097 messageLength = ( short ) ( HEADER_LENGTH + applicationReplyLen + 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 buf.putShort( applicationReplyLen ); 109 110 applicationReply.encode( buf ); 111 privateMessage.encode( buf ); 112 113 return buf; 114 } 115 116 117 /** 118 * Decodes a {@link ByteBuffer} into a {@link ChangePasswordReply}. 119 * 120 * @param buf 121 * @return The {@link ChangePasswordReply}. 122 * @throws ChangePasswordException If teh decoding failed 123 */ 124 public static ChangePasswordReply decode( ByteBuffer buf ) throws ChangePasswordException 125 { 126 try 127 { 128 short messageLength = buf.getShort(); 129 short protocolVersion = buf.getShort(); 130 short encodedAppReplyLength = buf.getShort(); 131 132 byte[] encodedAppReply = new byte[encodedAppReplyLength]; 133 buf.get( encodedAppReply ); 134 135 ApRep applicationReply = KerberosDecoder.decodeApRep( encodedAppReply ); 136 137 int privateBytesLength = messageLength - HEADER_LENGTH - encodedAppReplyLength; 138 byte[] encodedPrivateMessage = new byte[privateBytesLength]; 139 buf.get( encodedPrivateMessage ); 140 141 KrbPriv privateMessage = KerberosDecoder.decodeKrbPriv( encodedPrivateMessage ); 142 143 return new ChangePasswordReply( protocolVersion, applicationReply, privateMessage ); 144 } 145 catch ( KerberosException e ) 146 { 147 throw new ChangePasswordException( ChangePasswdErrorType.KRB5_KPASSWD_MALFORMED, e ); 148 } 149 } 150}