moved builder hooks to a separate file
[strongswan.git] / src / pluto / builder.c
1 /* Pluto certificate/CRL/AC builder hooks.
2 * Copyright (C) 2002-2009 Andreas Steffen
3 * Copyright (C) 2009 Martin Willi
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "builder.h"
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <freeswan.h>
25
26 #include "library.h"
27
28 #include "constants.h"
29 #include "defs.h"
30 #include "log.h"
31 #include "id.h"
32 #include "certs.h"
33
34 /**
35 * currently building cert_t
36 */
37 static cert_t *cert;
38
39 /**
40 * builder add function
41 */
42 static void add(builder_t *this, builder_part_t part, ...)
43 {
44 chunk_t blob;
45 va_list args;
46
47 va_start(args, part);
48 blob = va_arg(args, chunk_t);
49 va_end(args);
50
51 switch (part)
52 {
53 case BUILD_BLOB_PGP:
54 {
55 pgpcert_t *pgpcert = malloc_thing(pgpcert_t);
56 *pgpcert = pgpcert_empty;
57 if (parse_pgp(blob, pgpcert))
58 {
59 cert->type = CERT_PGP;
60 cert->u.pgp = pgpcert;
61 }
62 else
63 {
64 plog(" error in OpenPGP certificate");
65 free_pgpcert(pgpcert);
66 }
67 break;
68 }
69 case BUILD_BLOB_ASN1_DER:
70 {
71 x509cert_t *x509cert = malloc_thing(x509cert_t);
72 *x509cert = empty_x509cert;
73 if (parse_x509cert(blob, 0, x509cert))
74 {
75 cert->type = CERT_X509_SIGNATURE;
76 cert->u.x509 = x509cert;
77 }
78 else
79 {
80 plog(" error in X.509 certificate");
81 free_x509cert(x509cert);
82 }
83 break;
84 }
85 default:
86 builder_cancel(this);
87 break;
88 }
89 }
90
91 /**
92 * builder build function
93 */
94 static void *build(builder_t *this)
95 {
96 free(this);
97 if (cert->type == CERT_NONE)
98 {
99 return NULL;
100 }
101 return cert;
102 }
103
104 /**
105 * certificate builder in cert_t format.
106 */
107 static builder_t *cert_builder(credential_type_t type, int subtype)
108 {
109 builder_t *this;
110
111 if (subtype != CRED_TYPE_CERTIFICATE)
112 {
113 return NULL;
114 }
115 this = malloc_thing(builder_t);
116 this->add = add;
117 this->build = build;
118
119 cert->type = CERT_NONE;
120 cert->u.x509 = NULL;
121 cert->u.pgp = NULL;
122
123 return this;
124 }
125
126 void init_builder(void)
127 {
128 lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, CRED_TYPE_CERTIFICATE,
129 (builder_constructor_t)cert_builder);
130 }
131
132 void free_builder(void)
133 {
134 lib->creds->remove_builder(lib->creds, (builder_constructor_t)cert_builder);
135 }
136