Merge rick's bio_read_write branch
diff --git a/ChangeLog b/ChangeLog
index 75a9745..32067d8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2009-04-25  Jean-Paul Calderone  <exarkun@twistedmatrix.com>
+
+	* Release 0.9
+
+2009-04-01  Jean-Paul Calderone  <exarkun@twistedmatrix.com>
+            Samuele Pedroni  <pedronis@openend.se>
+
+	* src/util.h: Delete the TLS key before trying to set a new value
+	  for it in case the current thread identifier is a recycled one (if
+	  it is recycled, the key won't be set because there is already a
+	  value from the previous thread to have this identifier and to use
+	  the pyOpenSSL API).
+
+2009-04-01  Jean-Paul Calderone  <exarkun@twistedmatrix.com>
+
+	* src/crypto/crypto.c: Add FILETYPE_TEXT for dumping keys and
+	  certificates and certificate signature requests to a text format.
+
 2008-12-31  Jean-Paul Calderone  <exarkun@twistedmatrix.com>
 
 	* src/crypto/x509ext.c, test/test_crypto.py: Add the get_short_name
diff --git a/doc/html/about.html b/doc/html/about.html
index b783b94..244c51e 100644
--- a/doc/html/about.html
+++ b/doc/html/about.html
@@ -96,7 +96,7 @@
 <b class="navlabel">Previous:</b> <a class="sectref" href="socket-methods.html">4.3 Acessing Socket Methods</A>
 <b class="navlabel">Up:</b> <a class="sectref" HREF="pyOpenSSL.html">Python OpenSSL Manual</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/building-unix.html b/doc/html/building-unix.html
index 3b83ef2..d28728e 100644
--- a/doc/html/building-unix.html
+++ b/doc/html/building-unix.html
@@ -117,7 +117,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="building.html">2 Building and Installing</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="building-windows.html">2.2 Building the Module</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/building-windows.html b/doc/html/building-windows.html
index 79432bd..5b81f9a 100644
--- a/doc/html/building-windows.html
+++ b/doc/html/building-windows.html
@@ -109,7 +109,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="building.html">2 Building and Installing</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl.html">3 OpenSSL  </A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/building.html b/doc/html/building.html
index 810c913..0b5c466 100644
--- a/doc/html/building.html
+++ b/doc/html/building.html
@@ -97,7 +97,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" HREF="pyOpenSSL.html">Python OpenSSL Manual</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="building-unix.html">2.1 Building the Module</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/callbacks.html b/doc/html/callbacks.html
index 2a4b375..49565a3 100644
--- a/doc/html/callbacks.html
+++ b/doc/html/callbacks.html
@@ -113,7 +113,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="internals.html">4 Internals</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="socket-methods.html">4.3 Acessing Socket Methods</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/contents.html b/doc/html/contents.html
index 1349d2b..f773e89 100644
--- a/doc/html/contents.html
+++ b/doc/html/contents.html
@@ -106,7 +106,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" HREF="pyOpenSSL.html">Python OpenSSL Manual</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="intro.html">1 Introduction</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/exceptions.html b/doc/html/exceptions.html
index 2b1ba25..acbc8ce 100644
--- a/doc/html/exceptions.html
+++ b/doc/html/exceptions.html
@@ -89,7 +89,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="internals.html">4 Internals</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="callbacks.html">4.2 Callbacks</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/index.html b/doc/html/index.html
index 965ced7..24c4dfd 100644
--- a/doc/html/index.html
+++ b/doc/html/index.html
@@ -159,7 +159,7 @@
 </tr></table>
 <b class="navlabel">Next:</b> <a class="sectref" href="contents.html">Contents</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/internals.html b/doc/html/internals.html
