listing ca_info items
[strongswan.git] / src / libstrongswan / crypto / ca.c
1 /**
2 * @file ca.c
3 *
4 * @brief Implementation of ca_info_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Andreas Steffen
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include "ca.h"
29
30 #include <library.h>
31 #include <debug.h>
32 #include <utils/linked_list.h>
33 #include <utils/identification.h>
34
35 typedef struct private_ca_info_t private_ca_info_t;
36
37 /**
38 * Private data of a ca_info_t object.
39 */
40 struct private_ca_info_t {
41 /**
42 * Public interface for this ca info record
43 */
44 ca_info_t public;
45
46 /**
47 * Name of the ca info record
48 */
49 char *name;
50
51 /**
52 * Time when ca info record was installed
53 */
54 time_t installed;
55
56 /**
57 * Distinguished Name of the CA
58 */
59 x509_t *cacert;
60
61 /**
62 * List of crlDistributionPoints
63 */
64 linked_list_t *crlURIs;
65
66 /**
67 * List of ocspAccessPoints
68 */
69 linked_list_t *ocspURIs;
70 };
71
72 /**
73 * Implements ca_info_t.add_crluri
74 */
75 static void add_crluri(private_ca_info_t *this, const char* uri)
76 {
77 if (uri == NULL)
78 {
79 return;
80 }
81 if (strncasecmp(uri, "http", 4) != 0
82 && strncasecmp(uri, "ldap", 4) != 0
83 && strncasecmp(uri, "file", 4) != 0
84 && strncasecmp(uri, "ftp", 3) != 0)
85 {
86 DBG1(" invalid crl uri '%s'", uri);
87 return;
88 }
89 }
90
91 /**
92 * Implements ca_info_t.add_ocspuri
93 */
94 static void add_ocspuri(private_ca_info_t *this, const char* uri)
95 {
96 if (uri == NULL)
97 {
98 return;
99 }
100 if (strncasecmp(uri, "http", 4) != 0)
101 {
102 DBG1(" invalid ocsp uri '%s'", uri);
103 return;
104 }
105 }
106
107 /**
108 * Implements ca_info_t.destroy
109 */
110 static void destroy(private_ca_info_t *this)
111 {
112 this->crlURIs->destroy_offset(this->crlURIs,
113 offsetof(identification_t, destroy));
114 this->ocspURIs->destroy_offset(this->ocspURIs,
115 offsetof(identification_t, destroy));
116 free(this->name);
117 free(this);
118 }
119
120 /**
121 * output handler in printf()
122 */
123 static int print(FILE *stream, const struct printf_info *info,
124 const void *const *args)
125 {
126 private_ca_info_t *this = *((private_ca_info_t**)(args[0]));
127 bool utc = TRUE;
128 int written = 0;
129 x509_t *cacert;
130 chunk_t keyid;
131
132 if (info->alt)
133 {
134 utc = *((bool*)args[1]);
135 }
136
137 if (this == NULL)
138 {
139 return fprintf(stream, "(null)");
140 }
141
142 written += fprintf(stream, "%#T, \"%s\"\n", &this->installed, utc, this->name);
143
144 cacert = this->cacert;
145 written += fprintf(stream, " authname: '%D'\n", cacert->get_subject(cacert));
146
147 keyid = cacert->get_keyid(cacert);
148 written += fprintf(stream, " keyid: %#B\n", &keyid);
149
150 return written;
151 }
152
153 /**
154 * register printf() handlers
155 */
156 static void __attribute__ ((constructor))print_register()
157 {
158 register_printf_function(PRINTF_CAINFO, print, arginfo_ptr_alt_ptr_int);
159 }
160
161 /*
162 * Described in header.
163 */
164 ca_info_t *ca_info_create(const char *name, const x509_t *cacert)
165 {
166 private_ca_info_t *this = malloc_thing(private_ca_info_t);
167
168 /* initialize */
169 this->installed = time(NULL);
170 this->name = strdup(name);
171 this->cacert = cacert;
172 this->crlURIs = linked_list_create();
173 this->ocspURIs = linked_list_create();
174
175 /* public functions */
176 this->public.add_crluri = (void (*) (ca_info_t*,const char*))add_crluri;
177 this->public.add_ocspuri = (void (*) (ca_info_t*,const char*))add_ocspuri;
178 this->public.destroy = (void (*) (ca_info_t*))destroy;
179
180 return &this->public;
181 }