summaryrefslogtreecommitdiffhomepage
path: root/plugin/plugin.c
blob: e283c2cb89caa4fd36db985c131eead0a71dd557 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

/*

  Copyright (c) 2009-2012 Samuel Lidén Borell <samuel@kodafritt.se>
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#define _BSD_SOURCE 1
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include "../common/biderror.h"
#include <glib.h> // for g_ascii_strcasecmp

#include "plugin.h"


/**
 * Sets parameters that have some other value than "" or 0 as
 * their default value.
 */
static void setInitialParamValues(Plugin *plugin) {
    plugin->info.sign.messageEncoding = strdup("UTF-8");
}

Plugin *plugin_new(PluginType pluginType, const char *url,
                   const char *hostname, const char *ip,
                   Window windowId) {
    Plugin *plugin = calloc(1, sizeof(Plugin));

    if (!plugin) return NULL;
    plugin->type = pluginType;
    plugin->url = strdup(url);
    plugin->hostname = strdup(hostname);
    plugin->ip = strdup(ip);
    plugin->windowId = windowId;
    
    if (!plugin->url || !plugin->hostname || !plugin->ip) {
        plugin_free(plugin);
        return NULL;
    }
    
    setInitialParamValues(plugin);
    return plugin;
}

static void freePKCS10s(RegutilPKCS10 *pkcs10, bool freeSelf) {
    while (pkcs10) {
        RegutilPKCS10 *next = pkcs10->next;
        free(pkcs10->subjectDN);
        if (freeSelf) free(pkcs10);
        pkcs10 = next;
    }
}

static void freeCMCs(RegutilCMC *cmc, bool freeSelf) {
    while (cmc) {
        RegutilCMC *next = cmc->next;
        free(cmc->oneTimePassword);
        free(cmc->rfc2729cmcoid);
        if (freeSelf) free(cmc);
        cmc = next;
    }
}


void plugin_free(Plugin *plugin) {
    plugin_reset(plugin);
    free(plugin->url);
    free(plugin->hostname);
    free(plugin->ip);
    free(plugin);
}

void plugin_reset(Plugin *plugin) {
    switch (plugin->type) {
        case PT_Version:
        case PT_Webadmin:
        case PT_OldSigner:
            break;
        case PT_Authentication:
            free(plugin->info.auth.challenge);
            free(plugin->info.auth.policys);
            free(plugin->info.auth.subjectFilter);
            free(plugin->info.auth.signature);
            memset(&plugin->info.auth, 0, sizeof(plugin->info.auth));
            break;
        case PT_Signer:
            free(plugin->info.sign.challenge);
            free(plugin->info.sign.policys);
            free(plugin->info.sign.subjectFilter);
            free(plugin->info.sign.messageEncoding);
            free(plugin->info.sign.message);
            free(plugin->info.sign.invisibleMessage);
            free(plugin->info.sign.signature);
            memset(&plugin->info.sign, 0, sizeof(plugin->info.sign));
            break;
        case PT_Regutil:
            freePKCS10s(&plugin->info.regutil.currentPKCS10, false);
            freePKCS10s(plugin->info.regutil.input.pkcs10, true);
            freeCMCs(&plugin->info.regutil.currentCMC, false);
            freeCMCs(&plugin->info.regutil.input.cmc, false);
            memset(&plugin->info.regutil, 0, sizeof(plugin->info.regutil));
            break;
    }
    setInitialParamValues(plugin);
}

static char **getCommonParamPointer(Plugin *plugin, const char *name) {
    if (!g_ascii_strcasecmp(name, "Policys")) return &plugin->info.auth.policys;
    if (!g_ascii_strcasecmp(name, "Signature")) return &plugin->info.auth.signature;
    if (!g_ascii_strcasecmp(name, "Subjects")) return &plugin->info.sign.subjectFilter;
    return NULL;
}

static char **getParamPointer(Plugin *plugin, const char *name) {
    switch (plugin->type) {
        case PT_Authentication:
            if (!g_ascii_strcasecmp(name, "Challenge")) return &plugin->info.auth.challenge;
            return getCommonParamPointer(plugin, name);
        case PT_Signer:
            if (!g_ascii_strcasecmp(name, "Nonce")) return &plugin->info.sign.challenge;
            if (!g_ascii_strcasecmp(name, "TextCharacterEncoding")) return &plugin->info.sign.messageEncoding;
            if (!g_ascii_strcasecmp(name, "TextToBeSigned")) return &plugin->info.sign.message;
            if (!g_ascii_strcasecmp(name, "NonVisibleData")) return &plugin->info.sign.invisibleMessage;
            return getCommonParamPointer(plugin, name);
        case PT_Regutil:
            if (!g_ascii_strcasecmp(name, "SubjectDN")) return &plugin->info.regutil.currentPKCS10.subjectDN;
            if (!g_ascii_strcasecmp(name, "OneTimePassword")) return &plugin->info.regutil.currentCMC.oneTimePassword;
            return NULL;
        default:
            return NULL;
    }
}

