Skip to main content

Reusable django app to generate and manage x509 certificates

Project description

CI build status Test Coverage Dependency monitoring chat Pypi Version downloads code style: black

Simple reusable django app implementing x509 PKI certificates management.

Want to help OpenWISP? Find out how to help us grow here.

Current features

  • CA generation

  • Import existing CAs

  • End entity certificate generation

  • Import existing certificates

  • Certificate revocation

  • CRL view (public or protected)

  • Possibility to specify x509 extensions on each certificate

  • Random serial numbers based on uuid4 integers (see why is this a good idea)

  • Possibility to generate and import passphrase protected x509 certificates/CAs

  • Passphrase protected x509 content will be shown encrypted in the web UI

Project goals

  • provide a simple and reusable x509 PKI management django app

  • provide abstract models that can be imported and extended in larger django projects


  • Python >= 3.7

  • OpenSSL

Install stable version from pypi

Install from pypi:

pip install django-x509

Install development version

Install tarball:

pip install

Alternatively you can install via pip using git:

pip install -e git+git://

If you want to contribute, install your cloned fork:

git clone<your_fork>/django-x509.git
cd django-x509
python develop

Setup (integrate in an existing django project)

Add django_x509 to INSTALLED_APPS:

    # other apps

Add the URLs to your main

from django.contrib import admin

