host_key2.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 keys.
  6. */
  7. /* TODO: change all 'return 0', 'return 1' into meaningful return codes */
  8. #include <openssl/pem.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include "2sysincludes.h"
  13. #include "2common.h"
  14. #include "2rsa.h"
  15. #include "2sha.h"
  16. #include "host_common.h"
  17. #include "host_key.h"
  18. #include "host_key2.h"
  19. #include "host_misc.h"
  20. #include "vb2_common.h"
  21. #include "vboot_common.h"
  22. enum vb2_crypto_algorithm vb2_get_crypto_algorithm(
  23. enum vb2_hash_algorithm hash_alg,
  24. enum vb2_signature_algorithm sig_alg)
  25. {
  26. /* Make sure algorithms are in the range supported by crypto alg */
  27. if (sig_alg < VB2_SIG_RSA1024 || sig_alg > VB2_SIG_RSA8192)
  28. return VB2_ALG_COUNT;
  29. if (hash_alg < VB2_HASH_SHA1 || hash_alg > VB2_HASH_SHA512)
  30. return VB2_ALG_COUNT;
  31. return (sig_alg - VB2_SIG_RSA1024)
  32. * (VB2_HASH_SHA512 - VB2_HASH_SHA1 + 1)
  33. + (hash_alg - VB2_HASH_SHA1);
  34. };
  35. struct vb2_private_key *vb2_read_private_key(const char *filename)
  36. {
  37. uint8_t *buf = NULL;
  38. uint32_t bufsize = 0;
  39. if (VB2_SUCCESS != vb2_read_file(filename, &buf, &bufsize)) {
  40. VbExError("unable to read from file %s\n", filename);
  41. return NULL;
  42. }
  43. struct vb2_private_key *key =
  44. (struct vb2_private_key *)calloc(sizeof(*key), 1);
  45. if (!key) {
  46. VbExError("Unable to allocate private key\n");
  47. free(buf);
  48. return NULL;
  49. }
  50. uint64_t alg = *(uint64_t *)buf;
  51. key->hash_alg = vb2_crypto_to_hash(alg);
  52. key->sig_alg = vb2_crypto_to_signature(alg);
  53. const unsigned char *start = buf + sizeof(alg);
  54. key->rsa_private_key =
  55. d2i_RSAPrivateKey(0, &start, bufsize - sizeof(alg));
  56. if (!key->rsa_private_key) {
  57. VbExError("Unable to parse RSA private key\n");
  58. free(buf);
  59. free(key);
  60. return NULL;
  61. }
  62. free(buf);
  63. return key;
  64. }
  65. struct vb2_private_key *vb2_read_private_key_pem(
  66. const char* filename,
  67. enum vb2_crypto_algorithm algorithm)
  68. {
  69. if (algorithm >= VB2_ALG_COUNT) {
  70. VB2_DEBUG("%s() called with invalid algorithm!\n",
  71. __FUNCTION__);
  72. return NULL;
  73. }
  74. /* Read private key */
  75. FILE *f = fopen(filename, "r");
  76. if (!f) {
  77. VB2_DEBUG("%s(): Couldn't open key file: %s\n",
  78. __FUNCTION__, filename);
  79. return NULL;
  80. }
  81. struct rsa_st *rsa_key = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL);
  82. fclose(f);
  83. if (!rsa_key) {
  84. VB2_DEBUG("%s(): Couldn't read private key from file: %s\n",
  85. __FUNCTION__, filename);
  86. return NULL;
  87. }
  88. /* Store key and algorithm in our struct */
  89. struct vb2_private_key *key =
  90. (struct vb2_private_key *)calloc(sizeof(*key), 1);
  91. if (!key) {
  92. RSA_free(rsa_key);
  93. return NULL;
  94. }
  95. key->rsa_private_key = rsa_key;
  96. key->hash_alg = vb2_crypto_to_hash(algorithm);
  97. key->sig_alg = vb2_crypto_to_signature(algorithm);
  98. /* Return the key */
  99. return key;
  100. }
  101. void vb2_free_private_key(struct vb2_private_key *key)
  102. {
  103. if (!key)
  104. return;
  105. if (key->rsa_private_key)
  106. RSA_free(key->rsa_private_key);
  107. free(key);
  108. }
  109. int vb2_write_private_key(const char *filename,
  110. const struct vb2_private_key *key)
  111. {
  112. /* Convert back to legacy vb1 algorithm enum */
  113. uint64_t alg = vb2_get_crypto_algorithm(key->hash_alg, key->sig_alg);
  114. if (alg == VB2_ALG_COUNT)
  115. return VB2_ERROR_VB1_CRYPTO_ALGORITHM;
  116. uint8_t *outbuf = NULL;
  117. int buflen = i2d_RSAPrivateKey(key->rsa_private_key, &outbuf);
  118. if (buflen <= 0) {
  119. fprintf(stderr, "Unable to write private key buffer\n");
  120. return VB2_ERROR_PRIVATE_KEY_WRITE_RSA;
  121. }
  122. FILE *f = fopen(filename, "wb");
  123. if (!f) {
  124. fprintf(stderr, "Unable to open file %s\n", filename);
  125. free(outbuf);
  126. return VB2_ERROR_PRIVATE_KEY_WRITE_FILE;
  127. }
  128. if (1 != fwrite(&alg, sizeof(alg), 1, f) ||
  129. 1 != fwrite(outbuf, buflen, 1, f)) {
  130. fprintf(stderr, "Unable to write to file %s\n", filename);
  131. fclose(f);
  132. unlink(filename); /* Delete any partial file */
  133. free(outbuf);
  134. return VB2_ERROR_PRIVATE_KEY_WRITE_FILE;
  135. }
  136. fclose(f);
  137. free(outbuf);
  138. return VB2_SUCCESS;
  139. }
  140. void vb2_init_packed_key(struct vb2_packed_key *key, uint8_t *key_data,
  141. uint32_t key_size)
  142. {
  143. memset(key, 0, sizeof(*key));
  144. key->key_offset = vb2_offset_of(key, key_data);
  145. key->key_size = key_size;
  146. key->algorithm = VB2_ALG_COUNT; /* Key not present yet */
  147. }
  148. struct vb2_packed_key *vb2_alloc_packed_key(uint32_t key_size,
  149. uint32_t algorithm,
  150. uint32_t version)
  151. {
  152. struct vb2_packed_key *key =
  153. (struct vb2_packed_key *)calloc(sizeof(*key) + key_size, 1);
  154. if (!key)
  155. return NULL;
  156. key->algorithm = algorithm;
  157. key->key_version = version;
  158. key->key_size = key_size;
  159. key->key_offset = sizeof(*key);
  160. return key;
  161. }
  162. int vb2_copy_packed_key(struct vb2_packed_key *dest,
  163. const struct vb2_packed_key *src)
  164. {
  165. if (dest->key_size < src->key_size)
  166. return VB2_ERROR_COPY_KEY_SIZE;
  167. dest->key_size = src->key_size;
  168. dest->algorithm = src->algorithm;
  169. dest->key_version = src->key_version;
  170. memcpy((uint8_t *)vb2_packed_key_data(dest),
  171. vb2_packed_key_data(src), src->key_size);
  172. return VB2_SUCCESS;
  173. }
  174. struct vb2_packed_key *vb2_read_packed_key(const char *filename)
  175. {
  176. struct vb2_packed_key *key = NULL;
  177. uint32_t file_size = 0;
  178. if (VB2_SUCCESS !=
  179. vb2_read_file(filename, (uint8_t **)&key, &file_size)) {
  180. return NULL;
  181. }
  182. if (packed_key_looks_ok(key, file_size))
  183. return key;
  184. /* Error */
  185. free(key);
  186. return NULL;
  187. }
  188. struct vb2_packed_key *vb2_read_packed_keyb(const char *filename,
  189. uint32_t algorithm,
  190. uint32_t version)
  191. {
  192. if (algorithm >= VB2_ALG_COUNT) {
  193. fprintf(stderr, "%s() - invalid algorithm\n", __func__);
  194. return NULL;
  195. }
  196. if (version > VB2_MAX_KEY_VERSION) {
  197. /* Currently, TPM only supports 16-bit version */
  198. fprintf(stderr, "%s() - invalid version 0x%x\n", __func__,
  199. version);
  200. return NULL;
  201. }
  202. uint8_t *key_data = NULL;
  203. uint32_t key_size = 0;
  204. if (VB2_SUCCESS != vb2_read_file(filename, &key_data, &key_size))
  205. return NULL;
  206. uint32_t expected_key_size =
  207. vb2_packed_key_size(vb2_crypto_to_signature(algorithm));
  208. if (!expected_key_size || expected_key_size != key_size) {
  209. fprintf(stderr, "%s() - wrong key size %u for algorithm %u\n",
  210. __func__, key_size, algorithm);
  211. free(key_data);
  212. return NULL;
  213. }
  214. struct vb2_packed_key *key =
  215. vb2_alloc_packed_key(key_size, algorithm, version);
  216. if (!key) {
  217. free(key_data);
  218. return NULL;
  219. }
  220. memcpy((uint8_t *)vb2_packed_key_data(key), key_data, key_size);
  221. free(key_data);
  222. return key;
  223. }
  224. int vb2_write_packed_key(const char *filename,
  225. const struct vb2_packed_key *key)
  226. {
  227. /* Copy the key, so its data is contiguous with the header */
  228. struct vb2_packed_key *kcopy =
  229. vb2_alloc_packed_key(key->key_size, 0, 0);
  230. if (!kcopy)
  231. return VB2_ERROR_PACKED_KEY_ALLOC;
  232. if (VB2_SUCCESS != vb2_copy_packed_key(kcopy, key)) {
  233. free(kcopy);
  234. return VB2_ERROR_PACKED_KEY_COPY;
  235. }
  236. /* Write the copy, then free it */
  237. int rv = vb2_write_file(filename, kcopy,
  238. kcopy->key_offset + kcopy->key_size);
  239. free(kcopy);
  240. return rv;
  241. }