Example #1
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = Fernet.generate_key().decode('utf-8')

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        with mktemppath() as jks_tmp:
            create_keystore(body, chain, jks_tmp, key, alias, passphrase)

            with open(jks_tmp, 'rb') as f:
                raw = f.read()

        return "jks", passphrase, raw
Example #2
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = Fernet.generate_key().decode('utf-8')

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        with mktemppath() as jks_tmp:
            create_keystore(body, chain, jks_tmp, key, alias, passphrase)

            with open(jks_tmp, 'rb') as f:
                raw = f.read()

        return "jks", passphrase, raw
Example #3
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore or Truststore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """
        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = get_psuedo_random_string()

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        type = self.get_option('type', options)

        with mktemppath() as output_tmp:
            if type == 'PKCS12 (.p12)':
                create_pkcs12(body, output_tmp, key, alias, passphrase)
                extension = "p12"
            else:
                raise Exception(
                    "Unable to export, unsupported type: {0}".format(type))

            with open(output_tmp, 'rb') as f:
                raw = f.read()

        return extension, passphrase, raw
Example #4
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = Fernet.generate_key()

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        with mktemppath() as jks_tmp:
            if not key:
                raise Exception("Unable to export, no private key found.")

            create_keystore(body, chain, jks_tmp, key, alias, passphrase)

            with open(jks_tmp, 'rb') as f:
                raw = f.read()

        return "jks", passphrase, raw
Example #5
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Truststore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = get_psuedo_random_string()

        with mktemppath() as jks_tmp:
            create_truststore(body, chain, jks_tmp, alias, passphrase)

            with open(jks_tmp, 'rb') as f:
                raw = f.read()

        return "jks", passphrase, raw
Example #6
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore or Truststore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """
        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = get_psuedo_random_string()

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        type = self.get_option('type', options)

        with mktemppath() as output_tmp:
            if type == 'PKCS12 (.p12)':
                create_pkcs12(body, chain, output_tmp, key, alias, passphrase)
                extension = "p12"
            else:
                raise Exception("Unable to export, unsupported type: {0}".format(type))

            with open(output_tmp, 'rb') as f:
                raw = f.read()

        return extension, passphrase, raw
Example #7
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Truststore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option("alias", options):
            alias = self.get_option("alias", options)
        else:
            alias = "blah"

        if self.get_option("passphrase", options):
            passphrase = self.get_option("passphrase", options)
        else:
            passphrase = Fernet.generate_key()

        with mktemppath() as jks_tmp:
            create_truststore(body, chain, jks_tmp, alias, passphrase)

            with open(jks_tmp, "rb") as f:
                raw = f.read()

        return "jks", passphrase, raw
Example #8
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore or Truststore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = get_psuedo_random_string()

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        type = self.get_option('type', options)

        with mktemppath() as jks_tmp:
            if type == 'Truststore (JKS)':
                create_truststore(body, chain, jks_tmp, alias, passphrase)

            elif type == 'Keystore (JKS)':
                if not key:
                    raise Exception("Unable to export, no private key found.")

                create_truststore(body, chain, jks_tmp, alias, passphrase)
                create_keystore(body, jks_tmp, key, alias, passphrase)

            else:
                raise Exception("Unable to export, unsupported type: {0}".format(type))

            with open(jks_tmp, 'rb') as f:
                raw = f.read()

        return "jks", passphrase, raw
Example #9
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Creates CSR from certificate

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """
        with mktemppath() as output_tmp:
            if not key:
                raise Exception("Private Key required by CSR")

            create_csr(body, chain, output_tmp, key)
            extension = "csr"

            with open(output_tmp, "rb") as f:
                raw = f.read()
        # passphrase is None
        return extension, None, raw
Example #10
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Creates CSR from certificate

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """
        with mktemppath() as output_tmp:
            if not key:
                raise Exception("Private Key required by CSR")

            create_csr(body, chain, output_tmp, key)
            extension = "csr"

            with open(output_tmp, 'rb') as f:
                raw = f.read()
        # passphrase is None
        return extension, None, raw
Example #11
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a PKCS#12 archive.

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """
        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = get_psuedo_random_string()

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = common_name(parse_certificate(body))

        type = self.get_option('type', options)

        with mktemppath() as output_tmp:
            if type == 'PKCS12 (.p12)':
                if not key:
                    raise Exception("Private Key required by {0}".format(type))

                create_pkcs12(body, chain, output_tmp, key, alias, passphrase)
                extension = "p12"
            else:
                raise Exception(
                    "Unable to export, unsupported type: {0}".format(type))

            with open(output_tmp, 'rb') as f:
                raw = f.read()

        return extension, passphrase, raw
Example #12
0
    def export(self, body, chain, key, options, **kwargs):
        """
        Generates a Java Keystore or Truststore

        :param key:
        :param chain:
        :param body:
        :param options:
        :param kwargs:
        """

        if self.get_option('passphrase', options):
            passphrase = self.get_option('passphrase', options)
        else:
            passphrase = get_psuedo_random_string()

        if self.get_option('alias', options):
            alias = self.get_option('alias', options)
        else:
            alias = "blah"

        if not key:
            raise Exception("Unable to export, no private key found.")

        with mktempfile() as cert_tmp:
            with open(cert_tmp, 'w') as f:
                f.write(body)

            with mktempfile() as key_tmp:
                with open(key_tmp, 'w') as f:
                    f.write(key)

                # Create PKCS12 keystore from private key and public certificate
                with mktempfile() as p12_tmp:
                    run_process([
                        "openssl",
                        "pkcs12",
                        "-export",
                        "-name", alias,
                        "-in", cert_tmp,
                        "-inkey", key_tmp,
                        "-out", p12_tmp,
                        "-password", "pass:{}".format(passphrase)
                    ])

                    # Convert PKCS12 keystore into a JKS keystore
                    with mktemppath() as jks_tmp:
                        run_process([
                            "keytool",
                            "-importkeystore",
                            "-destkeystore", jks_tmp,
                            "-srckeystore", p12_tmp,
                            "-srcstoretype", "PKCS12",
                            "-alias", alias,
                            "-srcstorepass", passphrase,
                            "-deststorepass", passphrase
                        ])

                        # Import leaf cert in to JKS keystore
                        run_process([
                            "keytool",
                            "-importcert",
                            "-file", cert_tmp,
                            "-keystore", jks_tmp,
                            "-alias", "{0}_cert".format(alias),
                            "-storepass", passphrase,
                            "-noprompt"
                        ])

                        # Import the entire chain
                        for idx, cert in enumerate(split_chain(chain)):
                            with mktempfile() as c_tmp:
                                with open(c_tmp, 'w') as f:
                                    f.write(cert)
                                # Import signed cert in to JKS keystore
                                run_process([
                                    "keytool",
                                    "-importcert",
                                    "-file", c_tmp,
                                    "-keystore", jks_tmp,
                                    "-alias", "{0}_cert_{1}".format(alias, idx),
                                    "-storepass", passphrase,
                                    "-noprompt"
                                ])

                        with open(jks_tmp, 'rb') as f:
                            raw = f.read()

                        return "jks", passphrase, raw