module_hooks.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* module_hooks.c -- module load/unload hooks for libwolfssl.ko
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/error-crypt.h>
  26. #include <wolfssl/ssl.h>
  27. #ifndef NO_CRYPT_TEST
  28. #include <wolfcrypt/test/test.h>
  29. #include <linux/delay.h>
  30. #endif
  31. static int libwolfssl_cleanup(void) {
  32. int ret;
  33. #ifdef WOLFCRYPT_ONLY
  34. ret = wolfCrypt_Cleanup();
  35. if (ret != 0)
  36. pr_err("wolfCrypt_Cleanup() failed: %s", wc_GetErrorString(ret));
  37. else
  38. pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
  39. #else
  40. ret = wolfSSL_Cleanup();
  41. if (ret != WOLFSSL_SUCCESS)
  42. pr_err("wolfSSL_Cleanup() failed: %s", wc_GetErrorString(ret));
  43. else
  44. pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " cleanup complete.\n");
  45. #endif
  46. return ret;
  47. }
  48. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  49. static int __init wolfssl_init(void)
  50. #else
  51. static int wolfssl_init(void)
  52. #endif
  53. {
  54. int ret;
  55. #ifdef WOLFCRYPT_ONLY
  56. ret = wolfCrypt_Init();
  57. if (ret != 0) {
  58. pr_err("wolfCrypt_Init() failed: %s", wc_GetErrorString(ret));
  59. return -ENOTRECOVERABLE;
  60. }
  61. #else
  62. ret = wolfSSL_Init();
  63. if (ret != WOLFSSL_SUCCESS) {
  64. pr_err("wolfSSL_Init() failed: %s", wc_GetErrorString(ret));
  65. return -ENOTRECOVERABLE;
  66. }
  67. #endif
  68. #ifndef NO_CRYPT_TEST
  69. ret = wolfcrypt_test(NULL);
  70. if (ret < 0) {
  71. pr_err("wolfcrypt self-test failed with return code %d.", ret);
  72. (void)libwolfssl_cleanup();
  73. msleep(10);
  74. return -ENOTRECOVERABLE;
  75. }
  76. pr_info("wolfCrypt self-test passed.\n");
  77. #endif
  78. #ifdef WOLFCRYPT_ONLY
  79. pr_info("wolfCrypt " LIBWOLFSSL_VERSION_STRING " loaded. See https://www.wolfssl.com/ for information.\n");
  80. #else
  81. pr_info("wolfSSL " LIBWOLFSSL_VERSION_STRING " loaded. See https://www.wolfssl.com/ for information.\n");
  82. #endif
  83. pr_info("Copyright (C) 2006-2020 wolfSSL Inc. All Rights Reserved.\n");
  84. return 0;
  85. }
  86. module_init(wolfssl_init);
  87. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  88. static void __exit wolfssl_exit(void)
  89. #else
  90. static void wolfssl_exit(void)
  91. #endif
  92. {
  93. (void)libwolfssl_cleanup();
  94. return;
  95. }
  96. module_exit(wolfssl_exit);
  97. MODULE_LICENSE("GPL v2");
  98. MODULE_AUTHOR("https://www.wolfssl.com/");
  99. MODULE_DESCRIPTION("libwolfssl cryptographic and protocol facilities");
  100. MODULE_VERSION(LIBWOLFSSL_VERSION_STRING);