Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > HttpWebRequest using Certificates

Reply
Thread Tools

HttpWebRequest using Certificates

 
 
Josef Brunner
Guest
Posts: n/a
 
      03-03-2006
Hi everybody,

my VB.NET (Framework 2.0) client application has to do a HttpWebRequest (for
reading web-pages and downloading files) on a web server. The server uses a
self-signed certifiacte and the client application should also use a
self-signed certificate (of course, signed by the same self-made CA) so we
would have an authentication of both directions: the server to the client
and the other way round.

Is there a way to programmatically load the self-signed server certificate
in my VB application? Something like:

Private _WebClient As HttpWebRequest

Private _ClientCert As X509Certificate2 = LoadCert() ' This already works

_WebClient = CType(WebRequest.Create(_Server + "site.html"), HttpWebRequest)

_WebClient.ClientCertificates.Add(_ClientCert)

' Something like this.....

_WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")

Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
HttpWebResponse)



So far my client does not accept the server certificate since it could not
establish the trust relationship! Of course, since my client does not know
about the CA. And I don't want to have to install the certificate/CA on each
machine that I need to install the software on.

Any ideas?

Thank you very much,

Josef


 
Reply With Quote
 
 
 
 
Luke Zhang [MSFT]
Guest
Posts: n/a
 
      03-03-2006
Hello,

Here is a sample may help:

HttpWebRequest httprq = (HttpWebRequest)HttpWebRequest.Create(uri);
httprq.Method = "POST";
httprq.ContentType = "text/xml; charset=utf-8";

string certificateName = "ABC";
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates =
store.Certificates.Find(X509FindType.FindBySubject Name, certificateName,
true);
X509Certificate certificate = certificates[0];
httprq.ClientCertificates.Add(certificate);

//Response
HttpWebResponse httprp = (HttpWebResponse)httprq.GetResponse();

Also, The server certificate's root authority must be trusted by client and
the client certificate's root authority must be trusted by the server.

Regards,

Luke Zhang
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
Reply With Quote
 
 
 
 
Dominick Baier [DevelopMentor]
Guest
Posts: n/a
 
      03-03-2006
Hi,

sure - get a cert from a CA that is already trusted on every single Windows
machine, e.g. VeriSign.

Then you don't have to install anything extra.

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

> Hi everybody,
>
> my VB.NET (Framework 2.0) client application has to do a
> HttpWebRequest (for reading web-pages and downloading files) on a web
> server. The server uses a self-signed certifiacte and the client
> application should also use a self-signed certificate (of course,
> signed by the same self-made CA) so we would have an authentication of
> both directions: the server to the client and the other way round.
>
> Is there a way to programmatically load the self-signed server
> certificate in my VB application? Something like:
>
> Private _WebClient As HttpWebRequest
>
> Private _ClientCert As X509Certificate2 = LoadCert() ' This already
> works
>
> _WebClient = CType(WebRequest.Create(_Server + "site.html"),
> HttpWebRequest)
>
> _WebClient.ClientCertificates.Add(_ClientCert)
>
> ' Something like this.....
>
> _WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")
>
> Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
> HttpWebResponse)
>
> So far my client does not accept the server certificate since it could
> not establish the trust relationship! Of course, since my client does
> not know about the CA. And I don't want to have to install the
> certificate/CA on each machine that I need to install the software on.
>
> Any ideas?
>
> Thank you very much,
>
> Josef
>



 
Reply With Quote
 
Josef Brunner
Guest
Posts: n/a
 
      03-03-2006
Hi Luke,

thanks for the advice with the certificat sore. What I'm trying to do write
know is to load all certificates (client, server, ca) into the corresponding
certificate stores. But
1. I still get the ..."Could not establish trust relationship for the
SSL'/TLS secure channel" error message
2. I cannot find the certificates I just added to the differen certificate
stores wihin the IE...

Here's the code...maybe I do something wrong while adding them...

Private _ClientCert As X509Certificate2

Private _ServerCert As X509Certificate2

Private _CACert As X509Certificate2



Public Sub New(ByVal ClientCertFile As String, ByVal ServerCertFile As
String, ByVal CACertFile As String)

_ClientCert = ReadCertificate(ClientCertFile)

_ServerCert = ReadCertificate(ServerCertFile)

_CACert = ReadCertificate(CACertFile)



Dim CAstore As New X509Store(StoreName.CertificateAuthority,
StoreLocation.LocalMachine)

CAstore.Open(OpenFlags.ReadWrite)

CAstore.Add(_CACert)

CAstore.Close()

