error-mapping.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* error-mapping.c - Scute error mapping.
  2. Copyright (C) 2006 g10 Code GmbH
  3. This file is part of Scute.
  4. Scute is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Scute is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Scute; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, g10 Code GmbH gives permission
  16. to link this library: with the Mozilla Foundation's code for
  17. Mozilla (or with modified versions of it that use the same license
  18. as the "Mozilla" code), and distribute the linked executables. You
  19. must obey the GNU General Public License in all respects for all of
  20. the code used other than "Mozilla". If you modify this file, you
  21. may extend this exception to your version of the file, but you are
  22. not obligated to do so. If you do not wish to do so, delete this
  23. exception statement from your version. */
  24. #if HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <errno.h>
  28. #include <gpg-error.h>
  29. #include "cryptoki.h"
  30. #include "debug.h"
  31. #include "error-mapping.h"
  32. /* Map a system error code to a cryptoki return value. */
  33. CK_RV
  34. scute_sys_to_ck (int err)
  35. {
  36. switch (err)
  37. {
  38. case 0:
  39. return CKR_OK;
  40. case ENOMEM:
  41. return CKR_HOST_MEMORY;
  42. default:
  43. /* CKR_GENERAL_ERROR is too strong. */
  44. return CKR_FUNCTION_FAILED;
  45. }
  46. }
  47. /* Map a GnuPG error code to a cryptoki return value. */
  48. CK_RV
  49. scute_gpg_err_to_ck (gpg_error_t err)
  50. {
  51. if (err)
  52. DEBUG (DBG_CRIT, "Error occurred: %s (%s)\n", gpg_strerror (err),
  53. gpg_strsource (err));
  54. switch (gpg_err_code (err))
  55. {
  56. case GPG_ERR_NO_ERROR:
  57. return CKR_OK;
  58. case GPG_ERR_NO_AGENT:
  59. return CKR_GENERAL_ERROR;
  60. case GPG_ERR_ENOMEM:
  61. return CKR_HOST_MEMORY;
  62. case GPG_ERR_BAD_PIN:
  63. return CKR_PIN_INCORRECT;
  64. case GPG_ERR_PIN_BLOCKED:
  65. return CKR_PIN_LOCKED;
  66. default:
  67. /* CKR_GENERAL_ERROR is too strong. */
  68. return CKR_FUNCTION_FAILED;
  69. }
  70. }