Beispiel #1
0
    /*
     * Send a "normal" message to the peer, of the specified
     * length: this is a sequence of 'len' random bytes, distinct
     * from 0x0A, followed one 0x0A byte. The peer is supposed to
     * respond with the SHA-1 hash of the message bytes (excluding
     * the final 0x0A), encoded in hexadecimal (lowercase) and
     * followed by a newline (0x0A). An exception is thrown if the
     * expected value is not obtained.
     */
    void SendMessageNormal(SSLEngine eng, int len)
    {
        SHA1 sha1 = new SHA1();

        byte[] buf = new byte[len + 1];
        RNG.GetBytesNonZero(buf, 0, len);
        for (int i = 0; i < len; i++)
        {
            buf[i] ^= 0x0A;
        }
        buf[len] = 0x0A;
        if (len == 1)
        {
            buf[0] = (byte)('a' + (buf[0] & 0x0F));
        }
        StringBuilder sb = new StringBuilder();

        foreach (byte b in sha1.Hash(buf, 0, len))
        {
            sb.AppendFormat("{0:x2}", b);
        }
        sb.Append('\n');
        eng.Write(buf, 0, buf.Length);
        eng.Flush();
        for (int i = 0; i < sb.Length; i++)
        {
            int x = eng.ReadByte();
            int y = sb[i];
            if (x != y)
            {
                throw new Exception(string.Format(
                                        "received {0} (exp: {1})", y, x));
            }
        }
    }
Beispiel #2
0
    string ReadLine(SSLEngine eng)
    {
        StringBuilder sb = new StringBuilder();

        for (;;)
        {
            int c = eng.ReadByte();
            if (c < 0)
            {
                throw new Exception("Unexpected EOF");
            }
            if (c == 0x0A)
            {
                return(sb.ToString());
            }
            sb.Append((char)c);
        }
    }