Parse and split PEM files painlessly.
Project description
pem: Easy PEM file parsing
pem is an MIT-licensed Python module for parsing and splitting of PEM files, i.e. Base64 encoded DER keys and certificates.
It runs on Python 2.6, 2.7, 3.3, and PyPy 2.0+, has no dependencies and does not attempt to interpret the certificate data in any way. pem is intended to ease the handling of PEM files in combination with PyOpenSSL and – by extension – Twisted.
It’s born from my personal need because of the inconsistent handling of chain certificates by various servers: some servers (like Apache) expect them to be a separate file while others (like nginx) expect them concatenated to the server certificate. Since I want my Python software to be universal and to be able to cope with both, pem was born.
The core API call is the function parse():
import pem
with open('cert.pem', 'rb') as f:
certs = pem.parse(f.read())
The function returns a list of valid PEM objects found in the string supplied. Currently possible types are Certificate and RSAPrivateKey. Both can be transformed using str() into plain strings for other APIs. They don’t offer any other public API at the moment.
Convenience
Since pem is mostly a convenience module, there are several helper functions.
Files
parse_file(file_name) reads the file file_name and parses its contents. So the following example is equivalent with the first one:
import pem
certs = pem.parse_file('cert.pem')
Twisted
A typical use case in Twisted with the APIs above would be:
import pem
from twisted.internet import ssl
key = pem.parse_file('key.pem')
cert, chain = pem.parse_file('cert_and_chain.pem')
cert = ssl.PrivateCertificate.loadPEM(str(key) + str(cert))
chainCert = ssl.Certificate.loadPEM(str(chain))
ctxFactory = ssl.CertificateOptions(
privateKey=cert.privateKey.original,
certificate=cert.original,
extraCertChain=[chainCert.original],
)
Turns out, this is the major use case for me. Therefore it can be simplified to:
import pem
ctxFactory = pem.certificateOptionsFromFiles(
'key.pem', 'cert_and_chain.pem',
)
The first certificate found will be used as the server certificate, the rest is passed as the chain. You can pass as many PEM files as you like. Therefore you can distribute your key, certificate, and chain certificates over a arbitrary number of files. A ValueError is raised if more than one key, no key, or no certificate are found. Any further keyword arguments will be passed to CertificateOptions.
Future
pem currently only supports the PyOpenSSL/Twisted combo because that’s what I’m using. I’d be more than happy to merge support for additional frameworks though!
Changelog for pem
0.1.0 (2013-07-18)
Initial release.
Credits
“pem” is written and maintained by Hynek Schlawack.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pem-0.1.0.tar.gz
.
File metadata
- Download URL: pem-0.1.0.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b65fa4924f01371ca4db213b96397ab5951c3f09f9001c0757ef4b269f698217 |
|
MD5 | 55a596b9a7fe0750849c9042ed89bb1b |
|
BLAKE2b-256 | 3ca5ade442a58ecda5689fdba39f339e157a4d9b6ecf85c320b3e79c36a9660a |
File details
Details for the file pem-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: pem-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3f252e7f4f386b0a855230bf6fc8cc925422ea970d78b78f17a817b95018b83 |
|
MD5 | 769d58d533a665f79be7be9ef9c2579f |
|
BLAKE2b-256 | 9e0d653e96d1efc7ca04a858c1e062c444107b33ff2aa6cf9c5fecdef9257148 |