<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Geekality &#187; SSL</title>
	<atom:link href="http://www.geekality.net/tag/ssl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Sun, 05 Feb 2012 17:53:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP: cURL SSL Verification</title>
		<link>http://www.geekality.net/2010/12/07/php-curl-ssl-verification/</link>
		<comments>http://www.geekality.net/2010/12/07/php-curl-ssl-verification/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 17:04:12 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1413</guid>
		<description><![CDATA[While trying to figure out of PayPal Payment Data Transfers I came over an issue that caused my cURL requests to fail. These were the cURL options I used: curl_setopt_array&#40;$request, array &#40; &#160; &#160; CURLOPT_URL =&#62; 'https://www.sandbox.paypal.com/cgi-bin/webscr', &#160; &#160; CURLOPT_POST &#8230; <a href="http://www.geekality.net/2010/12/07/php-curl-ssl-verification/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While trying to figure out of <a href="/?p=1210">PayPal Payment Data Transfers</a> I came over an issue that caused my <a href="http://php.net/manual/en/book.curl.php">cURL</a> requests to fail.</p>
<p><span id="more-1413"></span></p>
<p>These were the <a href="http://php.net/manual/en/function.curl-setopt.php">cURL options</a> I used:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">curl_setopt_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; CURLOPT_URL <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'https://www.sandbox.paypal.com/cgi-bin/webscr'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_POST <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_POSTFIELDS <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'cmd'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_notify-synch'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'tx'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$tx</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'at'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$your_pdt_identity_token</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_HEADER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>It worked great on my web host, but on my local machine I could not get any response. I just got an error saying something about the certificate failing to be verified or something like that (don&#8217;t remember the exact message). After some research I found out the problem.</p>
<p>Since we are using the <a href="http://en.wikipedia.org/wiki/Https">HTTPS</a> protocol here we need to somehow verify the one we are talking to. Your browser does this by checking their certificate against several built-in certificates from trusted certificate suppliers, like for example Verisign. cURL does this verification automatically as well, except on my local machine it didn&#8217;t have any certificates to check against! Not sure why&#8230; but it didn&#8217;t. So, I had to find a collection of certificates on my own and tell cURL to use that instead of the non-existing one.</p>
<p>I found a collection like that at <a href="http://curl.haxx.se/docs/caextract.html">curl.haxx.se/docs/caextract.html</a>, a so called CA bundle. Think that&#8217;s the one used by Mozilla based browsers, like FireFox, or something like that. Just download that file and stick it somewhere your PHP script can access it. Then we need to tell cURL to use it by adding two cURL options.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">curl_setopt_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; CURLOPT_URL <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'https://www.sandbox.paypal.com/cgi-bin/webscr'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_POST <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_POSTFIELDS <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'cmd'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_notify-synch'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'tx'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$transaction_id</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'at'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pdt_identity_token</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_HEADER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; CURLOPT_SSL_VERIFYPEER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Verify peers</span><br />
&nbsp; &nbsp; CURLOPT_CAINFO <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cacert.pem'</span> <span style="color: #666666; font-style: italic;">// Path to file with certificates</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Voila!</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/12/07/php-curl-ssl-verification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: How to send emails</title>
		<link>http://www.geekality.net/2010/05/16/c-how-to-send-emails/</link>
		<comments>http://www.geekality.net/2010/05/16/c-how-to-send-emails/#comments</comments>
		<pubDate>Sun, 16 May 2010 00:21:02 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=991</guid>
		<description><![CDATA[Sending a basic email message in a C# application is quite easy thanks to a class called SmptClient. We simply need an address to send to, an address to send from, the message we want to send and the address &#8230; <a href="http://www.geekality.net/2010/05/16/c-how-to-send-emails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sending a basic email message in a C# application is quite easy thanks to a class called <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx">SmptClient</a>. We simply need an address to send to, an address to send from, the message we want to send and the address of an SMTP server, hand it all to the SMTP client, and you&#8217;re done:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var <span style="color: #0600FF; font-weight: bold;">from</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;me@example.com&quot;</span>, <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
var to <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;you@example.com&quot;</span>, <span style="color: #666666;">&quot;You&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span>, to<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Subject <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Greetings!&quot;</span>,<br />
&nbsp; &nbsp; Body <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;How are you doing today?&quot;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>SmtpException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>That was pretty simple, wasn&#8217;t it? But what if we need to authenticate with our server? And what if we want to send our message in a more secure manner?</p>
<p><span id="more-991"></span></p>
<h2>Authentication</h2>
<p>You will quite often find that an <a href="http://en.wikipedia.org/wiki/Smtp_server">SMTP server</a> requires you to authenticate yourself before it will let you do anything. This is however very simple to do. All we need, is to provide our credentials to the client:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></div></div>
<p>Now, when you try to send your message like before, it should be sent without problems <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' />  But what if you want to send your email securely?</p>
<h2>Using SSL to encrypt the connection</h2>
<p>To send emails through an encrypted connection, all you have to do is to set the <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx">EnableSSL</a> property of the SmtpClient to <code class="codecolorer text default"><span class="text">true</span></code>:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; EnableSSL <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></div></div>
<p>When you now try to send your email, it will use the <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">Secure Socket Layer</a> to encrypt the connection. Like me, you might run into a problem though; an <a href="http://msdn.microsoft.com/en-us/library/system.security.authentication.authenticationexception.aspx">AuthenticationException</a> with the following message: <cite>The remote certificate is invalid according to the validation procedure.</cite></p>
<h2>Imperfect certificates</h2>
<p>After reading about <a href="http://www.verisign.com/ssl/ssl-information-center/how-ssl-security-works/">how these certificates work</a> and some more digging around I found out that the certificate of the SMTP server I use is considered invalid for two reasons: Firstly it was not issued by a trusted Certification Authority (like VeriSign or Thawte), but by my host (<a href="http://www.dreamhost.com/r.cgi?322827">Dreamhost</a> <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=')' class='wp-smiley smiley-20' /> themselves. And finally there is a mismatch between the domain I&#8217;m connecting to (smtp.example.com) and the domain the certificate was issued to (mail.dreamhost.com).</p>
<h2>Manual certificate validation</h2>
<p>SSL has two purposes: security and authentication. And the security will actually work fine and all the traffic will be encrypted even if the authentication fails. In other words, as long as we can look at the certificate ourselves and be sure that <em>we</em> thrust it, we can go ahead and continue with our work.</p>
<p>To check the certificate ourselves we can provide a <a href="http://msdn.microsoft.com/en-us/library/system.net.security.remotecertificatevalidationcallback.aspx">RemoteCertificate&shy;ValidationCallback</a> delegate to a class called the <a href="http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.aspx">ServicePoint&shy;Manager</a>.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span><br />
&nbsp; &nbsp; <span style="color: #008000;">=</span> OurCertificateValidation<span style="color: #008000;">;</span></div></div>
<p>But how can we decide if we should trust the certificate or not? Well, I have found three ways that works nicely. Well, actually just two since the first is just to cover your eyes and let anything pass&#8230;</p>
<h3>Pretend validation</h3>
<p>The following implementation is a way to simply say that <cite>I don&#8217;t care</cite>:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>This is of course not very good&#8230; we should at least make <em>some</em> effort and check if the certificate is the one we should be getting. We can for example&#8230;</p>
<h3>Check the certificate fingerprint</h3>
<p>Certificates have something called a fingerprint and if we know the fingerprint of the certificate we <em>should</em> get, then we can compare it with the fingerprint of the one we actually got.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCertHashString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;6B8C79AB966D70277BA86E6F820859A2B5B8CCC0&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// SHA-1 fingerprint</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>If you don&#8217;t think this is enough, you can for example&#8230;</p>
<h3>Check the whole certificate</h3>
<p>If you can export the certificate to a file somehow, you can then later compare the certificate you get with the one you have stored and make sure they are equal.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var actualCertificate <span style="color: #008000;">=</span> X509Certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromCertFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>actualCertificate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I got a hold of the certificate file by sticking the following snippet inside my validation delegate, ran my code once, and then changed it back to normal again:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var file <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var cert <span style="color: #008000;">=</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Export</span><span style="color: #008000;">&#40;</span>X509ContentType<span style="color: #008000;">.</span><span style="color: #0000FF;">Cert</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; file<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>cert, <span style="color: #FF0000;">0</span>, cert<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I also successfully exported it from Opera, which I use as an email client, by going to Preferences, Security, Manage Certificates, Approved and then export the one I want. This of course assumes you have have previously approved the certificate for use with Opera.</p>
<h2>Wrap-up</h2>
<p>That&#8217;s all for now! Found it kind of fun to get this working and thought I could share it in case someone else struggles with this. Please comment and let me know if I have misunderstood something or done a big blunder or something like that! I mostly just know that my code is working, so if you know more about how these security issues work and you know something you think i should know as well: Please share! I&#8217;d like to learn <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' class='wp-smiley smiley-1' /> </p>
<p>Here is my final, complete code:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Mail</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Authentication</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Cryptography.X509Certificates</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Security</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> Geekality<span style="color: #008000;">.</span><span style="color: #0000FF;">SecureEmail</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">class</span> Program<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var actualCertificate <span style="color: #008000;">=</span> X509Certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromCertFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.com.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>actualCertificate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Register our own certificate validation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">=</span> OurCertificateValidation<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var <span style="color: #0600FF; font-weight: bold;">from</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;me@example.com&quot;</span>, <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var to <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;you@example.com&quot;</span>, <span style="color: #666666;">&quot;Myself&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span>, to<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Subject <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Greetings!&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Body <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;How are you doing today?&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Create client</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnableSsl <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Try to send</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message sent!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>AuthenticationException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>SmtpException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/05/16/c-how-to-send-emails/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

