object.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. #include "files.h"
  3. #include "log.h"
  4. #include "object.h"
  5. #include "tool_rc.h"
  6. #include "tpm2_auth_util.h"
  7. #define NULL_OBJECT "null"
  8. #define NULL_OBJECT_LEN (sizeof(NULL_OBJECT) - 1)
  9. static tool_rc do_ctx_file(ESYS_CONTEXT *ctx, const char *objectstr, FILE *f,
  10. tpm2_loaded_object *outobject) {
  11. /* assign a dummy transient handle */
  12. outobject->handle = TPM2_TRANSIENT_FIRST;
  13. outobject->path = objectstr;
  14. return files_load_tpm_context_from_file(ctx, &outobject->tr_handle, f);
  15. }
  16. static tool_rc tpm2_util_object_load2(ESYS_CONTEXT *ctx, const char *objectstr,
  17. const char *auth,
  18. bool do_auth, tpm2_loaded_object *outobject,
  19. bool is_restricted_pswd_session, tpm2_handle_flags flags) {
  20. ESYS_CONTEXT *tmp_ctx = is_restricted_pswd_session ? NULL : ctx;
  21. if (do_auth) {
  22. tpm2_session *s = NULL;
  23. tool_rc rc = tpm2_auth_util_from_optarg(tmp_ctx, auth, &s,
  24. is_restricted_pswd_session);
  25. if (rc != tool_rc_success) {
  26. return rc;
  27. }
  28. outobject->session = s;
  29. }
  30. if (!objectstr) {
  31. LOG_ERR("object string is empty");
  32. return tool_rc_general_error;
  33. }
  34. // 1. Always attempt file
  35. FILE *f = fopen(objectstr, "rb");
  36. if (f) {
  37. tool_rc rc = do_ctx_file(ctx, objectstr, f, outobject);
  38. fclose(f);
  39. return rc;
  40. }
  41. // 2. Try to convert a hierarchy or raw handle
  42. TPMI_RH_PROVISION handle;
  43. bool result = tpm2_util_handle_from_optarg(objectstr, &handle, flags);
  44. if (result) {
  45. outobject->handle = handle;
  46. outobject->path = NULL;
  47. return tpm2_util_sys_handle_to_esys_handle(ctx, outobject->handle,
  48. &outobject->tr_handle);
  49. }
  50. LOG_ERR("Cannot make sense of object context \"%s\"", objectstr);
  51. return tool_rc_general_error;
  52. }
  53. tool_rc tpm2_util_object_load(ESYS_CONTEXT *ctx, const char *objectstr,
  54. tpm2_loaded_object *outobject, tpm2_handle_flags flags) {
  55. return tpm2_util_object_load2(ctx, objectstr, NULL, false, outobject,
  56. false, flags);
  57. }
  58. tool_rc tpm2_util_object_load_auth(ESYS_CONTEXT *ctx, const char *objectstr,
  59. const char *auth, tpm2_loaded_object *outobject,
  60. bool is_restricted_pswd_session, tpm2_handle_flags flags) {
  61. return tpm2_util_object_load2(ctx, objectstr, auth, true, outobject,
  62. is_restricted_pswd_session, flags);
  63. }