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 * Utility functions for file and key handling. | 5 * Utility functions for file and key handling. |
6 */ | 6 */ |
7 | 7 |
8 #include "file_keys.h" | 8 #include "file_keys.h" |
9 | 9 |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include <string.h> | 13 #include <string.h> |
14 #include <sys/stat.h> | 14 #include <sys/stat.h> |
15 #include <sys/types.h> | 15 #include <sys/types.h> |
16 #include <unistd.h> | 16 #include <unistd.h> |
17 | 17 |
18 #include "cryptolib.h" | 18 #include "cryptolib.h" |
19 #include "signature_digest.h" | 19 #include "signature_digest.h" |
20 #include "utility.h" | 20 #include "utility.h" |
21 | 21 |
22 uint8_t* BufferFromFile(const char* input_file, uint64_t* len) { | 22 uint8_t* BufferFromFile(const char* input_file, uint64_t* len) { |
23 int fd; | 23 int fd; |
24 struct stat stat_fd; | 24 struct stat stat_fd; |
25 uint8_t* buf = NULL; | 25 uint8_t* buf = NULL; |
26 | 26 |
27 if ((fd = open(input_file, O_RDONLY)) == -1) { | 27 if ((fd = open(input_file, O_RDONLY)) == -1) { |
28 fprintf(stderr, "Couldn't open file.\n"); | 28 debug("Couldn't open file.\n"); |
29 return NULL; | 29 return NULL; |
30 } | 30 } |
31 | 31 |
32 if (-1 == fstat(fd, &stat_fd)) { | 32 if (-1 == fstat(fd, &stat_fd)) { |
33 fprintf(stderr, "Couldn't stat key file\n"); | 33 debug("Couldn't stat key file\n"); |
34 return NULL; | 34 return NULL; |
35 } | 35 } |
36 *len = stat_fd.st_size; | 36 *len = stat_fd.st_size; |
37 | 37 |
38 /* Read entire key binary blob into a buffer. */ | 38 /* Read entire key binary blob into a buffer. */ |
39 buf = (uint8_t*) Malloc(*len); | 39 buf = (uint8_t*) Malloc(*len); |
40 if (!buf) | 40 if (!buf) |
41 return NULL; | 41 return NULL; |
42 | 42 |
43 if (*len != read(fd, buf, *len)) { | 43 if (*len != read(fd, buf, *len)) { |
44 fprintf(stderr, "Couldn't read key into a buffer.\n"); | 44 debug("Couldn't read key into a buffer.\n"); |
45 return NULL; | 45 return NULL; |
46 } | 46 } |
47 | 47 |
48 close(fd); | 48 close(fd); |
49 return buf; | 49 return buf; |
50 } | 50 } |
51 | 51 |
52 RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { | 52 RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { |
53 uint64_t len; | 53 uint64_t len; |
54 RSAPublicKey* key = NULL; | 54 RSAPublicKey* key = NULL; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 2 + 1 + /* For [algorithm]. */ | 96 2 + 1 + /* For [algorithm]. */ |
97 strlen(key_file) + 1 + /* +1 for space. */ | 97 strlen(key_file) + 1 + /* +1 for space. */ |
98 strlen(input_file) + | 98 strlen(input_file) + |
99 1); /* For the trailing '\0'. */ | 99 1); /* For the trailing '\0'. */ |
100 cmd = (char*) Malloc(cmd_len); | 100 cmd = (char*) Malloc(cmd_len); |
101 snprintf(cmd, cmd_len, "%s %d %s %s", sign_utility, algorithm, key_file, | 101 snprintf(cmd, cmd_len, "%s %d %s %s", sign_utility, algorithm, key_file, |
102 input_file); | 102 input_file); |
103 cmd_out = popen(cmd, "r"); | 103 cmd_out = popen(cmd, "r"); |
104 Free(cmd); | 104 Free(cmd); |
105 if (!cmd_out) { | 105 if (!cmd_out) { |
106 fprintf(stderr, "Couldn't execute: %s\n", cmd); | 106 debug("Couldn't execute: %s\n", cmd); |
107 return NULL; | 107 return NULL; |
108 } | 108 } |
109 | 109 |
110 signature = (uint8_t*) Malloc(signature_size); | 110 signature = (uint8_t*) Malloc(signature_size); |
111 if (fread(signature, signature_size, 1, cmd_out) != 1) { | 111 if (fread(signature, signature_size, 1, cmd_out) != 1) { |
112 fprintf(stderr, "Couldn't read signature.\n"); | 112 debug("Couldn't read signature.\n"); |
113 pclose(cmd_out); | 113 pclose(cmd_out); |
114 Free(signature); | 114 Free(signature); |
115 return NULL; | 115 return NULL; |
116 } | 116 } |
117 | 117 |
118 pclose(cmd_out); | 118 pclose(cmd_out); |
119 return signature; | 119 return signature; |
120 } | 120 } |
OLD | NEW |