// NewTPMContext initialises a new TPM context and sets up the // internal TSS structures. func NewTPMContext() (*TPMContext, error) { var result C.TSS_RESULT var ctx TPMContext C.Tspi_Context_Create(&ctx.ctx) result = C.Tspi_Context_Connect(ctx.ctx, nil) if result != C.TSS_SUCCESS { switch result { case C.TSS_E_INVALID_HANDLE: return nil, ErrInvalidHandle case C.TSS_E_INTERNAL_ERROR: return nil, ErrInternalError default: return nil, ErrUnknown } } result = C.Tspi_Context_GetTpmObject(ctx.ctx, &ctx.tpm) if result != C.TSS_SUCCESS { switch result { case C.TSS_E_INVALID_HANDLE: return nil, ErrInvalidHandle case C.TSS_E_INTERNAL_ERROR: return nil, ErrInternalError case C.TSS_E_BAD_PARAMETER: return nil, ErrBadParameter default: return nil, ErrUnknown } } return &ctx, nil }
// Connect opens a connection between the context and the TSS daemon. It // returns an error on failure. func (context *Context) Connect() error { var tpmhandle C.TSS_HTPM err := tspiError(C.Tspi_Context_Connect(context.context, nil)) if err != nil { return err } C.Tspi_Context_GetTpmObject(context.context, &tpmhandle) context.tpm = TPM{handle: tpmhandle, context: context.context} return nil }
func NewContext() (*Context, error) { c := new(Context) if result := C.Tspi_Context_Create(&c.ctx); isError(result) { return nil, Error{result} } if result := C.Tspi_Context_Connect(c.ctx, nil /* local TPM */); isError(result) { return nil, Error{result} } if result := C.Tspi_Context_GetTpmObject(c.ctx, &c.tpm); isError(result) { return nil, Error{result} } return c, nil }