pyOpenSSL Documentation
repository·main·Indexed 21 days ago
https://github.com/pyca/pyopensslA Python library providing high-level interfaces to the OpenSSL library for SSL/TLS connection management, certificate handling, and key generation. It features the OpenSSL.ssl module for secure communications and the OpenSSL.crypto module for cryptographic operations. The library uses Calendar Versioning (CalVer) and provides tools for serializing cryptographic data in PEM and ASN.1 formats.
What's inside pyOpenSSL
- pyOpenSSL is a thin wrapper around a subset of the OpenSSL library. Most object methods in pyOpenSSL function by calling the corresponding underlying functions in the OpenSSL library directly.
Overview of pyOpenSSL capabilities
mainpyOpenSSL is a high-level Python wrapper around a subset of the OpenSSL library. Its core features include:
SSL.Connectionobjects: These wrap the methods of Python's portable sockets.- Python-written callbacks.
- Extensive error-handling mechanisms that mirror OpenSSL's native error codes.
Use the OpenSSL module as a Python interface to OpenSSL
mainThe
OpenSSLpackage provides a high-level Python interface to the functions available in the OpenSSL library. It is organized into two primary functional modules:OpenSSL.crypto: Used for cryptographic operations (e.g., generating keys, handling certificates, hashing).OpenSSL.ssl: Used for SSL/TLS communication, including managing SSL contexts and handling secure connections.
To use the library, you must have the
OpenSSLmodule available in your Python environment.import OpenSSLUse custom transport objects with SSL.Connection
mainThe
SSL.Connectionclass wraps a transport object to provide SSL/TLS capabilities. Instead of requiring a standardsocket.socketobject,SSL.Connectionaccepts any socket-like transport object.Requirements for the transport object:
- Basic Requirement: The object must implement a
fileno()method that returns a file descriptor valid at the C level (compatible with systemreadandwritecalls). - For
connect()oraccept(): If you intend to use the.connect()or.accept()methods on theSSL.Connectioninstance, the underlying transport object must also implement these methods.
Method Delegation:
SSL.Connectionuses a delegation pattern. If a method is called on theSSL.Connectionobject that is not defined within the SSL wrapper, the call is automatically passed through to the underlying transport object.- Basic Requirement: The object must implement a
Understand the pyOpenSSL versioning policy
mainpyOpenSSL uses
CalVer(Calendar Versioning) in theYY.MINOR.MICROformat.Unlike Semantic Versioning (SemVer), the major version number represents the year of release and is not an indicator of breaking changes. You should check the
changelogfor information regarding breaking changes or deprecations.Install pyOpenSSL via pip
mainTo install the standard version of pyOpenSSL, use
pip install pyopenssl.$ pip install pyopensslInstall pyOpenSSL for development
mainTo install pyOpenSSL in editable mode with testing dependencies for development purposes, navigate to the root directory of your checkout and run
pip install -e .[test].$ pip install -e .[test]Use pyca/cryptography instead of pyOpenSSL
mainThe Python Cryptographic Authority (pyca) strongly recommends using the
pyca/cryptographylibrary instead ofpyOpenSSLwhenever possible.Guidance:
- If your use case is anything other than establishing a TLS connection, you should migrate to
cryptographyand removepyOpenSSLfrom your dependencies.
- If your use case is anything other than establishing a TLS connection, you should migrate to
Update pyOpenSSL safely
mainpyOpenSSL maintains a strong backward compatibility policy. When breaking changes are necessary, the project follows a strict lifecycle:
- Announcement: Breaking changes are first announced in the
changelog. - Deprecation Period: The old behavior will continue to work but will raise a
DeprecationWarningfor one year. - Removal: The breaking change is finalized and the old behavior is removed, following a second announcement in the
changelog.
Because of this policy, updating to newer versions is generally safe.
- Announcement: Breaking changes are first announced in the
Install pyOpenSSL
mainTo install pyOpenSSL, use
pip. It is recommended to install it within a virtual environment to avoid conflicts with system packages.pip install pyOpenSSLMigrate from OpenSSL.crypto to pyca/cryptography
mainThe
OpenSSL.cryptomodule is pending deprecation. It is highly recommended to usepyca/cryptographyinstead, as it provides a more complete set of cryptographic primitives and a more powerful X509 API.If you must interoperate between the two libraries, you can convert objects using the following methods on
X509,CRL, andPKeyobjects:to_cryptography(): Converts an OpenSSL object to acryptographyobject.from_cryptography(): Creates an OpenSSL object from acryptographyobject.
Use SSL Sessions for connection reuse
mainAn
SSL.Sessionobject represents a set of connection parameters that can be reused to speed up subsequent connections (session resumption).Sessions are tied to the
Contextthat created them. When usingConnection.set_session(), pyOpenSSL ensures the session is only reused with a compatibleContextto prevent security issues.