gui/backend/gui_plugin/core/lib/certs/management.py
def create_certificate(cert_path):
    """Creates a certificate and related files

    Args:
        cert_path (str): The path the certificate and related files should
            be created in

    Returns:
       The output of the process
    """

    try:
        # ----------------------------------------------------
        # Attributes to be used on the certificate generation
        # ----------------------------------------------------
        subject = issuer = x509.Name([
            x509.NameAttribute(NameOID.COUNTRY_NAME, u"US"),
            x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"Texas"),
            x509.NameAttribute(NameOID.LOCALITY_NAME, u"Austin"),
            x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"Oracle"),
            x509.NameAttribute(
                NameOID.ORGANIZATIONAL_UNIT_NAME, u"MySQL"),
            x509.NameAttribute(NameOID.EMAIL_ADDRESS,
                               u"support@mysql.com"),
            x509.NameAttribute(NameOID.COMMON_NAME,
                               u"MySQL Shell Auto Generated CA Certificate"),
        ])

        today = datetime.datetime.today()
        one_day = datetime.timedelta(1, 0, 0)

        # Default validity for the certificates is 10 years
        validity = one_day * 365 * 10

        # In macOS TLS server certificates must have a validity period of 825
        # days or fewer: to be set as today + validity
        if platform.system() == "Darwin":
            validity = one_day * 824

        # ---------------------------------------------
        # Generation of the Private Key for the Root CA
        # ---------------------------------------------
        ca_private_key = rsa.generate_private_key(
            public_exponent=65537,
            key_size=2048,
            backend=backends.default_backend())

        # --------------------------------
        # Generation of the CA certificate
        # --------------------------------
        from cryptography.hazmat.primitives import hashes

        ca_public_key = ca_private_key.public_key()
        ca_builder = x509.CertificateBuilder()
        ca_builder = ca_builder.subject_name(subject)
        ca_builder = ca_builder.issuer_name(issuer)
        ca_builder = ca_builder.not_valid_before(today - one_day)
        ca_builder = ca_builder.not_valid_after(today + validity)
        ca_builder = ca_builder.serial_number(x509.random_serial_number())
        ca_builder = ca_builder.public_key(ca_public_key)

        # CA should be True in order enable registration in Firefox
        ca_builder = ca_builder.add_extension(
            x509.BasicConstraints(ca=True, path_length=None), critical=False)

        ca_builder = ca_builder.add_extension(
            x509.SubjectAlternativeName(
                [x509.DNSName(u'localhost')]
            ),
            critical=False
        )

        # Signs the CA certificate and creates the file
        ca_cert = ca_builder.sign(
            private_key=ca_private_key, algorithm=hashes.SHA256(),
            backend=backends.default_backend()
        )

        # Serialization of the root CA certificate
        from cryptography.hazmat.primitives import serialization
        root_ca_path = os.path.join(cert_path, "rootCA.crt")
        with open(root_ca_path, "wb") as f:
            f.write(ca_cert.public_bytes(serialization.Encoding.PEM))

        # --------------------------------
        # Server Key
        # --------------------------------
        server_key = rsa.generate_private_key(
            public_exponent=65537,
            key_size=2048,
            backend=backends.default_backend())

        # cSpell:ignore PKCS
        # Serialization of the Private Key
        pem = server_key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=serialization.NoEncryption()
        )

        server_key_path = os.path.join(cert_path, "server.key")
        with open(server_key_path, 'wb') as pem_out:
            pem_out.write(pem)

        # ------------------
        # Server Certificate
        # ------------------
        cert_subject = x509.Name([
            x509.NameAttribute(NameOID.COUNTRY_NAME, u"US"),
            x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"Texas"),
            x509.NameAttribute(NameOID.LOCALITY_NAME, u"Austin"),
            x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"Oracle"),
            x509.NameAttribute(
                NameOID.ORGANIZATIONAL_UNIT_NAME, u"MySQL"),
            x509.NameAttribute(NameOID.EMAIL_ADDRESS,
                               u"support@mysql.com"),
            x509.NameAttribute(NameOID.COMMON_NAME, u"localhost"),
        ])
        server_public_key = server_key.public_key()
        builder = x509.CertificateBuilder()
        builder = builder.issuer_name(ca_cert.issuer)

        builder = builder.subject_name(cert_subject)
        builder = builder.not_valid_before(today - one_day)
        builder = builder.not_valid_after(today + validity)

        builder = builder.add_extension(
            x509.SubjectAlternativeName(
                [x509.DNSName(u'localhost')]
            ),
            critical=False
        )

        builder = builder.serial_number(x509.random_serial_number())
        builder = builder.public_key(server_public_key)
        builder = builder.add_extension(
            x509.BasicConstraints(
                ca=False, path_length=None), critical=False)

        # Sets the CA Authority for the server cert using the CA Key
        authority_key = x509.AuthorityKeyIdentifier.from_issuer_public_key(
            ca_public_key)
        builder = builder.add_extension(authority_key, critical=False)

        # Defines the use cases for the server certificate
        key_usage = x509.KeyUsage(digital_signature=True, key_encipherment=True, key_cert_sign=False,
                                  key_agreement=False, content_commitment=True, data_encipherment=True,
                                  crl_sign=False, encipher_only=False, decipher_only=False)
        builder = builder.add_extension(key_usage, critical=False)

        builder = builder.add_extension(
            x509.ExtendedKeyUsage([x509.oid.ExtendedKeyUsageOID.SERVER_AUTH]), critical=False
        )

        # Signs the server certificate using the CA private key
        certificate = builder.sign(
            private_key=ca_private_key, algorithm=hashes.SHA256(), backend=backends.default_backend())

        # Serialization of the Server Certificate
        server_crt_path = os.path.join(cert_path, "server.crt")
        with open(server_crt_path, "wb") as f:
            f.write(certificate.public_bytes(serialization.Encoding.PEM))

    except Exception as e:
        logger.exception(e)
        return False

    return True
