Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 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 |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Utility functions for file and key handling. |
| 6 | */ |
| 7 | |
| 8 | #include "file_keys.h" |
| 9 | |
| 10 | #include <fcntl.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <unistd.h> |
| 17 | |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 18 | #include "padding.h" |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 19 | #include "rsa_utility.h" |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 20 | #include "signature_digest.h" |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 21 | #include "utility.h" |
| 22 | |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 23 | uint8_t* BufferFromFile(const char* input_file, uint32_t* len) { |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 24 | int fd; |
| 25 | struct stat stat_fd; |
| 26 | uint8_t* buf = NULL; |
| 27 | |
| 28 | if ((fd = open(input_file, O_RDONLY)) == -1) { |
| 29 | fprintf(stderr, "Couldn't open file.\n"); |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | if (-1 == fstat(fd, &stat_fd)) { |
| 34 | fprintf(stderr, "Couldn't stat key file\n"); |
| 35 | return NULL; |
| 36 | } |
| 37 | *len = stat_fd.st_size; |
| 38 | |
| 39 | /* Read entire key binary blob into a buffer. */ |
| 40 | buf = (uint8_t*) Malloc(*len); |
| 41 | if (!buf) |
| 42 | return NULL; |
| 43 | |
| 44 | if (*len != read(fd, buf, *len)) { |
| 45 | fprintf(stderr, "Couldn't read key into a buffer.\n"); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | close(fd); |
| 50 | return buf; |
| 51 | } |
| 52 | |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 53 | RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) { |
| 54 | uint32_t len; |
Gaurav Shah | c7daf38 | 2010-03-01 20:24:37 -0800 | [diff] [blame] | 55 | RSAPublicKey* key = NULL; |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 56 | uint8_t* buf = BufferFromFile(input_file, &len); |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 57 | if (buf) |
| 58 | key = RSAPublicKeyFromBuf(buf, len); |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 59 | Free(buf); |
| 60 | return key; |
| 61 | } |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 62 | |
Gaurav Shah | 23a2f3a | 2010-02-26 15:09:43 -0800 | [diff] [blame] | 63 | uint8_t* SignatureFile(const char* input_file, const char* key_file, |
| 64 | int algorithm) { |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 65 | char* sign_utility = "./sign_data.sh"; |
| 66 | char* cmd; /* Command line to invoke. */ |
| 67 | int cmd_len; |
| 68 | FILE* cmd_out; /* File descriptor to command output. */ |
| 69 | uint8_t* signature = NULL; |
Gaurav Shah | cae5fa6 | 2010-02-28 20:02:29 -0800 | [diff] [blame] | 70 | int signature_size = siglen_map[algorithm]; |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 71 | |
| 72 | /* Build command line: |
| 73 | * sign_data.sh <algorithm> <key file> <input file> |
| 74 | */ |
| 75 | cmd_len = (strlen(sign_utility) + 1 + /* +1 for space. */ |
| 76 | 2 + 1 + /* For [algorithm]. */ |
| 77 | strlen(key_file) + 1 + /* +1 for space. */ |
| 78 | strlen(input_file) + |
| 79 | 1); /* For the trailing '\0'. */ |
| 80 | cmd = (char*) Malloc(cmd_len); |
| 81 | snprintf(cmd, cmd_len, "%s %d %s %s", sign_utility, algorithm, key_file, |
| 82 | input_file); |
| 83 | cmd_out = popen(cmd, "r"); |
| 84 | Free(cmd); |
| 85 | if (!cmd_out) { |
| 86 | fprintf(stderr, "Couldn't execute: %s\n", cmd); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | signature = (uint8_t*) Malloc(signature_size); |
| 91 | if (fread(signature, signature_size, 1, cmd_out) != 1) { |
| 92 | fprintf(stderr, "Couldn't read signature.\n"); |
| 93 | pclose(cmd_out); |
| 94 | Free(signature); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | pclose(cmd_out); |
| 99 | return signature; |
| 100 | } |