summaryrefslogtreecommitdiff
path: root/src/libstrongswan/crypto/certinfo.c
blob: 8a125e2473bf9c64d50e280d5624aca5e6b943d9 (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
/**
 * @file certinfo.c
 * 
 * @brief Implementation of certinfo_t.
 * 
 */

/*
 * Copyright (C) 2006 Andreas Steffen
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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 General Public License
 * for more details.
 */

#include <time.h>
#include <stdio.h>

#include <library.h>

#include "certinfo.h"

typedef struct private_certinfo_t private_certinfo_t;

/**
 * Private data of a certinfo_t object.
 */
struct private_certinfo_t {
	/**
	 * Public interface for this certificate status information object.
	 */
	certinfo_t public;
	
	/**
	 * Serial number of the certificate
	 */
	chunk_t serialNumber;

	/**
	 * Certificate status
	 */
	cert_status_t status;

	/**
	 * Certificate status is for one-time use only
	 */
	bool once;

	/**
	 * Time when the certificate status info was generated
	 */
	time_t thisUpdate;

	/**
	 * Time when an updated certifcate status info will be available
	 */
	time_t nextUpdate;

	/**
	 * Time of certificate revocation
	 */
    time_t revocationTime;

	/**
	 * Reason of certificate revocation
	 */
    crl_reason_t revocationReason;
};

ENUM(cert_status_names, CERT_GOOD, CERT_UNTRUSTED,
	"good",
	"revoked",
	"unknown",
	"unknown",
	"untrusted",
);

ENUM(crl_reason_names, REASON_UNSPECIFIED, REASON_REMOVE_FROM_CRL,
	"unspecified",
	"key compromise",
	"ca compromise",
	"affiliation changed",
	"superseded",
	"cessation of operation",
	"certificate hold",
	"reason #7",
	"remove from crl",
);

/**
 * Implements certinfo_t.compare_serialNumber
 */
static int compare_serialNumber(const private_certinfo_t *this, const private_certinfo_t *that)
{
	return chunk_compare(this->serialNumber, that->serialNumber);
}

/**
 * Implements certinfo_t.equals_serialNumber
 */
static bool equals_serialNumber(const private_certinfo_t *this, const private_certinfo_t *that)
{
	return chunk_equals(this->serialNumber, that->serialNumber);
}

/**
 * Implements certinfo_t.get_serialNumber
 */
static chunk_t get_serialNumber(const private_certinfo_t *this)
{
	return this->serialNumber;
}

/**
 * Implements certinfo_t.set_status
 */
static void set_status(private_certinfo_t *this, cert_status_t status)
{
	this->status = status;
}

/**
 * Implements certinfo_t.get_status
 */
static cert_status_t get_status(const private_certinfo_t *this)
{
	return this->status;
}

/**
 * Implements certinfo_t.set_thisUpdate
 */
static void set_thisUpdate(private_certinfo_t *this, time_t thisUpdate)
{
	this->thisUpdate = thisUpdate;
}

/**
 * Implements certinfo_t.get_thisUpdate
 */
static time_t get_thisUpdate(const private_certinfo_t *this)
{
	return this->thisUpdate;
}

/**
 * Implements certinfo_t.set_nextUpdate
 */
static void set_nextUpdate(private_certinfo_t *this, time_t nextUpdate)
{
	this->nextUpdate = nextUpdate;
}

/**
 * Implements certinfo_t.get_nextUpdate
 */
static time_t get_nextUpdate(const private_certinfo_t *this)
{
	return this->nextUpdate;
}

/**
 * Implements certinfo_t.set_revocationTime
 */
static void set_revocationTime(private_certinfo_t *this, time_t revocationTime)
{
	this->revocationTime = revocationTime;
}

/**
 * Implements certinfo_t.get_revocationTime
 */
static time_t get_revocationTime(const private_certinfo_t *this)
{
	return this->revocationTime;
}

/**
 * Implements certinfo_t.set_revocationReason
 */
static void set_revocationReason(private_certinfo_t *this, crl_reason_t reason)
{
	this->revocationReason = reason;
}

/**
 * Implements certinfo_t.get_revocationReason
 */
static crl_reason_t get_revocationReason(const private_certinfo_t *this)
{
	return this->revocationReason;
}

/**
 * Implements certinfo_t.update
 */
static void update(private_certinfo_t *this, const private_certinfo_t *that)
{
	if (equals_serialNumber(this, that))
	{
		chunk_t this_serialNumber = this->serialNumber;

		*this = *that;
		this->serialNumber = this_serialNumber;
	}
}

/**
 * Implements certinfo_t.destroy
 */
static void destroy(private_certinfo_t *this)
{
	free(this->serialNumber.ptr);
	free(this);
}

/*
 * Described in header.
 */
certinfo_t *certinfo_create(chunk_t serial)
{
	private_certinfo_t *this = malloc_thing(private_certinfo_t);
	
	/* initialize */
	this->serialNumber = chunk_clone(serial);
	this->status = CERT_UNDEFINED;
	this->thisUpdate = UNDEFINED_TIME;
	this->nextUpdate = UNDEFINED_TIME;
	this->revocationTime = UNDEFINED_TIME;
	this->revocationReason = REASON_UNSPECIFIED;

	/* public functions */
	this->public.compare_serialNumber = (int (*) (const certinfo_t*,const certinfo_t*))compare_serialNumber;
	this->public.equals_serialNumber = (bool (*) (const certinfo_t*,const certinfo_t*))equals_serialNumber;
	this->public.get_serialNumber = (chunk_t (*) (const certinfo_t*))get_serialNumber;
	this->public.set_status = (void (*) (certinfo_t*,cert_status_t))set_status;
	this->public.get_status = (cert_status_t (*) (const certinfo_t*))get_status;
	this->public.set_thisUpdate = (void (*) (certinfo_t*,time_t))set_thisUpdate;
	this->public.get_thisUpdate = (time_t (*) (const certinfo_t*))get_thisUpdate;
	this->public.set_nextUpdate = (void (*) (certinfo_t*,time_t))set_nextUpdate;
	this->public.get_nextUpdate = (time_t (*) (const certinfo_t*))get_nextUpdate;
	this->public.set_revocationTime = (void (*) (certinfo_t*,time_t))set_revocationTime;
	this->public.get_revocationTime = (time_t (*) (const certinfo_t*))get_revocationTime;
	this->public.set_revocationReason = (void (*) (certinfo_t*, crl_reason_t))set_revocationReason;
	this->public.get_revocationReason = (crl_reason_t(*) (const certinfo_t*))get_revocationReason;
	this->public.update = (void (*) (certinfo_t*, const certinfo_t*))update;
	this->public.destroy = (void (*) (certinfo_t*))destroy;

	return &this->public;
}