Creates a certificate and related files

    Args:
        cert_path (str): The path the certificate and related files should
            be created in

    Returns:
       The output of the process
    
Creates a certificate and related files
Creates a certificate and related files
def create_certificate(cert_path):
    try:
        subject = issuer = x509.Name([
            x509.NameAttribute(NameOID.COUNTRY_NAME, u"US"),
            x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"Texas"),
            x509.NameAttribute(NameOID.LOCALITY_NAME, u"Austin"),
            x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"Oracle"),
            x509.NameAttribute(
                NameOID.ORGANIZATIONAL_UNIT_NAME, u"MySQL"),
            x509.NameAttribute(NameOID.EMAIL_ADDRESS,
                               u"support@mysql.com"),
            x509.NameAttribute(NameOID.COMMON_NAME,
                               u"MySQL Shell Auto Generated CA Certificate"),
        ])
        today = datetime.datetime.today()
        one_day = datetime.timedelta(1, 0, 0)
        validity = one_day * 365 * 10
        if platform.system() == "Darwin":
            validity = one_day * 824
        ca_private_key = rsa.generate_private_key(
            public_exponent=65537,
            key_size=2048,
            backend=backends.default_backend())
        from cryptography.hazmat.primitives import hashes
        ca_public_key = ca_private_key.public_key()
        ca_builder = x509.CertificateBuilder()
        ca_builder = ca_builder.subject_name(subject)
        ca_builder = ca_builder.issuer_name(issuer)
        ca_builder = ca_builder.not_valid_before(today - one_day)
        ca_builder = ca_builder.not_valid_after(today + validity)
        ca_builder = ca_builder.serial_number(x509.random_serial_number())
        ca_builder = ca_builder.public_key(ca_public_key)
        ca_builder = ca_builder.add_extension(
            x509.BasicConstraints(ca=True, path_length=None), critical=False)
        ca_builder = ca_builder.add_extension(
            x509.SubjectAlternativeName(
                [x509.DNSName(u'localhost')]
            ),
            critical=False
        )
        ca_cert = ca_builder.sign(
            private_key=ca_private_key, algorithm=hashes.SHA256(),
            backend=backends.default_backend()
        )
        from cryptography.hazmat.primitives import serialization
        root_ca_path = os.path.join(cert_path, "rootCA.crt")
        with open(root_ca_path, "wb") as f:
            f.write(ca_cert.public_bytes(serialization.Encoding.PEM))
        server_key = rsa.generate_private_key(
            public_exponent=65537,
            key_size=2048,
            backend=backends.default_backend())
        pem = server_key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=serialization.NoEncryption()
        )
        server_key_path = os.path.join(cert_path, "server.key")
        with open(server_key_path, 'wb') as pem_out:
            pem_out.write(pem)
        cert_subject = x509.Name([
            x509.NameAttribute(NameOID.COUNTRY_NAME, u"US"),
            x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, u"Texas"),
            x509.NameAttribute(NameOID.LOCALITY_NAME, u"Austin"),
            x509.NameAttribute(NameOID.ORGANIZATION_NAME, u"Oracle"),
            x509.NameAttribute(
                NameOID.ORGANIZATIONAL_UNIT_NAME, u"MySQL"),
            x509.NameAttribute(NameOID.EMAIL_ADDRESS,
                               u"support@mysql.com"),
            x509.NameAttribute(NameOID.COMMON_NAME, u"localhost"),
        ])
        server_public_key = server_key.public_key()
        builder = x509.CertificateBuilder()
        builder = builder.issuer_name(ca_cert.issuer)
        builder = builder.subject_name(cert_subject)
        builder = builder.not_valid_before(today - one_day)
        builder = builder.not_valid_after(today + validity)
        builder = builder.add_extension(
            x509.SubjectAlternativeName(
                [x509.DNSName(u'localhost')]
            ),
            critical=False
        )
        builder = builder.serial_number(x509.random_serial_number())
        builder = builder.public_key(server_public_key)
        builder = builder.add_extension(
            x509.BasicConstraints(
                ca=False, path_length=None), critical=False)
        authority_key = x509.AuthorityKeyIdentifier.from_issuer_public_key(
            ca_public_key)
        builder = builder.add_extension(authority_key, critical=False)
        key_usage = x509.KeyUsage(digital_signature=True, key_encipherment=True, key_cert_sign=False,
                                  key_agreement=False, content_commitment=True, data_encipherment=True,
                                  crl_sign=False, encipher_only=False, decipher_only=False)
        builder = builder.add_extension(key_usage, critical=False)
        builder = builder.add_extension(
            x509.ExtendedKeyUsage([x509.oid.ExtendedKeyUsageOID.SERVER_AUTH]), critical=False
        )
        certificate = builder.sign(
            private_key=ca_private_key, algorithm=hashes.SHA256(), backend=backends.default_backend())
        server_crt_path = os.path.join(cert_path, "server.crt")
        with open(server_crt_path, "wb") as f:
            f.write(certificate.public_bytes(serialization.Encoding.PEM))
    except Exception as e:
        logger.exception(e)
        return False
    return True
