Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based signature verification program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 28 | |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 32 | #include "polarssl/error.h" |
| 33 | #include "polarssl/md.h" |
| 34 | #include "polarssl/pk.h" |
| 35 | #include "polarssl/sha1.h" |
| 36 | |
| 37 | #if defined _MSC_VER && !defined snprintf |
| 38 | #define snprintf _snprintf |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(POLARSSL_BIGNUM_C) || \ |
| 42 | !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_PK_PARSE_C) || \ |
| 43 | !defined(POLARSSL_FS_IO) |
| 44 | int main( int argc, char *argv[] ) |
| 45 | { |
| 46 | ((void) argc); |
| 47 | ((void) argv); |
| 48 | |
| 49 | printf("POLARSSL_BIGNUM_C and/or " |
| 50 | "POLARSSL_SHA1_C and/or POLARSSL_PK_PARSE_C and/or " |
| 51 | "POLARSSL_FS_IO not defined.\n"); |
| 52 | return( 0 ); |
| 53 | } |
| 54 | #else |
| 55 | int main( int argc, char *argv[] ) |
| 56 | { |
| 57 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 58 | int ret = 1; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 59 | size_t i; |
| 60 | pk_context pk; |
| 61 | unsigned char hash[20]; |
| 62 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; |
| 63 | char filename[512]; |
| 64 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 65 | pk_init( &pk ); |
| 66 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 67 | if( argc != 3 ) |
| 68 | { |
| 69 | printf( "usage: pk_verify <key_file> <filename>\n" ); |
| 70 | |
| 71 | #if defined(_WIN32) |
| 72 | printf( "\n" ); |
| 73 | #endif |
| 74 | |
| 75 | goto exit; |
| 76 | } |
| 77 | |
| 78 | printf( "\n . Reading public key from '%s'", argv[1] ); |
| 79 | fflush( stdout ); |
| 80 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 81 | if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) |
| 82 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 83 | printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 84 | goto exit; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Extract the signature from the text file |
| 89 | */ |
| 90 | ret = 1; |
| 91 | snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); |
| 92 | |
| 93 | if( ( f = fopen( filename, "rb" ) ) == NULL ) |
| 94 | { |
| 95 | printf( "\n ! Could not open %s\n\n", filename ); |
| 96 | goto exit; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | i = fread( buf, 1, sizeof(buf), f ); |
| 101 | |
| 102 | fclose( f ); |
| 103 | |
| 104 | /* |
| 105 | * Compute the SHA-1 hash of the input file and compare |
| 106 | * it with the hash decrypted from the signature. |
| 107 | */ |
| 108 | printf( "\n . Verifying the SHA-1 signature" ); |
| 109 | fflush( stdout ); |
| 110 | |
| 111 | if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) |
| 112 | { |
| 113 | printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); |
| 114 | goto exit; |
| 115 | } |
| 116 | |
| 117 | if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0, |
| 118 | buf, i ) ) != 0 ) |
| 119 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 120 | printf( " failed\n ! pk_verify returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 121 | goto exit; |
| 122 | } |
| 123 | |
| 124 | printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" ); |
| 125 | |
| 126 | ret = 0; |
| 127 | |
| 128 | exit: |
| 129 | pk_free( &pk ); |
| 130 | |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 131 | #if defined(POLARSSL_ERROR_C) |
| 132 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
| 133 | printf( " ! Last error was: %s\n", buf ); |
| 134 | #endif |
| 135 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 136 | #if defined(_WIN32) |
| 137 | printf( " + Press Enter to exit this program.\n" ); |
| 138 | fflush( stdout ); getchar(); |
| 139 | #endif |
| 140 | |
| 141 | return( ret ); |
| 142 | } |
| 143 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA1_C && |
| 144 | POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */ |