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 | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 18 | #include "cryptolib.h" |
Gaurav Shah | 431b988 | 2010-02-12 15:54:37 -0800 | [diff] [blame] | 19 | #include "file_keys.h" |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 20 | #include "verify_data.h" |
| 21 | |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 22 | /* ANSI Color coding sequences. */ |
| 23 | #define COL_GREEN "\e[1;32m" |
Gaurav Shah | fc70d72 | 2010-03-31 13:26:55 -0700 | [diff] [blame] | 24 | #define COL_RED "\e[0;31m" |
Gaurav Shah | 08df9b8 | 2010-02-23 16:16:23 -0800 | [diff] [blame] | 25 | #define COL_STOP "\e[m" |
| 26 | |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame] | 27 | uint8_t* read_signature(char* input_file, int len) { |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 28 | int i, sigfd; |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame] | 29 | uint8_t* signature = NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 30 | if ((sigfd = open(input_file, O_RDONLY)) == -1) { |
| 31 | fprintf(stderr, "Couldn't open signature file\n"); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | /* Read the signature into a buffer*/ |
| 36 | signature = (uint8_t*) malloc(len); |
| 37 | if (!signature) |
| 38 | return NULL; |
| 39 | |
| 40 | if( (i = read(sigfd, signature, len)) != len ) { |
| 41 | fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n", |
| 42 | len, i); |
| 43 | close(sigfd); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | close(sigfd); |
| 48 | return signature; |
| 49 | } |
| 50 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 51 | int main(int argc, char* argv[]) { |
| 52 | int i, algorithm, sig_len; |
Gaurav Shah | 6f55539 | 2010-02-11 21:04:49 -0800 | [diff] [blame] | 53 | int return_code = 1; /* Default to error. */ |
Gaurav Shah | e178fd9 | 2010-02-05 11:44:58 -0800 | [diff] [blame] | 54 | uint8_t* digest = NULL; |
| 55 | uint8_t* signature = NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 56 | RSAPublicKey* key = NULL; |
| 57 | |
| 58 | if (argc!=5) { |
| 59 | fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>" |
| 60 | " <input file>\n\n", argv[0]); |
| 61 | fprintf(stderr, "where <algorithm> depends on the signature algorithm" |
| 62 | " used:\n"); |
| 63 | for(i = 0; i<kNumAlgorithms; i++) |
| 64 | fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | algorithm = atoi(argv[1]); |
| 69 | if (algorithm >= kNumAlgorithms) { |
| 70 | fprintf(stderr, "Invalid Algorithm!\n"); |
| 71 | return 0; |
| 72 | } |
| 73 | /* Length of the RSA Signature/RSA Key */ |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 74 | sig_len = siglen_map[algorithm]; |
| 75 | if ((key = RSAPublicKeyFromFile(argv[2])) && |
| 76 | (signature = read_signature(argv[3], sig_len)) && |
| 77 | (digest = DigestFile(argv[4], algorithm))) { |
| 78 | if (RSAVerify(key, signature, sig_len, algorithm, digest)) { |
| 79 | return_code = 0; |
| 80 | fprintf(stderr, "Signature Verification " |
| 81 | COL_GREEN "SUCCEEDED" COL_STOP "\n"); |
| 82 | } else { |
| 83 | fprintf(stderr, "Signature Verification " |
| 84 | COL_RED "FAILED" COL_STOP "\n"); |
| 85 | } |
Gaurav Shah | 6f55539 | 2010-02-11 21:04:49 -0800 | [diff] [blame] | 86 | } |
Gaurav Shah | f5564fa | 2010-03-02 15:40:01 -0800 | [diff] [blame] | 87 | else |
| 88 | return_code = -1; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 89 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 90 | free(key); |
| 91 | free(signature); |
| 92 | free(digest); |
| 93 | |
Gaurav Shah | 6f55539 | 2010-02-11 21:04:49 -0800 | [diff] [blame] | 94 | return return_code; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 95 | } |