# Buggy code
def create_https_certificates(ssl_cert, ssl_key):
    """Create self-signed HTTPS certificares and store in paths 'ssl_cert' and 'ssl_key'.

    :param ssl_cert: Path of SSL certificate file to write
    :param ssl_key: Path of SSL keyfile to write
    :return: True on success, False on failure
    :rtype: bool
    """
    try:
        from OpenSSL import crypto
        from certgen import (
            createKeyPair,
            createCertRequest,
            createCertificate,
            TYPE_RSA,
        )
    except Exception:
        log.warning("pyopenssl module missing, please install for https access")
        return False

    # Serial number for the certificate
    serial = int(time.time())

    # Create the CA Certificate
    cakey = createKeyPair(TYPE_RSA, 1024)
    careq = createCertRequest(cakey, CN="Certificate Authority")
    cacert = createCertificate(
        careq, (careq, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)
    )  # ten years

    cname = "Medusa"
    pkey = createKeyPair(TYPE_RSA, 1024)
    req = createCertRequest(pkey, CN=cname)
    cert = createCertificate(
        req, (cacert, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)
    )  # ten years

    # Save the key and certificate to disk
    try:
        io.open(ssl_key, "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
        io.open(ssl_cert, "wb").write(
            crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
        )
    except Exception:
        log.error("Error creating SSL key and certificate")
        return False

    return True


# Traceback (ssl.SSLError)
ERROR:asyncio:Exception in callback BaseAsyncIOLoop._handle_events(13, 1)
handle: <Handle BaseAsyncIOLoop._handle_events(13, 1)>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/opt/medusa/ext/tornado/platform/asyncio.py", line 122, in _handle_events
handler_func(fileobj, events)
File "/opt/medusa/ext/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/opt/medusa/ext/tornado/netutil.py", line 262, in accept_handler
callback(connection, address)
File "/opt/medusa/ext/tornado/tcpserver.py", line 263, in _handle_connection
do_handshake_on_connect=False)
File "/opt/medusa/ext/tornado/netutil.py", line 565, in ssl_wrap_socket
context = ssl_options_to_context(ssl_options)
File "/opt/medusa/ext/tornado/netutil.py", line 540, in ssl_options_to_context
context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))
ssl.SSLError: [SSL: EE_KEY_TOO_SMALL] ee key too small (_ssl.c:3833)
ERROR:asyncio:Exception in callback BaseAsyncIOLoop._handle_events(13, 1)

# Fixed code
def create_https_certificates(ssl_cert, ssl_key):
    """Create self-signed HTTPS certificares and store in paths 'ssl_cert' and 'ssl_key'.

    :param ssl_cert: Path of SSL certificate file to write
    :param ssl_key: Path of SSL keyfile to write
    :return: True on success, False on failure
    :rtype: bool
    """
    try:
        from OpenSSL import crypto
        from certgen import (
            createKeyPair,
            createCertRequest,
            createCertificate,
            TYPE_RSA,
        )
    except Exception:
        log.warning("pyopenssl module missing, please install for https access")
        return False

    # Serial number for the certificate
    serial = int(time.time())

    # Create the CA Certificate
    cakey = createKeyPair(TYPE_RSA, 2048)
    careq = createCertRequest(cakey, CN="Certificate Authority")
    cacert = createCertificate(
        careq, (careq, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)
    )  # ten years

    cname = "Medusa"
    pkey = createKeyPair(TYPE_RSA, 2048)
    req = createCertRequest(pkey, CN=cname)
    cert = createCertificate(
        req, (cacert, cakey), serial, (0, 60 * 60 * 24 * 365 * 10)
    )  # ten years

    # Save the key and certificate to disk
    try:
        io.open(ssl_key, "wb").write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey))
        io.open(ssl_cert, "wb").write(
            crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
        )
    except Exception:
        log.error("Error creating SSL key and certificate")
        return False

    return True