-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathparams.c
More file actions
642 lines (537 loc) · 13.7 KB
/
params.c
File metadata and controls
642 lines (537 loc) · 13.7 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* $NetBSD: params.c,v 1.4 2002/12/04 05:02:29 elric Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Roland C. Dowdeswell.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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 <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD");
#endif
#include <sys/types.h>
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
/* include the resolver gunk in order that we can use b64 routines */
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include "params.h"
#include "utils.h"
static int params_setstring(char **, const char *);
static int params_setbinary(u_int8_t **, int *, const char *, int);
static int params_setb64(u_int8_t **, int *, const char *);
static void eatwhite(char *);
static int take_action(struct params *, FILE *, const char *, char *);
static void print_kvpair_str(FILE *, const char *, const char *);
static void print_kvpair_int(FILE *, const char *, int);
static void print_kvpair_b64(FILE *, const char *, const char *, int);
static void free_notnull(void *);
/* crypt defaults functions */
#define CRYPT_DEFAULTKEYSIZE 0x01
static int crypt_int_lookup(char *, int);
static int crypt_int_lookup_aes_cbc(int);
static int crypt_int_lookup_3des_cbc(int);
static int crypt_int_lookup_blowfish_cbc(int);
void
params_init(struct params *p)
{
p->alg = NULL;
p->ivmeth = NULL;
p->key = NULL;
p->keylen = -1;
p->bsize = -1;
p->keygen_method = KEYGEN_UNKNOWN;
p->keygen_salt = NULL;
p->keygen_saltlen = -1;
p->keygen_iterations = -1;
p->verify_method = VERIFY_UNKNOWN;
p->key_hash = NULL;
p->key_hashlen = -1;
p->xor_key = NULL;
p->xor_keylen = -1;
}
void
params_free(struct params *p)
{
free_notnull(p->alg);
free_notnull(p->ivmeth);
free_notnull(p->keygen_salt);
free_notnull(p->key_hash);
free_notnull(p->xor_key);
}
int
params_filldefaults(struct params *p)
{
if (p->keygen_method == KEYGEN_UNKNOWN)
p->keygen_method = KEYGEN_PKCS5_PBKDF2;
if (p->verify_method == VERIFY_UNKNOWN)
p->verify_method = VERIFY_NONE;
if (!p->ivmeth)
params_setivmeth(p, "encblkno");
if (p->keylen == -1)
p->keylen = crypt_int_lookup(p->alg, CRYPT_DEFAULTKEYSIZE);
if (p->keylen == -1) {
fprintf(stderr, "Could not determine key length\n");
return -1;
}
if (p->keygen_iterations < 1)
p->keygen_iterations = 128;
return 0;
}
int
params_changed(const struct params *c)
{
if (c->alg || c->ivmeth || c->key || c->keylen != -1 ||
c->bsize != -1 || c->keygen_method || c->keygen_salt ||
c->keygen_saltlen != -1 || c->keygen_iterations != -1 ||
c->key_hash)
return 1;
return 0;
}
static int
params_setstring(char **s, const char *in)
{
free_notnull(*s);
*s = strdup(in);
if (!in)
return -1;
return 0;
}
/*
* params_setbinary allocates a buffer of at least len bits and
* fills it in. It returns the number of bits in *l and the buffer
* in *s.
*/
static int
params_setbinary(u_int8_t **s, int *l, const char *in, int len)
{
*l = len;
len = BITS2BYTES(len);
*s = malloc(len);
if (!*s)
return -1;
memcpy(*s, in, len);
return 0;
}
/*
* params_setb64 reads an encoded base64 stream. We interpret
* the first 32 bits as an unsigned integer in network byte order
* specifying the number of bits in the stream.
*/
static int
params_setb64(u_int8_t **s, int *l, const char *in)
{
int len;
int nbits;
char *tmp;
len = strlen(in);
tmp = malloc(len);
if (!tmp)
return -1;
len = __b64_pton(in, tmp, len);
if (len == -1) {
fprintf(stderr, "params_setb64: mangled base64 stream\n");
return -1;
}
nbits = ntohl(*((u_int32_t *)tmp));
if (nbits > (len - 4) * 8) {
fprintf(stderr, "params_setb64: encoded bits claim to be "
"longer than they are (nbits=%u, stream len=%u bytes)\n",
(unsigned)nbits, (unsigned)len);
return -1;
}
*s = malloc(BITS2BYTES(nbits));
if (!*s) {
free(tmp);
return -1;
}
memcpy(*s, tmp + 4, BITS2BYTES(nbits));
*l = nbits;
return *l;
}
int
params_setalgorithm(struct params *p, const char *in)
{
return params_setstring(&p->alg, in);
}
int
params_setivmeth(struct params *p, const char *in)
{
return params_setstring(&p->ivmeth, in);
}
int
params_setkeylen(struct params *p, int keylen)
{
if (!keylen) {
fprintf(stderr, "zero keylen not permitted\n");
return -1;
}
p->keylen = keylen;
return 0;
}
int
params_setbsize(struct params *p, int bsize)
{
if (!bsize) {
fprintf(stderr, "zero blocksize not permitted\n");
return -1;
}
p->bsize = bsize;
return 0;
}
int
params_setkeygen_method(struct params *p, int in)
{
switch (in) {
case KEYGEN_RANDOMKEY:
case KEYGEN_PKCS5_PBKDF2:
break;
default:
fprintf(stderr, "params_setkeygen_method: unsupported "
"keygen_method (%d)\n", in);
return -1;
}
p->keygen_method = in;
return 0;
}
int
params_setkeygen_method_str(struct params *p, const char *in)
{
if (!strcmp("pkcs5_pbkdf2", in))
return params_setkeygen_method(p, KEYGEN_PKCS5_PBKDF2);
if (!strcmp("randomkey", in))
return params_setkeygen_method(p, KEYGEN_RANDOMKEY);
fprintf(stderr, "unrecognized key generation method \"%s\"\n", in);
return -1;
}
int
params_setkeygen_salt(struct params *p, const char *in, int len)
{
return params_setbinary(&p->keygen_salt, &p->keygen_saltlen, in, len);
}
int
params_setkeygen_salt_b64(struct params *p, const char *in)
{
return params_setb64(&p->keygen_salt, &p->keygen_saltlen, in);
}
int
params_setkeygen_iterations(struct params *p, int in)
{
if (in < 1) {
fprintf(stderr, "keygen_iterations < 1 not permitted\n");
return -1;
}
p->keygen_iterations = in;
return 0;
}
int
params_setverify_method(struct params *p, int in)
{
switch (in) {
case VERIFY_NONE:
case VERIFY_DISKLABEL:
break;
default:
fprintf(stderr, "params_setverify_method: unsupported "
"verify_method (%d)\n", in);
return -1;
}
p->verify_method = in;
return 0;
}
int
params_setverify_method_str(struct params *p, const char *in)
{
if (!strcmp("none", in))
return params_setverify_method(p, VERIFY_NONE);
if (!strcmp("disklabel", in))
return params_setverify_method(p, VERIFY_DISKLABEL);
fprintf(stderr, "params_setverify_method: unrecognized verify method "
"\"%s\"\n", in);
return -1;
}
int
params_setxor_key(struct params *p, const char *in, int len)
{
return params_setbinary(&p->xor_key, &p->xor_keylen, in, len);
}
int
params_setxor_key_b64(struct params *p, const char *in)
{
return params_setb64(&p->xor_key, &p->xor_keylen, in);
}
int
params_setkey_hash(struct params *p, const char *in, int len)
{
return params_setbinary(&p->key_hash, &p->key_hashlen, in, len);
}
int
params_setkey_hash_b64(struct params *p, const char *in)
{
return params_setb64(&p->key_hash, &p->key_hashlen, in);
}
/* eatwhite simply removes all the whitespace from a string, in line */
static void
eatwhite(char *s)
{
int i, j;
for (i=0,j=0; s[i]; i++)
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n')
s[j++] = s[i];
s[j++] = '\0';
}
static int
take_action(struct params *c, FILE *f, const char *key, char *val)
{
int ret;
eatwhite(val);
if (!strcmp(key, "algorithm")) {
return params_setalgorithm(c, val);
} else if (!strcmp(key, "iv-method")) {
return params_setivmeth(c, val);
} else if (!strcmp(key, "keylength")) {
return params_setkeylen(c, atoi(val));
} else if (!strcmp(key, "blocksize")) {
return params_setbsize(c, atoi(val));
} else if (!strcmp(key, "keygen_method")) {
return params_setkeygen_method_str(c, val);
} else if (!strcmp(key, "keygen_salt")) {
ret = params_setkeygen_salt_b64(c, val);
if (ret < 0) {
fprintf(stderr, "keygen_salt improperly encoded\n");
return -1;
}
} else if (!strcmp(key, "keygen_iterations")) {
return params_setkeygen_iterations(c, atoi(val));
} else if (!strcmp(key, "xor_key")) {
ret = params_setxor_key_b64(c, val);
if (ret < 0) {
fprintf(stderr, "xor_key improperly encoded\n");
return -1;
}
} else if (!strcmp(key, "verify_method")) {
return params_setverify_method_str(c, val);
} else if (!strcmp(key, "key_hash")) {
ret = params_setkey_hash_b64(c, val);
if (ret < 0) {
fprintf(stderr, "key_hash improperly encoded\n");
return -1;
}
} else {
fprintf(stderr, "unrecognised keyword (%s, %s)\n", key, val);
return -1;
}
return 0;
}
int
params_fget(struct params *p, FILE *f)
{
size_t len;
size_t lineno;
int ret;
char *line;
char *val;
lineno = 0;
for (;;) {
line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL);
if (!line)
break;
if (!*line)
continue;
/*
* our parameters file has two tokens per line,
* so we cut it up that way and ignore other cases.
*/
val = strpbrk(line, " \t");
if (!val) {
fprintf(stderr, "syntax error on line %lu\n",
(u_long)lineno);
return -1;
}
*val++ = '\0';
ret = take_action(p, f, line, val);
if (ret) {
fprintf(stderr, "parse failure on line %lu\n",
(u_long)lineno);
return -1;
}
}
return 0;
}
int
params_cget(struct params *p, const char *fn)
{
FILE *f;
f = fopen(fn, "r");
if (!f) {
fprintf(stderr, "failed to open params file \"%s\": %s\n",
fn, strerror(errno));
return -1;
}
return params_fget(p, f);
}
static void
print_kvpair_str(FILE *f, const char *key, const char *val)
{
if (key && val)
fprintf(f, "%-25.25s%s\n", key, val);
}
static void
print_kvpair_int(FILE *f, const char *key, int val)
{
if (key && val != -1)
fprintf(f, "%-25.25s%d\n", key, val);
}
/*
* prints out a base64 encoded k-v pair to f. It encodes the length
* of the bitstream as a 32bit unsigned integer in network byte order
* up front.
*/
static void
print_kvpair_b64(FILE *f, const char *key, const char *val, int vallen)
{
int col;
int i;
int len;
char *out;
char *tmp;
if (!key || !val || vallen == -1)
return;
/* compute the total size of the input stream */
len = BITS2BYTES(vallen) + 4;
tmp = malloc(len);
out = malloc(len * 2);
/* XXXrcd: errors ? */
if (!tmp || !out)
abort(); /* lame error handling here... */
/* stuff the length up front */
*((u_int32_t *)tmp) = htonl(vallen);
memcpy(tmp + 4, val, len - 4);
len = __b64_ntop(tmp, len, out, len * 2);
free(tmp);
fprintf(f, "%-25.25s", key);
col = 0;
for (i=0; i < len; i++) {
fputc(out[i], f);
if (col++ > 40) {
fprintf(f, " \\\n%-25.25s", "");
col = 0;
}
}
fprintf(f, "\n");
free(out);
}
int
params_fput(struct params *p, FILE *f)
{
print_kvpair_str(f, "algorithm", p->alg);
print_kvpair_str(f, "iv-method", p->ivmeth);
print_kvpair_int(f, "keylength", p->keylen);
print_kvpair_int(f, "blocksize", p->bsize);
switch (p->verify_method) {
case VERIFY_NONE:
print_kvpair_str(f, "verify_method", "none");
break;
case VERIFY_DISKLABEL:
print_kvpair_str(f, "verify_method", "disklabel");
break;
default:
fprintf(stderr, "unsupported verify_method (%d)\n",
p->verify_method);
return -1;
}
switch (p->keygen_method) {
case KEYGEN_RANDOMKEY:
print_kvpair_str(f, "keygen_method", "randomkey");
break;
case KEYGEN_PKCS5_PBKDF2:
print_kvpair_str(f, "keygen_method", "pkcs5_pbkdf2");
print_kvpair_b64(f, "keygen_salt", p->keygen_salt,
p->keygen_saltlen);
print_kvpair_int(f, "keygen_iterations", p->keygen_iterations);
print_kvpair_b64(f, "xor_key", p->xor_key, p->xor_keylen);
print_kvpair_b64(f, "key_hash", p->key_hash, p->key_hashlen);
break;
default:
fprintf(stderr, "unsupported keygen_method (%d)\n",
p->keygen_method);
return -1;
}
return 0;
}
static int
crypt_int_lookup(char *alg, int type)
{
if (!strcmp(alg, "aes-cbc"))
return crypt_int_lookup_aes_cbc(type);
if (!strcmp(alg, "3des-cbc"))
return crypt_int_lookup_3des_cbc(type);
if (!strcmp(alg, "blowfish-cbc"))
return crypt_int_lookup_blowfish_cbc(type);
return -1;
}
static int
crypt_int_lookup_aes_cbc(int type)
{
switch (type) {
case CRYPT_DEFAULTKEYSIZE:
return 256;
}
return -1;
}
static int
crypt_int_lookup_3des_cbc(int type)
{
switch (type) {
case CRYPT_DEFAULTKEYSIZE:
return 192;
}
return -1;
}
static int
crypt_int_lookup_blowfish_cbc(int type)
{
switch (type) {
case CRYPT_DEFAULTKEYSIZE:
return 128;
}
return -1;
}
static void
free_notnull(void *mem)
{
if (!mem)
free(mem);
}