Gaurav Shah | 322536d | 2010-01-28 15:01:23 -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 | |
| 6 | /* Routines for verifying a file's signature. Useful in testing the core |
| 7 | * RSA verification implementation. |
| 8 | */ |
| 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 | 321f310 | 2010-01-28 16:59:42 -0800 | [diff] [blame] | 18 | #include "digest_utility.h" |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 19 | #include "padding.h" |
| 20 | #include "rsa.h" |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 21 | #include "rsa_utility.h" |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 22 | #include "verify_data.h" |
| 23 | |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 24 | RSAPublicKey* read_RSAkey(char* input_file, int len) { |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 25 | int key_fd; |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 26 | int buf_len; |
| 27 | struct stat stat_fd; |
| 28 | uint8_t* buf = NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 29 | |
| 30 | if ((key_fd = open(input_file, O_RDONLY)) == -1) { |
| 31 | fprintf(stderr, "Couldn't open pre-processed key file\n"); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 35 | if (-1 == fstat(key_fd, &stat_fd)) { |
| 36 | fprintf(stderr, "Couldn't stat key file\n"); |
| 37 | return NULL; |
| 38 | } |
| 39 | buf_len = stat_fd.st_size; |
| 40 | |
| 41 | /* Read entire key binary blob into a buffer. */ |
| 42 | buf = (uint8_t*) malloc(buf_len); |
| 43 | if (!buf) |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 44 | return NULL; |
| 45 | |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 46 | if (buf_len != read(key_fd, buf, buf_len)) { |
| 47 | fprintf(stderr, "Couldn't read key into a buffer.\n"); |
| 48 | return NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 49 | } |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 50 | |
| 51 | close(key_fd); |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 52 | return RSAPublicKeyFromBuf(buf, buf_len); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 55 | uint8_t* read_signature(char* input_file, int len) { |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 56 | int i, sigfd; |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 57 | uint8_t* signature = NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 58 | if ((sigfd = open(input_file, O_RDONLY)) == -1) { |
| 59 | fprintf(stderr, "Couldn't open signature file\n"); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | /* Read the signature into a buffer*/ |
| 64 | signature = (uint8_t*) malloc(len); |
| 65 | if (!signature) |
| 66 | return NULL; |
| 67 | |
| 68 | if( (i = read(sigfd, signature, len)) != len ) { |
| 69 | fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n", |
| 70 | len, i); |
| 71 | close(sigfd); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | close(sigfd); |
| 76 | return signature; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | int main(int argc, char* argv[]) { |
| 81 | int i, algorithm, sig_len; |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame^] | 82 | uint8_t* digest = NULL; |
| 83 | uint8_t* signature = NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 84 | RSAPublicKey* key = NULL; |
| 85 | |
| 86 | if (argc!=5) { |
| 87 | fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>" |
| 88 | " <input file>\n\n", argv[0]); |
| 89 | fprintf(stderr, "where <algorithm> depends on the signature algorithm" |
| 90 | " used:\n"); |
| 91 | for(i = 0; i<kNumAlgorithms; i++) |
| 92 | fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | algorithm = atoi(argv[1]); |
| 97 | if (algorithm >= kNumAlgorithms) { |
| 98 | fprintf(stderr, "Invalid Algorithm!\n"); |
| 99 | return 0; |
| 100 | } |
| 101 | /* Length of the RSA Signature/RSA Key */ |
| 102 | sig_len = siglen_map[algorithm] * sizeof(uint32_t); |
| 103 | |
| 104 | if (!(key = read_RSAkey(argv[2], sig_len))) |
| 105 | goto failure; |
| 106 | if (!(signature = read_signature(argv[3], sig_len))) |
| 107 | goto failure; |
| 108 | if (!(digest = calculate_digest(argv[4], algorithm))) |
| 109 | goto failure; |
| 110 | if(RSA_verify(key, signature, sig_len, algorithm, digest)) |
| 111 | fprintf(stderr, "Signature Verification SUCCEEDED.\n"); |
| 112 | else |
| 113 | fprintf(stderr, "Signature Verification FAILED!\n"); |
| 114 | |
| 115 | failure: |
| 116 | free(key); |
| 117 | free(signature); |
| 118 | free(digest); |
| 119 | |
| 120 | return 0; |
| 121 | } |