removed %Q, %Y, %W, %U printf handlers
[strongswan.git] / src / libstrongswan / crypto / certinfo.c
1 /**
2 * @file certinfo.c
3 *
4 * @brief Implementation of certinfo_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 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 <time.h>
24 #include <stdio.h>
25
26 #include <library.h>
27
28 #include "certinfo.h"
29
30 typedef struct private_certinfo_t private_certinfo_t;
31
32 /**
33 * Private data of a certinfo_t object.
34 */
35 struct private_certinfo_t {
36 /**
37 * Public interface for this certificate status information object.
38 */
39 certinfo_t public;
40
41 /**
42 * Serial number of the certificate
43 */
44 chunk_t serialNumber;
45
46 /**
47 * Certificate status
48 */
49 cert_status_t status;
50
51 /**
52 * Certificate status is for one-time use only
53 */
54 bool once;
55
56 /**
57 * Time when the certificate status info was generated
58 */
59 time_t thisUpdate;
60
61 /**
62 * Time when an updated certifcate status info will be available
63 */
64 time_t nextUpdate;
65
66 /**
67 * Time of certificate revocation
68 */
69 time_t revocationTime;
70
71 /**
72 * Reason of certificate revocation
73 */
74 crl_reason_t revocationReason;
75 };
76
77 ENUM(cert_status_names, CERT_GOOD, CERT_UNTRUSTED,
78 "good",
79 "revoked",
80 "unknown",
81 "unknown",
82 "untrusted",
83 );
84
85 ENUM(crl_reason_names, REASON_UNSPECIFIED, REASON_REMOVE_FROM_CRL,
86 "unspecified",
87 "key compromise",
88 "ca compromise",
89 "affiliation changed",
90 "superseded",
91 "cessation of operation",
92 "certificate hold",
93 "reason #7",
94 "remove from crl",
95 );
96
97 /**
98 * Implements certinfo_t.compare_serialNumber
99 */
100 static int compare_serialNumber(const private_certinfo_t *this, const private_certinfo_t *that)
101 {
102 return chunk_compare(this->serialNumber, that->serialNumber);
103 }
104
105 /**
106 * Implements certinfo_t.equals_serialNumber
107 */
108 static bool equals_serialNumber(const private_certinfo_t *this, const private_certinfo_t *that)
109 {
110 return chunk_equals(this->serialNumber, that->serialNumber);
111 }
112
113 /**
114 * Implements certinfo_t.get_serialNumber
115 */
116 static chunk_t get_serialNumber(const private_certinfo_t *this)
117 {
118 return this->serialNumber;
119 }
120
121 /**
122 * Implements certinfo_t.set_status
123 */
124 static void set_status(private_certinfo_t *this, cert_status_t status)
125 {
126 this->status = status;
127 }
128
129 /**
130 * Implements certinfo_t.get_status
131 */
132 static cert_status_t get_status(const private_certinfo_t *this)
133 {
134 return this->status;
135 }
136
137 /**
138 * Implements certinfo_t.set_thisUpdate
139 */
140 static void set_thisUpdate(private_certinfo_t *this, time_t thisUpdate)
141 {
142 this->thisUpdate = thisUpdate;
143 }
144
145 /**
146 * Implements certinfo_t.get_thisUpdate
147 */
148 static time_t get_thisUpdate(const private_certinfo_t *this)
149 {
150 return this->thisUpdate;
151 }
152
153 /**
154 * Implements certinfo_t.set_nextUpdate
155 */
156 static void set_nextUpdate(private_certinfo_t *this, time_t nextUpdate)
157 {
158 this->nextUpdate = nextUpdate;
159 }
160
161 /**
162 * Implements certinfo_t.get_nextUpdate
163 */
164 static time_t get_nextUpdate(const private_certinfo_t *this)
165 {
166 return this->nextUpdate;
167 }
168
169 /**
170 * Implements certinfo_t.set_revocationTime
171 */
172 static void set_revocationTime(private_certinfo_t *this, time_t revocationTime)
173 {
174 this->revocationTime = revocationTime;
175 }
176
177 /**
178 * Implements certinfo_t.get_revocationTime
179 */
180 static time_t get_revocationTime(const private_certinfo_t *this)
181 {
182 return this->revocationTime;
183 }
184
185 /**
186 * Implements certinfo_t.set_revocationReason
187 */
188 static void set_revocationReason(private_certinfo_t *this, crl_reason_t reason)
189 {
190 this->revocationReason = reason;
191 }
192
193 /**
194 * Implements certinfo_t.get_revocationReason
195 */
196 static crl_reason_t get_revocationReason(const private_certinfo_t *this)
197 {
198 return this->revocationReason;
199 }
200
201 /**
202 * Implements certinfo_t.update
203 */
204 static void update(private_certinfo_t *this, const private_certinfo_t *that)
205 {
206 if (equals_serialNumber(this, that))
207 {
208 chunk_t this_serialNumber = this->serialNumber;
209
210 *this = *that;
211 this->serialNumber = this_serialNumber;
212 }
213 }
214
215 /**
216 * Implements certinfo_t.destroy
217 */
218 static void destroy(private_certinfo_t *this)
219 {
220 free(this->serialNumber.ptr);
221 free(this);
222 }
223
224 /*
225 * Described in header.
226 */
227 certinfo_t *certinfo_create(chunk_t serial)
228 {
229 private_certinfo_t *this = malloc_thing(private_certinfo_t);
230
231 /* initialize */
232 this->serialNumber = chunk_clone(serial);
233 this->status = CERT_UNDEFINED;
234 this->thisUpdate = UNDEFINED_TIME;
235 this->nextUpdate = UNDEFINED_TIME;
236 this->revocationTime = UNDEFINED_TIME;
237 this->revocationReason = REASON_UNSPECIFIED;
238
239 /* public functions */
240 this->public.compare_serialNumber = (int (*) (const certinfo_t*,const certinfo_t*))compare_serialNumber;
241 this->public.equals_serialNumber = (bool (*) (const certinfo_t*,const certinfo_t*))equals_serialNumber;
242 this->public.get_serialNumber = (chunk_t (*) (const certinfo_t*))get_serialNumber;
243 this->public.set_status = (void (*) (certinfo_t*,cert_status_t))set_status;
244 this->public.get_status = (cert_status_t (*) (const certinfo_t*))get_status;
245 this->public.set_thisUpdate = (void (*) (certinfo_t*,time_t))set_thisUpdate;
246 this->public.get_thisUpdate = (time_t (*) (const certinfo_t*))get_thisUpdate;
247 this->public.set_nextUpdate = (void (*) (certinfo_t*,time_t))set_nextUpdate;
248 this->public.get_nextUpdate = (time_t (*) (const certinfo_t*))get_nextUpdate;
249 this->public.set_revocationTime = (void (*) (certinfo_t*,time_t))set_revocationTime;
250 this->public.get_revocationTime = (time_t (*) (const certinfo_t*))get_revocationTime;
251 this->public.set_revocationReason = (void (*) (certinfo_t*, crl_reason_t))set_revocationReason;
252 this->public.get_revocationReason = (crl_reason_t(*) (const certinfo_t*))get_revocationReason;
253 this->public.update = (void (*) (certinfo_t*, const certinfo_t*))update;
254 this->public.destroy = (void (*) (certinfo_t*))destroy;
255
256 return &this->public;
257 }