Exemple #1
0
        protected bool doHandshaking(string encryption_type, string encryption_header)
        {
            if (encryption_type == kEncryptionHandshakeBegin)
            {
                // encryption list
                List <EncryptionType> encryption_list = new List <EncryptionType>();

                if (encryption_header.Length > 0)
                {
                    int            begin = 0;
                    int            end   = encryption_header.IndexOf(kDelim2);
                    EncryptionType type;

                    while (end != -1)
                    {
                        type = (EncryptionType)Convert.ToInt32(encryption_header.Substring(begin, end - begin));
                        encryption_list.Add(type);
                        begin = end + 1;
                        end   = encryption_header.IndexOf(kDelim2, begin);
                    }

                    type = (EncryptionType)Convert.ToInt32(encryption_header.Substring(begin));
                    encryption_list.Add(type);
                }

                // Create encryptors
                foreach (EncryptionType type in encryption_list)
                {
                    if (!createEncryptor(type))
                    {
                        return(false);
                    }
                }
            }
            else
            {
                // Encryption handshake message
                EncryptionType type = (EncryptionType)Convert.ToInt32(encryption_type);
                if (!encryptors_.ContainsKey(type))
                {
                    LogWarning("Encryptor.doHandshaking - Unavailable type: {0}", type);
                    return(false);
                }

                Encryptor encryptor = encryptors_[type];
                if (encryptor.state != Encryptor.State.kHandshaking)
                {
                    LogWarning("Encryptor.doHandshaking - Encryptor state is not handshaking. " +
                               "state: {0}", encryptor.state);
                    return(false);
                }

                string out_header = "";
                if (!encryptor.Handshake(encryption_header, ref out_header))
                {
                    LogWarning("Encryptor.doHandshaking - Failure in '{0}' Handshake.",
                               encryptor.name);
                    return(false);
                }

                FunDebug.Assert(encryptor.state == Encryptor.State.kEstablished);
            }

            bool handshake_complete = true;

            foreach (KeyValuePair <EncryptionType, Encryptor> pair in encryptors_)
            {
                if (pair.Value.state != Encryptor.State.kEstablished)
                {
                    handshake_complete = false;
                    break;
                }
            }

            return(handshake_complete);
        }