tags:

views:

858

answers:

6

Hello all,

Does anyone of you know, if and if so, how can I check, with my application code, if a server has ssl enable or not?

Thx

+1  A: 

not sure on your language of preference but here it is in c#

public bool IsSecureConnection()
{
    return HttpContext.Current.Request.IsSecureConnection || 
           HttpContext.Current.Request.Headers["HTTP_X_SSL_REQUEST"].Equals("1");
}

Please note this header is custom, but I think you get the idea. I've seen folk simply query request for "https" and besides looking dirty it's probably reasonably acceptable, depends on your security model.

Or are you asking whether it's simply available at all?

I

dove
A: 

Thx dove,

I think that is in the server-side, I want to know in client-side how can I know if a server accepts SSL connections. (If I can somehow).

I think I can do it with some exception handling, if I try to connect to the server with ssl enable and I get an exception, then the server (probably) doesn't have ssl enabled.

But can I do it in some other way?

Bruno Costa
A: 

You need to specify what protocol you're working with -- there are SSL versions of HTTP, IMAP, POP, etc.

Assuming it's HTTPS you're interested in, you could check to see if something is listening on port 443 on the server and go from there...

genehack
A: 

Yes, I thought on that too, but I think the port of the service depends on configuration of the server, am I right?

Btw, I'm working with https and smtp, sorry. And my language of preference is C#, but I accept any language as answer, if I got one :P.

Thank to you both ;)

Bruno Costa
Yes, the port does depend on the configuration -- but if you're just trying random sites using HTTPS, that's going to be on port 443. If it
genehack
+4  A: 

"It's easier to ask forgiveness than permission"

For example, to read stackoverflow.com via SSL, don't ask whether stackoverflow.com supports it, just do it. In Python:

>>> import urllib2
>>> urllib2.urlopen('https://stackoverflow.com')
Traceback (most recent call last):
...
urllib2.URLError: <urlopen error (10060, 'Operation timed out')>
>>> html = urllib2.urlopen('http://stackoverflow.com').read()
>>> len(html)
146271
>>> 

It shows that stackoverflow.com doesn't support SSL.

J.F. Sebastian
A: 

You don't specify a programming language, but you could do this from the command-line.

bash-3.2$ echo ^D | telnet www.google.com https
Trying 66.102.11.104...
Connected to www.l.google.com.
Escape character is '^]'.
Connection closed by foreign host.
bash-3.2$ echo ^D | telnet www.stackoverflow.com https
Trying 69.59.196.211...
telnet: connect to address 69.59.196.211: Connection refused
telnet: Unable to connect to remote host

There you go... Google does, StackOverflow does not.

Johnsyweb