Skip to main content

A tool to create certificate signing request and self-signed certificates

Project description

certipie

Pypi version Coverage Status Ruff License Apache 2 Poetry

A utility library to create certificate signing request and self-signed certificates.

Why?

There are three main reasons why I decide to do this:

  • I am a nerd who loves to code. Yeah, I need to be honest here, it was an opportunity for me to play with the excellent cryptography library.
  • I currently work in a company where we deal a lot with certificates, and I know it can be handy to have a simple tool to generate certificate signing request, so I decide to create one.
  • When creating or working with network servers, we often need to test certificate handling, so it is convenient to have a tool to quickly generate a self-signed certificate.

Notes:

  • If you want a tool to handle a whole certificate life cycle for your project, consider using a library like certbot.
  • If you just want a tool to create certificates on the fly during unit tests, consider using trustme.

Installation

The minimal python version supported is 3.9. You can install the library using pip with the following command:

$ pip install certipie

You can also use an alternative package manager like poetry:

$ poetry add certipie

Usage

There are three ways to use certipie.

The command line interface

This is probably the main way to use it. The project includes a convenient cert command line interface.

$ cert
Usage: cert [OPTIONS] COMMAND [ARGS]...

  A cli to generate certificate signing request and self-signed certificate
  that can be used for testing purpose.

Options:
  --version   Show the version and exit.
  -h, --help  Show this message and exit.

Commands:
  auto-cert           Creates a self-signed certificate useful for tests.
  csr                 Creates a certificate signing request file given...
  install-completion  Install completion script for bash, zsh and fish...
  rsa                 Creates a pair of private/public keys using the RSA...
  server              Serves a swagger UI where you can perform the same...

install-completion

The first command you will want to use is install-completion like the name said will allow options and commands completion. The shells currently supported are bash, fish and zsh.

$ cert install-completion

rsa

To create a certificate signing request or a self-signed certificate, you need a private key. The cert command allows you to create an RSA private key which is one of the most common (if not the most common) private keys used for this type of operation.

$ cert rsa
Enter the passphrase [passphrase]:
Repeat for confirmation:
The pair of keys was successfully in ...

Note: On all commands, you can use -h option to get their usage documentation.

csr

This commands helps you create a certificate signing request.

$ cert csr -f csr.pem
Organization: orga
State: Ile-de-France
Country: FR
City: Paris
Name: site.com
The certificate signing request has been successfully created in /home/kevin/...

Notes:

  • In the previous example since no private key was passed as input, the csr command creates an RSA one with no password in the same directory where the certificate signing request is created. If you want to provide one, use the -k option and gives the path to the private key, e.g: cert csr -k /path/to/private/key. It is not mandatory that the private key is of type RSA. In fact other algorithms are supported like DSA or elliptic curve. For a full list of supported algorithms, look PrivateKey type in module certipie.types.
  • If you pass a key file, you will be prompted to give the password. If you don't provide a password to your private key, just type Enter to continue.
  • The --country option must be a two-letters value. To know the two-letters code of a country, you can check this website.
  • The --name option value is used as the domain name to submit for the certificate signing request. In the previous example, it will be site.com. Wildcards like *.site.com are supported.
  • If you want to include many domain names in the certificate signing request, not only the --name value, you can use the --alt-names option like the following: cert csr --alt-names=site.com,foo.site.com. The values must be separated by commas.

auto-cert

This command helps you create a self-signed certificate useful when you want to test a network server like an HTTP server.

$ cert auto-cert -f cert.pem --validity=360
Organization: orga
State: Ile-de-France
Country: FR
City: Paris
The self-signed certificate has been successfully created in /home/kevin/...

Notes:

  • The notes from the csr command also applies here.
  • Since we often work on our own computer in this case, localhost is the default --name option.
  • --alt-names also accepts ip adresses and networks. You can have a value like the following: cert auto-cert --alt-names=localhost,192.168.1.0/24.
  • --validity option represents the number of days the certificate will be valid. If not provided, it is 365.

server

This commands runs a server which serves a Swagger UI where you can perform the previous commands explained above. This is handy if you want non-technical staff to be able to use the command line features.

$ cert server

If you open a browser on http://localhost:8000 you will see the swagger documentation.

Under the hood the server used is hypercorn. You can configure it passing a toml configuration file. To know all the settings available, refer to the official documentation.

$ cert server -c /path/to/toml/file

The REST API

Following the previous section about serving a swagger UI, if you think that it does not well suits your organization, you can adjust it. The api is created using the FastAPI web framework. For example if you want to integrate it in your own fastapi application, you can use the router provided by certipie.

This way you can adjust the url used for the certificate routes, for example /certificate instead of /certs, change documentation url, add some authentication to routes, etc... The router concept is a powerful feature from FastAPI.

from fastapi import FastAPI
from certipie import router

app = FastAPI(
    title='certificate api',
    description='Your description',
    redoc_url=None,
    docs_url='/documentation'
)
app.include_router(router, prefix='/certificate', tags=['certificate'])

Transform the api to suit your needs :)

The certipie API

The last way to leverage certipie is to interact programmatically with its api. The following functions are exposed:

  • create_private_key: Creates an RSA private key.
  • get_public_key_from_private_key: Extracts a public key from a private key.
  • create_csr: Creates a certificate signing request.
  • create_auto_cert: Creates a self-signed certificate.

Normally the api usage should be straightforward. The behaviour is the same as the command line. You can also look at tests if you are blocked.

Here is a snippet on how to create a certificate signing request using create_csr:

from certipie import create_csr

create_csr(
    'csr.pem',
    'FR',
    'Ile-de-France',
    'Paris',
    'organization',
    'site.com',
    alternative_names=['site.com', 'foo.site.com'],
    private_key='key.pem',
    passphrase='passphrase'
)

There are two others objets exported:

  • app: The fastapi application used when running cert server command. It is exposed so that you can mount it in another application if you want.
  • PrivateKey: The type annotation for all the private keys supported by the cryptography library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

certipie-0.3.0.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

certipie-0.3.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file certipie-0.3.0.tar.gz.

File metadata

  • Download URL: certipie-0.3.0.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.9.18 Linux/6.2.0-1016-azure

File hashes

Hashes for certipie-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6ef1cf7a4dfd42d67437d20b92a09fd0a74796956bf2f886af2c94c6542208aa
MD5 bd6da9c63efc482388f44d714d6dc85d
BLAKE2b-256 9becab359d7363bb01ff76d1781cf57dfc05553ed0421669d758af18ba3dbd08

See more details on using hashes here.

File details

Details for the file certipie-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: certipie-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.9.18 Linux/6.2.0-1016-azure

File hashes

Hashes for certipie-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56f6cb2b948f978a7188fda913866060861d8cca38cbed08c98d19b93423d694
MD5 6cc79c440c92c4bc06cf133e7b2d830d
BLAKE2b-256 948de9fbfe2b4b409cb591b0b588e279bf11dd8d7eb2895371a91b36090dd96c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page