test-x509v3.c 1.3 KB

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