def create_certificate ( cert_path ) : try : subject = issuer = x509 . Name ( [ x509 . NameAttribute ( NameOID . COUNTRY_NAME , u"US" ) , x509 . NameAttribute ( NameOID . STATE_OR_PROVINCE_NAME , u"Texas" ) , x509 . NameAttribute ( NameOID . LOCALITY_NAME , u"Austin" ) , x509 . NameAttribute ( NameOID . ORGANIZATION_NAME , u"Oracle" ) , x509 . NameAttribute ( NameOID . ORGANIZATIONAL_UNIT_NAME , u"MySQL" ) , x509 . NameAttribute ( NameOID . EMAIL_ADDRESS , u"support@mysql.com" ) , x509 . NameAttribute ( NameOID . COMMON_NAME , u"MySQL Shell Auto Generated CA Certificate" ) , ] ) today = datetime . datetime . today ( ) one_day = datetime . timedelta ( 1 , 0 , 0 ) validity = one_day * 365 * 10 if platform . system ( ) == "Darwin" : validity = one_day * 824 ca_private_key = rsa . generate_private_key ( public_exponent = 65537 , key_size = 2048 , backend = backends . default_backend ( ) ) from cryptography . hazmat . primitives import hashes ca_public_key = ca_private_key . public_key ( ) ca_builder = x509 . CertificateBuilder ( ) ca_builder = ca_builder . subject_name ( subject ) ca_builder = ca_builder . issuer_name ( issuer ) ca_builder = ca_builder . not_valid_before ( today - one_day ) ca_builder = ca_builder . not_valid_after ( today + validity ) ca_builder = ca_builder . serial_number ( x509 . random_serial_number ( ) ) ca_builder = ca_builder . public_key ( ca_public_key ) ca_builder = ca_builder . add_extension ( x509 . BasicConstraints ( ca = True , path_length = None ) , critical = False ) ca_builder = ca_builder . add_extension ( x509 . SubjectAlternativeName ( [ x509 . DNSName ( u'localhost' ) ] ) , critical = False ) ca_cert = ca_builder . sign ( private_key = ca_private_key , algorithm = hashes . SHA256 ( ) , backend = backends . default_backend ( ) ) from cryptography . hazmat . primitives import serialization root_ca_path = os . path . join ( cert_path , "rootCA.crt" ) with open ( root_ca_path , "wb" ) as f : f . write ( ca_cert . public_bytes ( serialization . Encoding . PEM ) ) server_key = rsa . generate_private_key ( public_exponent = 65537 , key_size = 2048 , backend = backends . default_backend ( ) ) pem = server_key . private_bytes ( encoding = serialization . Encoding . PEM , format = serialization . PrivateFormat . PKCS8 , encryption_algorithm = serialization . NoEncryption ( ) ) server_key_path = os . path . join ( cert_path , "server.key" ) with open ( server_key_path , 'wb' ) as pem_out : pem_out . write ( pem ) cert_subject = x509 . Name ( [ x509 . NameAttribute ( NameOID . COUNTRY_NAME , u"US" ) , x509 . NameAttribute ( NameOID . STATE_OR_PROVINCE_NAME , u"Texas" ) , x509 . NameAttribute ( NameOID . LOCALITY_NAME , u"Austin" ) , x509 . NameAttribute ( NameOID . ORGANIZATION_NAME , u"Oracle" ) , x509 . NameAttribute ( NameOID . ORGANIZATIONAL_UNIT_NAME , u"MySQL" ) , x509 . NameAttribute ( NameOID . EMAIL_ADDRESS , u"support@mysql.com" ) , x509 . NameAttribute ( NameOID . COMMON_NAME , u"localhost" ) , ] ) server_public_key = server_key . public_key ( ) builder = x509 . CertificateBuilder ( ) builder = builder . issuer_name ( ca_cert . issuer ) builder = builder . subject_name ( cert_subject ) builder = builder . not_valid_before ( today - one_day ) builder = builder . not_valid_after ( today + validity ) builder = builder . add_extension ( x509 . SubjectAlternativeName ( [ x509 . DNSName ( u'localhost' ) ] ) , critical = False ) builder = builder . serial_number ( x509 . random_serial_number ( ) ) builder = builder . public_key ( server_public_key ) builder = builder . add_extension ( x509 . BasicConstraints ( ca = False , path_length = None ) , critical = False ) authority_key = x509 . AuthorityKeyIdentifier . from_issuer_public_key ( ca_public_key ) builder = builder . add_extension ( authority_key , critical = False ) key_usage = x509 . KeyUsage ( digital_signature = True , key_encipherment = True , key_cert_sign = False , key_agreement = False , content_commitment = True , data_encipherment = True , crl_sign = False , encipher_only = False , decipher_only = False ) builder = builder . add_extension ( key_usage , critical = False ) builder = builder . add_extension ( x509 . ExtendedKeyUsage ( [ x509 . oid . ExtendedKeyUsageOID . SERVER_AUTH ] ) , critical = False ) certificate = builder . sign ( private_key = ca_private_key , algorithm = hashes . SHA256 ( ) , backend = backends . default_backend ( ) ) server_crt_path = os . path . join ( cert_path , "server.crt" ) with open ( server_crt_path , "wb" ) as f : f . write ( certificate . public_bytes ( serialization . Encoding . PEM ) ) except Exception as e : logger . exception ( e ) return False return True
Creates a certificate and related files
Creates a certificate and related files
"""Creates a certificate and related files

    Args:
        cert_path (str): The path the certificate and related files should
            be created in

    Returns:
       The output of the process
    """ # ---------------------------------------------------- # Attributes to be used on the certificate generation # ---------------------------------------------------- # Default validity for the certificates is 10 years # In macOS TLS server certificates must have a validity period of 825 # days or fewer: to be set as today + validity # --------------------------------------------- # Generation of the Private Key for the Root CA # --------------------------------------------- # -------------------------------- # Generation of the CA certificate # -------------------------------- # CA should be True in order enable registration in Firefox # Signs the CA certificate and creates the file # Serialization of the root CA certificate # -------------------------------- # Server Key # -------------------------------- # cSpell:ignore PKCS # Serialization of the Private Key # ------------------ # Server Certificate # ------------------ # Sets the CA Authority for the server cert using the CA Key # Defines the use cases for the server certificate # Signs the server certificate using the CA private key # Serialization of the Server Certificate
The output of the process
The output of the process
cert_path
The path the certificate and related files should
be created in
The path the certificate and related files should be created in