list-system-keys.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2022 David Woodhouse <dwmw2@infradead.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * version 2.1, as published by the Free Software Foundation.
  9. *
  10. * This program 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. * Lesser General Public License for more details.
  14. */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <gnutls/system-keys.h>
  18. int main(void)
  19. {
  20. gnutls_system_key_iter_t iter = NULL;
  21. char *cert, *key, *label;
  22. gnutls_datum_t der = { };
  23. int err;
  24. while ((err = gnutls_system_key_iter_get_info(&iter, GNUTLS_CRT_X509,
  25. &cert, &key, &label, &der, 0)) >= 0) {
  26. /* Skip anything without a key */
  27. if (cert && key) {
  28. printf("Label: %s\nCert URI: %s\nKey URI: %s\n", label, cert, key);
  29. gnutls_x509_crt_t crt = NULL;
  30. gnutls_datum_t buf = { };
  31. if (!gnutls_x509_crt_init(&crt) &&
  32. !gnutls_x509_crt_import(crt, &der, GNUTLS_X509_FMT_DER) &&
  33. !gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_ONELINE, &buf))
  34. printf("Cert info: %s\n", buf.data);
  35. gnutls_free(buf.data);
  36. gnutls_x509_crt_deinit(crt);
  37. printf("\n");
  38. }
  39. gnutls_free(der.data);
  40. der.data = NULL;
  41. gnutls_free(label);
  42. gnutls_free(key);
  43. gnutls_free(cert);
  44. }
  45. if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
  46. err = 0;
  47. else if (err == GNUTLS_E_UNIMPLEMENTED_FEATURE)
  48. fprintf(stderr, "GnuTLS does not support a concept of system keys on this platform.\n");
  49. else if (err < 0)
  50. fprintf(stderr, "Error listing keys: %s\n", gnutls_strerror(err));
  51. gnutls_system_key_iter_deinit(iter);
  52. return !!err;
  53. }