<?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>California Dreams &#187; m2crypto</title>
	<atom:link href="http://www.heikkitoivonen.net/blog/tag/m2crypto/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.heikkitoivonen.net/blog</link>
	<description>A Finn in Silicon Valley - Adventures in Technology</description>
	<lastBuildDate>Wed, 09 Feb 2011 05:57:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>M2Crypto Supports OpenSSL 1.0.x</title>
		<link>http://www.heikkitoivonen.net/blog/2011/01/16/m2crypto-supports-openssl-1-0-x/</link>
		<comments>http://www.heikkitoivonen.net/blog/2011/01/16/m2crypto-supports-openssl-1-0-x/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 05:32:40 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=1149</guid>
		<description><![CDATA[I was supposed to release new M2Crypto version in the summer of 2010 but &#8220;real life&#8221; got in the way, and this extended all the way until this week. I finally decided that I&#8217;d better push out a new release even though there was just one significant change: OpenSSL 1.0.x support. However, I felt this [...]]]></description>
			<content:encoded><![CDATA[<p>I was supposed to release new <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> version in the summer of 2010 but &#8220;real life&#8221; got in the way, and this extended all the way until this week. I finally decided that I&#8217;d better push out a new release even though there was just one significant change: <a href="http://www.openssl.org/">OpenSSL</a> 1.0.x support. However, I felt this was really important since OpenSSL 1.0.x has been out for almost a year now, and it is starting to get difficult to deal with software that does not work with pre-1.0.x.</p>
<p>Unfortunately I made a mistake in my first release to <a href="http://pypi.python.org/pypi/M2Crypto">PyPI</a>: I used the <code>setup.py</code> commands to build, sign and upload a source distribution, but I did this from a tree I had <code>svn export</code>ed. Due to the way the M2Crypto <code>setup.py</code> was constructed this meant that the tarball was lacking vital files. Yesterday I did a new 0.21.1 release from the Subversion checkout, so the tarball now includes everything.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2011/01/16/m2crypto-supports-openssl-1-0-x/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>SSL in Python 2.7</title>
		<link>http://www.heikkitoivonen.net/blog/2010/08/23/ssl-in-python-2-7/</link>
		<comments>http://www.heikkitoivonen.net/blog/2010/08/23/ssl-in-python-2-7/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 04:20:24 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=1109</guid>
		<description><![CDATA[It has been almost two years since I wrote about the state of SSL in Python 2.6. If you haven&#8217;t read that yet, I suggest you read that first and then continue here, since I will mostly just be talking about things that have changed since then, or things that I have discovered since then. [...]]]></description>
			<content:encoded><![CDATA[<p>It has been almost two years since I wrote about the state of <a href="http://www.heikkitoivonen.net/blog/2008/10/14/ssl-in-python-26/">SSL in Python 2.6</a>. If you haven&#8217;t read that yet, I suggest you read that first and then continue here, since I will mostly just be talking about things that have changed since then, or things that I have discovered since then.</p>
<p>The good news is that things have improved in the stdlib <a href="http://docs.python.org/library/ssl.html">ssl</a> module. The bad news is that it is still missing some critical pieces to make SSL secure. </p>
<p>Python 2.7 enables you to specify ciphers to use explicitly, rather than just relying on what comes default with the SSL version selection. Additionally, if you compile the <code>ssl</code> module with OpenSSL 1.0 and later, using <code>ssl.PROTOCOL_SSLv23</code> is safe (as in, it will not pick the insecure SSLv2 protocol) as long as you don&#8217;t enable SSLv2 specific ciphers (see the ssl module documentation for details).</p>
<h3>Servers</h3>
<p>With that out of the way, there isn&#8217;t really much difference to how you would write a simple SSL server with Python 2.7 compared to what I wrote in 2008. If you know your <code>ssl</code> module was compiled with OpenSSL 1.0 you can pick <code>ssl.PROTOCOL_SSLv23</code> for maximum compatibility. Otherwise you should stick with an explicit version other than v2.</p>
<p>The documentation for the ssl module in 2.7 has improved a lot, and includes good sample code for servers <a href="http://docs.python.org/library/ssl.html#server-side-operation">here</a>.</p>
<p>The <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> code hasn&#8217;t changed. The next M2Crypto release will add support for 0penSSL 1.0.</p>
<h3>Clients</h3>
<p>Checking the peer certificate&#8217;s hostname is still the weak point of the <code>ssl</code> module. The SSL version selection situation has improved slightly like I explained above. Othewise follow the example I wrote in 2008.</p>
<p>Again, the M2Crypto API hasn&#8217;t changed.</p>
<p>Lately I have been working with <a href="http://pycurl.sourceforge.net/">pycurl</a> at <a href="http://egnyte.com/">Egnyte</a>, so I decided to give a client example using that module.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> pycurl
&nbsp;
c = pycurl.<span style="color: black;">Curl</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
c.<span style="color: black;">setopt</span><span style="color: black;">&#40;</span>pycurl.<span style="color: black;">URL</span>, <span style="color: #483d8b;">'https://www.google.com'</span><span style="color: black;">&#41;</span>
c.<span style="color: black;">setopt</span><span style="color: black;">&#40;</span>pycurl.<span style="color: black;">HTTPGET</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
c.<span style="color: black;">setopt</span><span style="color: black;">&#40;</span>pycurl.<span style="color: black;">SSL_VERIFYPEER</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
c.<span style="color: black;">setopt</span><span style="color: black;">&#40;</span>pycurl.<span style="color: black;">CAINFO</span>, <span style="color: #483d8b;">'ca.pem'</span><span style="color: black;">&#41;</span>
c.<span style="color: black;">setopt</span><span style="color: black;">&#40;</span>pycurl.<span style="color: black;">SSL_VERIFYHOST</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    c.<span style="color: black;">perform</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">finally</span>:
    c.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>I am not a big fan of pycurl due to difficulties getting it compiled and the non-Pythonic API. But it is based on the very powerful curl library, so it comes full featured out of the box.</p>
<h3>Other Resources</h3>
<p>Besides the Python crypto libraries capable of doing SSL that I mentioned in my <a href="http://www.heikkitoivonen.net/blog/2008/10/14/ssl-in-python-26/">SSL in Python 2.6</a> article, I have found pycurl. Another find in the Python crypto front is <a href="http://www.cryptlib.com/">cryptlib</a>. </p>
<p><a href="http://www.mikeivanov.com/">Mike Ivanov</a> wrote a great series about crypto in Python: <a href="http://www.activestate.com/blog/2010/03/python-crypto-state-art-part-2">part 2</a>, <a href="http://www.activestate.com/blog/2010/05/python-crypto-state-art-part-3">part 3</a> (link to part 1 seems to have rotted). Mike also produced a <a href="http://mikeivanov.com/pc/python-crypto.pdf">comparison of different Python crypto libraries</a> (PDF).</p>
<p>The future is also looking bright for the <code>ssl</code> module. Already the upcoming <a href="http://docs.python.org/dev/library/ssl.html">Python 3.2 ssl module</a> will include support for <code>SSLContext</code>s so that you can set options for multiple SSL connections at once, allows you to selectively disable SSL versions, and it allows you to check the OpenSSL version as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2010/08/23/ssl-in-python-2-7/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pulling Android Market Sales Data Programmatically</title>
		<link>http://www.heikkitoivonen.net/blog/2010/02/08/pulling-android-market-sales-data-programmatically/</link>
		<comments>http://www.heikkitoivonen.net/blog/2010/02/08/pulling-android-market-sales-data-programmatically/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 04:48:51 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=1023</guid>
		<description><![CDATA[Android Market handles sales through Google Checkout. I haven&#8217;t tried selling anything else online before, but what this setup provides for me as the seller leaves a lot to be desired. One issue you will have trouble with is getting the data needed to file taxes. Google provides a Google Checkout Notification History API that [...]]]></description>
			<content:encoded><![CDATA[<p>Android Market handles sales through Google Checkout. I haven&#8217;t tried selling anything else online before, but what this setup provides for me as the seller leaves a lot to be desired. One issue you will have trouble with is getting the data needed to file taxes.</p>
<p>Google provides a <a href="http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Notification_History_API.html">Google Checkout Notification History API</a> that lets you programmatically query sales data. For my purposes the API requests are really simple: just post a small XML document with the date range I am interested in, get back XML documents that contain my data. If there is more data that fits in a single response, look for an element that specifies the token for the next page and keep pulling until you get all data.</p>
<p>Below is a really simple Python script that uses <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> to handle the SSL parts for the connection (needed since <a href="http://www.heikkitoivonen.net/blog/2008/10/14/ssl-in-python-26/">Python doesn&#8217;t do secure SSL out of the box</a>). You will also need to <a href="http://www.heikkitoivonen.net/blog/2008/09/30/root-certificates-for-python-programs-using-python/">grab certificates</a>. You should save the script as <code>gnotif.py</code>, save the certificates as <code>cacert.pem</code> and create <code>gnotif.ini</code> as described in the script below all in the same directory. When you execute it, it will ask for start and end date (in <code>YYYY-MM-DD</code> format) and then fetch all the data, saving them in response-N.xml files, where N is a number.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Script to query Google Checkout Notification History</span>
<span style="color: #808080; font-style: italic;"># http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Notification_History_API.html</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Supporting file gnotif.ini:</span>
<span style="color: #808080; font-style: italic;">#[gnotif]</span>
<span style="color: #808080; font-style: italic;"># merchant_id = YOUR_MERCHANT_ID_HERE</span>
<span style="color: #808080; font-style: italic;"># merchant_key = YOUR_MERCHANT_KEY_HERE</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">base64</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">ConfigParser</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">ConfigParser</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> M2Crypto <span style="color: #ff7700;font-weight:bold;">import</span> SSL, httpslib
&nbsp;
ENVIRONMENT = <span style="color: #483d8b;">&quot;https://checkout.google.com/api/checkout/v2/reports/Merchant/&quot;</span>
XML = <span style="color: #483d8b;">&quot;&quot;&quot;<span style="color: #000099; font-weight: bold;">\</span>
&lt;notification-history-request xmlns=&quot;http://checkout.google.com/schema/2&quot;&gt;
%(query)s
&lt;/notification-history-request&gt;
&quot;&quot;&quot;</span>
&nbsp;
config = <span style="color: #dc143c;">ConfigParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
config.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gnotif.ini'</span><span style="color: black;">&#41;</span>
MERCHANT_ID = config.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gnotif'</span>, <span style="color: #483d8b;">'merchant_id'</span><span style="color: black;">&#41;</span>
MERCHANT_KEY = config.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gnotif'</span>, <span style="color: #483d8b;">'merchant_key'</span><span style="color: black;">&#41;</span>
&nbsp;
rawstr = r<span style="color: #483d8b;">&quot;&quot;&quot;&lt;next-page-token&gt;(.*)&lt;/next-page-token&gt;&quot;&quot;&quot;</span>
compile_obj = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>rawstr, <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span><span style="color: black;">&#41;</span>
&nbsp;
auth = <span style="color: #dc143c;">base64</span>.<span style="color: black;">encodestring</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s:%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>MERCHANT_ID, MERCHANT_KEY<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
ctx = SSL.<span style="color: black;">Context</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sslv3'</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># If you comment out the next 2 lines, the connection won't be secure</span>
ctx.<span style="color: black;">set_verify</span><span style="color: black;">&#40;</span>SSL.<span style="color: black;">verify_peer</span> | SSL.<span style="color: black;">verify_fail_if_no_peer_cert</span>, depth=<span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> ctx.<span style="color: black;">load_verify_locations</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cacert.pem'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'No CA certs'</span><span style="color: black;">&#41;</span>
&nbsp;
start = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Start date: '</span><span style="color: black;">&#41;</span>
end = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'End date: '</span><span style="color: black;">&#41;</span>
&nbsp;
data = XML <span style="color: #66cc66;">%</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'query'</span>: <span style="color: #483d8b;">&quot;&quot;&quot;&lt;start-time&gt;%(start)s&lt;/start-time&gt;
&lt;end-time&gt;%(end)s&lt;/end-time&gt;&quot;&quot;&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'start'</span>: start, <span style="color: #483d8b;">'end'</span>: end<span style="color: black;">&#125;</span><span style="color: black;">&#125;</span>
&nbsp;
i = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    c = httpslib.<span style="color: black;">HTTPSConnection</span><span style="color: black;">&#40;</span>host=<span style="color: #483d8b;">'checkout.google.com'</span>, port=<span style="color: #ff4500;">443</span>, ssl_context=ctx<span style="color: black;">&#41;</span>
    c.<span style="color: black;">request</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'POST'</span>, ENVIRONMENT + MERCHANT_ID, data,
             <span style="color: black;">&#123;</span><span style="color: #483d8b;">'content-type'</span>: <span style="color: #483d8b;">'application/xml; charset=UTF-8'</span>,
              <span style="color: #483d8b;">'accept'</span>: <span style="color: #483d8b;">'application/xml; charset=UTF-8'</span>,
              <span style="color: #483d8b;">'authorization'</span>: <span style="color: #483d8b;">'Basic '</span> + auth<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
&nbsp;
    r = c.<span style="color: black;">getresponse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'response-%d.xml'</span> <span style="color: #66cc66;">%</span> i, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    result = r.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span>
    f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> i, r.<span style="color: black;">status</span>
&nbsp;
    c.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    match_obj = compile_obj.<span style="color: black;">search</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> match_obj:
        i += <span style="color: #ff4500;">1</span>
        data = XML <span style="color: #66cc66;">%</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'query'</span>: <span style="color: #483d8b;">&quot;&quot;&quot;&lt;next-page-token&gt;%s&lt;/next-page-token&gt;&quot;&quot;&quot;</span> <span style="color: #66cc66;">%</span> match_obj.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">break</span></pre></div></div>

<p>As you take a look at the data you will probably notice that you are only getting the sale price information, but no information about the fees that Google is deducting. Officially it is a flat 30%, but I have found out a number of my sales have the fee as 5%. So we need to get this information somehow. Luckily you can <a href="http://googlecheckout.blogspot.com/2009/09/transaction-fee-information-now.html">toggle a checkbox in your Google Checkout Merchant Settings</a>. Unfortunately there is a bug, and the transaction fee shows as $0 for Android Market sales. I have reported this to Google, and they acknowledged it, but there is no ETA on when this will be fixed.</p>
<p>I also haven&#8217;t found any way to programmatically query when and how much did Google Checkout actually pay me. (I can get this info from my bank, but it would be nice to query for that with the Checkout API as well.)</p>
<p>Last but certainly not least, working with the monster XML files returned from Google Checkout API is a real pain. If someone has a script to turn those into a format that could be imported into a spreadsheet or database that would be nice&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2010/02/08/pulling-android-market-sales-data-programmatically/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using M2Crypto with boto &#8211; Secure Access to Amazon Web Services</title>
		<link>http://www.heikkitoivonen.net/blog/2009/10/12/using-m2crypto-with-boto-secure-access-to-amazon-web-services/</link>
		<comments>http://www.heikkitoivonen.net/blog/2009/10/12/using-m2crypto-with-boto-secure-access-to-amazon-web-services/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 01:35:03 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=936</guid>
		<description><![CDATA[Many companies run services in the Amazon cloud infrastructure, so it makes an attractive target for criminals as well. You need to make sure that you really are talking to the right Amazon servers when you use the cloud services. boto seems to have emerged as the winner in the scramble to develop Python libraries [...]]]></description>
			<content:encoded><![CDATA[<p>Many companies run services in the Amazon cloud infrastructure, so it makes an attractive target for criminals as well. You need to make sure that you really are talking to the right Amazon servers when you use the cloud services.</p>
<p><a href="http://code.google.com/p/boto/">boto</a> seems to have emerged as the winner in the scramble to develop Python libraries to deal with <a href="http://aws.amazon.com/">Amazon Web Services</a> (AWS). By default, boto will use the stdlib httplib.HTTPSConnection. This is a problem, because the <a href="http://www.heikkitoivonen.net/blog/2008/10/14/ssl-in-python-26/">stdlib does not provide secure SSL out of the box</a>. However, boto designers have made it easy to plug in alternative SSL implementations that conform to the httplib.HTTPSConnection interface. <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> provides this in <a href="http://www.heikkitoivonen.net/m2crypto/api/M2Crypto.httpslib.HTTPSConnection-class.html">httpslib.HTTPSConnection</a>.</p>
<p>The first step is to <a href="http://www.heikkitoivonen.net/blog/2008/09/30/root-certificates-for-python-programs-using-python/">get CA certificates</a> that we can use to verify that the Amazon servers we will be talking to have valid certificates issued by trusted certificate authorities.</p>
<p>Amazon offers various services that boto provides access to, so the exact details vary a little bit (namely what connection class to instantiate). I&#8217;ll use the SimpleDB as an example, because the first 25 machine hours per month are free so it makes a great test system (you still need to sign up for AWS and provide credit card information).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> M2Crypto <span style="color: #ff7700;font-weight:bold;">import</span> httpslib, SSL
<span style="color: #ff7700;font-weight:bold;">from</span> boto.<span style="color: black;">sdb</span>.<span style="color: black;">connection</span> <span style="color: #ff7700;font-weight:bold;">import</span> SDBConnection
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> https_connection_factory<span style="color: black;">&#40;</span>host, port=<span style="color: #008000;">None</span>, strict=<span style="color: #ff4500;">0</span>, <span style="color: #66cc66;">**</span>ssl<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;HTTPS connection factory that creates secure connections
    using M2Crypto.&quot;&quot;&quot;</span>
    ctx = SSL.<span style="color: black;">Context</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'tlsv1'</span><span style="color: black;">&#41;</span>
    ctx.<span style="color: black;">set_verify</span><span style="color: black;">&#40;</span>SSL.<span style="color: black;">verify_peer</span> | SSL.<span style="color: black;">verify_fail_if_no_peer_cert</span>, depth=<span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> ctx.<span style="color: black;">load_verify_locations</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cacert.pem'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'No CA certs'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> httpslib.<span style="color: black;">HTTPSConnection</span><span style="color: black;">&#40;</span>host, port=port, strict=strict,
                                    ssl_context=ctx<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> create_connection<span style="color: black;">&#40;</span>aws_access_key_id, aws_secret_access_key<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Create SimpleDB connection.&quot;&quot;&quot;</span>
    conn = SDBConnection<span style="color: black;">&#40;</span>aws_access_key_id=aws_access_key_id,
                         aws_secret_access_key=aws_secret_access_key,
                         https_connection_factory=<span style="color: black;">&#40;</span>https_connection_factory, <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> conn
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #808080; font-style: italic;"># Sample usage</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">3</span>:
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Usage: %s aws_access_key_id aws_secret_access_key'</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    conn = create_connection<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    domain = conn.<span style="color: black;">create_domain</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'mytest'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        item, key, value = <span style="color: #483d8b;">'item1'</span>, <span style="color: #483d8b;">'key1'</span>, <span style="color: #483d8b;">'value1'</span>    
        domain.<span style="color: black;">put_attributes</span><span style="color: black;">&#40;</span>item, <span style="color: black;">&#123;</span>key: value<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">assert</span> value == domain.<span style="color: black;">get_attributes</span><span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        conn.<span style="color: black;">delete_domain</span><span style="color: black;">&#40;</span>domain<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Usage:'</span>, conn.<span style="color: black;">get_usage</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The sample application takes your AWS access key and secret access key as parameters, and it assumes <code>cacert.pem</code> file containing the CA certificates is in the same directory. Typically running that application shows that it uses less than 0.006 <s>seconds</s>hours of Amazon computing facilities so you could run this application over <s>15 million</s>4500 times a month without charge.</p>
<p><strong>Update:</strong>I mixed up units, which Mocky pointed out; fixed above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2009/10/12/using-m2crypto-with-boto-secure-access-to-amazon-web-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>M2Crypto 0.20.2 for Ancient OpenSSL</title>
		<link>http://www.heikkitoivonen.net/blog/2009/10/06/m2crypto-0-20-2-for-ancient-openssl/</link>
		<comments>http://www.heikkitoivonen.net/blog/2009/10/06/m2crypto-0-20-2-for-ancient-openssl/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 05:40:20 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=933</guid>
		<description><![CDATA[M2Crypto has been claiming support for OpenSSL 0.9.7 but it actually turned out I wasn&#8217;t testing with quite that old OpenSSL version. Recently M2Crypto got support for RSA PSS stuff, but it turns out this was added in OpenSSL 0.9.7h, and you could not build/run M2Crypto against an older OpenSSL version. Arguably you should not [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> has been claiming support for OpenSSL 0.9.7 but it actually turned out I wasn&#8217;t testing with quite that old OpenSSL version. Recently M2Crypto got support for RSA PSS stuff, but it turns out this was added in OpenSSL 0.9.7h, and you could not build/run M2Crypto against an older OpenSSL version. Arguably you should not use those old OpenSSL versions, but apparently there are people who can&#8217;t help it. And since M2Crypto claims support all the way back to 0.9.7 it made sense to make it so.</p>
<p>The M2Crypto trunk and 0.20.2 now omit the RSA PSS stuff if you have too old OpenSSL. Additionally, to prevent this kind of error from happening in the future, I added &#8220;minreq&#8221; (for Minimum Requirements) <a href="http://tinderbox.heikkitoivonen.net/M2Crypto/status.html">Tinderbox</a> client that builds and tests M2Crypto trunk using Python 2.3, OpenSSL 0.9.7 and SWIG 1.3.28 (the current minimum requirements) on Ubuntu 8.04.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2009/10/06/m2crypto-0-20-2-for-ancient-openssl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>M2Crypto 0.20.1 Fixes Regression in httpslib.ProxyHTTPSConnection</title>
		<link>http://www.heikkitoivonen.net/blog/2009/08/27/m2crypto-0201-fixes-regression-in-httpslibproxyhttpsconnection/</link>
		<comments>http://www.heikkitoivonen.net/blog/2009/08/27/m2crypto-0201-fixes-regression-in-httpslibproxyhttpsconnection/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 23:38:50 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=896</guid>
		<description><![CDATA[Miloslav Trmac noticed a regression in httpslib.ProxyHTTPSConnection and provided a fix for it, and I&#8217;ve just tagged and uploaded the new 0.20.1 version to PyPI. See the 0.20 release announcement for general information about the 0.20 release series. I&#8217;ve also been getting a few requests for help on building M2Crypto on Fedora Core -based systems, [...]]]></description>
			<content:encoded><![CDATA[<p>Miloslav Trmac noticed a regression in httpslib.ProxyHTTPSConnection and provided a fix for it, and I&#8217;ve just tagged and uploaded the new 0.20.1 version to <a href="http://pypi.python.org/pypi/M2Crypto/">PyPI</a>. See the <a href="http://www.heikkitoivonen.net/blog/2009/08/10/m2crypto-020/">0.20 release announcement</a> for general information about the 0.20 release series.</p>
<p>I&#8217;ve also been getting a few requests for help on building <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> on Fedora Core -based systems, so I&#8217;ve added some info to the <a href="http://chandlerproject.org/Projects/MeTooCrypto#FAQ">FAQ</a>. Basically there is a <code>fedora_setup.sh</code> wrapper script in the source tarball that you can use instead of the plain <code>setup.py</code>. There are more <a href="http://www.heikkitoivonen.net/blog/2009/02/09/m2crypto-build-wrapper-for-fedora-core-based-distributions/">details about  the fedora_setup.sh script</a>, if you are interested.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2009/08/27/m2crypto-0201-fixes-regression-in-httpslibproxyhttpsconnection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>M2Crypto 0.20</title>
		<link>http://www.heikkitoivonen.net/blog/2009/08/10/m2crypto-020/</link>
		<comments>http://www.heikkitoivonen.net/blog/2009/08/10/m2crypto-020/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 05:20:34 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=892</guid>
		<description><![CDATA[I am pleased to announce M2Crypto 0.20, which has been in development for over nine months. It fixes over 30 bugs, and fixes and new functionality was contributed by more than ten people. Hooah! The CHANGES file lists the full list of changes, but personally I am most pleased by M2Crypto having reached the magical [...]]]></description>
			<content:encoded><![CDATA[<p>I am pleased to announce <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> 0.20, which has been in development for over nine months. It fixes over 30 bugs, and fixes and new functionality was contributed by more than ten people. Hooah!</p>
<p>The <a href="http://svn.osafoundation.org/m2crypto/tags/0.20/CHANGES">CHANGES</a> file lists the full list of changes, but personally I am most pleased by M2Crypto having reached the magical 80% unit test (code) coverage. Besides that technicality, there are Python 2.6 fixes, threading fixes, added support for RSASSA-PSS signing and verifying, certificates with large serial numbers, and more.</p>
<p><a href="http://pypi.python.org/packages/source/M/M2Crypto/M2Crypto-0.20.tar.gz#md5=43a4d958a6c892a37ca02ffcc650b66b">Download</a> from pypi.</p>
<p>Or use easy_install (may not work on all systems): easy_install M2Crypto</p>
<blockquote><p>M2Crypto is the most complete Python wrapper for OpenSSL featuring RSA, DSA, DH, HMACs, message digests, symmetric ciphers (including AES); SSL functionality to implement clients and servers; HTTPS extensions to Python&#8217;s httplib, urllib, and xmlrpclib; unforgeable HMAC&#8217;ing AuthCookies for web session management; FTP/TLS client and server; S/MIME; ZServerSSL: A HTTPS server for Zope and ZSmime: An S/MIME messenger for Zope. M2Crypto can also be used to provide SSL for Twisted. Smartcards supported through the Engine API.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2009/08/10/m2crypto-020/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>M2Crypto 0.20 Beta Cycle Begins</title>
		<link>http://www.heikkitoivonen.net/blog/2009/07/27/m2crypto-020-beta-cycle-begins/</link>
		<comments>http://www.heikkitoivonen.net/blog/2009/07/27/m2crypto-020-beta-cycle-begins/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 07:13:27 +0000</pubDate>
		<dc:creator>Heikki Toivonen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[m2crypto]]></category>

		<guid isPermaLink="false">http://www.heikkitoivonen.net/blog/?p=886</guid>
		<description><![CDATA[Better late than never&#8230; I am announcing the first beta of M2Crypto 0.20 release. M2Crypto is the most complete Python wrapper for OpenSSL. The 0.20 release has been in development for about nine months. About 30 bugs and new features have been implemented by more than ten people. Unit tests now cover 80% of the [...]]]></description>
			<content:encoded><![CDATA[<p>Better late than never&#8230; I am announcing the first beta of <a href="http://chandlerproject.org/Projects/MeTooCrypto">M2Crypto</a> 0.20 release. M2Crypto is the most complete Python wrapper for OpenSSL.</p>
<p>The 0.20 release has been in development for about nine months. About 30 bugs and new features have been implemented by more than ten people. Unit tests now cover 80% of the code base. Tinderbox is used to automatically test changes on various flavors of Ubuntu, Fedora Core, Redhat and Cygwin. We could use more Tinderbox clients, so please drop me a line if you have some spare machine cycles available.</p>
<p>The release include some fairly significant changes, including tricky ones in threading and so forth. See the <a href="http://svn.osafoundation.org/m2crypto/tags/0.20beta1/CHANGES">CHANGES</a> file for list of changes. Please test your applications, and go <a href="https://bugzilla.osafoundation.org/enter_bug.cgi?product=M2Crypto">file bugs</a> on any issues you notice. I&#8217;ll wait for feedback for a week, spin the next beta and so forth until there are no more release blockers found within a beta period.</p>
<p>There are a few issues I feel bad about that did not make the first beta. If you can help create fixes for these, I&#8217;d be willing to consider including the fixes in 0.20 if the changes don&#8217;t look too scary. Here is my wish list:</p>
<ul>
<li><a href="https://bugzilla.osafoundation.org/show_bug.cgi?id=12855">Should work with OpenSSL 1.0.0</a></li>
<li><a href="https://bugzilla.osafoundation.org/show_bug.cgi?id=12465">A regression dealing with RSA keys</a></li>
<li><a href="https://bugzilla.osafoundation.org/show_bug.cgi?id=12207">Problems with Unicode URLs</a></li>
<li><a href="https://bugzilla.osafoundation.org/show_bug.cgi?id=12207">verify_cert not implemented</a></li>
<li><a href="https://bugzilla.osafoundation.org/show_bug.cgi?id=11694">CRL bindings</a></li>
</ul>
<p><a href="http://pypi.python.org/packages/source/M/M2Crypto/M2Crypto-0.20beta1.tar.gz#md5=e1551fde39d75ebced9dd73b9a0ace50">Download</a> from pypi.</p>
<p>Or use easy_install (may not work on all systems): easy_install M2Crypto</p>
]]></content:encoded>
			<wfw:commentRss>http://www.heikkitoivonen.net/blog/2009/07/27/m2crypto-020-beta-cycle-begins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

