4 #include <gnutls/x509.h>
6 #define MAX_FILE_SIZE 16*1024
14 static struct file_res test_files
[] = {
16 {"test2.pem", GNUTLS_CERT_NOT_TRUSTED
},
17 {"test3.pem", GNUTLS_CERT_INVALID
| GNUTLS_CERT_NOT_TRUSTED
},
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
},
25 {"test25.pem", GNUTLS_CERT_INVALID
| GNUTLS_CERT_NOT_TRUSTED
},
30 #define CA_FILE "ca.pem"
32 int _verify_x509_file (const char *certfile
, const char *cafile
);
38 if (x
& GNUTLS_CERT_INVALID
)
39 printf ("- certificate is invalid\n");
41 printf ("- certificate is valid\n");
42 if (x
& GNUTLS_CERT_NOT_TRUSTED
)
43 printf ("- certificate is NOT trusted\n");
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");
60 int i
= 0, exp_result
;
62 gnutls_global_init ();
65 "This test will perform some checks on X.509 certificate\n");
66 fprintf (stderr
, "verification functions.\n\n");
70 exp_result
= test_files
[i
].result
;
71 file
= test_files
[i
++].test_file
;
75 x
= _verify_x509_file (file
, CA_FILE
);
79 fprintf (stderr
, "Unexpected error: %d\n", x
);
82 printf ("Test %d, file %s: ", i
, file
);
88 fprintf (stderr
, "Unexpected error in verification.\n");
89 fprintf (stderr
, "Certificate was found to be: \n");
102 gnutls_global_deinit ();
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
)
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
;
132 ret
= gnutls_x509_crt_init (&x509_ca
);
135 fprintf (stderr
, "Error parsing the CA certificate: %s\n",
136 gnutls_strerror (ret
));
140 ret
= gnutls_x509_crt_import (x509_ca
, &tmp
, GNUTLS_X509_FMT_PEM
);
144 fprintf (stderr
, "Error parsing the CA certificate: %s\n",
145 gnutls_strerror (ret
));
149 /* Decode the CRL list
156 if (strstr (ptr
, CRL_SEP
) != NULL
) /* if CRLs exist */
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");
168 tmp
.data
= (char *) ptr
;
171 ret
= gnutls_x509_crl_init (&x509_crl_list
[i
- 1]);
174 fprintf (stderr
, "Error parsing the CRL[%d]: %s\n", i
,
175 gnutls_strerror (ret
));
180 gnutls_x509_crl_import (x509_crl_list
[i
- 1], &tmp
,
181 GNUTLS_X509_FMT_PEM
);
184 fprintf (stderr
, "Error parsing the CRL[%d]: %s\n", i
,
185 gnutls_strerror (ret
));
189 /* now we move ptr after the pem header */
190 ptr
= strstr (ptr
, CRL_SEP
);
196 while ((ptr
= strstr (ptr
, CRL_SEP
)) != NULL
);
201 /* Decode the certificate chain.
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");
219 tmp
.data
= (char *) ptr
;
222 ret
= gnutls_x509_crt_init (&x509_cert_list
[i
- 1]);
225 fprintf (stderr
, "Error parsing the certificate[%d]: %s\n", i
,
226 gnutls_strerror (ret
));
231 gnutls_x509_crt_import (x509_cert_list
[i
- 1], &tmp
,
232 GNUTLS_X509_FMT_PEM
);
235 fprintf (stderr
, "Error parsing the certificate[%d]: %s\n", i
,
236 gnutls_strerror (ret
));
240 /* now we move ptr after the pem header */
241 ptr
= strstr (ptr
, CERT_SEP
);
247 while ((ptr
= strstr (ptr
, CERT_SEP
)) != NULL
);
251 ret
= gnutls_x509_crt_list_verify (x509_cert_list
, x509_ncerts
,
252 &x509_ca
, 1, x509_crl_list
, x509_ncrls
,
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
);
272 fprintf (stderr
, "Error in verification: %s\n", gnutls_strerror (ret
));
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
];
291 fd1
= fopen (certfile
, "rb");
294 fprintf (stderr
, "error opening %s\n", certfile
);
295 return GNUTLS_E_FILE_ERROR
;
298 cert_size
= fread (cert
, 1, sizeof (cert
) - 1, fd1
);
304 fd1
= fopen (cafile
, "rb");
307 fprintf (stderr
, "error opening %s\n", cafile
);
308 return GNUTLS_E_FILE_ERROR
;
311 ca_size
= fread (ca
, 1, sizeof (ca
) - 1, fd1
);
316 return _verify_x509_mem (cert
, cert_size
, ca
, ca_size
, cert
, cert_size
);