forked from OpenSC/libp11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eng_parse.c
400 lines (352 loc) · 9.48 KB
/
eng_parse.c
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
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright (c) 2001 Markus Friedl
* Copyright (c) 2002 Juha Yrjölä
* Copyright (c) 2002 Olaf Kirch
* Copyright (c) 2003 Kevin Stefanik
* Copyright (c) 2016-2017 Michał Trojnara <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "engine.h"
#include <stdio.h>
#include <string.h>
#if defined(_WIN32) || defined(_WIN64)
#define strncasecmp _strnicmp
#endif
static int hex_to_bin(ENGINE_CTX *ctx,
const char *in, unsigned char *out, size_t *outlen)
{
size_t left, count = 0;
if (!in || *in == '\0') {
*outlen = 0;
return 1;
}
left = *outlen;
while (*in != '\0') {
int byte = 0, nybbles = 2;
while (nybbles-- && *in && *in != ':') {
char c;
byte <<= 4;
c = *in++;
if ('0' <= c && c <= '9')
c -= '0';
else if ('a' <= c && c <= 'f')
c = c - 'a' + 10;
else if ('A' <= c && c <= 'F')
c = c - 'A' + 10;
else {
ctx_log(ctx, 0,
"hex_to_bin(): invalid char '%c' in hex string\n",
c);
*outlen = 0;
return 0;
}
byte |= c;
}
if (*in == ':')
in++;
if (left == 0) {
ctx_log(ctx, 0, "hex_to_bin(): hex string too long\n");
*outlen = 0;
return 0;
}
out[count++] = (unsigned char)byte;
left--;
}
*outlen = count;
return 1;
}
/* parse string containing slot and id information */
int parse_slot_id_string(ENGINE_CTX *ctx,
const char *slot_id, int *slot,
unsigned char *id, size_t *id_len, char **label)
{
int n, i;
/* support for several formats */
#define HEXDIGITS "01234567890ABCDEFabcdef"
#define DIGITS "0123456789"
/* first: pure hex number (id, slot is undefined) */
if (strspn(slot_id, HEXDIGITS) == strlen(slot_id)) {
/* ah, easiest case: only hex. */
if ((strlen(slot_id) + 1) / 2 > *id_len) {
ctx_log(ctx, 0, "ID string too long!\n");
return 0;
}
*slot = -1;
return hex_to_bin(ctx, slot_id, id, id_len);
}
/* second: slot:id. slot is an digital int. */
if (sscanf(slot_id, "%d", &n) == 1) {
i = strspn(slot_id, DIGITS);
if (slot_id[i] != ':') {
ctx_log(ctx, 0, "Could not parse string!\n");
return 0;
}
i++;
if (slot_id[i] == 0) {
*slot = n;
*id_len = 0;
return 1;
}
if (strspn(slot_id + i, HEXDIGITS) + i != strlen(slot_id)) {
ctx_log(ctx, 0, "Could not parse string!\n");
return 0;
}
/* ah, rest is hex */
if ((strlen(slot_id) - i + 1) / 2 > *id_len) {
ctx_log(ctx, 0, "ID string too long!\n");
return 0;
}
*slot = n;
return hex_to_bin(ctx, slot_id + i, id, id_len);
}
/* third: id_<id>, slot is undefined */
if (strncmp(slot_id, "id_", 3) == 0) {
if (strspn(slot_id + 3, HEXDIGITS) + 3 != strlen(slot_id)) {
ctx_log(ctx, 0, "Could not parse string!\n");
return 0;
}
/* ah, rest is hex */
if ((strlen(slot_id) - 3 + 1) / 2 > *id_len) {
ctx_log(ctx, 0, "ID string too long!\n");
return 0;
}
*slot = -1;
return hex_to_bin(ctx, slot_id + 3, id, id_len);
}
/* label_<label>, slot is undefined */
if (strncmp(slot_id, "label_", 6) == 0) {
*slot = -1;
*label = OPENSSL_strdup(slot_id + 6);
*id_len = 0;
return *label != NULL;
}
/* last try: it has to be slot_<slot> and then "-id_<cert>" */
if (strncmp(slot_id, "slot_", 5) != 0) {
ctx_log(ctx, 0, "Format not recognized!\n");
return 0;
}
/* slot is an digital int. */
if (sscanf(slot_id + 5, "%d", &n) != 1) {
ctx_log(ctx, 0, "Could not decode slot number!\n");
return 0;
}
i = strspn(slot_id + 5, DIGITS);
if (slot_id[i + 5] == 0) {
*slot = n;
*id_len = 0;
return 1;
}
if (slot_id[i + 5] != '-') {
ctx_log(ctx, 0, "Could not parse string!\n");
return 0;
}
i = 5 + i + 1;
/* now followed by "id_" */
if (strncmp(slot_id + i, "id_", 3) == 0) {
if (strspn(slot_id + i + 3, HEXDIGITS) + 3 + i != strlen(slot_id)) {
ctx_log(ctx, 0, "Could not parse string!\n");
return 0;
}
/* ah, rest is hex */
if ((strlen(slot_id) - i - 3 + 1) / 2 > *id_len) {
ctx_log(ctx, 0, "ID string too long!\n");
return 0;
}
*slot = n;
return hex_to_bin(ctx, slot_id + i + 3, id, id_len);
}
/* ... or "label_" */
if (strncmp(slot_id + i, "label_", 6) == 0) {
*slot = n;
*label = OPENSSL_strdup(slot_id + i + 6);
*id_len = 0;
return *label != NULL;
}
ctx_log(ctx, 0, "Could not parse string!\n");
return 0;
}
static int parse_uri_attr(ENGINE_CTX *ctx,
const char *attr, int attrlen, unsigned char **field,
size_t *field_len)
{
size_t max, outlen = 0;
unsigned char *out;
int ret = 1;
if (field_len) {
out = *field;
max = *field_len;
} else {
out = OPENSSL_malloc(attrlen + 1);
if (!out)
return 0;
max = attrlen + 1;
}
while (ret && attrlen && outlen < max) {
if (*attr == '%') {
if (attrlen < 3) {
ret = 0;
} else {
char tmp[3];
size_t l = 1;
tmp[0] = attr[1];
tmp[1] = attr[2];
tmp[2] = 0;
ret = hex_to_bin(ctx, tmp, &out[outlen++], &l);
attrlen -= 3;
attr += 3;
}
} else {
out[outlen++] = *(attr++);
attrlen--;
}
}
if (attrlen && outlen == max)
ret = 0;
if (ret) {
if (field_len) {
*field_len = outlen;
} else {
out[outlen] = 0;
*field = out;
}
} else {
if (!field_len)
OPENSSL_free(out);
}
return ret;
}
static int read_from_file(ENGINE_CTX *ctx,
const char *path, char *field, size_t *field_len)
{
BIO *fp;
fp = BIO_new_file(path, "r");
if (!fp) {
ctx_log(ctx, 0, "Could not open file %s\n", path);
return 0;
}
if (BIO_gets(fp, field, *field_len) > 0) {
*field_len = strlen(field);
} else {
*field_len = 0;
}
BIO_free(fp);
return 1;
}
static int parse_pin_source(ENGINE_CTX *ctx,
const char *attr, int attrlen, unsigned char *field,
size_t *field_len)
{
unsigned char *val;
int ret = 1;
if (!parse_uri_attr(ctx, attr, attrlen, &val, NULL)) {
return 0;
}
if (!strncasecmp((const char *)val, "file:", 5)) {
ret = read_from_file(ctx, (const char *)(val + 5), (char *)field, field_len);
} else if (*val == '|') {
ret = 0;
ctx_log(ctx, 0, "Unsupported pin-source syntax\n");
/* 'pin-source=/foo/bar' is commonly used */
} else {
ret = read_from_file(ctx, (const char *)val, (char *)field, field_len);
}
OPENSSL_free(val);
return ret;
}
int parse_pkcs11_uri(ENGINE_CTX *ctx,
const char *uri, PKCS11_TOKEN **p_tok,
unsigned char *id, size_t *id_len, char *pin, size_t *pin_len,
char **label)
{
PKCS11_TOKEN *tok;
char *newlabel = NULL;
const char *end, *p;
int rv = 1, id_set = 0, pin_set = 0;
tok = OPENSSL_malloc(sizeof(PKCS11_TOKEN));
if (!tok) {
ctx_log(ctx, 0, "Could not allocate memory for token info\n");
return 0;
}
memset(tok, 0, sizeof(PKCS11_TOKEN));
/* We are only ever invoked if the string starts with 'pkcs11:' */
end = uri + 6;
while (rv && end[0] && end[1]) {
p = end + 1;
end = strpbrk(p, ";?&");
if (!end)
end = p + strlen(p);
if (!strncmp(p, "model=", 6)) {
p += 6;
rv = parse_uri_attr(ctx, p, end - p, (void *)&tok->model, NULL);
} else if (!strncmp(p, "manufacturer=", 13)) {
p += 13;
rv = parse_uri_attr(ctx, p, end - p, (void *)&tok->manufacturer, NULL);
} else if (!strncmp(p, "token=", 6)) {
p += 6;
rv = parse_uri_attr(ctx, p, end - p, (void *)&tok->label, NULL);
} else if (!strncmp(p, "serial=", 7)) {
p += 7;
rv = parse_uri_attr(ctx, p, end - p, (void *)&tok->serialnr, NULL);
} else if (!strncmp(p, "object=", 7)) {
p += 7;
rv = parse_uri_attr(ctx, p, end - p, (void *)&newlabel, NULL);
} else if (!strncmp(p, "id=", 3)) {
p += 3;
rv = parse_uri_attr(ctx, p, end - p, (void *)&id, id_len);
id_set = 1;
} else if (!strncmp(p, "pin-value=", 10)) {
p += 10;
rv = pin_set ? 0 : parse_uri_attr(ctx, p, end - p, (void *)&pin, pin_len);
pin_set = 1;
} else if (!strncmp(p, "pin-source=", 11)) {
p += 11;
rv = pin_set ? 0 : parse_pin_source(ctx, p, end - p, (unsigned char *)pin, pin_len);
pin_set = 1;
} else if (!strncmp(p, "type=", 5) || !strncmp(p, "object-type=", 12)) {
p = strchr(p, '=') + 1;
if ((end - p == 4 && !strncmp(p, "cert", 4)) ||
(end - p == 6 && !strncmp(p, "public", 6)) ||
(end - p == 7 && !strncmp(p, "private", 7))) {
/* Actually, just ignore it */
} else {
ctx_log(ctx, 0, "Unknown object type\n");
rv = 0;
}
} else {
rv = 0;
}
}
if (!id_set)
*id_len = 0;
if (!pin_set)
*pin_len = 0;
if (rv) {
*label = newlabel;
*p_tok = tok;
} else {
OPENSSL_free(tok);
tok = NULL;
OPENSSL_free(newlabel);
}
return rv;
}
/* vim: set noexpandtab: */