host_keyblock.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Host functions for verified boot.
  6. */
  7. #include <stdio.h>
  8. #include "2sysincludes.h"
  9. #include "2api.h"
  10. #include "2common.h"
  11. #include "2rsa.h"
  12. #include "2sha.h"
  13. #include "host_common.h"
  14. #include "host_key.h"
  15. #include "host_key2.h"
  16. #include "host_keyblock.h"
  17. #include "vb2_common.h"
  18. #include "vb2_struct.h"
  19. #include "vboot_common.h"
  20. struct vb2_keyblock *vb2_create_keyblock(
  21. const struct vb2_packed_key *data_key,
  22. const struct vb2_private_key *signing_key,
  23. uint32_t flags)
  24. {
  25. /* Allocate key block */
  26. uint32_t signed_size = sizeof(struct vb2_keyblock) + data_key->key_size;
  27. uint32_t sig_data_size =
  28. (signing_key ? vb2_rsa_sig_size(signing_key->sig_alg) : 0);
  29. uint32_t block_size =
  30. signed_size + VB2_SHA512_DIGEST_SIZE + sig_data_size;
  31. struct vb2_keyblock *h = (struct vb2_keyblock *)calloc(block_size, 1);
  32. if (!h)
  33. return NULL;
  34. uint8_t *data_key_dest = (uint8_t *)(h + 1);
  35. uint8_t *block_chk_dest = data_key_dest + data_key->key_size;
  36. uint8_t *block_sig_dest = block_chk_dest + VB2_SHA512_DIGEST_SIZE;
  37. memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE);
  38. h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR;
  39. h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR;
  40. h->keyblock_size = block_size;
  41. h->keyblock_flags = flags;
  42. /* Copy data key */
  43. vb2_init_packed_key(&h->data_key, data_key_dest, data_key->key_size);
  44. vb2_copy_packed_key(&h->data_key, data_key);
  45. /* Set up signature structs so we can calculate the signatures */
  46. vb2_init_signature(&h->keyblock_hash, block_chk_dest,
  47. VB2_SHA512_DIGEST_SIZE, signed_size);
  48. if (signing_key) {
  49. vb2_init_signature(&h->keyblock_signature, block_sig_dest,
  50. sig_data_size, signed_size);
  51. } else {
  52. memset(&h->keyblock_signature, 0,
  53. sizeof(h->keyblock_signature));
  54. }
  55. /* Calculate hash */
  56. struct vb2_signature *chk =
  57. vb2_sha512_signature((uint8_t*)h, signed_size);
  58. vb2_copy_signature(&h->keyblock_hash, chk);
  59. free(chk);
  60. /* Calculate signature */
  61. if (signing_key) {
  62. struct vb2_signature *sigtmp =
  63. vb2_calculate_signature((uint8_t*)h,
  64. signed_size,
  65. signing_key);
  66. vb2_copy_signature(&h->keyblock_signature, sigtmp);
  67. free(sigtmp);
  68. }
  69. /* Return the header */
  70. return h;
  71. }
  72. /* TODO(gauravsh): This could easily be integrated into the function above
  73. * since the code is almost a mirror - I have kept it as such to avoid changing
  74. * the existing interface. */
  75. struct vb2_keyblock *vb2_create_keyblock_external(
  76. const struct vb2_packed_key *data_key,
  77. const char *signing_key_pem_file,
  78. uint32_t algorithm,
  79. uint32_t flags,
  80. const char *external_signer)
  81. {
  82. if (!signing_key_pem_file || !data_key || !external_signer)
  83. return NULL;
  84. uint32_t signed_size = sizeof(struct vb2_keyblock) + data_key->key_size;
  85. uint32_t sig_data_size = vb2_rsa_sig_size(algorithm);
  86. uint32_t block_size =
  87. signed_size + VB2_SHA512_DIGEST_SIZE + sig_data_size;
  88. /* Allocate key block */
  89. struct vb2_keyblock *h = (struct vb2_keyblock *)calloc(block_size, 1);
  90. if (!h)
  91. return NULL;
  92. uint8_t *data_key_dest = (uint8_t *)(h + 1);
  93. uint8_t *block_chk_dest = data_key_dest + data_key->key_size;
  94. uint8_t *block_sig_dest = block_chk_dest + VB2_SHA512_DIGEST_SIZE;
  95. memcpy(h->magic, KEY_BLOCK_MAGIC, KEY_BLOCK_MAGIC_SIZE);
  96. h->header_version_major = KEY_BLOCK_HEADER_VERSION_MAJOR;
  97. h->header_version_minor = KEY_BLOCK_HEADER_VERSION_MINOR;
  98. h->keyblock_size = block_size;
  99. h->keyblock_flags = flags;
  100. /* Copy data key */
  101. vb2_init_packed_key(&h->data_key, data_key_dest, data_key->key_size);
  102. vb2_copy_packed_key(&h->data_key, data_key);
  103. /* Set up signature structs so we can calculate the signatures */
  104. vb2_init_signature(&h->keyblock_hash, block_chk_dest,
  105. VB2_SHA512_DIGEST_SIZE, signed_size);
  106. vb2_init_signature(&h->keyblock_signature, block_sig_dest,
  107. sig_data_size, signed_size);
  108. /* Calculate checksum */
  109. struct vb2_signature *chk =
  110. vb2_sha512_signature((uint8_t*)h, signed_size);
  111. vb2_copy_signature(&h->keyblock_hash, chk);
  112. free(chk);
  113. /* Calculate signature */
  114. struct vb2_signature *sigtmp =
  115. vb2_external_signature((uint8_t*)h, signed_size,
  116. signing_key_pem_file, algorithm,
  117. external_signer);
  118. free(sigtmp);
  119. /* Return the header */
  120. return h;
  121. }
  122. struct vb2_keyblock *vb2_read_keyblock(const char *filename)
  123. {
  124. uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE];
  125. struct vb2_workbuf wb;
  126. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  127. struct vb2_keyblock *block;
  128. uint32_t file_size;
  129. if (VB2_SUCCESS !=
  130. vb2_read_file(filename, (uint8_t **)&block, &file_size)) {
  131. fprintf(stderr, "Error reading key block file: %s\n", filename);
  132. return NULL;
  133. }
  134. /* Verify the hash of the key block, since we can do that without
  135. * the public signing key. */
  136. if (VB2_SUCCESS != vb2_verify_keyblock_hash(block, file_size, &wb)) {
  137. fprintf(stderr, "Invalid key block file: %s\n", filename);
  138. free(block);
  139. return NULL;
  140. }
  141. return block;
  142. }
  143. int vb2_write_keyblock(const char *filename,
  144. const struct vb2_keyblock *keyblock)
  145. {
  146. return vb2_write_file(filename, keyblock, keyblock->keyblock_size);
  147. }