tpm2-software / tpm2-pytss

Python bindings for TSS
https://tpm2-pytss.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
64 stars 45 forks source link

flush parent handle durint create and load #595

Closed salrashid123 closed 1 month ago

salrashid123 commented 2 months ago

the parent context isn't flushed during a create and load commands cleanly which causes issues in using pytss with a swtpm (no resource mgr, etc).

suggestion is to add in the flush commands shown below to tpm2_pytss/tsskey.py.

i can submit a small pr if needed

    def load(self, ectx, password=None):
        if not password and not self.empty_auth:
            raise RuntimeError("no password specified but it is required")
        elif password and self.empty_auth:
            warnings.warn("password specified but empty_auth is true")
        phandle = self._getparent(ectx, self.public.publicArea.type, self.parent)
        handle = ectx.load(phandle, self.private, self.public)
        ectx.tr_set_auth(handle, password)
        ectx.flush_context(phandle)  # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        return handle

    @classmethod
    def create(cls, ectx, template, parent=lib.TPM2_RH_OWNER, password=None):
        insens = TPM2B_SENSITIVE_CREATE()
        emptyauth = True
        if password:
            insens.sensitive.userAuth = password
            emptyauth = False
        phandle = cls._getparent(ectx, template.type, parent)
        private, public, _, _, _ = ectx.create(
            parent_handle=phandle,
            in_sensitive=insens,
            in_public=TPM2B_PUBLIC(publicArea=template),
            outside_info=TPM2B_DATA(),
            creation_pcr=TPML_PCR_SELECTION(),
        )
        ectx.flush_context(phandle)        # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        return cls(private, public, emptyauth, parent)
whooo commented 2 months ago

We have a ticket open for this kind of issue, https://github.com/tpm2-software/tpm2-pytss/issues/372 A context manager might be better then just flush context in case of any exception