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 "rsa_utility.h" | 19 #include "rsa_utility.h" |
19 #include "utility.h" | 20 #include "utility.h" |
20 | 21 |
21 uint8_t* BufferFromFile(char* input_file, int* len) { | 22 uint8_t* BufferFromFile(char* input_file, int* len) { |
22 int fd; | 23 int fd; |
23 struct stat stat_fd; | 24 struct stat stat_fd; |
24 uint8_t* buf = NULL; | 25 uint8_t* buf = NULL; |
25 | 26 |
26 if ((fd = open(input_file, O_RDONLY)) == -1) { | 27 if ((fd = open(input_file, O_RDONLY)) == -1) { |
27 fprintf(stderr, "Couldn't open file.\n"); | 28 fprintf(stderr, "Couldn't open file.\n"); |
(...skipping 20 matching lines...) Expand all Loading... |
48 return buf; | 49 return buf; |
49 } | 50 } |
50 | 51 |
51 RSAPublicKey* RSAPublicKeyFromFile(char* input_file) { | 52 RSAPublicKey* RSAPublicKeyFromFile(char* input_file) { |
52 int len; | 53 int len; |
53 uint8_t* buf = BufferFromFile(input_file, &len); | 54 uint8_t* buf = BufferFromFile(input_file, &len); |
54 RSAPublicKey* key = RSAPublicKeyFromBuf(buf, len); | 55 RSAPublicKey* key = RSAPublicKeyFromBuf(buf, len); |
55 Free(buf); | 56 Free(buf); |
56 return key; | 57 return key; |
57 } | 58 } |
| 59 |
| 60 uint8_t* SignatureFile(char* input_file, char* key_file, int algorithm) { |
| 61 char* sign_utility = "./sign_data.sh"; |
| 62 char* cmd; /* Command line to invoke. */ |
| 63 int cmd_len; |
| 64 FILE* cmd_out; /* File descriptor to command output. */ |
| 65 uint8_t* signature = NULL; |
| 66 int signature_size = siglen_map[algorithm] * sizeof(uint32_t); |
| 67 |
| 68 /* Build command line: |
| 69 * sign_data.sh <algorithm> <key file> <input file> |
| 70 */ |
| 71 cmd_len = (strlen(sign_utility) + 1 + /* +1 for space. */ |
| 72 2 + 1 + /* For [algorithm]. */ |
| 73 strlen(key_file) + 1 + /* +1 for space. */ |
| 74 strlen(input_file) + |
| 75 1); /* For the trailing '\0'. */ |
| 76 cmd = (char*) Malloc(cmd_len); |
| 77 snprintf(cmd, cmd_len, "%s %d %s %s", sign_utility, algorithm, key_file, |
| 78 input_file); |
| 79 cmd_out = popen(cmd, "r"); |
| 80 Free(cmd); |
| 81 if (!cmd_out) { |
| 82 fprintf(stderr, "Couldn't execute: %s\n", cmd); |
| 83 return NULL; |
| 84 } |
| 85 |
| 86 signature = (uint8_t*) Malloc(signature_size); |
| 87 if (fread(signature, signature_size, 1, cmd_out) != 1) { |
| 88 fprintf(stderr, "Couldn't read signature.\n"); |
| 89 pclose(cmd_out); |
| 90 Free(signature); |
| 91 return NULL; |
| 92 } |
| 93 |
| 94 pclose(cmd_out); |
| 95 return signature; |
| 96 } |
OLD | NEW |