Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / test-x509v3.c
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
9 #include "includes.h"
10
11 #include "common.h"
12 #include "tls/asn1.h"
13 #include "tls/x509v3.h"
14
15
16 int main(int argc, char *argv[])
17 {
18         char *buf;
19         size_t len;
20         struct x509_certificate *certs = NULL, *last = NULL, *cert;
21         int i, reason;
22
23         wpa_debug_level = 0;
24
25         if (argc < 3 || strcmp(argv[1], "-v") != 0) {
26                 printf("usage: test_x509v3 -v <cert1.der> <cert2.der> ..\n");
27                 return -1;
28         }
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
38                 cert = x509_certificate_parse((u8 *) buf, len);
39                 if (cert == NULL) {
40                         printf("Failed to parse X.509 certificate\n");
41                         return -1;
42                 }
43
44                 free(buf);
45
46                 if (certs == NULL)
47                         certs = cert;
48                 else
49                         last->next = cert;
50                 last = cert;
51         }
52
53         printf("\n\nValidating certificate chain\n");
54         if (x509_certificate_chain_validate(last, certs, &reason, 0) < 0) {
55                 printf("\nCertificate chain validation failed: %d\n", reason);
56                 return -1;
57         }
58         printf("\nCertificate chain is valid\n");
59
60         return 0;
61 }