index 14a5066..142cf33 100644
--- a/doc/html/internals.html
+++ b/doc/html/internals.html
@@ -96,7 +96,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" HREF="pyOpenSSL.html">Python OpenSSL Manual</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="exceptions.html">4.1 Exceptions</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/intro.html b/doc/html/intro.html
index e781927..45d16c0 100644
--- a/doc/html/intro.html
+++ b/doc/html/intro.html
@@ -102,7 +102,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" HREF="pyOpenSSL.html">Python OpenSSL Manual</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="building.html">2 Building and Installing</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-509ext.html b/doc/html/openssl-509ext.html
index cbffb32..50a91c9 100644
--- a/doc/html/openssl-509ext.html
+++ b/doc/html/openssl-509ext.html
@@ -93,7 +93,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-netscape-spki.html">3.1.9 NetscapeSPKI objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-connection.html b/doc/html/openssl-connection.html
index 1cab1df..6ce5ae2 100644
--- a/doc/html/openssl-connection.html
+++ b/doc/html/openssl-connection.html
@@ -302,7 +302,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-ssl.html">3.3 SSL  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="internals.html">4 Internals</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-context.html b/doc/html/openssl-context.html
index 3d155bf..64ff08b 100644
--- a/doc/html/openssl-context.html
+++ b/doc/html/openssl-context.html
@@ -280,7 +280,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-ssl.html">3.3 SSL  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-connection.html">3.3.2 Connection objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-crypto.html b/doc/html/openssl-crypto.html
index 8e01fa0..6a06a8f 100644
--- a/doc/html/openssl-crypto.html
+++ b/doc/html/openssl-crypto.html
@@ -284,7 +284,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl.html">3 OpenSSL  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-x509.html">3.1.1 X509 objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-netscape-spki.html b/doc/html/openssl-netscape-spki.html
index 046d04a..404ee2b 100644
--- a/doc/html/openssl-netscape-spki.html
+++ b/doc/html/openssl-netscape-spki.html
@@ -113,7 +113,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-rand.html">3.2 rand  </A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-pkcs12.html b/doc/html/openssl-pkcs12.html
index 8325d57..a5409d0 100644
--- a/doc/html/openssl-pkcs12.html
+++ b/doc/html/openssl-pkcs12.html
@@ -100,7 +100,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-509ext.html">3.1.8 X509Extension objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-pkcs7.html b/doc/html/openssl-pkcs7.html
index e1d78e8..dff42f1 100644
--- a/doc/html/openssl-pkcs7.html
+++ b/doc/html/openssl-pkcs7.html
@@ -111,7 +111,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-pkcs12.html">3.1.7 PKCS12 objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-pkey.html b/doc/html/openssl-pkey.html
index 0843b90..b504c9b 100644
--- a/doc/html/openssl-pkey.html
+++ b/doc/html/openssl-pkey.html
@@ -100,7 +100,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-pkcs7.html">3.1.6 PKCS7 objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-rand.html b/doc/html/openssl-rand.html
index 8d1498b..fc21189 100644
--- a/doc/html/openssl-rand.html
+++ b/doc/html/openssl-rand.html
@@ -145,7 +145,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl.html">3 OpenSSL  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-ssl.html">3.3 SSL  </A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-ssl.html b/doc/html/openssl-ssl.html
index 053c715..7d12f84 100644
--- a/doc/html/openssl-ssl.html
+++ b/doc/html/openssl-ssl.html
@@ -237,7 +237,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl.html">3 OpenSSL  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-context.html">3.3.1 Context objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-x509.html b/doc/html/openssl-x509.html
index 95f1a21..20af102 100644
--- a/doc/html/openssl-x509.html
+++ b/doc/html/openssl-x509.html
@@ -238,7 +238,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-x509name.html">3.1.2 X509Name objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-x509name.html b/doc/html/openssl-x509name.html
index 7a298fd..21a4f0b 100644
--- a/doc/html/openssl-x509name.html
+++ b/doc/html/openssl-x509name.html
@@ -151,7 +151,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-x509req.html">3.1.3 X509Req objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-x509req.html b/doc/html/openssl-x509req.html
index 2d62c5e..be646b7 100644
--- a/doc/html/openssl-x509req.html
+++ b/doc/html/openssl-x509req.html
@@ -125,7 +125,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-x509store.html">3.1.4 X509Store objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl-x509store.html b/doc/html/openssl-x509store.html
index f42da95..ec6aafe 100644
--- a/doc/html/openssl-x509store.html
+++ b/doc/html/openssl-x509store.html
@@ -87,7 +87,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-pkey.html">3.1.5 PKey objects</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/openssl.html b/doc/html/openssl.html
index 5b8879e..d410112 100644
--- a/doc/html/openssl.html
+++ b/doc/html/openssl.html
@@ -143,7 +143,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" HREF="pyOpenSSL.html">Python OpenSSL Manual</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="openssl-crypto.html">3.1 crypto  </A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/pyOpenSSL.html b/doc/html/pyOpenSSL.html
index 965ced7..24c4dfd 100644
--- a/doc/html/pyOpenSSL.html
+++ b/doc/html/pyOpenSSL.html
@@ -159,7 +159,7 @@
 </tr></table>
 <b class="navlabel">Next:</b> <a class="sectref" href="contents.html">Contents</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/html/socket-methods.html b/doc/html/socket-methods.html
