test_x509v3.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Testing tool for X.509v3 routines
  3. * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "tls/asn1.h"
  17. #include "tls/x509v3.h"
  18. extern int wpa_debug_level;
  19. int main(int argc, char *argv[])
  20. {
  21. char *buf;
  22. size_t len;
  23. struct x509_certificate *certs = NULL, *last = NULL, *cert;
  24. int i, reason;
  25. wpa_debug_level = 0;
  26. if (argc < 3 || strcmp(argv[1], "-v") != 0) {
  27. printf("usage: test_x509v3 -v <cert1.der> <cert2.der> ..\n");
  28. return -1;
  29. }
  30. for (i = 2; i < argc; i++) {
  31. printf("Reading: %s\n", argv[i]);
  32. buf = os_readfile(argv[i], &len);
  33. if (buf == NULL) {
  34. printf("Failed to read '%s'\n", argv[i]);
  35. return -1;
  36. }
  37. cert = x509_certificate_parse((u8 *) buf, len);
  38. if (cert == NULL) {
  39. printf("Failed to parse X.509 certificate\n");
  40. return -1;
  41. }
  42. free(buf);
  43. if (certs == NULL)
  44. certs = cert;
  45. else
  46. last->next = cert;
  47. last = cert;
  48. }
  49. printf("\n\nValidating certificate chain\n");
  50. if (x509_certificate_chain_validate(last, certs, &reason) < 0) {
  51. printf("\nCertificate chain validation failed: %d\n", reason);
  52. return -1;
  53. }
  54. printf("\nCertificate chain is valid\n");
  55. return 0;
  56. }