コード例 #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);
        }
コード例 #2
0
        public TlsPskConnection Accept()
        {
            OpenSSL.OpenSSLCheck(OpenSSL.BIO_do_handshake(in_bio) == 1, "BIO_do_handshake");
            IntPtr conn = OpenSSL.BIO_pop(in_bio);

            IntPtr ssl;

            OpenSSL.BIO_get_ssl(conn, out ssl);
            OpenSSL.OpenSSLCheck(ssl != IntPtr.Zero, "BIO_get_ssl");
            OpenSSL.SSL_use_psk_identity_hint(ssl, identityHint);
            OpenSSL.SSL_set_psk_server_callback(ssl, (ssl_, identity, pskOut, max_psk_len) =>
            {
                Marshal.Copy(psk, 0, pskOut, (int)Math.Min(psk.Length, max_psk_len));
                return((uint)psk.Length);
            });

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

            return(new TlsPskConnection(conn));
        }