google / go-tpm

Apache License 2.0
562 stars 161 forks source link

missing Name for 'SignHandle' parameter #361

Open jeremyhahn opened 5 months ago

jeremyhahn commented 5 months ago

I'm running into strange behavior. I'm getting this error when I invoke my remote attestation process via grpc to issue a quote, but when I run the same method the grpc service is calling via a unit test, it passes without the error.

The test is here: https://github.com/jeremyhahn/go-trusted-platform/blob/fdcdbf0f626afb7c9a80311fbdcd2ccb3bcb3a55/tpm2/tpm_test.go#L59

The quote method is here: https://github.com/jeremyhahn/go-trusted-platform/blob/fdcdbf0f626afb7c9a80311fbdcd2ccb3bcb3a55/tpm2/tpm.go#L1081

To run the remote attestation service, from the project root:

Attestor

make attestor

Verifier

make verifier

Verifier log: rpc error: code = Unknown desc = missing Name for 'SignHandle' parameter

If I change the KnownName() method in tpm2/structures.go to the include TPMIDHSavedTransientClear in the Empty Buffer return, the error goes away and the service runs as expected.

func (h TPMHandle) KnownName() *TPM2BName {
    switch (TPMHT)(h >> 24) {
    case TPMHTPCR, TPMHTHMACSession, TPMHTPolicySession, TPMHTPermanent:
        result := make([]byte, 4)
        binary.BigEndian.PutUint32(result, h.HandleValue())
        return &TPM2BName{Buffer: result}
    case TPMHTTransient:
        // The Name of a sequence object is an Empty Buffer
        // See part 1: Architecture, section 32.4.5
        if h == TPMIDHSavedSequence || h == TPMIDHSavedTransientClear {
            return &TPM2BName{
                Buffer: []byte{},
            }
        }
    }
    return nil
}

I'm just getting my feet wet with the tpm2 library and don't fully understand the implications of this change. It appears this code is intentionally ignoring transient handles with the stClear attribute set (which seems to make sense). I've scoured the internet and TCG docs trying to find anything that specifies whether the stClear attribute should also default to an Empty Buffer but I haven't had any luck.

Is this a bug in the KnownName method that needs to be fixed or am I barking up the wrong tree? Any insights into why the code works fine in the unit tests but fails during the grpc calls?

chrisfenner commented 5 months ago

Thanks for filing this @jeremyhahn! I suspect you've found an edge-case in object naming that's not handled well by KnownName. Let me see if I can reproduce your error and come to a working theory of the issue.

Is your device/TPM undergoing any type of power state transition?

jeremyhahn commented 5 months ago

The tests are using the simulator but I get the same error on a real TPM. No power state transitions.

nckrss commented 2 months ago

@jeremyhahn you may have been seeing a false positive in the test related to #370.

In your example you need to use a NamedHandle or AuthHandle to provide the Name because CreateLoadedResponse returns a transient handle, which means that the Name is calculated and not known based on the Most Significant Octet of the handle. Since it is calculated (Part 1 Architecture Section 16 Table 3) you must provide it from the response, hence the CreateLoadedResponse.Name field.