blob: 2401a15e70d589447cb5c4103003e542817a4025 [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"
20#include "utility.h"
21
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080022uint8_t* BufferFromFile(const char* input_file, uint32_t* len) {
Gaurav Shah431b9882010-02-12 15:54:37 -080023 int fd;
24 struct stat stat_fd;
25 uint8_t* buf = NULL;
26
27 if ((fd = open(input_file, O_RDONLY)) == -1) {
28 fprintf(stderr, "Couldn't open file.\n");
29 return NULL;
30 }
31
32 if (-1 == fstat(fd, &stat_fd)) {
33 fprintf(stderr, "Couldn't stat key file\n");
34 return NULL;
35 }
36 *len = stat_fd.st_size;
37
38 /* Read entire key binary blob into a buffer. */
39 buf = (uint8_t*) Malloc(*len);
40 if (!buf)
41 return NULL;
42
43 if (*len != read(fd, buf, *len)) {
44 fprintf(stderr, "Couldn't read key into a buffer.\n");
45 return NULL;
46 }
47
48 close(fd);
49 return buf;
50}
51
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080052RSAPublicKey* RSAPublicKeyFromFile(const char* input_file) {
53 uint32_t len;
54 RSAPublicKey* key;
Gaurav Shah431b9882010-02-12 15:54:37 -080055 uint8_t* buf = BufferFromFile(input_file, &len);
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080056 if (buf)
57 key = RSAPublicKeyFromBuf(buf, len);
Gaurav Shah431b9882010-02-12 15:54:37 -080058 Free(buf);
59 return key;
60}
Gaurav Shah08df9b82010-02-23 16:16:23 -080061
Gaurav Shah23a2f3a2010-02-26 15:09:43 -080062uint8_t* SignatureFile(const char* input_file, const char* key_file,
63 int algorithm) {
Gaurav Shah08df9b82010-02-23 16:16:23 -080064 char* sign_utility = "./sign_data.sh";
65 char* cmd; /* Command line to invoke. */
66 int cmd_len;
67 FILE* cmd_out; /* File descriptor to command output. */
68 uint8_t* signature = NULL;
69 int signature_size = siglen_map[algorithm] * sizeof(uint32_t);
70
71 /* Build command line:
72 * sign_data.sh <algorithm> <key file> <input file>
73 */
74 cmd_len = (strlen(sign_utility) + 1 + /* +1 for space. */
75 2 + 1 + /* For [algorithm]. */
76 strlen(key_file) + 1 + /* +1 for space. */
77 strlen(input_file) +
78 1); /* For the trailing '\0'. */
79 cmd = (char*) Malloc(cmd_len);
80 snprintf(cmd, cmd_len, "%s %d %s %s", sign_utility, algorithm, key_file,
81 input_file);
82 cmd_out = popen(cmd, "r");
83 Free(cmd);
84 if (!cmd_out) {
85 fprintf(stderr, "Couldn't execute: %s\n", cmd);
86 return NULL;
87 }
88
89 signature = (uint8_t*) Malloc(signature_size);
90 if (fread(signature, signature_size, 1, cmd_out) != 1) {
91 fprintf(stderr, "Couldn't read signature.\n");
92 pclose(cmd_out);
93 Free(signature);
94 return NULL;
95 }
96
97 pclose(cmd_out);
98 return signature;
99}