index f416fb7..a63ddd4 100644
--- a/doc/html/socket-methods.html
+++ b/doc/html/socket-methods.html
@@ -111,7 +111,7 @@
 <b class="navlabel">Up:</b> <a class="sectref" href="internals.html">4 Internals</A>
 <b class="navlabel">Next:</b> <a class="sectref" href="about.html">About this document ...</A>
 <hr>
-<span class="release-info">Release 0.8.</span>
+<span class="release-info">Release 0.9.</span>
 </DIV>
 <!--End of Navigation Panel-->
 
diff --git a/doc/pyOpenSSL.ps b/doc/pyOpenSSL.ps
index cfd9199..2c7991b 100644
--- a/doc/pyOpenSSL.ps
+++ b/doc/pyOpenSSL.ps
@@ -1,7 +1,7 @@
 %!PS-Adobe-2.0
 %%Creator: dvips(k) 5.96.1 Copyright 2007 Radical Eye Software
 %%Title: pyOpenSSL.dvi
-%%CreationDate: Sat Mar  7 09:06:10 2009
+%%CreationDate: Sat Apr 25 08:24:21 2009
 %%Pages: 15
 %%PageOrder: Ascend
 %%BoundingBox: 0 0 596 842
@@ -10,7 +10,7 @@
 %DVIPSWebPage: (www.radicaleye.com)
 %DVIPSCommandLine: dvips -N0 -o pyOpenSSL.ps pyOpenSSL
 %DVIPSParameters: dpi=600
