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