aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/crypto/certinfo.c
blob: a289d6562a62ea3be3b7c1fbfa2a30cecee60e10 (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
/**
 * @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 <types.h>
#include <definitions.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;

	/**
	 * 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;
};

/**
 * RFC 2560 OCSP - certificate status
 */
static const char *const cert_status_name[] = {
	"good",
	"revoked",
	"unknown",
	"unknown",
	"untrusted"
    };

enum_names cert_status_names =
    { CERT_GOOD, CERT_UNTRUSTED, cert_status_name, NULL};

/**
 * RFC 2459 CRL reason codes
 */
static const char *const crl_reason_name[] = {
	"unspecified",
	"key compromise",
	"ca compromise",
	"affiliation changed",
	"superseded",
	"cessation of operation",
	"certificate hold",
	"reason #7",
	"remove from crl"
    };

enum_names crl_reason_names =
    { REASON_UNSPECIFIED, REASON_REMOVE_FROM_CRL, crl_reason_name, NULL};

/**
 * 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_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 const char *get_revocationReason(const private_certinfo_t *this)
{
	return enum_name(&crl_reason_names, this->revocationReason);
}

/**
 * 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->nextUpdate = UNDEFINED_TIME;
	this->revocationTime = UNDEFINED_TIME;
	this->revocationReason = REASON_UNSPECIFIED;

	/* public functions */
	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_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 = (const char *(*) (const certinfo_t*))get_revocationReason;
	this->public.destroy = (void (*) (certinfo_t*))destroy;

	return &this->public;
}