gpgsm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* gpgsm.c - Talking to gpgsm.
  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 Foundation'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 <assert.h>
  30. #include <locale.h>
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <stdbool.h>
  34. #include <time.h>
  35. #include <assuan.h>
  36. #include <gpg-error.h>
  37. #include "cryptoki.h"
  38. #include "support.h"
  39. #include "cert.h"
  40. #include "gpgsm.h"
  41. struct search
  42. {
  43. bool found;
  44. cert_get_cb_t cert_get_cb;
  45. void *hook;
  46. };
  47. static gpg_error_t
  48. search_cb (void *hook, struct cert *cert)
  49. {
  50. struct search *ctx = hook;
  51. gpg_error_t err = 0;
  52. CK_ATTRIBUTE_PTR attrp;
  53. CK_ULONG attr_countp;
  54. /* Add the private key object only once. */
  55. if (!ctx->found)
  56. {
  57. err = scute_attr_prv (cert, &attrp, &attr_countp);
  58. if (err)
  59. return err;
  60. err = (*ctx->cert_get_cb) (ctx->hook, attrp, attr_countp);
  61. if (err)
  62. {
  63. scute_attr_free (attrp, attr_countp);
  64. return err;
  65. }
  66. ctx->found = true;
  67. }
  68. /* Add the certificate chain recursively before adding the
  69. certificate. */
  70. if (strcmp (cert->chain_id, cert->fpr))
  71. err = scute_gpgsm_search_certs_by_fpr (cert->chain_id, search_cb, ctx);
  72. /* Turn this certificate into a certificate object. */
  73. err = scute_attr_cert (cert, &attrp, &attr_countp);
  74. if (err)
  75. return err;
  76. err = (*ctx->cert_get_cb) (ctx->hook, attrp, attr_countp);
  77. if (err)
  78. {
  79. scute_attr_free (attrp, attr_countp);
  80. return err;
  81. }
  82. return err;
  83. }
  84. /* Create the attributes required for a new certificate object.
  85. Returns allocated attributes for the certificate object in ATTRP
  86. and ATTR_COUNTP, and for the private key object in PRV_ATTRP
  87. and PRV_ATTR_COUNTP. */
  88. gpg_error_t
  89. scute_gpgsm_get_cert (char *grip, cert_get_cb_t cert_get_cb, void *hook)
  90. {
  91. gpg_error_t err;
  92. struct search search;
  93. search.found = false;
  94. search.cert_get_cb = cert_get_cb;
  95. search.hook = hook;
  96. err = scute_gpgsm_search_certs_by_grip (grip, search_cb, &search);
  97. return err;
  98. }