c# - Mono HTTPListener throws exception when using self signed SSL certificate -


i wrote little web server on linux using mono httplistener class. works fine http requests. however, if use self-signed ssl certificate (created openssl , installed httpcfg) throw un-catchable exception request forom browser comes in.

the exception is:

unhandled exception: system.io.ioexception: authentication or decryption has failed. ---> mono.security.protocol.tls.tlsexception: client stopped handshake. @ mono.security.protocol.tls.sslserverstream.endnegotiatehandshake (iasyncresult asyncresult) <0xb4b079c8 + 0x001cf> in <filename unknown>:0  @ mono.security.protocol.tls.sslstreambase.asynchandshakecallback (iasyncresult asyncresult) <0xb4b07428 + 0x0005f> in <filename unknown>:0 

here complete code:

using system; using system.net; using system.io; using system.text; using system.threading;  namespace ssltest {     class mainclass     {         static void main ()         {             try             {                 httplistener l = new httplistener ();                 l.prefixes.add ("https://*:8443/");                 l.start ();                  console.writeline("server running.");                 while (l.islistening)                 {                     //create worker thread                     httplistenercontext ctx = l.getcontext();   //.getcontext() blocks until comes in                     if(ctx != null)                     {                         if(ctx.request.remoteendpoint != null)                         {                             thread workerthread = new thread(() => runworker(ctx));                             workerthread.start();                         }                     }                 }                 console.writeline("server stopped.");             }             catch(exception ex)              {                 console.writeline ("exception in main: " + ex);             }         }           static void runworker(httplistenercontext ctx)         {             try             {                 if(ctx.request != null)                 {                     if(ctx.request.remoteendpoint != null)                     {                         console.writeline ("got request " + ctx.request.remoteendpoint.tostring());                         string rstr = "test website!\n" + datetime.now.tostring();                         byte[] buf = encoding.utf8.getbytes(rstr);                         if(buf!=null)                         {                             ctx.response.contentlength64 = buf.length;                             ctx.response.outputstream.write(buf, 0, buf.length);                         }                     }                 }             }             catch(exception ex)             {                 console.writeline ("@exception in runworker: " + ex.message);             }         }      } } 

this case when using browser first time. browser show "unsafe certificate! want continue (not recommended)?". if click on yes , restart crashed server app work moment on.

how can fix this?

also, not able catch exception try block. terminate application. how can prevent that?

should fixed unreleased bug fix https://bugzilla.xamarin.com/show_bug.cgi?id=52675... although have not had chance test.

i have seen , trying find way handle exception. looks bug in mono.

it occurs when certificate verification fails in way.

i obtained ca signed cert fixed problem long certificate common name (dns) same dns used in url i'm trying send request to. if instead specify public ip address in url (which cert not registered with) mono app crash unhandled exception.

one option we're considering implementing tcp based webserver use tcplistener instead of heavy httplistener in turn fix other problems we're seeing mono httplistener prefixes not working correctly when bound internal ip behind nat. mean cert bound pro-grammatically well, takes more work.


Comments