2
0

opkg_pathfinder.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* vi: set noexpandtab sw=4 sts=4: */
  2. /* opkg_pathfinder.c - the opkg package management system
  3. Copyright (C) 2009 Camille Moncelier <moncelier@devlife.org>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program 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. */
  13. #include "config.h"
  14. #include <openssl/ssl.h>
  15. #include <libpathfinder.h>
  16. #include <stdlib.h>
  17. #if defined(HAVE_SSLCURL)
  18. #include <curl/curl.h>
  19. #endif
  20. #include "libbb/libbb.h"
  21. #include "opkg_message.h"
  22. #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
  23. /*
  24. * This callback is called instead of X509_verify_cert to perform path
  25. * validation on a certificate using pathfinder.
  26. *
  27. */
  28. static int pathfinder_verify_callback(X509_STORE_CTX * ctx, void *arg)
  29. {
  30. char *errmsg;
  31. const char *hex = "0123456789ABCDEF";
  32. size_t size = i2d_X509(ctx->cert, NULL);
  33. unsigned char *keybuf, *iend;
  34. iend = keybuf = xmalloc(size);
  35. i2d_X509(ctx->cert, &iend);
  36. char *certdata_str = xmalloc(size * 2 + 1);
  37. unsigned char *cp = keybuf;
  38. char *certdata_str_i = certdata_str;
  39. while (cp < iend) {
  40. unsigned char ch = *cp++;
  41. *certdata_str_i++ = hex[(ch >> 4) & 0xf];
  42. *certdata_str_i++ = hex[ch & 0xf];
  43. }
  44. *certdata_str_i = 0;
  45. free(keybuf);
  46. const char *policy = "2.5.29.32.0"; // anyPolicy
  47. int validated =
  48. pathfinder_dbus_verify(certdata_str, policy, 0, 0, &errmsg);
  49. if (!validated)
  50. opkg_msg(ERROR, "Path verification failed: %s.\n", errmsg);
  51. free(certdata_str);
  52. free(errmsg);
  53. return validated;
  54. }
  55. #endif
  56. #if defined(HAVE_OPENSSL)
  57. int pkcs7_pathfinder_verify_signers(PKCS7 * p7)
  58. {
  59. STACK_OF(X509) * signers;
  60. int i, ret = 1; /* signers are verified by default */
  61. signers = PKCS7_get0_signers(p7, NULL, 0);
  62. for (i = 0; i < sk_X509_num(signers); i++) {
  63. X509_STORE_CTX ctx = {
  64. .cert = sk_X509_value(signers, i),
  65. };
  66. if (!pathfinder_verify_callback(&ctx, NULL)) {
  67. /* Signer isn't verified ! goto jail; */
  68. ret = 0;
  69. break;
  70. }
  71. }
  72. sk_X509_free(signers);
  73. return ret;
  74. }
  75. #endif
  76. #if defined(HAVE_SSLCURL)
  77. CURLcode curl_ssl_ctx_function(CURL * curl, void *sslctx, void *parm)
  78. {
  79. SSL_CTX *ctx = (SSL_CTX *) sslctx;
  80. SSL_CTX_set_cert_verify_callback(ctx, pathfinder_verify_callback, parm);
  81. return CURLE_OK;
  82. }
  83. #endif