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