Dim ServerStore As New X509Store(StoreName.TrustedPeople,
StoreLocation.LocalMachine)

ServerStore.Open(OpenFlags.ReadWrite)

ServerStore.Add(_ServerCert)

ServerStore.Close()

Dim ClientStore As New X509Store(StoreName.My, StoreLocation.LocalMachine)

ClientStore.Open(OpenFlags.ReadWrite)

ClientStore.Add(_ClientCert)

ClientStore.Close()



Thanx,

Josef


 
Reply With Quote
 
Josef Brunner
Guest
Posts: n/a
 
      03-03-2006
Hi Dominick,

"Dominick Baier [DevelopMentor]" <>
schrieb im Newsbeitrag
news:. com...
> sure - get a cert from a CA that is already trusted on every single
> Windows machine, e.g. VeriSign.
>
> Then you don't have to install anything extra.


I'm sure this will solve my problem, but right now I don't have (the
permission to get) such a cert

Any other idea?
J


 
Reply With Quote
 
Dominick Baier [DevelopMentor]
Guest
Posts: n/a
 
      03-03-2006
Hi,

so what was your original question then - how to get it to work with your
test cert?

Or how to avoid installing certs on every client machine..?

these are mutually exclusive.

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

> Hi everybody,
>
> my VB.NET (Framework 2.0) client application has to do a
> HttpWebRequest (for reading web-pages and downloading files) on a web
> server. The server uses a self-signed certifiacte and the client
> application should also use a self-signed certificate (of course,
> signed by the same self-made CA) so we would have an authentication of
> both directions: the server to the client and the other way round.
>
> Is there a way to programmatically load the self-signed server
> certificate in my VB application? Something like:
>
> Private _WebClient As HttpWebRequest
>
> Private _ClientCert As X509Certificate2 = LoadCert() ' This already
> works
>
> _WebClient = CType(WebRequest.Create(_Server + "site.html"),
> HttpWebRequest)
>
> _WebClient.ClientCertificates.Add(_ClientCert)
>
> ' Something like this.....
>
> _WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")
>
> Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
> HttpWebResponse)
>
> So far my client does not accept the server certificate since it could
> not establish the trust relationship! Of course, since my client does
> not know about the CA. And I don't want to have to install the
> certificate/CA on each machine that I need to install the software on.
>
> Any ideas?
>
> Thank you very much,
>
> Josef
>



 
Reply With Quote
 
Josef Brunner
Guest
Posts: n/a
 
      03-03-2006
Hi,

you are right, the question should be:
how do I get it to work with my test certs?

sorry for not being specific,
J

"Dominick Baier [DevelopMentor]" <>
schrieb im Newsbeitrag
news:. com...
> Hi,
> so what was your original question then - how to get it to work with your
> test cert?
>
> Or how to avoid installing certs on every client machine..?
>
> these are mutually exclusive.
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
>> Hi everybody,
>>
>> my VB.NET (Framework 2.0) client application has to do a
>> HttpWebRequest (for reading web-pages and downloading files) on a web
>> server. The server uses a self-signed certifiacte and the client
>> application should also use a self-signed certificate (of course,
>> signed by the same self-made CA) so we would have an authentication of
>> both directions: the server to the client and the other way round.
>>
>> Is there a way to programmatically load the self-signed server
>> certificate in my VB application? Something like:
>>
>> Private _WebClient As HttpWebRequest
>>
>> Private _ClientCert As X509Certificate2 = LoadCert() ' This already
>> works
>>
>> _WebClient = CType(WebRequest.Create(_Server + "site.html"),
>> HttpWebRequest)
>>
>> _WebClient.ClientCertificates.Add(_ClientCert)
>>
>> ' Something like this.....
>>
>> _WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")
>>
>> Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
>> HttpWebResponse)
>>
>> So far my client does not accept the server certificate since it could
>> not establish the trust relationship! Of course, since my client does
>> not know about the CA. And I don't want to have to install the
>> certificate/CA on each machine that I need to install the software on.
>>
>> Any ideas?
>>
>> Thank you very much,
>>
>> Josef
>>

>
>



 
Reply With Quote
 
Dominick Baier [DevelopMentor]
Guest
Posts: n/a
 
      03-03-2006
Hi,

ok - your client has to trust the server cert and vice versa

the cert has to be imported into the trusted root ca store on both machines
- the ca cert must be set to provider "authentication" purpose

read more here:
http://www.leastprivilege.com/IIS6An...tificates.aspx

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

