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 "cryptolib.h" |
19 #include "rsa_utility.h" | |
20 #include "signature_digest.h" | 19 #include "signature_digest.h" |
21 #include "utility.h" | 20 #include "utility.h" |
22 | 21 |
23 uint8_t* BufferFromFile(const char* input_file, uint64_t* len) { | 22 uint8_t* BufferFromFile(const char* input_file, uint64_t* len) { |
24 int fd; | 23 int fd; |
25 struct stat stat_fd; | 24 struct stat stat_fd; |
26 uint8_t* buf = NULL; | 25 uint8_t* buf = NULL; |
27 | 26 |
28 if ((fd = open(input_file, O_RDONLY)) == -1) { | 27 if ((fd = open(input_file, O_RDONLY)) == -1) { |
29 fprintf(stderr, "Couldn't open file.\n"); | 28 fprintf(stderr, "Couldn't open file.\n"); |
(...skipping 23 matching lines...) Expand all Loading... |
53 RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { | 52 RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { |
54 uint64_t len; | 53 uint64_t len; |
55 RSAPublicKey* key = NULL; | 54 RSAPublicKey* key = NULL; |
56 uint8_t* buf = BufferFromFile(input_file, &len); | 55 uint8_t* buf = BufferFromFile(input_file, &len); |
57 if (buf) | 56 if (buf) |
58 key = RSAPublicKeyFromBuf(buf, len); | 57 key = RSAPublicKeyFromBuf(buf, len); |
59 Free(buf); | 58 Free(buf); |
60 return key; | 59 return key; |
61 } | 60 } |
62 | 61 |
| 62 uint8_t* DigestFile(char* input_file, int sig_algorithm) { |
| 63 int input_fd, len; |
| 64 uint8_t data[SHA1_BLOCK_SIZE]; |
| 65 uint8_t* digest = NULL; |
| 66 DigestContext ctx; |
| 67 |
| 68 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) { |
| 69 debug("Couldn't open input file.\n"); |
| 70 return NULL; |
| 71 } |
| 72 DigestInit(&ctx, sig_algorithm); |
| 73 while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) == |
| 74 SHA1_BLOCK_SIZE) |
| 75 DigestUpdate(&ctx, data, len); |
| 76 if (len != -1) |
| 77 DigestUpdate(&ctx, data, len); |
| 78 digest = DigestFinal(&ctx); |
| 79 close(input_fd); |
| 80 return digest; |
| 81 } |
| 82 |
63 uint8_t* SignatureFile(const char* input_file, const char* key_file, | 83 uint8_t* SignatureFile(const char* input_file, const char* key_file, |
64 int algorithm) { | 84 int algorithm) { |
65 char* sign_utility = "./sign_data.sh"; | 85 char* sign_utility = "./sign_data.sh"; |
66 char* cmd; /* Command line to invoke. */ | 86 char* cmd; /* Command line to invoke. */ |
67 int cmd_len; | 87 int cmd_len; |
68 FILE* cmd_out; /* File descriptor to command output. */ | 88 FILE* cmd_out; /* File descriptor to command output. */ |
69 uint8_t* signature = NULL; | 89 uint8_t* signature = NULL; |
70 int signature_size = siglen_map[algorithm]; | 90 int signature_size = siglen_map[algorithm]; |
71 | 91 |
72 /* Build command line: | 92 /* Build command line: |
(...skipping 18 matching lines...) Expand all Loading... |
91 if (fread(signature, signature_size, 1, cmd_out) != 1) { | 111 if (fread(signature, signature_size, 1, cmd_out) != 1) { |
92 fprintf(stderr, "Couldn't read signature.\n"); | 112 fprintf(stderr, "Couldn't read signature.\n"); |
93 pclose(cmd_out); | 113 pclose(cmd_out); |
94 Free(signature); | 114 Free(signature); |
95 return NULL; | 115 return NULL; |
96 } | 116 } |
97 | 117 |
98 pclose(cmd_out); | 118 pclose(cmd_out); |
99 return signature; | 119 return signature; |
100 } | 120 } |
OLD | NEW |