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
|
/* Pluto certificate/CRL/AC builder hooks.
* Copyright (C) 2002-2009 Andreas Steffen
* Copyright (C) 2009 Martin Willi
* HSR 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 "builder.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <freeswan.h>
#include "library.h"
#include "constants.h"
#include "defs.h"
#include "log.h"
#include "id.h"
#include "certs.h"
#include "ac.h"
#include "crl.h"
typedef struct private_builder_t private_builder_t;
struct private_builder_t {
/** implements builder interface */
builder_t public;
/** built credential */
union {
void *cred;
cert_t *cert;
x509crl_t *crl;
x509acert_t *ac;
};
};
/**
* builder add function for certificates
*/
static void cert_add(private_builder_t *this, builder_part_t part, ...)
{
chunk_t blob;
va_list args;
va_start(args, part);
blob = va_arg(args, chunk_t);
va_end(args);
switch (part)
{
case BUILD_BLOB_PGP:
{
pgpcert_t *pgpcert = malloc_thing(pgpcert_t);
*pgpcert = pgpcert_empty;
if (parse_pgp(blob, pgpcert))
{
this->cert = malloc_thing(cert_t);
this->cert->type = CERT_PGP;
this->cert->u.pgp = pgpcert;
}
else
{
plog(" error in OpenPGP certificate");
free_pgpcert(pgpcert);
}
break;
}
case BUILD_BLOB_ASN1_DER:
{
x509cert_t *x509cert = malloc_thing(x509cert_t);
*x509cert = empty_x509cert;
if (parse_x509cert(blob, 0, x509cert))
{
this->cert = malloc_thing(cert_t);
this->cert->type = CERT_X509_SIGNATURE;
this->cert->u.x509 = x509cert;
}
else
{
plog(" error in X.509 certificate");
free_x509cert(x509cert);
}
break;
}
default:
if (this->cert)
{
switch (this->cert->type)
{
case CERT_X509_SIGNATURE:
free_x509cert(this->cert->u.x509);
break;
case CERT_PGP:
free_pgpcert(this->cert->u.pgp);
break;
default:
break;
}
free(this->cert);
}
builder_cancel(&this->public);
break;
}
}
/**
* builder add function for attribute certificates
*/
static void ac_add(private_builder_t *this, builder_part_t part, ...)
{
chunk_t blob;
va_list args;
switch (part)
{
case BUILD_BLOB_ASN1_DER:
{
va_start(args, part);
blob = va_arg(args, chunk_t);
va_end(args);
this->ac = malloc_thing(x509acert_t);
*this->ac = empty_ac;
if (!parse_ac(blob, this->ac) && !verify_x509acert(this->ac, FALSE))
{
free_acert(this->ac);
this->ac = NULL;
}
break;
}
default:
free_acert(this->ac);
builder_cancel(&this->public);
break;
}
}
/**
* builder add function for crls
*/
static void crl_add(private_builder_t *this, builder_part_t part, ...)
{
chunk_t blob;
va_list args;
switch (part)
{
case BUILD_BLOB_ASN1_DER:
{
va_start(args, part);
blob = va_arg(args, chunk_t);
va_end(args);
this->crl = malloc_thing(x509crl_t);
*this->crl = empty_x509crl;
if (!parse_x509crl(blob, 0, this->crl))
{
plog(" error in X.509 crl");
free_crl(this->crl);
this->crl = NULL;
}
break;
}
default:
free_crl(this->crl);
builder_cancel(&this->public);
break;
}
}
/**
* builder build function
*/
static void *build(private_builder_t *this)
{
void *cred;
cred = this->cred;
free(this);
return cred;
}
/**
* builder for pluto credentials
*/
static builder_t *builder(credential_type_t type, int subtype)
{
private_builder_t *this = malloc_thing(private_builder_t);
switch (subtype)
{
case CRED_TYPE_CERTIFICATE:
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))cert_add;
break;
case CRED_TYPE_AC:
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))ac_add;
break;
case CRED_TYPE_CRL:
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))crl_add;
break;
default:
free(this);
return NULL;
}
this->public.build = (void*(*)(builder_t*))build;
this->cred = NULL;
return &this->public;
}
void init_builder(void)
{
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, 0,
(builder_constructor_t)builder);
}
void free_builder(void)
{
lib->creds->remove_builder(lib->creds, (builder_constructor_t)builder);
}
|