-%DVIPSSource:  TeX output 2009.03.07:0906
+%DVIPSSource:  TeX output 2009.04.25:0824
 %%BeginProcSet: tex.pro 0 0
 %!
 /TeXDict 300 dict def TeXDict begin/N{def}def/B{bind def}N/S{exch}N/X{S
@@ -3918,19 +3918,19 @@
 00FE000001FC00FE000001FC00FE000003F800FE000003F800FE000007F000FE000007F0
 00FE00000FE000FE00000FE000FF00001FC0007F00003F80007F00007F80007F8000FF00
 003FC001FE00003FE007FC00003FF81FF800001FFFFFF000000FFFFFE0000007FFFFC000
-0003FFFF00000000FFFC000000001FE00000002C4577C231>48 D<0000001FF000000001
-FFFE00000007FFFF8000001FFFFFC000003FFFFFE00000FFFFFFF00001FFC03FF80003FE
-000FF80007F80007FC000FF00003FC000FE00001FC001FC00001FE003FC00000FE003F80
-0000FE007F000000FE007F000000FE007F000000FE007F000000FE00FE000001FC00FE00
-0001FC00FE000001FC00FE000003F8007E000007F0007F000007F0007F00000FE0003F80
-001FC0003FC0007F80001FE000FF00000FFC07FC000007FFFFF8000001FFFFE00000007F
-FF00000000FFFF80000003FFFFC000000FFFFFF000003FF01FF800007FC007FC0000FF00
-01FE0001FE0001FE0003FC0000FF0007F800007F000FF000007F800FE000007F801FE000
-007F803FC000007F803FC000007F803F8000007F807F8000007F807F8000007F807F8000
-007F807F800000FF00FF000000FF00FF000000FF00FF000001FE00FF000001FE007F8000
-03FC007F800003FC007F800007F8007FC0000FF0003FC0001FF0003FE0003FE0001FF000
-FFC0001FFC07FF80000FFFFFFF000007FFFFFC000003FFFFF8000001FFFFE00000007FFF
-000000000FF80000002F4579C231>56 D<0001FFFFFFF000000003FFFFFFFE00000003FF
+0003FFFF00000000FFFC000000001FE00000002C4577C231>48 D<0000003FC000000001
+FFF800000007FFFC0000001FFFFE0000003FFFFF0000007FFFFF800001FFC0FFC00003FE
+003FC00007FC001FE0000FF8000FE0001FF0000FF0001FE0000FF0003FC00007F0007F80
+0007F0007F000007F000FF000007F800FE000007F801FE000007F801FE000007F803FC00
+0007F803FC000007F803FC000007F807F8000007F807F8000007F807F8000007F807F800
+000FF007F800000FF007F000000FF00FF000000FF00FF000001FF00FF000001FF00FF000
+003FF00FF000003FE00FF000007FE007F800007FE007F80000FFE007F80001FFC007FC00
+03FFC003FC0007FFC003FE000FFFC003FF003FFF8001FFFFFF7F8001FFFFFC7F8000FFFF
+F8FF00007FFFE0FF00003FFF80FE00000FFE01FE0000000001FC0000000003FC00000000
+03F80000000007F80000000007F0000000000FF0000000001FE0000000001FC000000000
+3F80000000007F8000000000FF0000000001FE0000180003FC000038000FF800007E001F
+F000007F80FFE00000FFFFFFC000007FFFFF0000003FFFFE0000001FFFF80000000FFFE0
+00000001FF000000002D4578C231>57 D<0001FFFFFFF000000003FFFFFFFE00000003FF
 FFFFFFC0000003FFFFFFFFF0000003FFFFFFFFF8000007FFFFFFFFFC000007F800007FFE
 000007F800000FFF000007F8000003FF800007F8000000FF80000FF00000007FC0000FF0
 0000007FC0000FF00000003FE0000FF00000003FE0000FF00000003FE0001FE00000001F
@@ -4487,9 +4487,9 @@
 %%EndSetup
 %%Page: 1 1
 TeXDict begin 1 0 bop 0 83 3901 9 v 1890 451 a Ft(Python)64
-b(Op)5 b(enSSL)64 b(Manual)3443 619 y Fs(Release)33 b(0.8)3189
-974 y Fr(Ma)m(rtin)k(Sj\366gren)3383 1328 y Fq(Marc)n(h)27
-b(7,)g(2009)3301 1475 y Fp(ma)n(rtin@strakt.com)1781
+b(Op)5 b(enSSL)64 b(Manual)3443 619 y Fs(Release)33 b(0.9)3189
+974 y Fr(Ma)m(rtin)k(Sj\366gren)3386 1328 y Fq(April)27
+b(25,)g(2009)3301 1475 y Fp(ma)n(rtin@strakt.com)1781
 1697 y Fo(Abstract)208 1841 y Fn(This)32 b(mo)r(dule)f(is)h(a)g(rather)
 g(thin)f(wrapp)r(er)h(around)g(\(a)g(subset)f(of)6 b(\))32
 b(the)g(Op)r(enSSL)e(library)-6 b(.)52 b(With)32 b(thin)f(wrapp)r(er)
diff --git a/doc/pyOpenSSL.tex b/doc/pyOpenSSL.tex
index 604a5d9..d40ce52 100644
--- a/doc/pyOpenSSL.tex
+++ b/doc/pyOpenSSL.tex
@@ -2,7 +2,7 @@
 
 \title{Python OpenSSL Manual}
 
-\release{0.8}
+\release{0.9}
 
 \author{Martin Sjögren}
 \authoraddress{\email{martin@strakt.com}}
diff --git a/doc/pyOpenSSL.txt b/doc/pyOpenSSL.txt
index 3ac1acd..ce0e800 100644
--- a/doc/pyOpenSSL.txt
+++ b/doc/pyOpenSSL.txt
@@ -1031,4 +1031,4 @@
                             Python OpenSSL Manual
      __________________________________________________________________
 
-   Release 0.8.
+   Release 0.9.
diff --git a/src/crypto/crypto.c b/src/crypto/crypto.c
index a2b62c4..257c016 100644
--- a/src/crypto/crypto.c
+++ b/src/crypto/crypto.c
@@ -147,6 +147,7 @@
     pem_password_cb *cb = NULL;
     void *cb_arg = NULL;
     BIO *bio;
+    RSA *rsa;
     crypto_PKeyObj *pkey;
 
     if (!PyArg_ParseTuple(args, "iO!|sO:dump_privatekey", &type,
@@ -199,8 +200,14 @@
             ret = i2d_PrivateKey_bio(bio, pkey->pkey);
             break;
 
+        case X509_FILETYPE_TEXT:
+            rsa = EVP_PKEY_get1_RSA(pkey->pkey);
+            ret = RSA_print(bio, rsa, 0);
+            RSA_free(rsa); 
+            break;
+
         default:
-            PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1");
+            PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT");
             BIO_free(bio);
             return NULL;
     }
@@ -302,8 +309,12 @@
             ret = i2d_X509_bio(bio, cert->x509);
             break;
 
+        case X509_FILETYPE_TEXT:
+            ret = X509_print_ex(bio, cert->x509, 0, 0);
+            break;
+
         default:
-            PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1");
+            PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT");
             BIO_free(bio);
             return NULL;
     }
@@ -405,8 +416,12 @@
             ret = i2d_X509_REQ_bio(bio, req->x509_req);
             break;
 
+        case X509_FILETYPE_TEXT:
+            ret = X509_REQ_print_ex(bio, req->x509_req, 0, 0);
+            break;
+
         default:
-            PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1");
+            PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM, FILETYPE_ASN1, or FILETYPE_TEXT");
             BIO_free(bio);
             return NULL;
     }
