summaryrefslogtreecommitdiff
path: root/lib/x509_verify.c
blob: 9e69f8312f902af28c15ce77204ae0c16d45cfe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 * Copyright (C) 2001,2002 Nikos Mavroyanopoulos <nmav@hellug.gr>
 *
 * This file is part of GNUTLS.
 *
 *  The GNUTLS library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public   
 *  License as published by the Free Software Foundation; either 
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#include "gnutls_int.h"
#include "gnutls_errors.h"
#include "gnutls_cert.h"
#include "x509_asn1.h"
#include "x509_der.h"
#include "gnutls_global.h"
#include "gnutls_num.h"		/* GMAX */
#include <gnutls_sig.h>
#include <gnutls_str.h>


/* Return 0 or INVALID, if the issuer is a CA,
 * or not.
 */
static int check_if_ca(const gnutls_cert * cert,
		       const gnutls_cert * issuer)
{
	CertificateStatus ret = GNUTLS_CERT_INVALID;

	/* Check if the issuer is the same with the
	 * certificate. This is added in order for trusted
	 * certificates to be able to verify themselves.
	 */
	if (cert->raw.size == issuer->raw.size) {
		if (memcmp
		    (cert->raw.data, issuer->raw.data,
		     cert->raw.size) == 0) {
			return 0;
		}
	}

	if (issuer->CA == 1) {
		ret = 0;
	} else
		gnutls_assert();

	return ret;
}



void _gnutls_int2str(int k, char *data);

#define MAX_DN_ELEM 1024

/* This function checks if 'certs' issuer is 'issuer_cert'.
 * This does a straight (DER) compare of the issuer/subject fields in
 * the given certificates.
 *
 * FIXME: use a real DN comparison algorithm.
 */
static
int compare_dn(gnutls_cert * cert, gnutls_cert * issuer_cert)
{
	node_asn *c2, *c3;
	int result, len1;
	int len2;
	char tmpstr[512];
	int start1, start2, end1, end2;

	/* get the issuer of 'cert'
	 */
	if ((result =
	     asn1_create_structure(_gnutls_get_pkix(), "PKIX1.Certificate",
				   &c2, "certificate2")) != ASN_OK) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	result = asn1_get_der(c2, cert->raw.data, cert->raw.size);
	if (result != ASN_OK) {
		/* couldn't decode DER */
		gnutls_assert();
		asn1_delete_structure(c2);
		return _gnutls_asn2err(result);
	}



	/* get the 'subject' info of 'issuer_cert'
	 */
	if ((result =
	     asn1_create_structure(_gnutls_get_pkix(), "PKIX1.Certificate",
				   &c3, "certificate2")) != ASN_OK) {
		gnutls_assert();
		asn1_delete_structure(c2);
		return _gnutls_asn2err(result);
	}

	result =
	    asn1_get_der(c3, issuer_cert->raw.data, issuer_cert->raw.size);
	if (result != ASN_OK) {
		/* couldn't decode DER */
		gnutls_assert();
		asn1_delete_structure(c2);
		return _gnutls_asn2err(result);
	}


	_gnutls_str_cpy(tmpstr, sizeof(tmpstr),
			"certificate2.tbsCertificate.issuer");
	result =
	    asn1_get_start_end_der(c2, cert->raw.data, cert->raw.size,
				   tmpstr, &start1, &end1);
	asn1_delete_structure(c2);

	if (result != ASN_OK) {
		gnutls_assert();
		asn1_delete_structure(c3);
		return _gnutls_asn2err(result);
	}

	len1 = end1 - start1 + 1;

	_gnutls_str_cpy(tmpstr, sizeof(tmpstr),
			"certificate2.tbsCertificate.subject");
	result =
	    asn1_get_start_end_der(c3, issuer_cert->raw.data,
				   issuer_cert->raw.size, tmpstr, &start2,
				   &end2);
	asn1_delete_structure(c3);

	if (result != ASN_OK) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	len2 = end2 - start2 + 1;

	/* The error code returned does not really matter
	 * here.
	 */
	if (len1 != len2) {
		gnutls_assert();
		return GNUTLS_E_UNKNOWN_ERROR;
	}
	if (memcmp(&issuer_cert->raw.data[start2],
		   &cert->raw.data[start1], len1) != 0) {
		gnutls_assert();
		return GNUTLS_E_UNKNOWN_ERROR;
	}

	/* they match */
	return 0;

}

