forked from google/hiba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions.c
479 lines (413 loc) · 11.4 KB
/
extensions.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
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
/*
* Copyright 2021 The HIBA Authors
*
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "errors.h"
#include "extensions.h"
#include "log.h"
#include "ssherr.h"
#define HIBA_CURRENT_VERSION 0x1
#define HIBA_MIN_SUPPORTED_VERSION 0x1
struct pair {
char *key;
char *val;
struct pair *next;
};
struct hibaext {
u_int32_t type;
u_int32_t version;
u_int32_t min_version;
u_int32_t npairs;
struct pair pairs;
};
int
hibaext_decode(struct hibaext *ext, struct sshbuf *blob) {
int ret;
u_int32_t i;
u_int32_t magic;
struct pair *pair;
struct sshbuf *d = NULL;
if (blob == NULL || ext == NULL)
return HIBA_BAD_PARAMS;
memset(ext, 0, sizeof(struct hibaext));
if ((ret = sshbuf_peek_u32(blob, 0, &magic)) < 0) {
debug3("hibaext_decode: sshbuf_peek_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if (magic != HIBA_MAGIC) {
debug3("hibaext_decode: trying base64 decode");
d = sshbuf_new();
if ((ret = sshbuf_b64tod(d, (const char*)sshbuf_ptr(blob))) < 0) {
debug3("hibaext_decode: sshbuf_b64tod returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
} else {
d = sshbuf_fromb(blob);
}
debug3("hibaext_decode: reading header");
if ((ret = sshbuf_get_u32(d, &magic)) != 0) {
debug3("hibaext_decode: sshbuf_get_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if (magic != HIBA_MAGIC)
return HIBA_INVALID_EXT;
if ((ret = sshbuf_get_u32(d, &ext->type)) != 0) {
debug3("hibaext_decode: sshbuf_get_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_get_u32(d, &ext->version)) != 0) {
debug3("hibaext_decode: sshbuf_get_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_get_u32(d, &ext->min_version)) != 0) {
debug3("hibaext_decode: sshbuf_get_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_get_u32(d, &ext->npairs)) != 0) {
debug3("hibaext_decode: sshbuf_get_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((HIBA_MIN_SUPPORTED_VERSION > ext->version) ||
(ext->min_version > HIBA_CURRENT_VERSION)) {
ret = HIBA_BAD_VERSION;
goto err;
}
debug3("hibaext_decode: reading %d pairs", ext->npairs);
pair = &ext->pairs;
for (i = 0; i < ext->npairs; ++i) {
pair->next = calloc(sizeof(struct pair), 1);
pair = pair->next;
if ((ret = sshbuf_get_cstring(d, &pair->key, NULL)) != 0) {
debug3("hibaext_decode: sshbuf_get_cstring returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_get_cstring(d, &pair->val, NULL)) != 0) {
debug3("hibaext_decode: sshbuf_get_cstring returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
debug3("hibaext_decode: reading pair%d: %s = %s", i, pair->key, pair->val);
}
debug3("hibaext_decode: %zu bytes left in buffer", sshbuf_len(d));
if (sshbuf_len(d) > 0)
ret = HIBA_EXT_TOOBIG;
ret = hibaext_sanity_check(ext);
err:
sshbuf_free(d);
return ret;
}
int
hibaext_encode(const struct hibaext *ext, struct sshbuf *blob) {
int ret;
u_int32_t count = 0;
u_int32_t sz = 0;
const struct pair *pair;
struct sshbuf *d;
if (blob == NULL || ext == NULL)
return HIBA_BAD_PARAMS;
if ((ret = hibaext_sanity_check(ext)) != 0)
return ret;
// Pre-calculate size
pair = &ext->pairs;
while(pair->next != NULL && count < ext->npairs) {
pair = pair->next;
sz += sizeof(u_int32_t) + strlen(pair->key);
sz += sizeof(u_int32_t) + strlen(pair->val);
++count;
}
d = sshbuf_new();
if ((ret = sshbuf_allocate(d, sizeof(u_int32_t) + sizeof(struct hibaext) + sz)) < 0) {
debug3("hibaext_encode: sshbuf_allocate returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
// Construct the sshbuf
debug3("hibaext_encode: encoding header");
if ((ret = sshbuf_put_u32(d, HIBA_MAGIC)) != 0) {
debug3("hibaext_encode: sshbuf_put_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_put_u32(d, ext->type)) != 0) {
debug3("hibaext_encode: sshbuf_put_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_put_u32(d, ext->version)) != 0) {
debug3("hibaext_encode: sshbuf_put_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_put_u32(d, ext->min_version)) != 0) {
debug3("hibaext_encode: sshbuf_put_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_put_u32(d, count)) != 0) {
debug3("hibaext_encode: sshbuf_put_u32 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
debug3("hibaext_encode: encoding %d pairs", ext->npairs);
count = 0;
pair = &ext->pairs;
while(pair->next != NULL && count < ext->npairs) {
pair = pair->next;
if ((ret = sshbuf_put_cstring(d, pair->key)) != 0) {
debug3("hibaext_encode: sshbuf_put_cstring returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
if ((ret = sshbuf_put_cstring(d, pair->val)) != 0) {
debug3("hibaext_encode: sshbuf_put_cstring returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
goto err;
}
++count;
}
debug3("hibaext_encode: base64 encode");
if ((ret = sshbuf_dtob64(d, blob, 0)) < 0) {
debug3("hibaext_decode: sshbuf_dtob64 returned %d: %s", ret, ssh_err(ret));
ret = HIBA_INTERNAL_ERROR;
}
err:
sshbuf_free(d);
return ret;
}
struct hibaext*
hibaext_new() {
struct hibaext *ext = calloc(sizeof(struct hibaext), 1);
return ext;
}
int
hibaext_init(struct hibaext *ext, int type) {
if (ext == NULL)
return -1;
if ((type != HIBA_IDENTITY_EXT) && (type != HIBA_GRANT_EXT))
return HIBA_UNKNOWN_EXT;
memset(ext, 0, sizeof(struct hibaext));
ext->type = type;
ext->version = HIBA_CURRENT_VERSION;
ext->min_version = HIBA_MIN_SUPPORTED_VERSION;
debug2("hibaext_init: initialize type %s: version %d (>= %d)", hibaext_id(ext), HIBA_CURRENT_VERSION, HIBA_MIN_SUPPORTED_VERSION);
return HIBA_OK;
}
void
hibaext_free(struct hibaext *ext) {
struct pair *pair;
if (ext == NULL)
return;
pair = ext->pairs.next;
while (pair != NULL) {
struct pair *tbd = pair;
pair = pair->next;
free(tbd->key);
free(tbd->val);
free(tbd);
}
free(ext);
}
inline u_int32_t
hibaext_type(const struct hibaext *ext) {
if (ext == NULL)
return HIBA_UNKNOWN_EXT;
return ext->type;
}
const char*
hibaext_id(const struct hibaext *ext) {
if (ext == NULL)
return hiba_err(-HIBA_UNKNOWN_EXT);
switch (ext->type) {
case HIBA_IDENTITY_EXT:
return HIBA_IDENTITY_ID;
case HIBA_GRANT_EXT:
return HIBA_GRANT_ID;
default:
return hiba_err(-HIBA_UNKNOWN_EXT);
}
}
int
hibaext_versions(const struct hibaext *ext, u_int32_t *vers, u_int32_t *min_vers) {
if (ext == NULL)
return HIBA_BAD_PARAMS;
if (vers != NULL)
*vers = ext->version;
if (min_vers != NULL)
*min_vers = ext->min_version;
return HIBA_OK;
}
u_int32_t
hibaext_pairs_len(const struct hibaext *ext) {
if (ext == NULL)
return HIBA_BAD_PARAMS;
return ext->npairs;
}
int
hibaext_key_value_at(const struct hibaext *ext, u_int32_t position, char **key, char **value) {
struct pair *pair;
if (ext == NULL)
return HIBA_BAD_PARAMS;
if (position >= ext->npairs)
return HIBA_BAD_PARAMS;
pair = ext->pairs.next;
while(position != 0) {
pair = pair->next;
--position;
}
if (key != NULL)
*key = strdup(pair->key);
if (value != NULL)
*value = strdup(pair->val);
return HIBA_OK;
}
int
hibaext_value_for_key(const struct hibaext *ext, const char *key, char **value) {
struct pair *pair;
if (ext == NULL)
return HIBA_BAD_PARAMS;
if ((ext->type != HIBA_IDENTITY_EXT) && (ext->type != HIBA_GRANT_EXT))
return HIBA_BAD_PARAMS;
pair = ext->pairs.next;
while(pair != NULL && strcmp(pair->key, key) != 0)
pair = pair->next;
if (pair == NULL)
return HIBA_EXT_NOKEY;
if (value != NULL)
*value = strdup(pair->val);
return HIBA_OK;
}
int
hibaext_add_pair(struct hibaext *ext, const char *key, const char *value) {
struct pair *pair;
struct pair *new;
if (ext == NULL || key == NULL || value == NULL)
return HIBA_BAD_PARAMS;
if (ext->type == HIBA_IDENTITY_EXT && (hibaext_value_for_key(ext, key, NULL) == HIBA_OK))
return HIBA_PAIR_EXISTS;
debug3("hibaext_add_pair: add key '%s' = '%s'", key, value);
ext->npairs++;
new = calloc(sizeof(struct pair), 1);
new->key = strdup(key);
new->val = strdup(value);
pair = &ext->pairs;
while (pair->next != NULL)
pair = pair->next;
pair->next = new;
return HIBA_OK;
}
int
hibaext_update_pair(struct hibaext *ext, const char *key, const char *value) {
struct pair *pair;
if (ext == NULL || key == NULL || value == NULL)
return HIBA_BAD_PARAMS;
pair = ext->pairs.next;
while(pair != NULL && strcmp(pair->key, key) != 0)
pair = pair->next;
if (pair == NULL)
return HIBA_EXT_NOKEY;
debug3("hibaext_update_pair: update key '%s': '%s' -> '%s'", key, pair->val, value);
free(pair->val);
pair->val = strdup(value);
return HIBA_OK;
}
int
hibaext_sanity_check(const struct hibaext *ext) {
int ret = 0;
if (ext == NULL)
return HIBA_BAD_PARAMS;
if ((ext->type != HIBA_IDENTITY_EXT) && (ext->type != HIBA_GRANT_EXT))
return HIBA_UNKNOWN_EXT;
if (hibaext_value_for_key(ext, HIBA_KEY_DOMAIN, NULL) < 0)
return HIBA_EXT_NODOMAIN;
if (ext->type == HIBA_GRANT_EXT) {
int i = 0;
char *key;
char *value;
int found_validity = 0;
while (hibaext_key_value_at(ext, i, &key, &value) == HIBA_OK) {
if (strcmp(key, HIBA_KEY_VALIDITY) == 0) {
char *ok;
int v = strtol(value, &ok, 0);
if (found_validity > 0)
ret = HIBA_GRANT_BADVALIDITY;
else if (ok == value || *ok != '\0')
ret = HIBA_GRANT_BADVALIDITY;
else if (v < 0)
ret = HIBA_GRANT_BADVALIDITY;
++found_validity;
} else if (strcmp(key, HIBA_KEY_OPTIONS) == 0) {
size_t i;
int quoted = 0;
int dquoted = 0;
for (i = 0; i < strlen(value); ++i) {
switch (value[i]) {
case '\n':
ret = HIBA_GRANT_BADOPTIONS;
break;
case ' ':
if (!dquoted)
ret = HIBA_GRANT_BADOPTIONS;
break;
case '\'':
if (!dquoted)
quoted = (quoted+1)%2;
break;
case '"':
if (!quoted)
dquoted = (dquoted+1)%2;
break;
}
if (ret != 0)
break;
}
if (quoted || dquoted)
ret = HIBA_GRANT_BADOPTIONS;
}
free(key);
free(value);
if (ret != 0)
return ret;
++i;
}
} else if (ext->type == HIBA_IDENTITY_EXT) {
int i = 0;
char *key;
char *value;
while (hibaext_key_value_at(ext, i, &key, &value) == HIBA_OK) {
char *v = NULL;
debug3("hibaext_sanity_check: checking key '%s'", key);
if (strcmp(key, HIBA_KEY_HOSTNAME) == 0)
ret = HIBA_UNEXPECTED_KEY;
else if (strcmp(key, HIBA_KEY_ROLE) == 0)
ret = HIBA_UNEXPECTED_KEY;
else if (strcmp(key, HIBA_KEY_OPTIONS) == 0)
ret = HIBA_UNEXPECTED_KEY;
else if (strcmp(key, HIBA_KEY_VALIDITY) == 0)
ret = HIBA_UNEXPECTED_KEY;
else if (hibaext_value_for_key(ext, key, &v) == HIBA_OK &&
strcmp(value, v) != 0)
ret = HIBA_UNEXPECTED_KEY;
free(v);
free(value);
free(key);
if (ret != 0)
return ret;
++i;
}
}
return HIBA_OK;
}