Properly restore compression method on session resumption. Reported by Dash Shendy.
[gnutls.git] / tests / x509_test.c
blob27b6ad28ed78445336b93e746d480d7750778d1f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <gnutls/x509.h>
6 #define MAX_FILE_SIZE 16*1024
8 struct file_res
10 char *test_file;
11 int result;
14 static struct file_res test_files[] = {
15 {"test1.pem", 0},
16 {"test2.pem", GNUTLS_CERT_NOT_TRUSTED},
17 {"test3.pem", GNUTLS_CERT_INVALID | GNUTLS_CERT_NOT_TRUSTED},
18 {"test10.pem", 0},
19 {"test13.pem", GNUTLS_CERT_INVALID | GNUTLS_CERT_NOT_TRUSTED},
20 {"test20.pem", GNUTLS_CERT_REVOKED | GNUTLS_CERT_NOT_TRUSTED},
21 {"test21.pem", GNUTLS_CERT_REVOKED | GNUTLS_CERT_NOT_TRUSTED},
22 {"test22.pem", GNUTLS_CERT_INVALID | GNUTLS_CERT_NOT_TRUSTED},
23 {"test23.pem", GNUTLS_CERT_INVALID | GNUTLS_CERT_NOT_TRUSTED},
24 {"test24.pem", 0},
25 {"test25.pem", GNUTLS_CERT_INVALID | GNUTLS_CERT_NOT_TRUSTED},
26 {"test26.pem", 0},
27 {NULL, 0}
30 #define CA_FILE "ca.pem"
32 int _verify_x509_file (const char *certfile, const char *cafile);
35 static void
36 print_res (int x)
38 if (x & GNUTLS_CERT_INVALID)
39 printf ("- certificate is invalid\n");
40 else
41 printf ("- certificate is valid\n");
42 if (x & GNUTLS_CERT_NOT_TRUSTED)
43 printf ("- certificate is NOT trusted\n");
44 else
45 printf ("- certificate is trusted\n");
47 if (x & GNUTLS_CERT_CORRUPTED)
48 printf ("- Found a corrupted certificate.\n");
50 if (x & GNUTLS_CERT_REVOKED)
51 printf ("- certificate is revoked.\n");
54 int
55 main ()
58 int x;
59 char *file;
60 int i = 0, exp_result;
62 gnutls_global_init ();
64 fprintf (stderr,
65 "This test will perform some checks on X.509 certificate\n");
66 fprintf (stderr, "verification functions.\n\n");
68 for (;;)
70 exp_result = test_files[i].result;
71 file = test_files[i++].test_file;
73 if (file == NULL)
74 break;
75 x = _verify_x509_file (file, CA_FILE);
77 if (x < 0)
79 fprintf (stderr, "Unexpected error: %d\n", x);
80 exit (1);
82 printf ("Test %d, file %s: ", i, file);
84 if (x != exp_result)
86 printf ("failed.\n");
87 fflush (stdout);
88 fprintf (stderr, "Unexpected error in verification.\n");
89 fprintf (stderr, "Certificate was found to be: \n");
90 print_res (x);
92 else
94 printf ("ok.");
96 printf ("\n");
100 printf ("\n");
102 gnutls_global_deinit ();
104 return 0;
108 #define CERT_SEP "-----BEGIN CERT"
109 #define CRL_SEP "-----BEGIN X509 CRL"
111 /* Verifies a base64 encoded certificate list from memory
114 _verify_x509_mem (const char *cert, int cert_size,
115 const char *ca, int ca_size, const char *crl, int crl_size)
117 int siz, i;
118 const char *ptr;
119 int ret;
120 unsigned int output;
121 gnutls_datum_t tmp;
122 gnutls_x509_crt *x509_cert_list = NULL;
123 gnutls_x509_crt x509_ca;
124 gnutls_x509_crl *x509_crl_list = NULL;
125 int x509_ncerts, x509_ncrls;
127 /* Decode the CA certificate
129 tmp.data = (char *) ca;
130 tmp.size = ca_size;
132 ret = gnutls_x509_crt_init (&x509_ca);
133 if (ret < 0)
135 fprintf (stderr, "Error parsing the CA certificate: %s\n",
136 gnutls_strerror (ret));
137 exit (1);
140 ret = gnutls_x509_crt_import (x509_ca, &tmp, GNUTLS_X509_FMT_PEM);
142 if (ret < 0)
144 fprintf (stderr, "Error parsing the CA certificate: %s\n",
145 gnutls_strerror (ret));
146 exit (1);
149 /* Decode the CRL list
151 siz = crl_size;
152 ptr = crl;
154 i = 1;
156 if (strstr (ptr, CRL_SEP) != NULL) /* if CRLs exist */
159 x509_crl_list =
160 (gnutls_x509_crl *) realloc (x509_crl_list,
161 i * sizeof (gnutls_x509_crl));
162 if (x509_crl_list == NULL)
164 fprintf (stderr, "memory error\n");
165 exit (1);
168 tmp.data = (char *) ptr;
169 tmp.size = siz;
171 ret = gnutls_x509_crl_init (&x509_crl_list[i - 1]);
172 if (ret < 0)
174 fprintf (stderr, "Error parsing the CRL[%d]: %s\n", i,
175 gnutls_strerror (ret));
176 exit (1);
179 ret =
180 gnutls_x509_crl_import (x509_crl_list[i - 1], &tmp,
181 GNUTLS_X509_FMT_PEM);
182 if (ret < 0)
184 fprintf (stderr, "Error parsing the CRL[%d]: %s\n", i,
185 gnutls_strerror (ret));
186 exit (1);
189 /* now we move ptr after the pem header */
190 ptr = strstr (ptr, CRL_SEP);
191 if (ptr != NULL)
192 ptr++;
194 i++;
196 while ((ptr = strstr (ptr, CRL_SEP)) != NULL);
198 x509_ncrls = i - 1;
201 /* Decode the certificate chain.
203 siz = cert_size;
204 ptr = cert;
206 i = 1;
210 x509_cert_list =
211 (gnutls_x509_crt *) realloc (x509_cert_list,
212 i * sizeof (gnutls_x509_crt));
213 if (x509_cert_list == NULL)
215 fprintf (stderr, "memory error\n");
216 exit (1);
219 tmp.data = (char *) ptr;
220 tmp.size = siz;
222 ret = gnutls_x509_crt_init (&x509_cert_list[i - 1]);
223 if (ret < 0)
225 fprintf (stderr, "Error parsing the certificate[%d]: %s\n", i,
226 gnutls_strerror (ret));
227 exit (1);
230 ret =
231 gnutls_x509_crt_import (x509_cert_list[i - 1], &tmp,
232 GNUTLS_X509_FMT_PEM);
233 if (ret < 0)
235 fprintf (stderr, "Error parsing the certificate[%d]: %s\n", i,
236 gnutls_strerror (ret));
237 exit (1);
240 /* now we move ptr after the pem header */
241 ptr = strstr (ptr, CERT_SEP);
242 if (ptr != NULL)
243 ptr++;
245 i++;
247 while ((ptr = strstr (ptr, CERT_SEP)) != NULL);
249 x509_ncerts = i - 1;
251 ret = gnutls_x509_crt_list_verify (x509_cert_list, x509_ncerts,
252 &x509_ca, 1, x509_crl_list, x509_ncrls,
253 0, &output);
255 gnutls_x509_crt_deinit (x509_ca);
257 for (i = 0; i < x509_ncerts; i++)
259 gnutls_x509_crt_deinit (x509_cert_list[i]);
262 for (i = 0; i < x509_ncrls; i++)
264 gnutls_x509_crl_deinit (x509_crl_list[i]);
267 free (x509_cert_list);
268 free (x509_crl_list);
270 if (ret < 0)
272 fprintf (stderr, "Error in verification: %s\n", gnutls_strerror (ret));
273 exit (1);
276 return output;
281 /* Reads and verifies a base64 encoded certificate file
284 _verify_x509_file (const char *certfile, const char *cafile)
286 int ca_size, cert_size;
287 char ca[MAX_FILE_SIZE];
288 char cert[MAX_FILE_SIZE];
289 FILE *fd1;
291 fd1 = fopen (certfile, "rb");
292 if (fd1 == NULL)
294 fprintf (stderr, "error opening %s\n", certfile);
295 return GNUTLS_E_FILE_ERROR;
298 cert_size = fread (cert, 1, sizeof (cert) - 1, fd1);
299 fclose (fd1);
301 cert[cert_size] = 0;
304 fd1 = fopen (cafile, "rb");
305 if (fd1 == NULL)
307 fprintf (stderr, "error opening %s\n", cafile);
308 return GNUTLS_E_FILE_ERROR;
311 ca_size = fread (ca, 1, sizeof (ca) - 1, fd1);
312 fclose (fd1);
314 ca[ca_size] = 0;
316 return _verify_x509_mem (cert, cert_size, ca, ca_size, cert, cert_size);