> Hi,
>
> you are right, the question should be:
> how do I get it to work with my test certs?
> sorry for not being specific,
> J
> "Dominick Baier [DevelopMentor]"
> <> schrieb im Newsbeitrag
> news:. com...
>
>> Hi,
>> so what was your original question then - how to get it to work with
>> your
>> test cert?
>> Or how to avoid installing certs on every client machine..?
>>
>> these are mutually exclusive.
>>
>> ---------------------------------------
>> Dominick Baier - DevelopMentor
>> http://www.leastprivilege.com
>>> Hi everybody,
>>>
>>> my VB.NET (Framework 2.0) client application has to do a
>>> HttpWebRequest (for reading web-pages and downloading files) on a
>>> web server. The server uses a self-signed certifiacte and the client
>>> application should also use a self-signed certificate (of course,
>>> signed by the same self-made CA) so we would have an authentication
>>> of both directions: the server to the client and the other way
>>> round.
>>>
>>> Is there a way to programmatically load the self-signed server
>>> certificate in my VB application? Something like:
>>>
>>> Private _WebClient As HttpWebRequest
>>>
>>> Private _ClientCert As X509Certificate2 = LoadCert() ' This already
>>> works
>>>
>>> _WebClient = CType(WebRequest.Create(_Server + "site.html"),
>>> HttpWebRequest)
>>>
>>> _WebClient.ClientCertificates.Add(_ClientCert)
>>>
>>> ' Something like this.....
>>>
>>> _WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")
>>>
>>> Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
>>> HttpWebResponse)
>>>
>>> So far my client does not accept the server certificate since it
>>> could not establish the trust relationship! Of course, since my
>>> client does not know about the CA. And I don't want to have to
>>> install the certificate/CA on each machine that I need to install
>>> the software on.
>>>
>>> Any ideas?
>>>
>>> Thank you very much,
>>>
>>> Josef
>>>



 
Reply With Quote
 
Mitch Gallant
Guest
Posts: n/a
 
      03-03-2006
See also comments (for server-side cert install) at end of section 1 here:
http://www.jensign.com/JavaScience/dotnet/SSLCapicom

You could deploy the root CA certificate to the clients and have them
import it ito the trusteed CA store (in .NET 2 only, or using CAPICOM
interop in .NET 1.1) .. but each client will be presented with a "warning
on importing a trusted root CA cert) dialog .. which is of course very
important.

- Mitch Gallant

"Dominick Baier [DevelopMentor]" <> wrote in message
news:. com...
> Hi,
> ok - your client has to trust the server cert and vice versa
>
> the cert has to be imported into the trusted root ca store on both machines - the ca cert must be set to provider
> "authentication" purpose
>
> read more here:
> http://www.leastprivilege.com/IIS6An...tificates.aspx
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
>> Hi,
>>
>> you are right, the question should be:
>> how do I get it to work with my test certs?
>> sorry for not being specific,
>> J
>> "Dominick Baier [DevelopMentor]"
>> <> schrieb im Newsbeitrag
>> news:. com...
>>
>>> Hi,
>>> so what was your original question then - how to get it to work with
>>> your
>>> test cert?
>>> Or how to avoid installing certs on every client machine..?
>>>
>>> these are mutually exclusive.
>>>
>>> ---------------------------------------
>>> Dominick Baier - DevelopMentor
>>> http://www.leastprivilege.com
>>>> Hi everybody,
>>>>
>>>> my VB.NET (Framework 2.0) client application has to do a
>>>> HttpWebRequest (for reading web-pages and downloading files) on a
>>>> web server. The server uses a self-signed certifiacte and the client
>>>> application should also use a self-signed certificate (of course,
>>>> signed by the same self-made CA) so we would have an authentication
>>>> of both directions: the server to the client and the other way
>>>> round.
>>>>
>>>> Is there a way to programmatically load the self-signed server
>>>> certificate in my VB application? Something like:
>>>>
>>>> Private _WebClient As HttpWebRequest
>>>>
>>>> Private _ClientCert As X509Certificate2 = LoadCert() ' This already
>>>> works
>>>>
>>>> _WebClient = CType(WebRequest.Create(_Server + "site.html"),
>>>> HttpWebRequest)
>>>>
>>>> _WebClient.ClientCertificates.Add(_ClientCert)
>>>>
>>>> ' Something like this.....
>>>>
>>>> _WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")
>>>>
>>>> Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
>>>> HttpWebResponse)
>>>>
>>>> So far my client does not accept the server certificate since it
>>>> could not establish the trust relationship! Of course, since my
>>>> client does not know about the CA. And I don't want to have to
>>>> install the certificate/CA on each machine that I need to install
>>>> the software on.
>>>>
>>>> Any ideas?
>>>>
>>>> Thank you very much,
>>>>
>>>> Josef
>>>>

