summaryrefslogtreecommitdiff
path: root/lib/x509/crl.c
blob: 1b4aad67ffaaf7d42448eec58c0cd9be9a0f060e (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
 *  Copyright (C) 2003 Nikos Mavroyanopoulos
 *
 *  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 <libtasn1.h>
#include <gnutls_int.h>
#include <gnutls_datum.h>
#include <gnutls_global.h>
#include <gnutls_errors.h>
#include <common.h>
#include <x509_b64.h>
#include <x509.h>
#include <dn.h>

/**
  * gnutls_x509_crl_init - This function initializes a gnutls_x509_crl structure
  * @crl: The structure to be initialized
  *
  * This function will initialize a CRL structure. CRL stands for
  * Certificate Revocation List.
  *
  * Returns 0 on success.
  *
  **/
int gnutls_x509_crl_init(gnutls_x509_crl * crl)
{
	*crl = gnutls_calloc(1, sizeof(gnutls_x509_crl_int));

	if (*crl) {
		(*crl)->crl = ASN1_TYPE_EMPTY;
		return 0;	/* success */
	}
	return GNUTLS_E_MEMORY_ERROR;
}

/**
  * gnutls_x509_crl_deinit - This function deinitializes memory used by a gnutls_x509_crl structure
  * @crl: The structure to be initialized
  *
  * This function will deinitialize a CRL structure. 
  *
  **/
void gnutls_x509_crl_deinit(gnutls_x509_crl crl)
{
	if (crl->crl)
		asn1_delete_structure(&crl->crl);
	_gnutls_free_datum(&crl->signed_data);
	_gnutls_free_datum(&crl->signature);

	gnutls_free(crl);
}

/**
  * gnutls_x509_crl_import - This function will import a DER or PEM encoded CRL
  * @crl: The structure to store the parsed CRL.
  * @data: The DER or PEM encoded CRL.
  * @format: One of DER or PEM
  *
  * This function will convert the given DER or PEM encoded CRL
  * to the native gnutls_x509_crl format. The output will be stored in 'crl'.
  *
  * If the CRL is PEM encoded it should have a header of "X509 CRL", and
  * it must be a null terminated string.
  *
  * Returns 0 on success.
  *
  **/
int gnutls_x509_crl_import(gnutls_x509_crl crl, const gnutls_datum * data,
			   gnutls_x509_certificate_format format)
{
	int result = 0, need_free = 0;
	int start, end;
	gnutls_datum _data = { data->data, data->size };

	/* If the CRL is in PEM format then decode it
	 */
	if (format == GNUTLS_X509_FMT_PEM) {
		opaque *out;

		result =
		    _gnutls_fbase64_decode(PEM_CRL, data->data, data->size,
					   &out);

		if (result <= 0) {
			if (result == 0)
				result = GNUTLS_E_INTERNAL_ERROR;
			gnutls_assert();
			return result;
		}

		_data.data = out;
		_data.size = result;

		need_free = 1;
	}

	crl->crl = ASN1_TYPE_EMPTY;

	result = asn1_create_element(_gnutls_get_pkix(),
				     "PKIX1.CertificateList",
				     &crl->crl, "crl2");
	if (result != ASN1_SUCCESS) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	result =
	    asn1_der_decoding(&crl->crl, _data.data, _data.size, NULL);
	if (result != ASN1_SUCCESS) {
		result = _gnutls_asn2err(result);
		gnutls_assert();
		goto cleanup;
	}

	/* Get the signed data
	 */
	result =
	    asn1_der_decoding_startEnd(crl->crl, _data.data, _data.size,
				       "crl2.tbsCertList", &start, &end);
	if (result != ASN1_SUCCESS) {
		result = _gnutls_asn2err(result);
		gnutls_assert();
		goto cleanup;
	}


	result =
	    _gnutls_set_datum(&crl->signed_data, &_data.data[start],
			      end - start + 1);
	if (result < 0) {
		gnutls_assert();
		goto cleanup;
	}

	/* Read the signature */
	{
		opaque signature[640];
		int len;

		/* read the bit string of the signature
		 */
		len = sizeof(signature);
		result =
		    asn1_read_value(crl->crl, "crl2.signature", signature,
				    &len);

		if (result != ASN1_SUCCESS) {
			result = _gnutls_asn2err(result);
			gnutls_assert();
			goto cleanup;
		}

		if (len % 8 != 0) {
			gnutls_assert();
			result = GNUTLS_E_UNIMPLEMENTED_FEATURE;
			goto cleanup;
		}

		if ((result =
		     _gnutls_set_datum(&crl->signature, signature,
				       len / 8)) < 0) {
			gnutls_assert();
			goto cleanup;
		}

		/* Read the signature algorithm. Note that parameters are not
		 * read. They will be read from the issuer's certificate if needed.
		 */

		len = sizeof(signature);
		result =
		    asn1_read_value(crl->crl,
				    "crl2.signatureAlgorithm.algorithm",
				    signature, &len);

		if (result != ASN1_SUCCESS) {
			result = _gnutls_asn2err(result);
			gnutls_assert();
			goto cleanup;
		}

		crl->signature_algorithm =
		    _gnutls_x509_oid2pk_algorithm(signature);
	}

	if (need_free)
		_gnutls_free_datum(&_data);

	return 0;

      cleanup:
	if (crl->crl)
		asn1_delete_structure(&crl->crl);
	_gnutls_free_datum(&crl->signed_data);
	_gnutls_free_datum(&crl->signature);
	if (need_free)
		_gnutls_free_datum(&_data);
	return result;
}


/**
  * gnutls_x509_crl_get_issuer_dn - This function returns the CRL's issuer distinguished name
  * @crl: should contain a gnutls_x509_crl structure
  * @buf: a pointer to a structure to hold the peer's name (may be null)
  * @sizeof_buf: initialy holds the size of 'buf'
  *
  * This function will copy the name of the CRL issuer in the provided buffer. The name 
  * will be in the form "C=xxxx,O=yyyy,CN=zzzz" as described in RFC2253.
  *
  * If buf is null then only the size will be filled.
  *
  * Returns GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not long enough, and
  * in that case the sizeof_buf will be updated with the required size.
  * On success zero is returned.
  *
  **/
int gnutls_x509_crl_get_issuer_dn(gnutls_x509_crl crl, char *buf,
				  int *sizeof_buf)
{
	if (sizeof_buf == 0 || crl == NULL) {
		return GNUTLS_E_INVALID_REQUEST;
	}

	return _gnutls_x509_parse_dn(crl->crl,
				     "crl2.tbsCertList.issuer.rdnSequence",
				     buf, sizeof_buf);


}

/**
  * gnutls_x509_crl_get_issuer_dn_by_oid - This function returns the CRL's issuer distinguished name
  * @crl: should contain a gnutls_x509_crl structure
  * @oid: holds an Object Identified in null terminated string
  * @indx: In case multiple same OIDs exist in the RDN, this specifies which to send. Use zero to get the first one.
  * @buf: a pointer to a structure to hold the peer's name (may be null)
  * @sizeof_buf: initialy holds the size of 'buf'
  *
  * This function will extract the part of the name of the CRL issuer specified
  * by the given OID. The output will be encoded as described in RFC2253.
  *
  * Some helper macros with popular OIDs can be found in gnutls/x509.h
  *
  * If buf is null then only the size will be filled.
  *
  * Returns GNUTLS_E_SHORT_MEMORY_BUFFER if the provided buffer is not long enough, and
  * in that case the sizeof_buf will be updated with the required size.
  * On success zero is returned.
  *
  **/
int gnutls_x509_crl_get_issuer_dn_by_oid(gnutls_x509_crl crl,
					 const char *oid, int indx,
					 char *buf, int *sizeof_buf)
{
	if (sizeof_buf == 0 || crl == NULL) {
		return GNUTLS_E_INVALID_REQUEST;
	}

	return _gnutls_x509_parse_dn_oid(crl->crl,
					 "crl2.tbsCertList.issuer.rdnSequence",
					 oid, indx, buf, sizeof_buf);


}

/**
  * gnutls_x509_crl_get_signed_data - This function returns the CRL's signed portion
  * @crl: should contain a gnutls_x509_crl structure
  * @data: a datum which points to the signed data
  *
  * This function will return a datum that points on the CRL signed portion.
  * The output on data should be treated as constant and must not be freed.
  *
  * Returns 0 on success.
  *
  **/
int gnutls_x509_crl_get_signed_data(gnutls_x509_crl crl,
				    gnutls_const_datum * data)
{
	data->data = crl->signed_data.data;
	data->size = crl->signed_data.size;

	return 0;
}

/**
  * gnutls_x509_crl_get_signature - This function returns the CRL's signature data
  * @crl: should contain a gnutls_x509_crl structure
  * @data: a datum which points to the signed data
  *
  * This function will return a datum that points on the CRL signature portion.
  * The output on data should be treated as constant and must not be freed.
  *
  * Returns 0 on success.
  *
  **/
int gnutls_x509_crl_get_signature(gnutls_x509_crl crl,
				  gnutls_const_datum * data)
{
	data->data = crl->signature.data;
	data->size = crl->signature.size;

	return 0;
}

/**
  * gnutls_x509_crl_get_signature_algorithm - This function returns the CRL's signature algorithm
  * @crl: should contain a gnutls_x509_crl structure
  *
  * This function will return a value of the gnutls_pk_algorithm enumeration that 
  * is the signature algorithm. 
  *
  * Returns a negative value on error.
  *
  **/
int gnutls_x509_crl_get_signature_algorithm(gnutls_x509_crl crl)
{
	return crl->signature_algorithm;

	return 0;
}

/**
  * gnutls_x509_crl_get_version - This function returns the CRL's version number
  * @crl: should contain a gnutls_x509_crl structure
  *
  * This function will return the version of the specified CRL.
  *
  * Returns a negative value on error.
  *
  **/
int gnutls_x509_crl_get_version(gnutls_x509_crl crl)
{
	opaque version[5];
	int len, result;

	len = sizeof(version);
	if ((result =
	     asn1_read_value(crl->crl, "crl2.tbsCertList.version", version,
			     &len)) != ASN1_SUCCESS) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	return (int) version[0] + 1;
}

/**
  * gnutls_x509_crl_get_this_update - This function returns the CRL's thisUpdate time
  * @crl: should contain a gnutls_x509_crl structure
  *
  * This function will return the time this CRL was issued.
  *
  * Returns (time_t)-1 on error.
  *
  **/
time_t gnutls_x509_crl_get_this_update(gnutls_x509_crl crl)
{
	return _gnutls_x509_get_time(crl->crl,
				     "crl2.tbsCertList.thisUpdate");
}

/**
  * gnutls_x509_crl_get_next_update - This function returns the CRL's nextUpdate time
  * @crl: should contain a gnutls_x509_crl structure
  *
  * This function will return the time the next CRL will be issued.
  * This field is optional in a CRL so it might be normal to get
  * an error instead.
  *
  * Returns (time_t)-1 on error.
  *
  **/
time_t gnutls_x509_crl_get_next_update(gnutls_x509_crl crl)
{
	return _gnutls_x509_get_time(crl->crl,
				     "crl2.tbsCertList.nextUpdate");
}

/**
  * gnutls_x509_crl_get_certificate_count - This function returns the number of revoked certificates in a CRL
  * @crl: should contain a gnutls_x509_crl structure
  *
  * This function will return the number of revoked certificates in the
  * given CRL.
  *
  * Returns a negative value on failure.
  *
  **/
int gnutls_x509_crl_get_certificate_count(gnutls_x509_crl crl)
{

	int count, result;

	result =
	    asn1_number_of_elements(crl->crl,
				    "crl2.tbsCertList.revokedCertificates",
				    &count);

	if (result != ASN1_SUCCESS) {
		gnutls_assert();
		return 0;	/* no certificates */
	}

	return count;
}

/**
  * gnutls_x509_crl_get_certificate - This function returns the serial number of a revoked certificate
  * @crl: should contain a gnutls_x509_crl structure
  * @index: the index of the certificate to extract (starting from 0)
  * @serial: where the serial number will be copied
  * @serial_size: initialy holds the size of serial
  * @time: if non null, will hold the time this certificate was revoked
  *
  * This function will return the serial number of the specified, by the index, 
  * revoked certificate.
  *
  * Returns a negative value on failure.
  *
  **/
int gnutls_x509_crl_get_certificate(gnutls_x509_crl crl, int index,
				    unsigned char *serial,
				    int *serial_size, time_t * time)
{

	int result;
	char str_index[MAX_INT_DIGITS];
	char serial_name[64];
	char date_name[64];

	_gnutls_int2str(index + 1, str_index);
	_gnutls_str_cpy(serial_name, sizeof(serial_name),
			"crl2.tbsCertList.revokedCertificates.?");
	_gnutls_str_cat(serial_name, sizeof(serial_name), str_index);
	_gnutls_str_cat(serial_name, sizeof(serial_name),
			".userCertificate");

	_gnutls_str_cpy(date_name, sizeof(date_name),
			"crl2.tbsCertList.revokedCertificates.?");
	_gnutls_str_cat(date_name, sizeof(date_name), str_index);
	_gnutls_str_cat(date_name, sizeof(date_name), ".revocationDate");


	if ((result =
	     asn1_read_value(crl->crl, serial_name, serial,
			     serial_size)) != ASN1_SUCCESS) {
		gnutls_assert();
		if (result == ASN1_ELEMENT_NOT_FOUND)
			return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
		return _gnutls_asn2err(result);
	}

	if (time) {
		*time = _gnutls_x509_get_time(crl->crl, date_name);
	}

	return 0;
}

/*-
  * _gnutls_x509_crl_get_raw_issuer_dn - This function returns the issuer's DN DER encoded
  * @crl: should contain a gnutls_x509_crl structure
  * @dn: will hold the starting point of the DN
  *
  * This function will return a pointer to the DER encoded DN structure and
  * the length.
  *
  * Returns a negative value on error, and zero on success.
  *
  -*/
int _gnutls_x509_crl_get_raw_issuer_dn(gnutls_x509_crl crl,
				       gnutls_const_datum * dn)
{
	ASN1_TYPE c2 = ASN1_TYPE_EMPTY;
	int result, len1;
	int start1, end1;

	/* get the issuer of 'crl'
	 */
	if ((result =
	     asn1_create_element(_gnutls_get_pkix(), "PKIX1.TBSCertList",
				 &c2, "c2")) != ASN1_SUCCESS) {
		gnutls_assert();
		return _gnutls_asn2err(result);
	}

	result =
	    asn1_der_decoding(&c2, crl->signed_data.data,
			      crl->signed_data.size, NULL);
	if (result != ASN1_SUCCESS) {
		/* couldn't decode DER */
		gnutls_assert();
		asn1_delete_structure(&c2);
		return _gnutls_asn2err(result);
	}

	result =
	    asn1_der_decoding_startEnd(c2, crl->signed_data.data,
				       crl->signed_data.size, "c2.issuer",
				       &start1, &end1);
	asn1_delete_structure(&c2);

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

	len1 = end1 - start1 + 1;

	dn->data = &crl->signed_data.data[start1];
	dn->size = len1;

	return 0;

}