serverhash.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2016 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. /* Normally it's nice for header files to automatically include anything
  21. * they need. But winsock is a horrid can of worms; we 're not going to
  22. * make openconnect.h include anything for itself. So just do this... */
  23. #ifdef _WIN32
  24. #define SOCKET int
  25. #endif
  26. #include "../openconnect.h"
  27. static void progress(void *privdata, int level, const char *fmt, ...)
  28. {
  29. va_list args;
  30. if (level > PRG_ERR)
  31. return;
  32. va_start(args, fmt);
  33. vfprintf(stderr, fmt, args);
  34. va_end(args);
  35. }
  36. static int validate_peer_cert(void *_vpninfo, const char *reason)
  37. {
  38. printf("%s\n", openconnect_get_peer_cert_hash(_vpninfo));
  39. exit(0);
  40. }
  41. /* We do this in a separate test tool because we *really* don't want
  42. * people scripting it to recover the --no-cert-check functionality.
  43. * Validate your server certs properly, people! */
  44. int main(int argc, char **argv)
  45. {
  46. struct openconnect_info *vpninfo;
  47. if (argc != 2) {
  48. fprintf(stderr, "usage: serverhash <server>\n");
  49. exit(1);
  50. }
  51. openconnect_init_ssl();
  52. vpninfo = openconnect_vpninfo_new(NULL, validate_peer_cert, NULL, NULL, progress, NULL);
  53. if (openconnect_parse_url(vpninfo, argv[1])) {
  54. fprintf(stderr, "Failed to parse URL\n");
  55. exit(1);
  56. }
  57. openconnect_set_system_trust(vpninfo, 0);
  58. openconnect_obtain_cookie(vpninfo);
  59. return -1;
  60. }