>
>



 
Reply With Quote
 
Josef Brunner
Guest
Posts: n/a
 
      03-03-2006
Thank you all!

I got it to work... but you were right: If you don't use a known CA like
VeriSign & Co. you will have to install the CA on each client machine

But I could load the client certificate programmatically, which is pretty
smooth. So when delivering the software the user will just get a client
certficate signed by a known CA that he'll have to put in his config
diretory...and that's it

Have a great weekend,
J

"Mitch Gallant" <> schrieb im Newsbeitrag
news:...
> See also comments (for server-side cert install) at end of section 1 here:
> http://www.jensign.com/JavaScience/dotnet/SSLCapicom
>
> You could deploy the root CA certificate to the clients and have them
> import it ito the trusteed CA store (in .NET 2 only, or using CAPICOM
> interop in .NET 1.1) .. but each client will be presented with a "warning
> on importing a trusted root CA cert) dialog .. which is of course very
> important.
>
> - Mitch Gallant
>
> "Dominick Baier [DevelopMentor]" <>
> wrote in message news:. com...
>> Hi,
>> ok - your client has to trust the server cert and vice versa
>>
>> the cert has to be imported into the trusted root ca store on both
>> machines - the ca cert must be set to provider "authentication" purpose
>>
>> read more here:
>> http://www.leastprivilege.com/IIS6An...tificates.aspx
>>
>> ---------------------------------------
>> Dominick Baier - DevelopMentor
>> http://www.leastprivilege.com
>>
>>> Hi,
>>>
>>> you are right, the question should be:
>>> how do I get it to work with my test certs?
>>> sorry for not being specific,
>>> J
>>> "Dominick Baier [DevelopMentor]"
>>> <> schrieb im Newsbeitrag
>>> news:. com...
>>>
>>>> Hi,
>>>> so what was your original question then - how to get it to work with
>>>> your
>>>> test cert?
>>>> Or how to avoid installing certs on every client machine..?
>>>>
>>>> these are mutually exclusive.
>>>>
>>>> ---------------------------------------
>>>> Dominick Baier - DevelopMentor
>>>> http://www.leastprivilege.com
>>>>> Hi everybody,
>>>>>
>>>>> my VB.NET (Framework 2.0) client application has to do a
>>>>> HttpWebRequest (for reading web-pages and downloading files) on a
>>>>> web server. The server uses a self-signed certifiacte and the client
>>>>> application should also use a self-signed certificate (of course,
>>>>> signed by the same self-made CA) so we would have an authentication
>>>>> of both directions: the server to the client and the other way
>>>>> round.
>>>>>
>>>>> Is there a way to programmatically load the self-signed server
>>>>> certificate in my VB application? Something like:
>>>>>
>>>>> Private _WebClient As HttpWebRequest
>>>>>
>>>>> Private _ClientCert As X509Certificate2 = LoadCert() ' This already
>>>>> works
>>>>>
>>>>> _WebClient = CType(WebRequest.Create(_Server + "site.html"),
>>>>> HttpWebRequest)
>>>>>
>>>>> _WebClient.ClientCertificates.Add(_ClientCert)
>>>>>
>>>>> ' Something like this.....
>>>>>
>>>>> _WebClient.AuthorizedCertificateAuthorities.Add("M yCA.crt")
>>>>>
>>>>> Dim NewResponse As HttpWebResponse = CType(_WebClient.GetResponse(),
>>>>> HttpWebResponse)
>>>>>
>>>>> So far my client does not accept the server certificate since it
>>>>> could not establish the trust relationship! Of course, since my
>>>>> client does not know about the CA. And I don't want to have to
>>>>> install the certificate/CA on each machine that I need to install
>>>>> the software on.
>>>>>
>>>>> Any ideas?
>>>>>
>>>>> Thank you very much,
>>>>>
>>>>> Josef
>>>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are SSL certificates and x.509 certificates the same? n33470 ASP .Net Web Services 0 12-14-2005 03:30 PM
X.509 certificates and HTTPwebrequest Deepak ASP .Net Web Services 2 05-06-2005 07:42 PM
httpwebrequest and client certificates =?Utf-8?B?RGVlcGFr?= ASP .Net 1 02-07-2005 11:12 PM
VPN Client to PIX 515 - using certificates doesn't work Peter Cisco 7 08-29-2004 04:28 PM
Self-issued certificates and commercial certificates. Lord Amoeba Computer Security 2 05-05-2004 01:40 PM



Advertisments