Merge pull request #1206 from reaperhulk/static-windows-builds

switch to static linking on windows and update installation page
diff --git a/cryptography/hazmat/bindings/openssl/binding.py b/cryptography/hazmat/bindings/openssl/binding.py
index 554c3c3..4cd1b89 100644
--- a/cryptography/hazmat/bindings/openssl/binding.py
+++ b/cryptography/hazmat/bindings/openssl/binding.py
@@ -13,6 +13,7 @@
 
 from __future__ import absolute_import, division, print_function
 
+import os
 import sys
 import threading
 
@@ -97,7 +98,8 @@
         if sys.platform != "win32":
             libraries = ["crypto", "ssl"]
         else:  # pragma: no cover
-            libraries = ["libeay32", "ssleay32", "advapi32"]
+            link_type = os.environ.get("PYCA_WINDOWS_LINK_TYPE", "static")
+            libraries = _get_windows_libraries(link_type)
 
         cls.ffi, cls.lib = build_ffi(
             module_prefix=cls._module_prefix,
@@ -154,3 +156,15 @@
                     mode, n, file, line
                 )
             )
+
+
+def _get_windows_libraries(link_type):
+    if link_type == "dynamic":
+        return ["libeay32", "ssleay32", "advapi32"]
+    elif link_type == "static" or link_type == "":
+        return ["libeay32mt", "ssleay32mt", "advapi32",
+                "crypt32", "gdi32", "user32", "ws2_32"]
+    else:
+        raise ValueError(
+            "PYCA_WINDOWS_LINK_TYPE must be 'static' or 'dynamic'"
+        )
diff --git a/docs/installation.rst b/docs/installation.rst
index 8fbbcb3..339d8b7 100644
--- a/docs/installation.rst
+++ b/docs/installation.rst
@@ -35,16 +35,35 @@
 On Windows
 ----------
 
-If you're on Windows you'll need to make sure you have OpenSSL installed.
-There are `pre-compiled binaries`_ available. If your installation is in
-an unusual location set the ``LIB`` and ``INCLUDE`` environment variables
-to include the corresponding locations. For example:
+The wheel package on Windows is a statically linked build (as of 0.5) so all
+dependencies are included. Just run
+
+.. code-block:: console
+
+    $ pip install cryptography
+
+If you prefer to compile it yourself you'll need to have OpenSSL installed.
+There are `pre-compiled binaries`_ available. If your installation is in an
+unusual location set the ``LIB`` and ``INCLUDE`` environment variables to
+include the corresponding locations.For example:
 
 .. code-block:: console
 
     C:\> \path\to\vcvarsall.bat x86_amd64
-    C:\> set LIB=C:\OpenSSL-1.0.1g-64bit\lib;%LIB%
-    C:\> set INCLUDE=C:\OpenSSL-1.0.1g-64bit\include;%INCLUDE%
+    C:\> set LIB=C:\OpenSSL\lib\VC\static;C:\OpenSSL\lib;%LIB%
+    C:\> set INCLUDE=C:\OpenSSL\include;%INCLUDE%
+    C:\> pip install cryptography
+
+You can also choose to build statically or dynamically using the
+``PYCA_WINDOWS_LINK_TYPE`` variable. Allowed values are ``static`` (default)
+and ``dynamic``.
+
+.. code-block:: console
+
+    C:\> \path\to\vcvarsall.bat x86_amd64
+    C:\> set LIB=C:\OpenSSL\lib\VC\static;C:\OpenSSL\lib;%LIB%
+    C:\> set INCLUDE=C:\OpenSSL\include;%INCLUDE%
+    C:\> set PYCA_WINDOWS_LINK_TYPE=dynamic
     C:\> pip install cryptography
 
 Building cryptography on Linux
diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py
index 58d7602..d22c4fd 100644
--- a/tests/hazmat/bindings/test_openssl.py
+++ b/tests/hazmat/bindings/test_openssl.py
@@ -15,7 +15,9 @@
 
 import pytest
 
-from cryptography.hazmat.bindings.openssl.binding import Binding
+from cryptography.hazmat.bindings.openssl.binding import (
+    Binding, _get_windows_libraries
+)
 
 
 class TestOpenSSL(object):
@@ -137,3 +139,13 @@
         resp = b.lib.SSL_set_mode(ssl, b.lib.SSL_OP_ALL)
         assert resp == b.lib.SSL_OP_ALL
         assert b.lib.SSL_OP_ALL == b.lib.SSL_get_mode(ssl)
+
+    def test_windows_static_dynamic_libraries(self):
+        assert "ssleay32mt" in _get_windows_libraries("static")
+
+        assert "ssleay32mt" in _get_windows_libraries("")
+
+        assert "ssleay32" in _get_windows_libraries("dynamic")
+
+        with pytest.raises(ValueError):
+            _get_windows_libraries("notvalid")