-
Notifications
You must be signed in to change notification settings - Fork 3
/
control.c
375 lines (334 loc) · 13.7 KB
/
control.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
// SPDX-License-Identifier: MIT
#include "common.h"
#include "utils.h"
#include <argp.h>
#define MAIN_DOC_STRING \
" device Opal-compliant device\n" \
" command One of the commands defined further\n"
#define PIN_MAX_LEN 512
enum ArgKey {
ARG_KEY_VERIFY_PIN = 'v',
ARG_KEY_VERIFY_PIN_HEX = 9,
ARG_KEY_ASSIGN_PIN = 1,
ARG_KEY_ASSIGN_PIN_HEX = 8,
ARG_KEY_USER = 'u',
ARG_KEY_ADMIN = 'a',
ARG_KEY_LOCKING_RANGE = 'l',
ARG_KEY_LOCKING_RANGE_START = 7,
ARG_KEY_LOCKING_RANGE_LENGTH = 6,
ARG_KEY_READ_LOCK_ENABLED = 2,
ARG_KEY_WRITE_LOCK_ENABLED = 3,
ARG_KEY_READ_LOCKED = 4,
ARG_KEY_WRITE_LOCKED = 5,
ARG_KEY_VERBOSE = 'V',
ARG_KEY_SCSI = 'S'
};
static struct argp_option options_setup_range[] = {
{ "verify-pin", ARG_KEY_VERIFY_PIN, "pin", 0, "Password of Admin1 authority", 0 },
{ "verify-pin-hex", ARG_KEY_VERIFY_PIN_HEX, "hex_pin", 0, "Password of Admin1 authority", 0 },
{ "user", ARG_KEY_USER, "id", 0, "User to have control over the locking range (can be repeated)", 0 },
{ "locking-range", ARG_KEY_LOCKING_RANGE, "id", 0, "Locking range to change", 0 },
{ "locking-range-start", ARG_KEY_LOCKING_RANGE_START, "position", 0, NULL, 0 },
{ "locking-range-length", ARG_KEY_LOCKING_RANGE_LENGTH, "length", 0, NULL, 0 },
{ 0 }
};
static struct argp_option options_list_range[] = {
{ "verify-pin", ARG_KEY_VERIFY_PIN, "pin", 0, "Password of user authority", 0 },
{ "verify-pin-hex", ARG_KEY_VERIFY_PIN_HEX, "hex_pin", 0, "Password of user authority", 0 },
{ "user", ARG_KEY_USER, "id", 0, "User authority id", 0 },
{ "locking-range", ARG_KEY_LOCKING_RANGE, "id", 0, "Locking range to list", 0 },
{ 0 }
};
static struct argp_option options_setup_user[] = {
{ "verify-pin", ARG_KEY_VERIFY_PIN, "pin", 0, "Password of Admin1 authority", 0 },
{ "verify-pin-hex", ARG_KEY_VERIFY_PIN_HEX, "hex_pin", 0, "Password of Admin1 authority", 0 },
{ "assign-pin", ARG_KEY_ASSIGN_PIN, "pin", 0, "Password to assign to selected user authority", 0 },
{ "assign-pin-hex", ARG_KEY_ASSIGN_PIN_HEX, "hex_pin", 0, "Password to assign to selected user authority", 0 },
{ "user", ARG_KEY_USER, "id", 0, "ID of the user authority", 0 },
{ 0 }
};
static struct argp_option options_setup_tper[] = {
{ "assign-pin", ARG_KEY_ASSIGN_PIN, "pin", 0, "Password to assign to the owner authority", 0 },
{ "assign-pin-hex", ARG_KEY_ASSIGN_PIN_HEX, "hex_pin", 0, "Password to assign to the owner authority", 0 },
{ 0 }
};
static struct argp_option options_psid_revert[] = {
{ "verify-pin", ARG_KEY_VERIFY_PIN, "pin", 0, "PSID", 0 },
{ "verify-pin-hex", ARG_KEY_VERIFY_PIN_HEX, "hex_pin", 0, "PSID", 0 },
{ 0 }
};
static struct argp_option options_regenerate_key[] = {
{ "verify-pin", ARG_KEY_VERIFY_PIN, "pin", 0, "Password of Admin1 authority", 0 },
{ "verify-pin-hex", ARG_KEY_VERIFY_PIN_HEX, "hex_pin", 0, "Password of Admin1 authority", 0 },
{ "locking-range", ARG_KEY_LOCKING_RANGE, "id", 0, "Locking range to re-generate", 0 },
{ 0 }
};
static struct argp_option options_unlock[] = {
{ "verify-pin", ARG_KEY_VERIFY_PIN, "pin", 0, "Password of the authority", 0 },
{ "verify-pin-hex", ARG_KEY_VERIFY_PIN_HEX, "hex_pin", 0, "Password of the authority", 0 },
{ "user", ARG_KEY_USER, "id", 0, "User authority to authenticate as", 0 },
{ "admin", ARG_KEY_ADMIN, "id", 0, "Admin authority to authenticate as", 0 },
{ "locking-range", ARG_KEY_LOCKING_RANGE, "id", 0, "Locking range to lock/unlock", 0 },
{ "read-locked", ARG_KEY_READ_LOCKED, "state", 0, NULL, 0 },
{ "write-locked", ARG_KEY_WRITE_LOCKED, "state", 0, NULL, 0 },
{ 0 }
};
static struct argp_option options_reset[] = {
{ "No options", 0, "NULL", OPTION_DOC},
{ 0 }
};
static struct argp_option options_stack_reset[] = {
{ "No options", 0, "NULL", OPTION_DOC},
{ 0 }
};
static struct argp_option options_main[] = {
{ "verbose", ARG_KEY_VERBOSE, NULL, 0, NULL, 0 },
{ "scsi", ARG_KEY_SCSI, NULL, 0, "Use SCSI security protocol command", 0 },
{ 0 }
};
struct Arguments {
enum {
NONE,
CMD_UNLOCK,
CMD_SETUP_TPER,
CMD_SETUP_RANGE,
CMD_SETUP_USER,
CMD_PSID_REVERT,
CMD_RESET,
CMD_REGENERATE_KEY,
CMD_LIST_RANGE,
CMD_STACK_RESET,
} command;
char *device;
bool use_scsi_sec;
uint16_t locking_range;
size_t user[32];
size_t user_count;
unsigned char verify_pin[PIN_MAX_LEN];
size_t verify_pin_len;
unsigned char assign_pin[PIN_MAX_LEN];
size_t assign_pin_len;
uint64_t locking_range_start;
uint64_t locking_range_length;
int8_t read_lock_enabled;
int8_t write_lock_enabled;
int8_t read_locked;
int8_t write_locked;
size_t parsed;
} args = {
.read_lock_enabled = VAL_UNDEFINED,
.write_lock_enabled = VAL_UNDEFINED,
.read_locked = VAL_UNDEFINED,
.write_locked = VAL_UNDEFINED,
.locking_range_start = VAL_UNDEFINED,
.locking_range_length = VAL_UNDEFINED,
};
static error_t parse_opt_pin(char *source, unsigned char *target, size_t *target_len)
{
size_t pin_len = strlen(source);
if (pin_len > PIN_MAX_LEN) {
return 1;
}
*target_len = pin_len;
strncpy((char *)target, source, PIN_MAX_LEN);
return 0;
}
static error_t parse_opt_hex(const char *source, unsigned char *target, size_t *target_len)
{
size_t pin_len = strlen(source);
if (pin_len > 2 * PIN_MAX_LEN || pin_len % 2 != 0) {
return 1;
}
pin_len /= 2;
unsigned char c;
for (size_t i = 0; i < pin_len; i++) {
if (sscanf(source, "%2hhx", &c) != 1) {
return 1;
}
target[i] = c;
source += 2;
}
*target_len = pin_len;
return 0;
}
static error_t parse_opt_bool(const char *source, int8_t *target)
{
if (source[0] == '0' && source[1] == 0) {
*target = 0;
} else if (source[0] == '1' && source[1] == 0) {
*target = 1;
} else {
return 1;
}
return 0;
}
static error_t parse_opt_main(int key, char *arg, struct argp_state *state)
{
struct Arguments *arguments = state->input;
switch (key) {
case ARGP_KEY_INIT:
// NOTE: 'state->root_argp->children[i].argp' does not seem to be correct.
for (int i = 0; i < 8; i++) {
state->child_inputs[i] = arguments;
}
break;
case ARG_KEY_VERBOSE:
current_log_level = current_log_level == EVERYTHING ? EVERYTHING : current_log_level + 1;
break;
case ARG_KEY_SCSI:
arguments->use_scsi_sec = true;
break;
case ARGP_KEY_ARG:
switch (arguments->parsed) {
case 0:
if (strcmp(arg, "unlock") == 0) {
arguments->command = CMD_UNLOCK;
} else if (strcmp(arg, "setup_range") == 0) {
arguments->command = CMD_SETUP_RANGE;
} else if (strcmp(arg, "setup_user") == 0) {
arguments->command = CMD_SETUP_USER;
} else if (strcmp(arg, "setup_tper") == 0) {
arguments->command = CMD_SETUP_TPER;
} else if (strcmp(arg, "psid_revert") == 0) {
arguments->command = CMD_PSID_REVERT;
} else if (strcmp(arg, "reset") == 0) {
arguments->command = CMD_RESET;
} else if (strcmp(arg, "stack_reset") == 0) {
arguments->command = CMD_STACK_RESET;
} else if (strcmp(arg, "regenerate_key") == 0) {
arguments->command = CMD_REGENERATE_KEY;
} else if (strcmp(arg, "list_range") == 0) {
arguments->command = CMD_LIST_RANGE;
} else {
printf("Unexpected command.\n");
return ARGP_ERR_UNKNOWN;
}
break;
case 1:
arguments->device = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
arguments->parsed += 1;
break;
}
return 0;
}
static error_t parse_opt_child(int key, char *arg, struct argp_state *state)
{
struct Arguments *args = state->input;
switch (key) {
case ARG_KEY_USER:
args->user[args->user_count] = USER_BASE_ID + strtol(arg, NULL, 10);
args->user_count += 1;
break;
case ARG_KEY_ADMIN:
args->user[args->user_count] = ADMIN_BASE_ID + strtol(arg, NULL, 10);
args->user_count += 1;
break;
case ARG_KEY_LOCKING_RANGE:
args->locking_range = strtol(arg, NULL, 10);
break;
case ARG_KEY_LOCKING_RANGE_START:
args->locking_range_start = strtoull(arg, NULL, 10);
break;
case ARG_KEY_LOCKING_RANGE_LENGTH:
args->locking_range_length = strtoull(arg, NULL, 10);
break;
case ARG_KEY_VERIFY_PIN:
return parse_opt_pin(arg, args->verify_pin, &args->verify_pin_len);
case ARG_KEY_VERIFY_PIN_HEX:
return parse_opt_hex(arg, args->verify_pin, &args->verify_pin_len);
case ARG_KEY_ASSIGN_PIN:
return parse_opt_pin(arg, args->assign_pin, &args->assign_pin_len);
case ARG_KEY_ASSIGN_PIN_HEX:
return parse_opt_hex(arg, args->assign_pin, &args->assign_pin_len);
case ARG_KEY_READ_LOCK_ENABLED:
return parse_opt_bool(arg, &args->read_lock_enabled);
case ARG_KEY_WRITE_LOCK_ENABLED:
return parse_opt_bool(arg, &args->write_lock_enabled);
case ARG_KEY_READ_LOCKED:
return parse_opt_bool(arg, &args->read_locked);
case ARG_KEY_WRITE_LOCKED:
return parse_opt_bool(arg, &args->write_locked);
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
int main(int argc, char **argv)
{
struct argp argp_unlock = { options_unlock, parse_opt_child, NULL, "unlock_doc", 0, 0, 0 };
struct argp argp_setup_range = { options_setup_range, parse_opt_child, NULL, "setup_range_doc", 0, 0, 0 };
struct argp argp_list_range = { options_list_range, parse_opt_child, NULL, "list_range_doc", 0, 0, 0 };
struct argp argp_setup_user = { options_setup_user, parse_opt_child, NULL, "setup_user_doc", 0, 0, 0 };
struct argp argp_setup_tper = { options_setup_tper, parse_opt_child, NULL, "setup_tper_doc", 0, 0, 0 };
struct argp argp_psid_revert = { options_psid_revert, parse_opt_child, NULL, "psid_revert_doc", 0, 0, 0 };
struct argp argp_regenerate_key = { options_regenerate_key, parse_opt_child, NULL, "regenerate_key_doc", 0, 0, 0 };
struct argp argp_reset = { options_reset, parse_opt_child, NULL, "reset_doc", 0, 0, 0 };
struct argp argp_stack_reset = { options_stack_reset, parse_opt_child, NULL, "stack_reset_doc", 0, 0, 0 };
struct argp_child argp_children[] = {
{ &argp_unlock, 0, "unlock - Lock or unlock a locking range", 0 },
{ &argp_setup_range, 0, "setup_range - Configure a locking range", 0 },
{ &argp_list_range, 0, "list_range - List the locking range", 0 },
{ &argp_setup_user, 0, "setup_user - Enable a user", 0 },
{ &argp_setup_tper, 0, "setup_tper - Take ownership over the device", 0 },
{ &argp_psid_revert, 0, "psid_revert - Revert the device to factory state", 0 },
{ &argp_regenerate_key, 0, "regenerate_key - Re-generate of a locking range", 0 },
{ &argp_reset, 0, "reset - Send a programmatic reset", 0 },
{ &argp_stack_reset, 0, "stack_reset - Send a stack reset", 0 },
{ .argp = NULL }
};
struct argp argp_main = { options_main, parse_opt_main, "command device", MAIN_DOC_STRING, NULL, 0, 0 };
argp_main.children = argp_children;
error_t err = argp_parse(&argp_main, argc, argv, 0, 0, &args);
if (err != 0) {
printf("Could not parse the arguments.\n");
return 1;
}
if (args.parsed < 2) {
printf("Command and disk need to be specified.\n\n");
argp_help(&argp_main, stdout, ARGP_HELP_SHORT_USAGE|ARGP_HELP_LONG, argv[0]);
return 1;
}
struct disk_device dev = { 0 };
if ((err = disk_device_open(&dev, args.device, args.use_scsi_sec))) {
return err;
}
if (args.command == CMD_UNLOCK) {
err = unlock_range(&dev, args.locking_range, args.user[0],
args.read_locked, args.write_locked,
args.verify_pin, args.verify_pin_len);
} else if (args.command == CMD_SETUP_RANGE) {
err = setup_range(&dev, args.locking_range,
args.verify_pin, args.verify_pin_len,
args.locking_range_start, args.locking_range_length,
args.user, args.user_count);
} else if (args.command == CMD_SETUP_USER) {
err = setup_user(&dev, args.user[0],
args.verify_pin, args.verify_pin_len,
args.assign_pin, args.assign_pin_len);
} else if (args.command == CMD_SETUP_TPER) {
err = setup_tper(&dev, args.assign_pin, args.assign_pin_len);
if (!err)
err = setup_programmatic_reset(&dev, args.assign_pin, args.assign_pin_len, -1);
} else if (args.command == CMD_PSID_REVERT) {
err = psid_revert(&dev, args.verify_pin, args.verify_pin_len);
} else if (args.command == CMD_RESET) {
err = tper_reset(&dev);
} else if (args.command == CMD_STACK_RESET) {
err = stack_reset(&dev);
} else if (args.command == CMD_REGENERATE_KEY) {
err = regenerate_key(&dev, args.locking_range,
args.verify_pin, args.verify_pin_len);
} else if (args.command == CMD_LIST_RANGE) {
err = list_range(&dev, args.locking_range,
args.verify_pin, args.verify_pin_len, args.user[0]);
} else {
printf("Invalid command.\n");
err = 1;
}
disk_device_close(&dev);
return err;
}