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 | |
| 18 | #include "rsa_utility.h" |
| 19 | #include "utility.h" |
| 20 | |
| 21 | uint8_t* BufferFromFile(char* input_file, int* len) { |
| 22 | int fd; |
| 23 | struct stat stat_fd; |
| 24 | uint8_t* buf = NULL; |
| 25 | |
| 26 | if ((fd = open(input_file, O_RDONLY)) == -1) { |
| 27 | fprintf(stderr, "Couldn't open file.\n"); |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | if (-1 == fstat(fd, &stat_fd)) { |
| 32 | fprintf(stderr, "Couldn't stat key file\n"); |
| 33 | return NULL; |
| 34 | } |
| 35 | *len = stat_fd.st_size; |
| 36 | |
| 37 | /* Read entire key binary blob into a buffer. */ |
| 38 | buf = (uint8_t*) Malloc(*len); |
| 39 | if (!buf) |
| 40 | return NULL; |
| 41 | |
| 42 | if (*len != read(fd, buf, *len)) { |
| 43 | fprintf(stderr, "Couldn't read key into a buffer.\n"); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | close(fd); |
| 48 | return buf; |
| 49 | } |
| 50 | |
| 51 | RSAPublicKey* RSAPublicKeyFromFile(char* input_file) { |
| 52 | int len; |
| 53 | uint8_t* buf = BufferFromFile(input_file, &len); |
| 54 | RSAPublicKey* key = RSAPublicKeyFromBuf(buf, len); |
| 55 | Free(buf); |
| 56 | return key; |
| 57 | } |