Fix creating self-signed certificates on Python 3

* Fixes Tautulli/Tautulli-Issues#248
This commit is contained in:
JonnyWong16 2020-06-01 16:40:25 -07:00
commit d8080fe506
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 10 additions and 5 deletions

View file

@ -100,7 +100,7 @@ def createSelfSignedCertificate(issuerName, issuerKey, serial, notBefore, notAft
cert.set_pubkey(issuerKey) cert.set_pubkey(issuerKey)
if altNames: if altNames:
cert.add_extensions([crypto.X509Extension("subjectAltName", False, altNames)]) cert.add_extensions([crypto.X509Extension(b"subjectAltName", False, altNames)])
cert.sign(issuerKey, digest) cert.sign(issuerKey, digest)
return cert return cert

View file

@ -52,10 +52,12 @@ import xmltodict
import plexpy import plexpy
if plexpy.PYTHON2: if plexpy.PYTHON2:
import common
import logger import logger
import request import request
from api2 import API2 from api2 import API2
else: else:
from plexpy import common
from plexpy import logger from plexpy import logger
from plexpy import request from plexpy import request
from plexpy.api2 import API2 from plexpy.api2 import API2
@ -445,22 +447,25 @@ def create_https_certificates(ssl_cert, ssl_key):
return False return False
from certgen import createKeyPair, createSelfSignedCertificate, TYPE_RSA from certgen import createKeyPair, createSelfSignedCertificate, TYPE_RSA
issuer = common.PRODUCT
serial = timestamp() serial = timestamp()
not_before = 0
not_after = 60 * 60 * 24 * 365 * 10 # ten years
domains = ['DNS:' + d.strip() for d in plexpy.CONFIG.HTTPS_DOMAIN.split(',') if d] domains = ['DNS:' + d.strip() for d in plexpy.CONFIG.HTTPS_DOMAIN.split(',') if d]
ips = ['IP:' + d.strip() for d in plexpy.CONFIG.HTTPS_IP.split(',') if d] ips = ['IP:' + d.strip() for d in plexpy.CONFIG.HTTPS_IP.split(',') if d]
altNames = ','.join(domains + ips) alt_names = ','.join(domains + ips).encode('utf-8')
# Create the self-signed Tautulli certificate # Create the self-signed Tautulli certificate
logger.debug("Generating self-signed SSL certificate.") logger.debug("Generating self-signed SSL certificate.")
pkey = createKeyPair(TYPE_RSA, 2048) pkey = createKeyPair(TYPE_RSA, 2048)
cert = createSelfSignedCertificate("Tautulli", pkey, serial, 0, 60 * 60 * 24 * 365 * 10, altNames) # ten years cert = createSelfSignedCertificate(issuer, pkey, serial, not_before, not_after, alt_names)
# Save the key and certificate to disk # Save the key and certificate to disk
try: try:
with open(ssl_cert, "w") as fp: with open(ssl_cert, "w") as fp:
fp.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) fp.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8'))
with open(ssl_key, "w") as fp: with open(ssl_key, "w") as fp:
fp.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)) fp.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey).decode('utf-8'))
except IOError as e: except IOError as e:
logger.error("Error creating SSL key and certificate: %s", e) logger.error("Error creating SSL key and certificate: %s", e)
return False return False