@@ -801,6 +816,7 @@
 
     PyModule_AddIntConstant(module, "FILETYPE_PEM",  X509_FILETYPE_PEM);
     PyModule_AddIntConstant(module, "FILETYPE_ASN1", X509_FILETYPE_ASN1);
+    PyModule_AddIntConstant(module, "FILETYPE_TEXT", X509_FILETYPE_TEXT);
 
     PyModule_AddIntConstant(module, "TYPE_RSA", crypto_TYPE_RSA);
     PyModule_AddIntConstant(module, "TYPE_DSA", crypto_TYPE_DSA);
diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h
index 0a71d2a..2322720 100644
--- a/src/crypto/crypto.h
+++ b/src/crypto/crypto.h
@@ -117,4 +117,12 @@
 
 #endif /* crypto_MODULE */
 
+/* Define a new type for emitting text.  Hopefully these don't collide with
+ * future official OpenSSL constants, but the switch statement of
+ * dump_certificate() will alert us if it matters.
+ */
+#ifndef X509_FILETYPE_TEXT 
+#define X509_FILETYPE_TEXT  (58)
+#endif
+
 #endif /* PyOpenSSL_CRYPTO_H_ */
diff --git a/src/util.h b/src/util.h
index b95e75b..1774956 100644
--- a/src/util.h
+++ b/src/util.h
@@ -37,6 +37,7 @@
  * some Python.
  */
 #  define MY_BEGIN_ALLOW_THREADS(ignored)                               \
