1 /* This example code is placed in the public domain. */
9 #include <gnutls/gnutls.h>
10 #include <gnutls/x509.h>
14 /* All the available CRLs
16 gnutls_x509_crl_t
*crl_list
;
19 /* All the available trusted CAs
21 gnutls_x509_crt_t
*ca_list
;
24 static void verify_cert2 (gnutls_x509_crt_t crt
,
25 gnutls_x509_crt_t issuer
,
26 gnutls_x509_crl_t
* crl_list
, int crl_list_size
);
27 static void verify_last_cert (gnutls_x509_crt_t crt
,
28 gnutls_x509_crt_t
* ca_list
, int ca_list_size
,
29 gnutls_x509_crl_t
* crl_list
,
33 /* This function will try to verify the peer's certificate chain, and
34 * also check if the hostname matches, and the activation, expiration dates.
37 verify_certificate_chain (gnutls_session_t session
,
39 const gnutls_datum_t
* cert_chain
,
40 int cert_chain_length
)
43 gnutls_x509_crt_t
*cert
;
45 cert
= malloc (sizeof (*cert
) * cert_chain_length
);
47 /* Import all the certificates in the chain to
48 * native certificate format.
50 for (i
= 0; i
< cert_chain_length
; i
++)
52 gnutls_x509_crt_init (&cert
[i
]);
53 gnutls_x509_crt_import (cert
[i
], &cert_chain
[i
], GNUTLS_X509_FMT_DER
);
56 /* If the last certificate in the chain is self signed ignore it.
57 * That is because we want to check against our trusted certificate
60 if (gnutls_x509_crt_check_issuer (cert
[cert_chain_length
- 1],
61 cert
[cert_chain_length
- 1]) > 0
62 && cert_chain_length
> 0)
67 /* Now verify the certificates against their issuers
70 for (i
= 1; i
< cert_chain_length
; i
++)
72 verify_cert2 (cert
[i
- 1], cert
[i
], crl_list
, crl_list_size
);
75 /* Here we must verify the last certificate in the chain against
76 * our trusted CA list.
78 verify_last_cert (cert
[cert_chain_length
- 1],
79 ca_list
, ca_list_size
, crl_list
, crl_list_size
);
81 /* Check if the name in the first certificate matches our destination!
83 if (!gnutls_x509_crt_check_hostname (cert
[0], hostname
))
85 printf ("The certificate's owner does not match hostname '%s'\n",
89 for (i
= 0; i
< cert_chain_length
; i
++)
90 gnutls_x509_crt_deinit (cert
[i
]);
96 /* Verifies a certificate against an other certificate
97 * which is supposed to be it's issuer. Also checks the
98 * crl_list if the certificate is revoked.
101 verify_cert2 (gnutls_x509_crt_t crt
, gnutls_x509_crt_t issuer
,
102 gnutls_x509_crl_t
* crl_list
, int crl_list_size
)
109 /* Print information about the certificates to
112 name_size
= sizeof (name
);
113 gnutls_x509_crt_get_dn (crt
, name
, &name_size
);
115 fprintf (stderr
, "\nCertificate: %s\n", name
);
117 name_size
= sizeof (name
);
118 gnutls_x509_crt_get_issuer_dn (crt
, name
, &name_size
);
120 fprintf (stderr
, "Issued by: %s\n", name
);
122 /* Get the DN of the issuer cert.
124 name_size
= sizeof (name
);
125 gnutls_x509_crt_get_dn (issuer
, name
, &name_size
);
127 fprintf (stderr
, "Checking against: %s\n", name
);
129 /* Do the actual verification.
131 gnutls_x509_crt_verify (crt
, &issuer
, 1, 0, &output
);
133 if (output
& GNUTLS_CERT_INVALID
)
135 fprintf (stderr
, "Not trusted");
137 if (output
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
138 fprintf (stderr
, ": no issuer was found");
139 if (output
& GNUTLS_CERT_SIGNER_NOT_CA
)
140 fprintf (stderr
, ": issuer is not a CA");
141 if (output
& GNUTLS_CERT_NOT_ACTIVATED
)
142 fprintf (stderr
, ": not yet activated\n");
143 if (output
& GNUTLS_CERT_EXPIRED
)
144 fprintf (stderr
, ": expired\n");
146 fprintf (stderr
, "\n");
149 fprintf (stderr
, "Trusted\n");
151 /* Check if the certificate is revoked.
153 ret
= gnutls_x509_crt_check_revocation (crt
, crl_list
, crl_list_size
);
156 fprintf (stderr
, "Revoked\n");
161 /* Verifies a certificate against our trusted CA list.
162 * Also checks the crl_list if the certificate is revoked.
165 verify_last_cert (gnutls_x509_crt_t crt
,
166 gnutls_x509_crt_t
* ca_list
, int ca_list_size
,
167 gnutls_x509_crl_t
* crl_list
, int crl_list_size
)
174 /* Print information about the certificates to
177 name_size
= sizeof (name
);
178 gnutls_x509_crt_get_dn (crt
, name
, &name_size
);
180 fprintf (stderr
, "\nCertificate: %s\n", name
);
182 name_size
= sizeof (name
);
183 gnutls_x509_crt_get_issuer_dn (crt
, name
, &name_size
);
185 fprintf (stderr
, "Issued by: %s\n", name
);
187 /* Do the actual verification.
189 gnutls_x509_crt_verify (crt
, ca_list
, ca_list_size
,
190 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT
, &output
);
192 if (output
& GNUTLS_CERT_INVALID
)
194 fprintf (stderr
, "Not trusted");
196 if (output
& GNUTLS_CERT_SIGNER_NOT_CA
)
197 fprintf (stderr
, ": Issuer is not a CA\n");
198 if (output
& GNUTLS_CERT_NOT_ACTIVATED
)
199 fprintf (stderr
, ": Not yet activated\n");
200 if (output
& GNUTLS_CERT_EXPIRED
)
201 fprintf (stderr
, ": Expired\n");
202 fprintf (stderr
, "\n");
205 fprintf (stderr
, "Trusted\n");
208 /* Check if the certificate is revoked.
210 ret
= gnutls_x509_crt_check_revocation (crt
, crl_list
, crl_list_size
);
213 fprintf (stderr
, "Revoked\n");