blob: 22807f1c9d3cea29d2e54b832abe72682ac2d1b6 [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
Alex Gaynor0f7f7812013-09-30 10:52:36 -07002
Alex Stapletonc5fffd32014-03-18 15:29:00 +00003OpenSSL backend
Alex Gaynor8f42fe42013-12-24 13:15:52 -08004===============
Donald Stuffte51fb932013-10-27 17:26:17 -04005
Alex Gaynor0e8cdf12016-12-13 21:05:35 -05006The `OpenSSL`_ C library. Cryptography supports OpenSSL version 1.0.1 and
Alex Gaynor1252dec2016-06-18 11:11:57 -04007greater.
Alex Gaynor6d02e2d2013-09-30 10:37:22 -07008
Alex Gaynorf8796b12013-12-13 20:28:55 -08009.. data:: cryptography.hazmat.backends.openssl.backend
Alex Gaynor6d02e2d2013-09-30 10:37:22 -070010
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060011 This is the exposed API for the OpenSSL backend.
Paul Kehrer2502ce52014-01-18 09:32:47 -060012
Alex Gaynor031c2cb2014-01-31 11:44:53 -080013 It implements the following interfaces:
14
15 * :class:`~cryptography.hazmat.backends.interfaces.CipherBackend`
Paul Kehrer3d754292014-05-01 09:09:34 -050016 * :class:`~cryptography.hazmat.backends.interfaces.CMACBackend`
Paul Kehrer99a249d2015-01-04 15:55:22 -060017 * :class:`~cryptography.hazmat.backends.interfaces.DERSerializationBackend`
Paul Kehrer9e5056c2016-12-11 09:00:52 -060018 * :class:`~cryptography.hazmat.backends.interfaces.DHBackend`
Mohammed Attia59edb612014-04-25 22:44:40 +020019 * :class:`~cryptography.hazmat.backends.interfaces.DSABackend`
Terry Chia7b59df12014-12-28 20:46:26 +080020 * :class:`~cryptography.hazmat.backends.interfaces.EllipticCurveBackend`
Alex Gaynor031c2cb2014-01-31 11:44:53 -080021 * :class:`~cryptography.hazmat.backends.interfaces.HashBackend`
22 * :class:`~cryptography.hazmat.backends.interfaces.HMACBackend`
23 * :class:`~cryptography.hazmat.backends.interfaces.PBKDF2HMACBackend`
Alex Stapleton8f2250f2014-02-08 12:24:02 +000024 * :class:`~cryptography.hazmat.backends.interfaces.RSABackend`
Terry Chia7b59df12014-12-28 20:46:26 +080025 * :class:`~cryptography.hazmat.backends.interfaces.PEMSerializationBackend`
26 * :class:`~cryptography.hazmat.backends.interfaces.X509Backend`
Alex Gaynor031c2cb2014-01-31 11:44:53 -080027
Terry Chiad8a27df2016-09-01 23:39:57 +080028 It also implements the following interface for OpenSSL versions ``1.1.0``
29 and above.
30
31 * :class:`~cryptography.hazmat.backends.interfaces.ScryptBackend`
32
Paul Kehrere4acd5d2014-02-03 21:59:29 -060033 It also exposes the following:
Paul Kehrer2502ce52014-01-18 09:32:47 -060034
Paul Kehrercfa2d622014-01-19 14:01:25 -060035 .. attribute:: name
Paul Kehrer2502ce52014-01-18 09:32:47 -060036
Paul Kehrercfa2d622014-01-19 14:01:25 -060037 The string name of this backend: ``"openssl"``
Alex Gaynor6d02e2d2013-09-30 10:37:22 -070038
Paul Kehrer5685baa2016-12-21 21:10:03 -060039 .. method:: openssl_version_text()
40
41 :return text: The friendly string name of the loaded OpenSSL library.
42 This is not necessarily the same version as it was compiled against.
43
44 .. method:: openssl_version_number()
45
46 .. versionadded:: 1.8
47
48 :return int: The integer version of the loaded OpenSSL library. This is
49 defined in ``opensslv.h`` as ``OPENSSL_VERSION_NUMBER`` and is
50 typically shown in hexadecimal (e.g. ``0x1010003f``). This is
51 not necessarily the same version as it was compiled against.
52
Paul Kehrerd52b89b2014-01-31 10:57:17 -060053 .. method:: activate_osrandom_engine()
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060054
Paul Kehrerd52b89b2014-01-31 10:57:17 -060055 Activates the OS random engine. This will effectively disable OpenSSL's
56 default CSPRNG.
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060057
Christian Heimes2e717762016-12-09 17:02:26 +010058 .. method:: osrandom_engine_implementation()
59
60 .. versionadded:: 1.7
61
62 Returns the implementation of OS random engine.
63
Paul Kehrerd2582222014-02-05 16:21:19 -060064 .. method:: activate_builtin_random()
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060065
Paul Kehrerd2582222014-02-05 16:21:19 -060066 This will activate the default OpenSSL CSPRNG.
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060067
Alex Stapletonc5fffd32014-03-18 15:29:00 +000068OS random engine
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060069----------------
70
Alex Gaynor01c634c2016-04-21 18:47:54 -040071By default OpenSSL uses a user-space CSPRNG that is seeded from system random (
Paul Kehrer136ff172014-01-29 21:23:11 -060072``/dev/urandom`` or ``CryptGenRandom``). This CSPRNG is not reseeded
73automatically when a process calls ``fork()``. This can result in situations
74where two different processes can return similar or identical keys and
75compromise the security of the system.
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060076
Paul Kehrer136ff172014-01-29 21:23:11 -060077The approach this project has chosen to mitigate this vulnerability is to
Alex Gaynor969f18e2014-05-17 20:07:35 -070078include an engine that replaces the OpenSSL default CSPRNG with one that
79sources its entropy from ``/dev/urandom`` on UNIX-like operating systems and
80uses ``CryptGenRandom`` on Windows. This method of pulling from the system pool
Paul Kehrer136ff172014-01-29 21:23:11 -060081allows us to avoid potential issues with `initializing the RNG`_ as well as
82protecting us from the ``fork()`` weakness.
83
Paul Kehrer8042b292014-01-31 10:44:36 -060084This engine is **active** by default when importing the OpenSSL backend. When
85active this engine will be used to generate all the random data OpenSSL
86requests.
87
Paul Kehrer8042b292014-01-31 10:44:36 -060088When importing only the binding it is added to the engine list but
89**not activated**.
90
Paul Kehrer3f17c7c2014-01-20 16:32:26 -060091
Alex Stapletonc5fffd32014-03-18 15:29:00 +000092OS random sources
Paul Kehrer55809a12014-01-29 21:41:16 -060093-----------------
Paul Kehrer9967bc52014-01-29 21:39:13 -060094
95On OS X and FreeBSD ``/dev/urandom`` is an alias for ``/dev/random`` and
96utilizes the `Yarrow`_ algorithm.
97
Paul Kehrer012bfbc2014-02-11 23:37:51 -060098On Windows the implementation of ``CryptGenRandom`` depends on which version of
Paul Kehrer039b4782014-02-11 23:50:56 -060099the operation system you are using. See the `Microsoft documentation`_ for more
Paul Kehrer012bfbc2014-02-11 23:37:51 -0600100details.
Paul Kehrer9967bc52014-01-29 21:39:13 -0600101
Alex Gaynor969f18e2014-05-17 20:07:35 -0700102Linux uses its own PRNG design. ``/dev/urandom`` is a non-blocking source
103seeded from the same pool as ``/dev/random``.
Paul Kehrer9967bc52014-01-29 21:39:13 -0600104
Christian Heimes2e717762016-12-09 17:02:26 +0100105+------------------------------------------+------------------------------+
106| Windows | ``CryptGenRandom()`` |
107+------------------------------------------+------------------------------+
108| Linux >= 3.4.17 with working | ``getrandom(GRND_NONBLOCK)`` |
109| ``SYS_getrandom`` syscall | |
110+------------------------------------------+------------------------------+
111| OpenBSD >= 5.6 | ``getentropy()`` |
112+------------------------------------------+------------------------------+
113| BSD family (including macOS 10.12+) with | ``getentropy()`` |
114| ``SYS_getentropy`` in ``sys/syscall.h`` | |
115+------------------------------------------+------------------------------+
116| fallback | ``/dev/urandom`` with |
117| | cached file descriptor |
118+------------------------------------------+------------------------------+
119
Paul Kehrer9967bc52014-01-29 21:39:13 -0600120
Alex Gaynor6d02e2d2013-09-30 10:37:22 -0700121.. _`OpenSSL`: https://www.openssl.org/
Alex Gaynor677c5312015-06-20 20:53:50 -0400122.. _`initializing the RNG`: https://en.wikipedia.org/wiki/OpenSSL#Predictable_private_keys_.28Debian-specific.29
Alex Gaynore9df2942014-12-12 10:56:26 -0800123.. _`Yarrow`: https://en.wikipedia.org/wiki/Yarrow_algorithm
Alex Gaynor3fad1902015-02-18 12:48:29 -0800124.. _`Microsoft documentation`: https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx