vici: Support loading of different certificate types
[strongswan.git] / src / libcharon / plugins / vici / vici_cred.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "vici_cred.h"
17 #include "vici_builder.h"
18
19 #include <credentials/sets/mem_cred.h>
20 #include <credentials/certificates/ac.h>
21 #include <credentials/certificates/crl.h>
22 #include <credentials/certificates/x509.h>
23
24 typedef struct private_vici_cred_t private_vici_cred_t;
25
26 /**
27 * Private data of an vici_cred_t object.
28 */
29 struct private_vici_cred_t {
30
31 /**
32 * Public vici_cred_t interface.
33 */
34 vici_cred_t public;
35
36 /**
37 * Dispatcher
38 */
39 vici_dispatcher_t *dispatcher;
40
41 /**
42 * credentials
43 */
44 mem_cred_t *creds;
45 };
46
47 /**
48 * Create a (error) reply message
49 */
50 static vici_message_t* create_reply(char *fmt, ...)
51 {
52 vici_builder_t *builder;
53 va_list args;
54
55 builder = vici_builder_create();
56 builder->add_kv(builder, "success", fmt ? "no" : "yes");
57 if (fmt)
58 {
59 va_start(args, fmt);
60 builder->vadd_kv(builder, "errmsg", fmt, args);
61 va_end(args);
62 }
63 return builder->finalize(builder);
64 }
65
66 CALLBACK(load_cert, vici_message_t*,
67 private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
68 {
69 certificate_type_t type;
70 x509_flag_t required_flags = 0, additional_flags = 0;
71 certificate_t *cert;
72 x509_t *x509;
73 chunk_t data;
74 char *str;
75
76 str = message->get_str(message, NULL, "type");
77 if (!str)
78 {
79 return create_reply("certificate type missing");
80 }
81 if (strcaseeq(str, "x509"))
82 {
83 type = CERT_X509;
84 }
85 else if (strcaseeq(str, "x509ca"))
86 {
87 type = CERT_X509;
88 required_flags = X509_CA;
89 }
90 else if (strcaseeq(str, "x509aa"))
91 {
92 type = CERT_X509;
93 additional_flags = X509_AA;
94 }
95 else if (strcaseeq(str, "x509crl"))
96 {
97 type = CERT_X509_CRL;
98 }
99 else if (strcaseeq(str, "x509ac"))
100 {
101 type = CERT_X509_AC;
102 }
103 else
104 {
105 return create_reply("invalid certificate type: %s", str);
106 }
107 data = message->get_value(message, chunk_empty, "data");
108 if (!data.len)
109 {
110 return create_reply("certificate data missing");
111 }
112 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
113 BUILD_BLOB_PEM, data,
114 BUILD_X509_FLAG, additional_flags,
115 BUILD_END);
116 if (!cert)
117 {
118 return create_reply("parsing %N certificate failed",
119 certificate_type_names, type);
120 }
121 if (cert->get_type(cert) == CERT_X509)
122 {
123 x509 = (x509_t*)cert;
124
125 if ((required_flags & x509->get_flags(x509)) != required_flags)
126 {
127 cert->destroy(cert);
128 return create_reply("certificate misses required flag, rejected");
129 }
130 }
131
132 DBG1(DBG_CFG, "loaded certificate '%Y'", cert->get_subject(cert));
133
134 this->creds->add_cert(this->creds, TRUE, cert);
135
136 return create_reply(NULL);
137 }
138
139 CALLBACK(clear_creds, vici_message_t*,
140 private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
141 {
142 vici_builder_t *builder;
143
144 this->creds->clear(this->creds);
145
146 builder = vici_builder_create();
147 return builder->finalize(builder);
148 }
149
150 static void manage_command(private_vici_cred_t *this,
151 char *name, vici_command_cb_t cb, bool reg)
152 {
153 this->dispatcher->manage_command(this->dispatcher, name,
154 reg ? cb : NULL, this);
155 }
156
157 /**
158 * (Un-)register dispatcher functions
159 */
160 static void manage_commands(private_vici_cred_t *this, bool reg)
161 {
162 manage_command(this, "clear-creds", clear_creds, reg);
163 manage_command(this, "load-cert", load_cert, reg);
164 }
165
166 METHOD(vici_cred_t, destroy, void,
167 private_vici_cred_t *this)
168 {
169 manage_commands(this, FALSE);
170
171 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
172 this->creds->destroy(this->creds);
173 free(this);
174 }
175
176 /**
177 * See header
178 */
179 vici_cred_t *vici_cred_create(vici_dispatcher_t *dispatcher)
180 {
181 private_vici_cred_t *this;
182
183 INIT(this,
184 .public = {
185 .destroy = _destroy,
186 },
187 .dispatcher = dispatcher,
188 .creds = mem_cred_create(),
189 );
190
191 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
192
193 manage_commands(this, TRUE);
194
195 return &this->public;
196 }