urlpatterns = [
    # ... other urls in your project ...


Then run:

./ migrate

Installing for development

Install sqlite:

sudo apt-get install sqlite3 libsqlite3-dev

Install your forked repo:

git clone git://<your_fork>/django-x509
cd django-x509/
python develop

Install test requirements:

pip install -r requirements-test.txt

Create database:

cd tests/
./ migrate
./ createsuperuser

Launch development server:

./ runserver

You can access the admin interface at

Run tests with:


Install and run on docker

Build from docker file:

sudo docker build -t openwisp/djangox509 .

Run the docker container:

sudo docker run -it -p 8000:8000 openwisp/djangox509







Default validity period (in days) when creating new x509 certificates.






Default validity period (in days) when creating new Certification Authorities.






Default key length for new CAs and new certificates.

Must be one of the following values:

  • 512

  • 1024

  • 2048

  • 4096






Default digest algorithm for new CAs and new certificates.

Must be one of the following values:

  • sha1

  • sha224

  • sha256

  • sha384

  • sha512






Whether the basicConstraint x509 extension must be flagged as critical when creating new CAs.



int or None



Value of the pathLenConstraint of basicConstraint x509 extension used when creating new CAs.

When this value is a positive int it represents the maximum number of non-self-issued intermediate certificates that may follow the generated certificate in a valid certification path.

Set this value to None to avoid imposing any limit.






Whether the keyUsage x509 extension should be flagged as “critical” for new CAs.





cRLSign, keyCertSign

Value of the keyUsage x509 extension for new CAs.






Whether the keyUsage x509 extension should be flagged as “critical” for new end-entity certificates.





digitalSignature, keyEncipherment

Value of the keyUsage x509 extension for new end-entity certificates.






Whether the view for downloading Certificate Revocation Lists should be protected with authentication or not.

Extending django-x509

One of the core values of the OpenWISP project is Software Reusability, for this reason django-x509 provides a set of base classes which can be imported, extended and reused to create derivative apps.

In order to implement your custom version of django-x509, you need to perform the steps described in this section.

When in doubt, the code in the test project and the sample app will serve you as source of truth: just replicate and adapt that code to get a basic derivative of django-x509 working.

Premise: if you plan on using a customized version of this module, we suggest to start with it since the beginning, because migrating your data from the default module to your extended version may be time consuming.

1. Initialize your custom module

The first thing you need to do is to create a new django app which will contain your custom version of django-x509.

A django app is nothing more than a python package (a directory of python scripts), in the following examples we’ll call this django app myx509, but you can name it how you want:

django-admin startapp myx509

Keep in mind that the command mentioned above must be called from a directory which is available in your PYTHON_PATH so that you can then import the result into your project.

Now you need to add myx509 to INSTALLED_APPS in your, ensuring also that django_x509 has been removed:

    # ... other apps ...
    # 'django_x509'  <-- comment out or delete this line

For more information about how to work with django projects and django apps, please refer to the django documentation.

2. Install django-x509 & openwisp-utils

Install (and add to the requirement of your project):

pip install django-x509 openwisp-utils


Add the following to your

EXTENDED_APPS = ['django_x509']

4. Add openwisp_utils.staticfiles.DependencyFinder

Add openwisp_utils.staticfiles.DependencyFinder to STATICFILES_FINDERS in your


5. Add openwisp_utils.loaders.DependencyLoader

Add openwisp_utils.loaders.DependencyLoader to TEMPLATES in your

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'loaders': [
            'context_processors': [

6. Inherit the AppConfig class

Please refer to the following files in the sample app of the test project:

You have to replicate and adapt that code in your project.

For more information regarding the concept of AppConfig please refer to the “Applications” section in the django documentation.

7. Create your custom models

Here we provide an example of how to extend the base models of django-x509. We added a simple “details” field to the models for demostration of modification:

from django.db import models
from django_x509.base.models import AbstractCa, AbstractCert

class DetailsModel(models.Model):
    details = models.CharField(max_length=64, blank=True, null=True)

    class Meta:
        abstract = True

class Ca(DetailsModel, AbstractCa):
    Concrete Ca model
    class Meta(AbstractCa.Meta):
        abstract = False

class Cert(DetailsModel, AbstractCert):
    Concrete Cert model
    class Meta(AbstractCert.Meta):
        abstract = False

You can add fields in a similar way in your file.

Note: for doubts regarding how to use, extend or develop models please refer to the “Models” section in the django documentation.

8. Add swapper configurations

Once you have created the models, add the following to your

# Setting models for swapper module
DJANGO_X509_CA_MODEL = 'myx509.Ca'
DJANGO_X509_CERT_MODEL = 'myx509.Cert'

Substitute myx509 with the name you chose in step 1.

9. Create database migrations

Create and apply database migrations:

./ makemigrations
./ migrate

For more information, refer to the “Migrations” section in the django documentation.

10. Create the admin

Refer to the file of the sample app.

To introduce changes to the admin, you can do it in two main ways which are described below.

Note: for more information regarding how the django admin works, or how it can be customized, please refer to “The django admin site” section in the django documentation.

1. Monkey patching

If the changes you need to add are relatively small, you can resort to monkey patching.

For example:

from django_x509.admin import CaAdmin, CertAdmin

# CaAdmin.list_display.insert(1, 'my_custom_field') <-- your custom change example
# CertAdmin.list_display.insert(1, 'my_custom_field') <-- your custom change example

2. Inheriting admin classes

If you need to introduce significant changes and/or you don’t want to resort to monkey patching, you can proceed as follows:

from django.contrib import admin
from swapper import load_model

from django_x509.base.admin import AbstractCaAdmin, AbstractCertAdmin

Ca = load_model('django_x509', 'Ca')
Cert = load_model('django_x509', 'Cert')

class CertAdmin(AbstractCertAdmin):
    # add your changes here

class CaAdmin(AbstractCaAdmin):
    # add your changes here, CaAdmin), CertAdmin)

11. Create root URL configuration

Please refer to the file in the test project.

For more information about URL configuration in django, please refer to the “URL dispatcher” section in the django documentation.

12. Import the automated tests

When developing a custom application based on this module, it’s a good idea to import and run the base tests too, so that you can be sure the changes you’re introducing are not breaking some of the existing features of django-x509.

In case you need to add breaking changes, you can overwrite the tests defined in the base classes to test your own behavior.

from django.test import TestCase
from django_x509.tests.base import TestX509Mixin
from django_x509.tests.test_admin import ModelAdminTests as BaseModelAdminTests
from django_x509.tests.test_ca import TestCa as BaseTestCa
from django_x509.tests.test_cert import TestCert as BaseTestCert

class ModelAdminTests(BaseModelAdminTests):
    app_label = 'myx509'

class TestCert(BaseTestCert):

class TestCa(BaseTestCa):

del BaseModelAdminTests
del BaseTestCa
del BaseTestCert

Now, you can then run tests with:

# the --parallel flag is optional
./ test --parallel myx509

Substitute myx509 with the name you chose in step 1.

For more information about automated tests in django, please refer to “Testing in Django”.


Please refer to the OpenWISP contributing guidelines.


See OpenWISP Support Channels.





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

django-x509-1.1.tar.gz (49.7 kB view hashes)

Uploaded Source

Built Distribution

django_x509-1.1-py2.py3-none-any.whl (52.7 kB view hashes)

Uploaded Python 2 Python 3

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