Search code examples
pythonpython-3.xssltwistedpki

Certificate Generation for Python Twisted SSL


I am trying to figure out how to setup a SSL link using the Python library Twisted. I have managed to create a certificate that works on the server side, but I am totally stuck when it comes to the client side.

The example from the twisted website states:

The following examples rely on the files server.pem (private key and self-signed certificate together) and public.pem (the server’s public certificate by itself).

I have generated myself a certificate and key using OpenSSL:

# Generate Private Key:
openssl genrsa -des3 -out certs/server.key 2048

# Generate Certificate Signing Request:
openssl req -new -key certs/server.key -sha256 -out certs/server.csr

# Generate a Self-Signed Certificate:
openssl x509 -req -days 365 -in certs/server.csr -signkey certs/server.key -sha256 -out certs/server.crt

# Convert the CRT to PEM format:
openssl x509 -in certs/server.crt -out certs/server.pem -outform PEM

For the server-side I am combining certs/server.crt and certs/server.key to create server.pem and trying to use server.crt for public.

When I try and run my test program using:

certificate = ssl.PrivateCertificate.loadPEM(certData)

I get an error about not starting line. Which certificate should I be using for the client if it's not server.crt please?


Solution

  • When I try and run my test program using:

    certificate = ssl.PrivateCertificate.loadPEM(certData) I get an error about not starting line. Which certificate should I be using for the client if it's not server.crt please?

    This should be ssl.Certificate.LoadPEM(certData) if you look at the example on the Twisted howto page.