| 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 "padding.h" | 18 #include "padding.h" |
| 19 #include "rsa_utility.h" | 19 #include "rsa_utility.h" |
| 20 #include "utility.h" | 20 #include "utility.h" |
| 21 | 21 |
| 22 uint8_t* BufferFromFile(char* input_file, int* len) { | 22 uint8_t* BufferFromFile(const char* input_file, uint32_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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "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(char* input_file) { | 52 RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { |
| 53 int len; | 53 uint32_t len; |
| 54 RSAPublicKey* key; |
| 54 uint8_t* buf = BufferFromFile(input_file, &len); | 55 uint8_t* buf = BufferFromFile(input_file, &len); |
| 55 RSAPublicKey* key = RSAPublicKeyFromBuf(buf, len); | 56 if (buf) |
| 57 key = RSAPublicKeyFromBuf(buf, len); |
| 56 Free(buf); | 58 Free(buf); |
| 57 return key; | 59 return key; |
| 58 } | 60 } |
| 59 | 61 |
| 60 uint8_t* SignatureFile(char* input_file, char* key_file, int algorithm) { | 62 uint8_t* SignatureFile(const char* input_file, const char* key_file, |
| 63 int algorithm) { |
| 61 char* sign_utility = "./sign_data.sh"; | 64 char* sign_utility = "./sign_data.sh"; |
| 62 char* cmd; /* Command line to invoke. */ | 65 char* cmd; /* Command line to invoke. */ |
| 63 int cmd_len; | 66 int cmd_len; |
| 64 FILE* cmd_out; /* File descriptor to command output. */ | 67 FILE* cmd_out; /* File descriptor to command output. */ |
| 65 uint8_t* signature = NULL; | 68 uint8_t* signature = NULL; |
| 66 int signature_size = siglen_map[algorithm] * sizeof(uint32_t); | 69 int signature_size = siglen_map[algorithm] * sizeof(uint32_t); |
| 67 | 70 |
| 68 /* Build command line: | 71 /* Build command line: |
| 69 * sign_data.sh <algorithm> <key file> <input file> | 72 * sign_data.sh <algorithm> <key file> <input file> |
| 70 */ | 73 */ |
| (...skipping 16 matching lines...) Expand all Loading... |
| 87 if (fread(signature, signature_size, 1, cmd_out) != 1) { | 90 if (fread(signature, signature_size, 1, cmd_out) != 1) { |
| 88 fprintf(stderr, "Couldn't read signature.\n"); | 91 fprintf(stderr, "Couldn't read signature.\n"); |
| 89 pclose(cmd_out); | 92 pclose(cmd_out); |
| 90 Free(signature); | 93 Free(signature); |
| 91 return NULL; | 94 return NULL; |
| 92 } | 95 } |
| 93 | 96 |
| 94 pclose(cmd_out); | 97 pclose(cmd_out); |
| 95 return signature; | 98 return signature; |
| 96 } | 99 } |
| OLD | NEW |