OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 * | 4 * |
5 * Host functions for verified boot. | 5 * Host functions for verified boot. |
6 */ | 6 */ |
7 | 7 |
8 /* TODO: change all 'return 0', 'return 1' into meaningful return codes */ | 8 /* TODO: change all 'return 0', 'return 1' into meaningful return codes */ |
9 | 9 |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
| 12 #include <string.h> |
12 #include <unistd.h> | 13 #include <unistd.h> |
13 | 14 |
14 #include "host_common.h" | 15 #include "host_common.h" |
15 | 16 |
16 #include "cryptolib.h" | 17 #include "cryptolib.h" |
17 #include "utility.h" | 18 #include "utility.h" |
18 #include "vboot_common.h" | 19 #include "vboot_common.h" |
19 | 20 |
20 | 21 |
| 22 char* StrCopy(char* dest, const char* src, int dest_size) { |
| 23 strncpy(dest, src, dest_size); |
| 24 dest[dest_size - 1] = '\0'; |
| 25 return dest; |
| 26 } |
| 27 |
| 28 |
21 uint8_t* ReadFile(const char* filename, uint64_t* size) { | 29 uint8_t* ReadFile(const char* filename, uint64_t* size) { |
22 FILE* f; | 30 FILE* f; |
23 uint8_t* buf; | 31 uint8_t* buf; |
24 | 32 |
25 f = fopen(filename, "rb"); | 33 f = fopen(filename, "rb"); |
26 if (!f) { | 34 if (!f) { |
27 VBDEBUG(("Unable to open file %s\n", filename)); | 35 VBDEBUG(("Unable to open file %s\n", filename)); |
28 return NULL; | 36 return NULL; |
29 } | 37 } |
30 | 38 |
(...skipping 12 matching lines...) Expand all Loading... |
43 fclose(f); | 51 fclose(f); |
44 Free(buf); | 52 Free(buf); |
45 return NULL; | 53 return NULL; |
46 } | 54 } |
47 | 55 |
48 fclose(f); | 56 fclose(f); |
49 return buf; | 57 return buf; |
50 } | 58 } |
51 | 59 |
52 | 60 |
| 61 char* ReadFileString(char* dest, int size, const char* filename) { |
| 62 char* got; |
| 63 FILE* f; |
| 64 |
| 65 f = fopen(filename, "rt"); |
| 66 if (!f) |
| 67 return NULL; |
| 68 |
| 69 got = fgets(dest, size, f); |
| 70 fclose(f); |
| 71 return got; |
| 72 } |
| 73 |
| 74 |
| 75 int ReadFileInt(const char* filename) { |
| 76 char buf[64]; |
| 77 int value; |
| 78 char* e = NULL; |
| 79 |
| 80 if (!ReadFileString(buf, sizeof(buf), filename)) |
| 81 return -1; |
| 82 |
| 83 /* Convert to integer. Allow characters after the int ("123 blah"). */ |
| 84 value = strtol(buf, &e, 0); |
| 85 if (e == buf) |
| 86 return -1; /* No characters consumed, so conversion failed */ |
| 87 |
| 88 return value; |
| 89 } |
| 90 |
| 91 |
| 92 int ReadFileBit(const char* filename, int bitmask) { |
| 93 int value = ReadFileInt(filename); |
| 94 if (value == -1) |
| 95 return -1; |
| 96 else return (value & bitmask ? 1 : 0); |
| 97 } |
| 98 |
| 99 |
53 int WriteFile(const char* filename, const void *data, uint64_t size) { | 100 int WriteFile(const char* filename, const void *data, uint64_t size) { |
54 FILE *f = fopen(filename, "wb"); | 101 FILE *f = fopen(filename, "wb"); |
55 if (!f) { | 102 if (!f) { |
56 VBDEBUG(("Unable to open file %s\n", filename)); | 103 VBDEBUG(("Unable to open file %s\n", filename)); |
57 return 1; | 104 return 1; |
58 } | 105 } |
59 | 106 |
60 if (1 != fwrite(data, size, 1, f)) { | 107 if (1 != fwrite(data, size, 1, f)) { |
61 VBDEBUG(("Unable to write to file %s\n", filename)); | 108 VBDEBUG(("Unable to write to file %s\n", filename)); |
62 fclose(f); | 109 fclose(f); |
63 unlink(filename); /* Delete any partial file */ | 110 unlink(filename); /* Delete any partial file */ |
64 } | 111 } |
65 | 112 |
66 fclose(f); | 113 fclose(f); |
67 return 0; | 114 return 0; |
68 } | 115 } |
69 | 116 |
70 void PrintPubKeySha1Sum(VbPublicKey* key) { | 117 void PrintPubKeySha1Sum(VbPublicKey* key) { |
71 uint8_t* buf = ((uint8_t *)key) + key->key_offset; | 118 uint8_t* buf = ((uint8_t *)key) + key->key_offset; |
72 uint64_t buflen = key->key_size; | 119 uint64_t buflen = key->key_size; |
73 uint8_t* digest = DigestBuf(buf, buflen, SHA1_DIGEST_ALGORITHM); | 120 uint8_t* digest = DigestBuf(buf, buflen, SHA1_DIGEST_ALGORITHM); |
74 int i; | 121 int i; |
75 for (i=0; i<SHA1_DIGEST_SIZE; i++) | 122 for (i=0; i<SHA1_DIGEST_SIZE; i++) |
76 printf("%02x", digest[i]); | 123 printf("%02x", digest[i]); |
77 Free(digest); | 124 Free(digest); |
78 } | 125 } |
OLD | NEW |