p11-initialize.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* p11-initialize.c - Cryptoki implementation.
  2. Copyright (C) 2006 g10 Code GmbH
  3. This file is part of Scute[1].
  4. [1] Derived from the RSA Security Inc. PKCS #11 Cryptographic Token
  5. Interface (Cryptoki).
  6. Scute is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. Scute is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Scute; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. In addition, as a special exception, g10 Code GmbH gives permission
  18. to link this library: with the Mozilla Fondations's code for
  19. Mozilla (or with modified versions of it that use the same license
  20. as the "Mozilla" code), and distribute the linked executables. You
  21. must obey the GNU General Public License in all respects for all of
  22. the code used other than "Mozilla". If you modify this file, you
  23. may extend this exception to your version of the file, but you are
  24. not obligated to do so. If you do not wish to do so, delete this
  25. exception statement from your version. */
  26. #if HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <stdbool.h>
  30. #include <assuan.h>
  31. #include <gpg-error.h>
  32. #include "cryptoki.h"
  33. #include "settings.h"
  34. #include "locking.h"
  35. #include "agent.h"
  36. #include "error-mapping.h"
  37. #include "slots.h"
  38. CK_DEFINE_FUNCTION(CK_RV, C_Initialize) (CK_VOID_PTR pInitArgs)
  39. {
  40. CK_RV err;
  41. /* This is one of the few functions which does not need to take the
  42. global lock. */
  43. /* Set the assuan error source, so that gpg_error_t becomes a valid
  44. substitute for assuan_error_t. */
  45. assuan_set_assuan_err_source (GPG_ERR_SOURCE_ANY);
  46. /* Check the threading configuration. */
  47. if (pInitArgs != NULL_PTR)
  48. {
  49. CK_C_INITIALIZE_ARGS_PTR args = pInitArgs;
  50. bool callbacks;
  51. if (args->pReserved != NULL_PTR)
  52. return CKR_ARGUMENTS_BAD;
  53. if (NEED_TO_CREATE_THREADS
  54. && (args->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS))
  55. return CKR_NEED_TO_CREATE_THREADS;
  56. /* Either all pointers are provided, or none are. */
  57. if (args->CreateMutex == NULL_PTR)
  58. {
  59. if (args->DestroyMutex != NULL_PTR || args->LockMutex != NULL_PTR
  60. || args->UnlockMutex != NULL_PTR)
  61. return CKR_ARGUMENTS_BAD;
  62. callbacks = false;
  63. }
  64. else
  65. {
  66. if (args->DestroyMutex == NULL_PTR || args->LockMutex == NULL_PTR
  67. || args->UnlockMutex == NULL_PTR)
  68. return CKR_ARGUMENTS_BAD;
  69. callbacks = true;
  70. }
  71. /* FIXME: At this point, we do not support using the native
  72. thread package. */
  73. if (!callbacks && (args->flags & CKF_OS_LOCKING_OK))
  74. return CKR_CANT_LOCK;
  75. }
  76. err = scute_locking_initialize (pInitArgs);
  77. if (err)
  78. return err;
  79. err = scute_agent_initialize ();
  80. if (err)
  81. {
  82. scute_locking_finalize ();
  83. return scute_gpg_err_to_ck (err);
  84. }
  85. err = scute_slots_initialize ();
  86. if (err)
  87. {
  88. scute_agent_finalize ();
  89. scute_locking_finalize ();
  90. return err;
  91. }
  92. return err;
  93. }