//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2011 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.net;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.security.GeneralSecurityException;

import javax.crypto.Cipher;

import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;

import com.threerings.presents.util.SecureUtil;

/**
 * Sends an AES encrypted auth request to the server.  It assumes that
 * {@link SecureUtil#ciphersSupported} has succeeded.
 */
public class AESAuthRequest extends AuthRequest
{
    /**
     * Zero argument constructor used when unserializing an instance.
     */
    public AESAuthRequest ()
    {
        super();
    }

    /**
     * Constructs a auth request with the supplied credentials and client version information.
     */
    public AESAuthRequest (byte[] key, Credentials creds, String version, String[] bootGroups)
    {
        super(null, version, bootGroups);
        _clearCreds = creds;
        _key = key;
    }

    @Override // documentation inherited
    public Credentials getCredentials ()
    {
        return _clearCreds;
    }

    @Override // documentation inherited
    public String toString ()
    {
        return "[type=AESAREQ, msgid=" + messageId + ", creds=" + _clearCreds +
            ", version=" + _version + "]";
    }

    /**
     * Decrypts the request after transmission.
     */
    public void decrypt (byte[] key)
        throws IOException, ClassNotFoundException
    {
        if (_clearCreds != null) {
            return;
        }
        _key = key;
        try {
           _contents = SecureUtil.getAESCipher(Cipher.DECRYPT_MODE, _key).doFinal(_contents);
        } catch (GeneralSecurityException gse) {
            throw new IOException("Failed to decrypt credentials", gse);
        }

        ByteArrayInputStream byteIn = new ByteArrayInputStream(_contents);
        ObjectInputStream cipherIn = new ObjectInputStream(byteIn);
        _clearCreds = (Credentials)cipherIn.readObject();
    }

    /**
     * A customized AES encrypting write object.
     */
    public void writeObject (ObjectOutputStream out)
        throws IOException
    {
        out.defaultWriteObject();
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream oOut = new ObjectOutputStream(byteOut);
        oOut.writeObject(_clearCreds);
        try {
            byte[] encrypted =
                SecureUtil.getAESCipher(Cipher.ENCRYPT_MODE, _key).doFinal(byteOut.toByteArray());
            out.writeInt(encrypted.length);
            out.write(encrypted);
        } catch (GeneralSecurityException gse) {
            throw new IOException("Failed to encrypt credentials", gse);
        }
    }

    /**
     * Read in our encrypted contents.
     */
    public void readObject (ObjectInputStream in)
        throws IOException, ClassNotFoundException
    {
        in.defaultReadObject();
        _contents = new byte[in.readInt()];
        in.read(_contents);
    }

    /** Our encryption key. */
    protected transient byte[] _key;

    /** Our encrypted contents. */
    protected transient byte[] _contents;

    /** Our unencrypted credentials. */
    protected transient Credentials _clearCreds;
}

@@ -2639,37 +2639,49 @@
 ) %7B%0A            
-throw
+IOException ioe =
  new IOException
@@ -2704,38 +2704,88 @@
 ypt credentials%22
-, gse)
+);%0A            ioe.initCause(gse);%0A            throw ioe
 ;%0A        %7D%0A%0A   
@@ -3632,21 +3632,33 @@
         
-throw
+IOException ioe =
  new IOE
@@ -3701,14 +3701,64 @@
 als%22
-, gse)
+);%0A            ioe.initCause(gse);%0A            throw ioe
 ;%0A  
@@ -3819,24 +3819,24 @@
 ntents.%0A
-
      */%0A
     publ
@@ -3819,32 +3819,46 @@
 ntents.%0A     */%0A
+    @Override%0A
     public void 

Fix some 1.6isms and add a missing @Override