001 package net.minecraft.network.packet; 002 003 import cpw.mods.fml.relauncher.Side; 004 import cpw.mods.fml.relauncher.SideOnly; 005 import java.io.DataInputStream; 006 import java.io.DataOutputStream; 007 import java.io.IOException; 008 import java.security.PrivateKey; 009 import java.security.PublicKey; 010 import javax.crypto.SecretKey; 011 import net.minecraft.util.CryptManager; 012 013 public class Packet252SharedKey extends Packet 014 { 015 private byte[] sharedSecret = new byte[0]; 016 private byte[] verifyToken = new byte[0]; 017 018 /** 019 * Secret AES key decrypted from sharedSecret via the server's private RSA key 020 */ 021 private SecretKey sharedKey; 022 023 public Packet252SharedKey() {} 024 025 @SideOnly(Side.CLIENT) 026 public Packet252SharedKey(SecretKey par1SecretKey, PublicKey par2PublicKey, byte[] par3ArrayOfByte) 027 { 028 this.sharedKey = par1SecretKey; 029 this.sharedSecret = CryptManager.encryptData(par2PublicKey, par1SecretKey.getEncoded()); 030 this.verifyToken = CryptManager.encryptData(par2PublicKey, par3ArrayOfByte); 031 } 032 033 /** 034 * Abstract. Reads the raw packet data from the data stream. 035 */ 036 public void readPacketData(DataInputStream par1DataInputStream) throws IOException 037 { 038 this.sharedSecret = readBytesFromStream(par1DataInputStream); 039 this.verifyToken = readBytesFromStream(par1DataInputStream); 040 } 041 042 /** 043 * Abstract. Writes the raw packet data to the data stream. 044 */ 045 public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException 046 { 047 writeByteArray(par1DataOutputStream, this.sharedSecret); 048 writeByteArray(par1DataOutputStream, this.verifyToken); 049 } 050 051 /** 052 * Passes this Packet on to the NetHandler for processing. 053 */ 054 public void processPacket(NetHandler par1NetHandler) 055 { 056 par1NetHandler.handleSharedKey(this); 057 } 058 059 /** 060 * Abstract. Return the size of the packet (not counting the header). 061 */ 062 public int getPacketSize() 063 { 064 return 2 + this.sharedSecret.length + 2 + this.verifyToken.length; 065 } 066 067 /** 068 * Return secretKey, decrypting it from the sharedSecret byte array if needed 069 */ 070 public SecretKey getSharedKey(PrivateKey par1PrivateKey) 071 { 072 return par1PrivateKey == null ? this.sharedKey : (this.sharedKey = CryptManager.decryptSharedKey(par1PrivateKey, this.sharedSecret)); 073 } 074 075 /** 076 * Return the secret AES sharedKey (used by client only) 077 */ 078 public SecretKey getSharedKey() 079 { 080 return this.getSharedKey((PrivateKey)null); 081 } 082 083 /** 084 * Return verifyToken 085 */ 086 public byte[] getVerifyToken(PrivateKey par1PrivateKey) 087 { 088 return par1PrivateKey == null ? this.verifyToken : CryptManager.decryptData(par1PrivateKey, this.verifyToken); 089 } 090 }