static int *getIntParamPointer(Plugin *plugin, const char *name) {
    switch (plugin->type) {
        case PT_Regutil:
            if (!g_ascii_strcasecmp(name, "KeySize")) return &plugin->info.regutil.currentPKCS10.keySize;
            if (!g_ascii_strcasecmp(name, "MinLen")) return &plugin->info.regutil.input.minPasswordLength;
            if (!g_ascii_strcasecmp(name, "MinChars")) return &plugin->info.regutil.input.minPasswordNonDigits;
            if (!g_ascii_strcasecmp(name, "MinDigits")) return &plugin->info.regutil.input.minPasswordDigits;
            return NULL;
        default:
            return NULL;
    }
}

char *sign_getParam(Plugin *plugin, const char *name) {
    // Handle special parameters
    bool authOrSign = (plugin->type == PT_Authentication ||
                       plugin->type == PT_Signer);
    
    // Server time
    if (authOrSign && !g_ascii_strcasecmp(name, "ServerTime")) {
        int32_t value = plugin->info.auth.serverTime;
        if (value <= 0) return strdup("");
        
        char *s = malloc(11);
        sprintf(s, "%" PRIu32, value);
        return s;
    }
    
    // OnlyAcceptMRU (show only last used cert in list)
    if (authOrSign && !g_ascii_strcasecmp(name, "OnlyAcceptMRU")) {
        return strdup(plugin->info.auth.onlyAcceptMRU ? "true" : "false");
    }
    
    // Handle string parameters
    char **valuePtr = getParamPointer(plugin, name);
    
    if (valuePtr && *valuePtr) return strdup(*valuePtr);
    else return NULL;
}

bool sign_setParam(Plugin *plugin, const char *name, const char *value) {
    // Handle special parameters
    bool authOrSign = (plugin->type == PT_Authentication ||
                       plugin->type == PT_Signer);
    
    // Server time: This value is a 10-digit integer
    if (authOrSign && !g_ascii_strcasecmp(name, "ServerTime")) {
        plugin->lastError = BIDERR_OK;
        
        size_t length = strlen(value);
        if (length > 10) {
            plugin->lastError = BIDERR_ValueTooLong;
            plugin->info.auth.serverTime = 0;
            return false;
        }
        
        plugin->info.auth.serverTime = (int32_t)atoi(value);
        
        if (plugin->info.auth.serverTime <= 0) {
            plugin->lastError = BIDERR_InvalidValue;
            plugin->info.auth.serverTime = 0;
            return false;
        }
        
        if (length < 10) {
            // Accept the value but return an error code
            plugin->lastError = BIDERR_InvalidValue;
            return false;
        }
        
        return true;
    }
    
    // OnlyAcceptMRU: boolean value
    if (authOrSign && !g_ascii_strcasecmp(name, "OnlyAcceptMRU")) {
        plugin->lastError = BIDERR_OK;
        
        if (!g_ascii_strcasecmp(value, "true")) {
            plugin->info.auth.onlyAcceptMRU = true;
            return true;
        } else if (!g_ascii_strcasecmp(value, "false")) {
            plugin->info.auth.onlyAcceptMRU = false;
            return true;
        }
        
        plugin->lastError = BIDERR_InvalidBoolean;
        return false;
    }
    
    // TextCharacterEncoding: Only the values "UTF-8" and "ISO-88591-1"
    // are allowed (case-sensitive). This value is NOT escaped in the XML
    // signature.
    if (plugin->type == PT_Signer &&
        !g_ascii_strcasecmp(name, "TextCharacterEncoding") &&
        strcmp(value, "UTF-8") && strcmp(value, "ISO-8859-1")) {
        // Not sure about the name of this error code...
        plugin->lastError = BIDERR_ValueTooLong;
        return false;
    }
    
    // Handle string parameters
    char **valuePtr = getParamPointer(plugin, name);
    
    if (valuePtr == NULL) {
        plugin->lastError = BIDERR_InvalidParameter;
        return false;
    }
    
    free(*valuePtr);
    *valuePtr = strdup(value);
    if (*valuePtr != NULL) {
        plugin->lastError = BIDERR_OK;
        return true;
    } else {
        plugin->lastError = BIDERR_InternalError;
        return false;
    }
}

