1 /*	$OpenBSD: crl.c,v 1.11 2021/10/26 10:52:49 claudio Exp $ */
2 /*
3  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/socket.h>
19 
20 #include <arpa/inet.h>
21 #include <assert.h>
22 #include <err.h>
23 #include <inttypes.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include "extern.h"
30 
31 X509_CRL *
crl_parse(const char * fn,const unsigned char * der,size_t len)32 crl_parse(const char *fn, const unsigned char *der, size_t len)
33 {
34 	int		 rc = 0;
35 	X509_CRL	*x = NULL;
36 
37 	/* just fail for empty buffers, the warning was printed elsewhere */
38 	if (der == NULL)
39 		return NULL;
40 
41 	if ((x = d2i_X509_CRL(NULL, &der, len)) == NULL) {
42 		cryptowarnx("%s: d2i_X509_CRL", fn);
43 		goto out;
44 	}
45 
46 	rc = 1;
47 out:
48 	if (rc == 0) {
49 		X509_CRL_free(x);
50 		x = NULL;
51 	}
52 	return x;
53 }
54 
55 static inline int
crlcmp(struct crl * a,struct crl * b)56 crlcmp(struct crl *a, struct crl *b)
57 {
58 	return strcmp(a->aki, b->aki);
59 }
60 
61 RB_GENERATE(crl_tree, crl, entry, crlcmp);
62 
63 void
free_crl(struct crl * crl)64 free_crl(struct crl *crl)
65 {
66 	free(crl->aki);
67 	X509_CRL_free(crl->x509_crl);
68 	free(crl);
69 }
70