blob: 4eafd524523adcee3d8c2a688ad26d87b4b09373 [file] [log] [blame]
Gaurav Shah431b9882010-02-12 15:54:37 -08001/* 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 Shah08df9b82010-02-23 16:16:23 -080018#include "padding.h"
Gaurav Shah431b9882010-02-12 15:54:37 -080019#include "rsa_utility.h"
Gaurav Shahf5564fa2010-03-02 15:40:01 -080020#include "signature_digest.h"
Gaurav Shah431b9882010-02-12 15:54:37 -080021#include "utility.h"
22
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080023uint8_t* BufferFromFile(const char* input_file, uint32_t* len) {
Gaurav Shah431b9882010-02-12 15:54:37 -080024 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 Shah23a2f3a2010-02-26 15:09:43 -080053RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) {
54 uint32_t len;
Gaurav Shahc7daf382010-03-01 20:24:37 -080055 RSAPublicKey* key = NULL;
Gaurav Shah431b9882010-02-12 15:54:37 -080056 uint8_t* buf = BufferFromFile(input_file, &len);
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080057 if (buf)
58 key = RSAPublicKeyFromBuf(buf, len);
Gaurav Shah431b9882010-02-12 15:54:37 -080059 Free(buf);
60 return key;
61}
Gaurav Shah08df9b82010-02-23 16:16:23 -080062
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080063uint8_t* SignatureFile(const char* input_file, const char* key_file,
64 int algorithm) {
Gaurav Shah08df9b82010-02-23 16:16:23 -080065 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 Shahcae5fa62010-02-28 20:02:29 -080070 int signature_size = siglen_map[algorithm];
Gaurav Shah08df9b82010-02-23 16:16:23 -080071
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}