tss2-dlopen-tctildr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2021, Fraunhofer SIT
  4. * All rights reserved.
  5. *******************************************************************************/
  6. /**
  7. * The purpose of this file is to copy it into your project and
  8. * include it during compilation if you don't want to link against
  9. * libtss2-tctildr at compile time.
  10. * It will attempt to load libtss2-esys.so during runtime.
  11. * It will either work similarly to directly linking to libtss2-tctildr.so
  12. * at compile-time or return a NOT_IMPLEMENTED error.
  13. *
  14. * For new versions of this file, please check:
  15. * http://github.com/tpm2-software/tpm2-tss/tss2-dlopen
  16. */
  17. #include <dlfcn.h>
  18. #include <stdio.h>
  19. #include <tss2/tss2_tctildr.h>
  20. #define str(s) xstr(s)
  21. #define xstr(s) #s
  22. #ifdef ENABLE_WARN
  23. #define WARN(str, ...) do { fprintf(stderr, "WARNING: " str "\n", ## __VA_ARGS__); } while (0)
  24. #else /* ENABLE_WARN */
  25. #define WARN(...) do { } while (0)
  26. #endif /* ENABLE_WARN */
  27. #define LIB "libtss2-tctildr.so.0"
  28. static void *dlhandle = NULL;
  29. static TSS2_RC
  30. init_dlhandle(void)
  31. {
  32. if (dlhandle)
  33. return TSS2_RC_SUCCESS;
  34. dlhandle = dlopen(LIB, RTLD_NOW | RTLD_LOCAL);
  35. if (!dlhandle) {
  36. WARN("Library " LIB " not found: %s.", dlerror());
  37. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  38. }
  39. return TSS2_RC_SUCCESS;
  40. }
  41. TSS2_RC
  42. Tss2_TctiLdr_Initialize_Ex (const char *name,
  43. const char *conf,
  44. TSS2_TCTI_CONTEXT **context)
  45. {
  46. if (init_dlhandle() != TSS2_RC_SUCCESS)
  47. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  48. static TSS2_RC (*sym) (const char *name, const char *conf, TSS2_TCTI_CONTEXT **context) = NULL;
  49. if (!sym)
  50. sym = dlsym(dlhandle, "Tss2_TctiLdr_Initialize_Ex");
  51. if (!sym) {
  52. WARN("Function Tss2_TctiLdr_Initialize_Ex not found.");
  53. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  54. }
  55. return sym(name, conf, context);
  56. }
  57. TSS2_RC
  58. Tss2_TctiLdr_Initialize (const char *nameConf,
  59. TSS2_TCTI_CONTEXT **context)
  60. {
  61. if (init_dlhandle() != TSS2_RC_SUCCESS)
  62. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  63. static TSS2_RC (*sym) (const char *nameConf, TSS2_TCTI_CONTEXT **context) = NULL;
  64. if (!sym)
  65. sym = dlsym(dlhandle, "Tss2_TctiLdr_Initialize");
  66. if (!sym) {
  67. WARN("Function Tss2_TctiLdr_Initialize not found.");
  68. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  69. }
  70. return sym(nameConf, context);
  71. }
  72. TSS2_RC
  73. Tss2_TctiLdr_GetInfo (const char *name,
  74. TSS2_TCTI_INFO **info)
  75. {
  76. if (init_dlhandle() != TSS2_RC_SUCCESS)
  77. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  78. static TSS2_RC (*sym) (const char *name, TSS2_TCTI_INFO **info) = NULL;
  79. if (!sym)
  80. sym = dlsym(dlhandle, "Tss2_TctiLdr_GetInfo");
  81. if (!sym) {
  82. WARN("Function Tss2_TctiLdr_GetInfo not found.");
  83. return TSS2_TCTI_RC_NOT_IMPLEMENTED;
  84. }
  85. return sym(name, info);
  86. }
  87. void
  88. Tss2_TctiLdr_Finalize (TSS2_TCTI_CONTEXT **context)
  89. {
  90. if (!context || !*context)
  91. return;
  92. static void (*sym) (TSS2_TCTI_CONTEXT **context) = NULL;
  93. if (!sym)
  94. sym = dlsym(dlhandle, "Tss2_TctiLdr_Finalize");
  95. if (!sym) {
  96. WARN("Function Tss2_TctiLdr_Finalize not found.");
  97. return;
  98. }
  99. sym(context);
  100. }
  101. void
  102. Tss2_TctiLdr_FreeInfo (TSS2_TCTI_INFO **info)
  103. {
  104. if (!info || !*info)
  105. return;
  106. static void (*sym) (TSS2_TCTI_INFO **info) = NULL;
  107. if (!sym)
  108. sym = dlsym(dlhandle, "Tss2_TctiLdr_FreeInfo");
  109. if (!sym) {
  110. WARN("Function Tss2_TctiLdr_FreeInfo not found.");
  111. return;
  112. }
  113. sym(info);
  114. }