static bool hasSignParams(const Plugin *plugin) {
    return (plugin->info.auth.challenge);
}

int sign_performAction(Plugin *plugin, const char *action) {
    int ret = BIDERR_InvalidAction;
    
    if ((plugin->type == PT_Authentication) && !g_ascii_strcasecmp(action, "Authenticate")) {
        ret = (hasSignParams(plugin) ?
            sign_performAction_Authenticate(plugin) : BIDERR_MissingParameter);
        
    } else if ((plugin->type == PT_Signer) && !g_ascii_strcasecmp(action, "Sign")) {
        if (!hasSignParams(plugin) || !plugin->info.sign.message) {
            return BIDERR_MissingParameter;
        }
        ret = (hasSignParams(plugin) && plugin->info.sign.message ?
            sign_performAction_Sign(plugin) : BIDERR_MissingParameter);
    }
    
    plugin->lastError = ret;
    return ret;
}

void regutil_setParam(Plugin *plugin, const char *name, const char *value) {
    char **strPtr;
    int *intPtr;
    
    // Special parameters
    if (!g_ascii_strcasecmp(name, "KeyUsage")) {
        if (!strcmp(value, "digitalSignature")) {
            plugin->info.regutil.currentPKCS10.keyUsage = KeyUsage_Authentication;
        } else if (!strcmp(value, "nonRepudiation")) {
            plugin->info.regutil.currentPKCS10.keyUsage = KeyUsage_Signing;
        }
        
        plugin->lastError = BIDERR_OK; // Never return failure
    } else if (!g_ascii_strcasecmp(name, "rfc2797cmcoid")) {
        // We always use CMC Oids
        if (!strcmp(value, "true")) {
            plugin->lastError = BIDERR_OK;
        } else {
            plugin->lastError = RUERR_InvalidParameter;
        }
    } else if (!g_ascii_strcasecmp(name, "MaxLen")) {
        // A low MaxLen does not make anything more secure so ignore it
        plugin->lastError = BIDERR_OK;
    } else if ((intPtr = getIntParamPointer(plugin, name)) != NULL) {
        // Integer parameters
        errno = 0;
        int intval = strtol(value, NULL, 10);
        if (!errno) *intPtr = intval;
        plugin->lastError = (!errno ? BIDERR_OK : RUERR_InvalidValue);
    } else if ((strPtr = getParamPointer(plugin, name)) != NULL) {
        // String parameters
        free(*strPtr);
        *strPtr = strdup(value);
        plugin->lastError = (*strPtr ? BIDERR_OK : BIDERR_InternalError);
        
        if (!g_ascii_strcasecmp(name, "SubjectDN")) {
            plugin->info.regutil.currentPKCS10.includeFullDN = true;
        }
        
    } else {
        // Invalid parameter name
        plugin->lastError = RUERR_InvalidParameter;
    }
}

static char *safestrdup(const char *s) {
    return (s ? strdup(s) : NULL);
}

/**
 * Stores the current parameters so they get included with the request.
 */
void regutil_initRequest(Plugin *plugin, const char *type) {
    if (!g_ascii_strcasecmp(type, "pkcs10")) {
        // Limit number of objects
        RegutilPKCS10 *other = plugin->info.regutil.input.pkcs10;
        size_t count = 0;
        for (; other; other = other->next) {
            if (++count > 10) {
                plugin->lastError = BIDERR_InternalError;
                return;
            }
        }
        
        // Add PKCS10
        RegutilPKCS10 *copy = malloc(sizeof(RegutilPKCS10));
        memcpy(copy, &plugin->info.regutil.currentPKCS10, sizeof(RegutilPKCS10));
        copy->subjectDN = safestrdup(plugin->info.regutil.currentPKCS10.subjectDN);
        
        copy->next = plugin->info.regutil.input.pkcs10;
        plugin->info.regutil.input.pkcs10 = copy;
        
        plugin->info.regutil.currentPKCS10.includeFullDN = false;
        plugin->lastError = BIDERR_OK;
    } else if (!g_ascii_strcasecmp(type, "cmc")) {
        // CMC
        RegutilCMC *cmc = &plugin->info.regutil.input.cmc;
        
        free(cmc->oneTimePassword);
        free(cmc->rfc2729cmcoid);
        cmc->oneTimePassword = safestrdup(plugin->info.regutil.currentCMC.oneTimePassword);
        cmc->rfc2729cmcoid = safestrdup(plugin->info.regutil.currentCMC.rfc2729cmcoid);
        
        plugin->lastError = BIDERR_OK;
    } else {
        plugin->lastError = RUERR_InvalidValue;
    }
}