197 {
198 struct gengetopt_args_info args_info;
199
200 int rc = EXIT_FAILURE;
202
203 FILE *input_file = NULL;
204 FILE *output_file = NULL;
205 FILE *wrapkey_file = NULL;
206
207#pragma pack(push, 1)
208 union {
209 struct {
212 };
214 } wrap_object = {{{0}, {0}}};
215#pragma pack(pop)
216 size_t wrap_object_len = sizeof(wrap_object.body);
217
218 if (cmdline_parser(argc,
argv, &args_info) != 0) {
219 goto main_exit;
220 }
221
225 fprintf(stderr,
"Unable to parse algorithm: %s\n",
yh_strerror(yhrc));
226 goto main_exit;
227 }
228
232 fprintf(stderr,
"Unable to parse capabilities: %s\n",
yh_strerror(yhrc));
233 goto main_exit;
234 }
235
239 fprintf(stderr,
"Unable to parse domains: %s\n",
yh_strerror(yhrc));
240 goto main_exit;
241 }
242
244
246 size_t label_len = strlen(args_info.label_arg);
248 fprintf(stderr,
249 "Unable to parse label: label too long, maximum length is %d\n",
251 goto main_exit;
252 }
254
257 fprintf(stderr, "Invalid algorithm\n");
258 goto main_exit;
259 }
260
262 bool has_delegated =
264 if (has_delegated == true) {
265 if (!args_info.delegated_given) {
266 fprintf(stderr, "Missing delegated capabilities argument\n");
267 goto main_exit;
268 }
269
272 fprintf(stderr, "Unable to parse delegated capabilities: %s\n",
274 goto main_exit;
275 }
276 }
277
278 input_file = open_file(args_info.in_arg, true);
279 if (input_file == NULL) {
280 perror("Unable to open input file");
281 goto main_exit;
282 }
283
284 switch (type) {
286 char password[256] = {0};
287 size_t password_len =
sizeof(
password);
288
289 if (input_file == stdin) {
290 const char *prompt = "Derivation Password: ";
291 if (EVP_read_pw_string(password, password_len, prompt, 1)) {
292 fprintf(stderr, "Unable to read password prompt\n");
293 goto main_exit;
294 }
295 password_len = strlen(password);
296 } else {
298 false) {
299 fprintf(stderr, "Unable to read input file\n");
300 goto main_exit;
301 }
302 if (password[password_len - 1] == '\n') {
303 password_len--;
304 }
305 if (password[password_len - 1] == '\r') {
306 password_len--;
307 }
308 password[password_len] = '\0';
309 }
310
313 PKCS5_PBKDF2_HMAC((const char *) password, password_len,
317 fprintf(stderr, "Unable to derive keys\n");
318 goto main_exit;
319 }
320
323
328 } break;
329
333
335 &wrap_object_len) == false) {
336 fprintf(stderr, "Unable to read input file\n");
337 goto main_exit;
338 }
340 } break;
341
344 if (
read_file(input_file, wrap_object.body, &wrap_object_len) ==
false) {
345 fprintf(stderr, "Unable to read input file\n");
346 goto main_exit;
347 }
348
350 wrap_object.body, &wrap_object_len, true) != true) {
351 fprintf(stderr, "Unable to read asymmetric private key\n");
352 goto main_exit;
353 }
354
356 fprintf(stderr, "Mismatched algorithm\n");
357 goto main_exit;
358 }
359 } break;
360
362 if (
read_file(input_file, wrap_object.body, &wrap_object_len) ==
false) {
363 fprintf(stderr, "Unable to read input file\n");
364 goto main_exit;
365 }
366
368 wrap_object.body, &wrap_object_len) != true) {
369 fprintf(stderr, "Unable to format hmac key\n");
370 goto main_exit;
371 }
372 } break;
373
374 default:
375 if (
read_file(input_file, wrap_object.body, &wrap_object_len) ==
false) {
376 fprintf(stderr, "Unable to read input file\n");
377 goto main_exit;
378 }
379 }
380
381 wrapkey_file = open_file(args_info.wrapkey_arg, true);
382 if (wrapkey_file == NULL) {
383 perror("Unable to open wrapkey file");
384 goto main_exit;
385 }
386
388 size_t wrapkey_buf_len = sizeof(wrapkey_buf);
389 if (
read_file(wrapkey_file, wrapkey_buf, &wrapkey_buf_len) ==
false) {
390 fprintf(stderr, "Unable to read wrapkey file\n");
391 }
392
394 switch (wrapkey_buf_len) {
395 case 16:
397 break;
398 case 24:
400 break;
401 case 32:
403 break;
404 default:
405 fprintf(stderr, "Unable to parse wrapkey: invalid length\n");
406 goto main_exit;
407 }
408
409 output_file = open_file(args_info.out_arg, false);
410 if (output_file == NULL) {
411 perror("Unable to open output file");
412 goto main_exit;
413 }
414
417
419 size_t wrapped_len = sizeof(wrapped);
420
421 if (wrap_data(wrapkey_buf, wrapkey_buf_len, wrap_object.buf,
423 &wrapped_len) == false) {
424 fprintf(stderr, "Unable to wrap data\n");
425 goto main_exit;
426 }
427
428 if (getenv("DEBUG") != NULL) {
430 }
433 fprintf(stderr, "Unable to write output file\n");
434 goto main_exit;
435 }
436
438
439main_exit:
440
441 cmdline_parser_free(&args_info);
442
443 if (input_file != NULL) {
444 fclose(input_file);
445 input_file = NULL;
446 }
447
448 if (output_file != NULL) {
449 fclose(output_file);
450 output_file = NULL;
451 }
452
453 if (wrapkey_file != NULL) {
454 fclose(wrapkey_file);
455 wrapkey_file = NULL;
456 }
457
459}
const char * yh_strerror(yh_rc err)
void YH_INTERNAL dump_hex(FILE *file, const uint8_t *ptr, uint16_t len)
Capabilities representation.
uint8_t capabilities[YH_CAPABILITIES_LEN]
Capabilities is represented as an 8 byte uint8_t array.
bool split_hmac_key(yh_algorithm algorithm, uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
bool algo2type(yh_algorithm algorithm, yh_object_type *type)
bool read_file(FILE *fp, uint8_t *buf, size_t *buf_len)
bool write_file(const uint8_t *buf, size_t buf_len, FILE *fp, format_t format)
bool read_private_key(uint8_t *buf, size_t len, yh_algorithm *algo, uint8_t *bytes, size_t *bytes_len, bool internal_repr)
#define OBJECT_HEADER_SIZE
yh_rc yh_string_to_domains(const char *domains, uint16_t *result)
yh_rc yh_string_to_capabilities(const char *capability, yh_capabilities *result)
yh_rc yh_string_to_algo(const char *string, yh_algorithm *algo)
#define YH_DEFAULT_ITERS
Number of iterations for PBKDF2 key derivation.
@ YH_HMAC_KEY
HMAC Key is a secret key used when computing and verifying HMAC signatures.
@ YH_ASYMMETRIC_KEY
Asymmetric Key is the private key of an asymmetric key-pair.
@ YH_AUTHENTICATION_KEY
Authentication Key is used to establish Sessions with a device.
#define YH_OBJ_LABEL_LEN
Max length of object labels.
@ YH_ALGO_AES256_CCM_WRAP
aes256-ccm-wrap
@ YH_ALGO_AES192_CCM_WRAP
aes192-ccm-wrap
@ YH_ALGO_AES128_CCM_WRAP
aes128-ccm-wrap
#define YH_DEFAULT_SALT
Salt to be used for PBKDF2 key derivation.
#define YH_KEY_LEN
Length of authentication keys.
#define YH_CAPABILITIES_LEN
Length of capabilities array.
@ YHR_SUCCESS
Returned value when function was successful.
yh_capabilities capabilities
memcpy((char *) pInfo->slotDescription, s, l)