static gnutls_cert *find_issuer(gnutls_cert * cert,
				gnutls_cert * trusted_cas, int tcas_size)
{
	int i;

	/* this is serial search. 
	 */

	for (i = 0; i < tcas_size; i++) {
		if (compare_dn(cert, &trusted_cas[i]) == 0)
			return &trusted_cas[i];
	}

	gnutls_assert();
	return NULL;
}

/* ret_trust is the value to return when the certificate chain is ok
 * ret_else is the value to return otherwise.
 */
int gnutls_verify_certificate2(gnutls_cert * cert,
			       gnutls_cert * trusted_cas, int tcas_size,
			       void *CRLs, int crls_size, int ret_trust,
			       int ret_else)
{
/* CRL is ignored for now */

	gnutls_cert *issuer;
	int ret;

	if (tcas_size >= 1)
		issuer = find_issuer(cert, trusted_cas, tcas_size);
	else {
		gnutls_assert();
		return ret_else;
	}

	/* issuer is not in trusted certificate
	 * authorities.
	 */
	if (issuer == NULL) {
		gnutls_assert();
		return ret_else;
	}

	ret = check_if_ca(cert, issuer);
	if (ret != 0) {
		gnutls_assert();
		return ret_else | GNUTLS_CERT_INVALID;
	}

	ret = gnutls_x509_verify_signature(cert, issuer);
	if (ret != 0) {
		gnutls_assert();
		return ret_else | GNUTLS_CERT_INVALID;
	}

	/* FIXME: Check CRL --not done yet.
	 */


	return ret_trust;
}

/* The algorithm used is:
 * 1. Check the certificate chain given by the peer, if it is ok.
 * 2. If any certificate in the chain are revoked, not
 *    valid, or they are not CAs then the certificate is invalid.
 * 3. If 1 is ok, then find a certificate in the trusted CAs file
 *    that has the DN of the issuer field in the last certificate
 *    in the peer's certificate chain.
 * 4. If it does exist then verify it. If verification is ok then
 *    it is trusted. Otherwise it is just valid (but not trusted).
 */
/* This function verifies a X.509 certificate list. The certificate list should
 * lead to a trusted CA in order to be trusted.
 */
int _gnutls_x509_verify_certificate(gnutls_cert * certificate_list,
				    int clist_size,
				    gnutls_cert * trusted_cas,
				    int tcas_size, void *CRLs,
				    int crls_size)
{
	int i = 0, ret;
	CertificateStatus status = 0;

	if (clist_size == 0) {
		return GNUTLS_E_NO_CERTIFICATE_FOUND;
	}

	/* Verify the certificate path */
	for (i = 0; i < clist_size; i++) {
		if (i + 1 >= clist_size)
			break;

		if ((ret =
		     gnutls_verify_certificate2(&certificate_list[i],
						&certificate_list[i + 1],
						1, NULL, 0, 0,
						GNUTLS_CERT_INVALID)) !=
		    0) {
			/*
			   * We only accept the first certificate to be
			   * expired, revoked etc. If any of the certificates in the
			   * certificate chain is expired then the certificate
			   * is not valid.
			 */
			if (ret > 0) {
				gnutls_assert();
				status |= ret;
			} else {
				gnutls_assert();
				return ret;
			}
		}
	}

	if (status > 0) { /* If there is any problem in the
			   * certificate chain then mark as not trusted
			   * and return immediately.
			   */
		return (status | GNUTLS_CERT_NOT_TRUSTED);
	}
	
	/* Now verify the last certificate in the certificate path
	 * against the trusted CA certificate list.
	 *
	 * If no CAs are present returns NOT_TRUSTED. Thus works
	 * in self signed etc certificates.
	 */
	ret =
	    gnutls_verify_certificate2(&certificate_list[i], trusted_cas,
				       tcas_size, CRLs, crls_size, 0,
				       GNUTLS_CERT_NOT_TRUSTED);

	if (ret > 0) {
		/* if the last certificate in the certificate
		 * list is invalid, then the certificate is not
		 * trusted.
		 */
		gnutls_assert();
		status |= ret;
	}

	if (ret < 0) {
		gnutls_assert();
		return ret;
	}

	/* if we got here, then it's trusted.
	 */
	return status;
}