+    PyThread_delete_key_value(_pyOpenSSL_tstate_key);			\
     PyThread_set_key_value(_pyOpenSSL_tstate_key, PyEval_SaveThread());
 
 /*
diff --git a/test/test_crypto.py b/test/test_crypto.py
index d724abf..214a4b8 100644
--- a/test/test_crypto.py
+++ b/test/test_crypto.py
@@ -4,14 +4,17 @@
 Unit tests for L{OpenSSL.crypto}.
 """
 
-from unittest import TestCase
+from unittest import TestCase, main
+from os import popen2
 
 from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey, PKeyType
 from OpenSSL.crypto import X509, X509Type, X509Name, X509NameType
 from OpenSSL.crypto import X509Req, X509ReqType
 from OpenSSL.crypto import X509Extension, X509ExtensionType
-from OpenSSL.crypto import FILETYPE_PEM, load_certificate, load_privatekey
-from OpenSSL.crypto import dump_privatekey
+from OpenSSL.crypto import load_certificate, load_privatekey
+from OpenSSL.crypto import FILETYPE_PEM, FILETYPE_ASN1, FILETYPE_TEXT
+from OpenSSL.crypto import dump_certificate, load_certificate_request
+from OpenSSL.crypto import dump_certificate_request, dump_privatekey
 
 
 cleartextCertificatePEM = """-----BEGIN CERTIFICATE-----
@@ -51,6 +54,19 @@
 -----END RSA PRIVATE KEY-----
 """
 
+cleartextCertificateRequestPEM = (
+    "-----BEGIN CERTIFICATE REQUEST-----\n"
+    "MIIBnjCCAQcCAQAwXjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQH\n"
+    "EwdDaGljYWdvMRcwFQYDVQQKEw5NeSBDb21wYW55IEx0ZDEXMBUGA1UEAxMORnJl\n"
+    "ZGVyaWNrIERlYW4wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANp6Y17WzKSw\n"
+    "BsUWkXdqg6tnXy8H8hA1msCMWpc+/2KJ4mbv5NyD6UD+/SqagQqulPbF/DFea9nA\n"
+    "E0zhmHJELcM8gUTIlXv/cgDWnmK4xj8YkjVUiCdqKRAKeuzLG1pGmwwF5lGeJpXN\n"
+    "xQn5ecR0UYSOWj6TTGXB9VyUMQzCClcBAgMBAAGgADANBgkqhkiG9w0BAQUFAAOB\n"
+    "gQAAJGuF/R/GGbeC7FbFW+aJgr9ee0Xbl6nlhu7pTe67k+iiKT2dsl2ti68MVTnu\n"
+    "Vrb3HUNqOkiwsJf6kCtq5oPn3QVYzTa76Dt2y3Rtzv6boRSlmlfrgS92GNma8JfR\n"
+    "oICQk3nAudi6zl1Dix3BCv1pUp5KMtGn3MeDEi6QFGy2rA==\n"
+    "-----END CERTIFICATE REQUEST-----\n")
+
 encryptedPrivateKeyPEM = """-----BEGIN RSA PRIVATE KEY-----
 Proc-Type: 4,ENCRYPTED
 DEK-Info: DES-EDE3-CBC,9573604A18579E9E
@@ -711,6 +727,73 @@
         self.assertEqual(loadedKey.bits(), key.bits())
 
 
+    def _runopenssl(self, pem, *args):
+        """
+        Run the command line openssl tool with the given arguments and write
+        the given PEM to its stdin.
+        """
+        write, read = popen2(" ".join(("openssl",) + args))
+        write.write(pem)
+        write.close()
+        return read.read()
+
+
+    def test_dump_certificate(self):
+        """
+        L{dump_certificate} writes PEM, DER, and text.
+        """
+        pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM
+        cert = load_certificate(FILETYPE_PEM, pemData)
+        dumped_pem = dump_certificate(FILETYPE_PEM, cert)
+        self.assertEqual(dumped_pem, cleartextCertificatePEM)
+        dumped_der = dump_certificate(FILETYPE_ASN1, cert)
+        good_der = self._runopenssl(dumped_pem, "x509", "-outform", "DER")
+        self.assertEqual(dumped_der, good_der)
+        cert2 = load_certificate(FILETYPE_ASN1, dumped_der)
+        dumped_pem2 = dump_certificate(FILETYPE_PEM, cert2)
+        self.assertEqual(dumped_pem2, cleartextCertificatePEM)
+        dumped_text = dump_certificate(FILETYPE_TEXT, cert)
+        good_text = self._runopenssl(dumped_pem, "x509", "-noout", "-text")
+        self.assertEqual(dumped_text, good_text)
+
+
+    def test_dump_privatekey(self):
+        """
+        L{dump_privatekey} writes a PEM, DER, and text.
+        """
+        key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
+        dumped_pem = dump_privatekey(FILETYPE_PEM, key)
+        self.assertEqual(dumped_pem, cleartextPrivateKeyPEM)
+        dumped_der = dump_privatekey(FILETYPE_ASN1, key)
+        # XXX This OpenSSL call writes "writing RSA key" to standard out.  Sad.
+        good_der = self._runopenssl(dumped_pem, "rsa", "-outform", "DER")
+        self.assertEqual(dumped_der, good_der)
+        key2 = load_privatekey(FILETYPE_ASN1, dumped_der)
+        dumped_pem2 = dump_privatekey(FILETYPE_PEM, key2)
+        self.assertEqual(dumped_pem2, cleartextPrivateKeyPEM)
+        dumped_text = dump_privatekey(FILETYPE_TEXT, key)
+        good_text = self._runopenssl(dumped_pem, "rsa", "-noout", "-text")
+        self.assertEqual(dumped_text, good_text)
+
+
+    def test_dump_certificate_request(self):
+        """
+        L{dump_certificate_request} writes a PEM, DER, and text.
+        """
+        req = load_certificate_request(FILETYPE_PEM, cleartextCertificateRequestPEM)
+        dumped_pem = dump_certificate_request(FILETYPE_PEM, req)
+        self.assertEqual(dumped_pem, cleartextCertificateRequestPEM)
+        dumped_der = dump_certificate_request(FILETYPE_ASN1, req)
+        good_der = self._runopenssl(dumped_pem, "req", "-outform", "DER")
+        self.assertEqual(dumped_der, good_der)
+        req2 = load_certificate_request(FILETYPE_ASN1, dumped_der)
+        dumped_pem2 = dump_certificate_request(FILETYPE_PEM, req2)
+        self.assertEqual(dumped_pem2, cleartextCertificateRequestPEM)
+        dumped_text = dump_certificate_request(FILETYPE_TEXT, req)
+        good_text = self._runopenssl(dumped_pem, "req", "-noout", "-text")
+        self.assertEqual(dumped_text, good_text)
+
+
     def test_dump_privatekey_passphraseCallback(self):
         """
         L{dump_privatekey} writes an encrypted PEM when given a callback which
@@ -729,3 +812,7 @@
         self.assertTrue(isinstance(loadedKey, PKeyType))
         self.assertEqual(loadedKey.type(), key.type())
         self.assertEqual(loadedKey.bits(), key.bits())
+
+
+if __name__ == '__main__':
+    main()
diff --git a/test/test_ssl.py b/test/test_ssl.py
index 247757a..82dacb3 100644
--- a/test/test_ssl.py
+++ b/test/test_ssl.py
@@ -313,6 +313,8 @@
     else:
         "OP_NO_TICKET unavailable - OpenSSL version may be too old"
 
+
+
 root_cert_pem = """-----BEGIN CERTIFICATE-----
 MIIC7TCCAlagAwIBAgIIPQzE4MbeufQwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UE
 BhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdU
@@ -502,4 +504,3 @@
 
 if __name__ == '__main__':
     main()
-
diff --git a/version.py b/version.py
index c374a66..11e32f1 100644
--- a/version.py
+++ b/version.py
@@ -1,8 +1,8 @@
 # Copyright (C) AB Strakt 2001-2004, All rights reserved
-# Copyright (C) Jean-Paul Calderone 2008, All rights reserved
+# Copyright (C) Jean-Paul Calderone 2008-2009, All rights reserved
 
 """
 pyOpenSSL - A simple wrapper around the OpenSSL library
 """
 
-__version__ = '0.8'
+__version__ = '0.9'