summary refs log tree commit diff
path: root/crypto/test/src/tls/test/TlsClientRawKeysTest.cs
blob: 510213fc742c4b2c670eccfabe50faf06bd45acf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

using NUnit.Framework;

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Tls.Tests
{
    /// <summary>A simple test designed to conduct a TLS handshake with an external TLS server.</summary>
    /// <remarks>
    /// <code>
    /// openssl genpkey -out ed25519.priv -algorithm ed25519
    /// openssl pkey -in ed25519.priv -pubout -out ed25519.pub
    /// 
    /// gnutls-serv --http --debug 10 --priority NORMAL:+CTYPE-CLI-RAWPK:+CTYPE-SRV-RAWPK --rawpkkeyfile ed25519.priv --rawpkfile ed25519.pub
    /// </code>
    /// </remarks>
    [TestFixture]
    public class TlsClientRawKeysTest
    {
        [Test, Explicit]
        public void TestConnection()
        {
            string host = "localhost";
            int port = 5556;

            RunTest(host, port, ProtocolVersion.TLSv12);
            RunTest(host, port, ProtocolVersion.TLSv13);
        }

        private static void RunTest(string host, int port, ProtocolVersion tlsVersion)
        {
            MockRawKeysTlsClient client = new MockRawKeysTlsClient(CertificateType.RawPublicKey,
                CertificateType.RawPublicKey, new short[]{ CertificateType.RawPublicKey },
                new short[]{ CertificateType.RawPublicKey }, new Ed25519PrivateKeyParameters(new SecureRandom()),
                tlsVersion);
            TlsClientProtocol protocol = OpenTlsClientConnection(host, port, client);

            Http11Get(host, port, protocol.Stream);

            protocol.Close();
        }

        private static void Http11Get(string host, int port, Stream s)
        {
            WriteUtf8Line(s, "GET / HTTP/1.1");
            //WriteUtf8Line(s, "Host: " + host + ":" + port);
            WriteUtf8Line(s, "");
            s.Flush();

            Console.WriteLine("---");

            string[] ends = new string[] { "</HTML>", "HTTP/1.1 3", "HTTP/1.1 4" };

            StreamReader reader = new StreamReader(s);

            bool finished = false;
            string line;
            while (!finished && (line = reader.ReadLine()) != null)
            {
                Console.WriteLine("<<< " + line);

                string upperLine = TlsTestUtilities.ToUpperInvariant(line);

                // TEST CODE ONLY. This is not a robust way of parsing the result!
                foreach (string end in ends)
                {
                    if (upperLine.IndexOf(end) >= 0)
                    {
                        finished = true;
                        break;
                    }
                }
            }

            Console.Out.Flush();
        }

        private static TlsClientProtocol OpenTlsClientConnection(string hostname, int port, TlsClient client)
        {
            TcpClient tcp = new TcpClient(hostname, port);

            TlsClientProtocol protocol = new TlsClientProtocol(tcp.GetStream());
            protocol.Connect(client);
            return protocol;
        }

        private static void WriteUtf8Line(Stream output, string line)
        {
            byte[] buf = Encoding.UTF8.GetBytes(line + "\r\n");
            output.Write(buf, 0, buf.Length);
            Console.WriteLine(">>> " + line);
        }
    }
}