![]() |
Web Development »
Web Services »
General
Intermediate
Set up IIS to work with server and client certificates and HTTPS: test your client or web service code in minutes (without using Win 2003 or Certificate Server).By Koushik BiswasHave you written a web service that will be deployed over HTTPS and you want to test it on your own box? Have you written a client that needs to call a web service using a client certificate, and want to test it without involving half your company? Here's how to do it! |
C#, Windows, .NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, ASP.NET, WebForms, IIS 5.1, IIS 6, VS.NET2003, IE 6.0, IE 5.5, IIS 7, Dev, QA
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I bet, from the commonality of the task I was facing, that there must be tons of others in my shoes. But amazingly, I did not find many good articles out there discussing a solution. Let me put it this way - there are a lot of articles discussing what I needed, but most of them discuss the problem, not the solution. So, armed with bits and pieces of information from everywhere, and my own 2 cents, I managed to find out how exactly to do the following: set up the IIS on your own machine to do HTTPS, and then create a client certificate for mere testing purposes if your client program needs to send one with its request. All this not using Win 2003 or Certificate Authority or Certificate Server. I was using XP Professional and IIS 5.0 - but this should work with IIS 6.0 as well.
This is a technique article, it involves steps, some of which I cannot explain like an expert. I do not know Perl, and I am definitely not an IIS Administrator. All I am is part of a team who wrote a C# client program which must access a 3rd party web service. This web service is protected like a fortress - it is a HTTPS link, and it requires its clients to use a client certificate. It also has HTTP Basic Authentication mechanism. Having written the client program and tested it functionally, my team had to test it "environmentally". Rather than involving the 3rd party developers to let us in for testing, we quickly wrote a dummy web service (again in C#) that follows the same WSDL as theirs. Having done that, our next goals were to:
In this article, I will describe how the 1st two bullets above can be achieved. The remaining two are actually easy - you will find lots of articles on how to do that. If you still cannot find, just drop me a comment, I will share.
First of all, a brief explanation of what a "server certificate" is. When you install IIS and host a web service, you are the server. Anybody who calls that web service (like a client program or IE browser window) is the client. If you install a server certificate on your server (IIS), clients can call your web service with a HTTPS in the URL. If they do, and if the client is the browser, it may receive a warning dialog box. But if the client is a program, you could write the code in such a way that the warning dialog box is programmatically responded with a "YES". In this article, I will assume that you know to write all that code - this article is only about setting up - things you do after writing and compiling the code, so that you could test it. However, if you still want to know the exact coding technique, please feel free to drop me a comment.
What does a server certificate achieve? Simple: It allows or enables clients to call with "HTTPS" in the URL. What that does is that it kicks in the HTTPS protocol instead of HTTP, and therefore, encrypts the data before sending it to the server. And vice versa. Remember that depending on how you (I mean the server owner) configure your IIS, the client can either (a) be forced to always use HTTPS or (b) have the option of either using HTTP or HTTPS. We will see that soon at a later point.
Why do you need to run a perl script? Because IIS does not allow you to create a server certificate directly. But it does allow you to create a "request for a certificate". In the real world, you would typically create such a request, and then submit it to external companies to issue you a certificate based on that request. But what we are going to do is we will use OPENSSL's open source freeware to create a server certificate from that request (instead of contacting an external company). This open source is a perl script (as you will soon see). So you need something that runs PERL. That's why.
$SSLEAY_CONFIG=$ENV{"SSLEAY_CONFIG"};$SSLEAY_CONFIG="-config openssl.cnf";perl CA.pl -newca
perl CA.pl -signreqYou cannot easily create a client certificate without software like Microsoft Certificate Server. But having said that, I did find a work around for my specific need. So may be this work around may help you! That is why I am listing it here.
First step is to configure your web service to require a client certificate. In the IIS window, right click on the virtual directory corresponding to your specific web service, click Properties. Directory Security tab. Edit... button. Select the "Require Client Certificates" radio, OK out. Now your web service is configured to require a client certificate. Which means any client calling it MUST send a client certificate with the request.
Go ahead and try to call your web service using the browser, see what happens.
But where would I get a client certificate from? The client program that we needed to test accepted a CER file path through configuration entries and added a X509 certificate object created from that CER file to the request's Certificates collection. In production, this 3rd party would provide us with a correct CER file. The deployment team will simply keep the CER file wherever I tell them to, and update the CONFIG file with its path. But how will I know that the code will work? I need to test it right now, right here!
Of course, I could tell that the code was working if we left the CER File config entry blank. Which means it would not find any CER file to bundle with the request, and hence send the request without any CER file. In that case, and having configured the dummy web service to require a client certificate, we received SOAP errors (protocol error to be exact). That was expected.
Then this idea struck me. I myself browse internet like an addict. Surely there would be some client certificates installed on my machine (that the browser uses to send to some web site that needs them). Why not try to export a CER file from any one of them? With IE, here is how you do that: Open IE, Tools --> Internet Options, Content tab, Certificates button, and the resulting dialog has many tabs. Starting from the tab "Personal" in the very left to the tab "Untrusted Publishers" to the very right - there is a delightfully large number of client certificates installed on my machine. I selected any one of them. As soon as you select a certficate, the Export... button becomes enabled. Click it, click Next, select "DER Encoded Binary X.509 (.CER)", click Next, give a full path and name (that ends in .CER), and export the certficate. Well, here was my CER file.
But the fact remained that I was using just any random CER file. I was not using the one and only that my web service needed. But I will leave complementing my explanation to my audience (to make it complete) - why it worked. I am keen to know the reason, too. So please leave a comment if you have a more correct explanation than me of the following fact: I used that CER file in my client program, and it worked fine! It called my dummy web service without any hiccups. The protocol error was no more coming, and that proves that the client code is capable of bundling a client certificate and sending it to its target web service just fine!
I have not configured my web service to do anything meaningful (like validation) with the client certificate. If I had done so, using just any random client certificate would not have worked. But as far as my testing was concerned, I had just proved that the code works, and that was my goal. After all, in real life, my client code will call a 3rd party web service, who would provide their own CER file. So I did not need to know what they would do with that CER file once my client program bundled and sent it along with the SOAP request!
Of course if you are reading this article as the web service author, and want to use a client certificate being sent to you by a caller, then my small experiment does not help you directly. Anyway, if you find that out, and want to share, so that the cyberspace is a little more enriched, please leave a message for everybody.
This was largely an article on how I had reacted to a particular problem. And it seemed to be a problem that many others would very likely face. That prompted me to write this up. Please pardon lack of in depth explanation in certain areas.
Also, this article refers to small bits and pieces of code which I have not elaborated. Please let me know if you need to get a hold of them.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Mar 2007 Editor: |
Copyright 2007 by Koushik Biswas Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |