Example #1
0
        public void Connect(string address, string identity, byte[] psk)
        {
            IntPtr tls, web, ssl;

            OpenSSL.OpenSSLCheck((tls = OpenSSL.TLSv1_2_method()) != IntPtr.Zero, "TLS_method");
            OpenSSL.OpenSSLCheck((ctx = OpenSSL.SSL_CTX_new(tls)) != IntPtr.Zero, "SSL_CTX_new");

            OpenSSL.OpenSSLCheck((web = OpenSSL.BIO_new_ssl_connect(ctx)) != IntPtr.Zero, "BIO_new_ssl_connect");
            OpenSSL.OpenSSLCheck(OpenSSL.BIO_set_conn_hostname(web, address) == 1, "BIO_set_conn_hostname");
            OpenSSL.BIO_get_ssl(web, out ssl);
            OpenSSL.OpenSSLCheck(ssl != IntPtr.Zero, "BIO_get_ssl");

            OpenSSL.SSL_set_psk_client_callback(ssl, (ssl_, hint, identityOut, max_identity_len, pskOut, max_psk_len) =>
            {
                byte[] identityBuf = Encoding.ASCII.GetBytes(identity);

                Marshal.Copy(identityBuf, 0, identityOut, (int)Math.Min(identityBuf.Length, max_identity_len));
                Marshal.Copy(psk, 0, pskOut, (int)Math.Min(psk.Length, max_psk_len));

                return((uint)psk.Length);
            });

            OpenSSL.OpenSSLCheck(OpenSSL.BIO_do_handshake(web) == 1, "BIO_do_handshake");

            base.Connect(web);
        }