package org.bouncycastle.crypto.tls;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;

import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Integers;

public abstract class SRPTlsClient
    extends AbstractTlsClient
{
    public static final Integer EXT_SRP = Integers.valueOf(ExtensionType.srp);

    protected byte[] identity;
    protected byte[] password;

    public SRPTlsClient(byte[] identity, byte[] password)
    {
        super();
        this.identity = Arrays.clone(identity);
        this.password = Arrays.clone(password);
    }

    public SRPTlsClient(TlsCipherFactory cipherFactory, byte[] identity, byte[] password)
    {
        super(cipherFactory);
        this.identity = Arrays.clone(identity);
        this.password = Arrays.clone(password);
    }

    public int[] getCipherSuites()
    {
        return new int[]{CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
            CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA, CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA,
            CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA, CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA,
            CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA,};
    }

    public Hashtable getClientExtensions()
        throws IOException
    {

        Hashtable clientExtensions = super.getClientExtensions();
        if (clientExtensions == null)
        {
            clientExtensions = new Hashtable();
        }

        ByteArrayOutputStream srpData = new ByteArrayOutputStream();
        TlsUtils.writeOpaque8(this.identity, srpData);
        clientExtensions.put(EXT_SRP, srpData.toByteArray());

        return clientExtensions;
    }

    public void processServerExtensions(Hashtable serverExtensions)
        throws IOException
    {
        // No explicit guidance in RFC 5054 here; we allow an optional empty extension from server
        if (serverExtensions != null)
        {
            byte[] extension_data = (byte[])serverExtensions.get(EXT_SRP);
            if (extension_data != null && extension_data.length > 0)
            {
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }
        }
    }

    public TlsKeyExchange getKeyExchange()
        throws IOException
    {

        switch (selectedCipherSuite)
        {
        case CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
            return createSRPKeyExchange(KeyExchangeAlgorithm.SRP);

        case CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
            return createSRPKeyExchange(KeyExchangeAlgorithm.SRP_RSA);

        case CipherSuite.TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
            return createSRPKeyExchange(KeyExchangeAlgorithm.SRP_DSS);

        default:
            /*
             * Note: internal error here; the TlsProtocol implementation verifies that the
             * server-selected cipher suite was in the list of client-offered cipher suites, so if
             * we now can't produce an implementation, we shouldn't have offered it!
             */
            throw new TlsFatalAlert(AlertDescription.internal_error);
        }
    }

    public TlsCipher getCipher()
        throws IOException
    {

        switch (selectedCipherSuite)
        {
        case CipherSuite.TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
            return cipherFactory.createCipher(context, EncryptionAlgorithm._3DES_EDE_CBC, MACAlgorithm.hmac_sha1);

        case CipherSuite.TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
            return cipherFactory.createCipher(context, EncryptionAlgorithm.AES_128_CBC, MACAlgorithm.hmac_sha1);

        case CipherSuite.TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
        case CipherSuite.TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
            return cipherFactory.createCipher(context, EncryptionAlgorithm.AES_256_CBC, MACAlgorithm.hmac_sha1);

        default:
            /*
             * Note: internal error here; the TlsProtocol implementation verifies that the
             * server-selected cipher suite was in the list of client-offered cipher suites, so if
             * we now can't produce an implementation, we shouldn't have offered it!
             */
            throw new TlsFatalAlert(AlertDescription.internal_error);
        }
    }

    protected TlsKeyExchange createSRPKeyExchange(int keyExchange)
    {
        return new TlsSRPKeyExchange(keyExchange, supportedSignatureAlgorithms, identity, password);
    }
}

@@ -35,46 +35,8 @@
 s;%0A%0A
-import java.io.ByteArrayOutputStream;%0A
 impo
@@ -128,47 +128,8 @@
 ays;
-%0Aimport org.bouncycastle.util.Integers;
 %0A%0Apu
@@ -193,16 +193,83 @@
 lient%0A%7B%0A
+    /**%0A     * @deprecated use TlsSRPUtils.EXT_SRP instead%0A     */%0A
     publ
@@ -306,43 +306,27 @@
 P = 
-Integers.valueOf(ExtensionType.srp)
+TlsSRPUtils.EXT_SRP
 ;%0A%0A 
@@ -1269,33 +1269,32 @@
 Exception%0A    %7B%0A
-%0A
         Hashtabl
@@ -1318,210 +1318,82 @@
 s = 
-super.getClientExtensions();%0A        if (clientExtensions == null)%0A        %7B%0A            clientExtensions = new Hashtable();%0A        %7D%0A%0A        ByteArrayOutputStream srpData = new ByteArrayOutputStream(
+TlsExtensionsUtils.ensureExtensionsInitialised(super.getClientExtensions()
 );%0A 
@@ -1406,115 +1406,67 @@
  Tls
+SRP
 Utils.
-writeOpaque8(this.identity, srpData);%0A        clientExtensions.put(EXT_SRP, srpData.toByteArray()
+addSRPExtension(clientExtensions, this.identity
 );%0A
-%0A
     
@@ -1614,86 +1614,47 @@
     
-// No explicit guidance in RFC 5054 here; we allow an optional e
+if (!TlsUtils.hasExpectedE
 mpty
- e
+E
 xtension
  fro
@@ -1653,317 +1653,201 @@
 sion
- from server%0A        if (serverExtensions != null)%0A        %7B%0A            byte%5B%5D extension_data = (byte%5B%5D)serverExtensions.get(EXT_SRP);%0A            if (extension_data != null && extension_data.length %3E 0)%0A            %7B%0A                throw new TlsFatalAlert(AlertDescription.illegal_parameter);%0A            %7D
+Data(serverExtensions, TlsSRPUtils.EXT_SRP, AlertDescription.illegal_parameter))%0A        %7B%0A            // No explicit guidance in RFC 5054 here; we allow an optional empty extension from server
 %0A   

Refactor to use utility class methods