Ejemplo n.º 1
0
    def add_cert(self, cert_obj, cert_file=None):
        """
        Adds a single cert into the database it is
        important to make the checks and see if you have it
        already there ...
        """
        # some sanity checks ...
        self.__initialize_db_ifnot()

        cert_subj = cert_obj.person_info()
        cert_hash = cert_obj.cert_hash()

        # firstly make a search for that cert in db wanto go deepr ?
        cert_result = self.search_and_get_cert(cert_hash)
        if cert_result:
            for cert in cert_result:
                # we can compare two cert you know :)
                if cert == cert_obj:
                    print "The cert you are trying to add already exists into db"
                    return False

        if cert_file:
            cert_file = self.__generate_filename(cert_file)
        else:
            # a default entry
            cert_file = self.__generate_filename("cert")

        cert_entry = self.__create_entry_index(cert_obj, cert_file, is_chain=False)
        write_index_data(self.__db_dir, cert_entry)
        cert_obj.store_to_file(cert_file)
        # reload the stuff
        self.load_db()
        return True
Ejemplo n.º 2
0
    def add_cert_chain(self, cert_chain_obj, chain_file=None):
        """
        Adds a chain into the db
        first check if it is a valid chain
        and also check if you have the exact chain into the db
        """

        # firstly make a search
        compare_chains = self.search_and_get_chain("*")  # get all chains
        if compare_chains:  # look insite em and search for a match
            for chain in compare_chains:
                if chain == cert_chain_obj:
                    print "The chain you try to insert into db already exists"
                    return False

        if chain_file:
            chain_file = self.__generate_filename(chain_file)
        else:
            # a default entry
            chain_file = self.__generate_filename("chain")

        # add one by one to the index file
        for cert_store in cert_chain_obj:
            cert_entry = self.__create_entry_index(
                cert_store, chain_file, is_chain=True, chain_hash=cert_chain_obj.get_chain_hash()
            )
            write_index_data(self.__db_dir, cert_entry)
        # store the file into a chain file
        cert_chain_obj.store_to_file(chain_file)
        # reload the stuff
        self.load_db()
        return True
Ejemplo n.º 3
0
    def recreate_internal_db(self):
        """
        Recreating the internal db because it is corrupted
        or not exists .The internal structure for every cert will be like :
        
            'cert_hash':{
                'cert_subject':"value of the subject",
                'cert_file':"value of the file name",
                'chain':True,False
            }
            
        """
        from imzaci.util.cert_util import parse_pem_cert
        from imzaci.cert.chain_manager import chain_manager_factory, X509ChainManager

        internal_file_path = os.path.join(self.__db_dir, INTERNAL_DB_FILE)
        if os.path.exists(internal_file_path):
            index_files = glob.glob("".join([internal_file_path, "*"]))
            # print "The index files to remove : ",index_files
            for index_file in index_files:
                os.remove(index_file)

        possible_certs = glob.glob("".join([self.__db_dir, "/", "*.pem"]))
        if not possible_certs:
            write_index_data(self.__db_dir, {})
            return True

        for cert_file in possible_certs:
            parsed_object = parse_pem_cert(cert_file)
            if not parsed_object:
                continue

            if len(parsed_object) > 1:  # it may be a chain
                chain = chain_manager_factory(parsed_object, X509ChainManager.X509_CERT)
                if not chain:  # it seems we dont have a valid chain here
                    continue
                else:
                    for c in chain:
                        cert_entry = self.__create_entry_index(
                            c, cert_file, is_chain=True, chain_hash=chain.get_chain_hash()
                        )
                        write_index_data(self.__db_dir, cert_entry)
            else:
                # it is a single one
                cert_entry = self.__create_entry_index(parsed_object[0], cert_file, is_chain=False)
                write_index_data(self.__db_dir, cert_entry)
        return True