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 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
| 32 | #define polarssl_printf printf |
| 33 | #define polarssl_fprintf fprintf |
| 34 | #define polarssl_malloc malloc |
| 35 | #define polarssl_free free |
| 36 | #endif |
| 37 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <stdio.h> |
| 40 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 41 | #include "polarssl/error.h" |
| 42 | #include "polarssl/md.h" |
| 43 | #include "polarssl/pk.h" |
| 44 | #include "polarssl/sha1.h" |
| 45 | |
| 46 | #if defined _MSC_VER && !defined snprintf |
| 47 | #define snprintf _snprintf |
| 48 | #endif |
| 49 | |
| 50 | #if !defined(POLARSSL_BIGNUM_C) || \ |
| 51 | !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_PK_PARSE_C) || \ |
| 52 | !defined(POLARSSL_FS_IO) |
| 53 | int main( int argc, char *argv[] ) |
| 54 | { |
| 55 | ((void) argc); |
| 56 | ((void) argv); |
| 57 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 58 | polarssl_printf("POLARSSL_BIGNUM_C and/or " |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 59 | "POLARSSL_SHA1_C and/or POLARSSL_PK_PARSE_C and/or " |
| 60 | "POLARSSL_FS_IO not defined.\n"); |
| 61 | return( 0 ); |
| 62 | } |
| 63 | #else |
| 64 | int main( int argc, char *argv[] ) |
| 65 | { |
| 66 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 67 | int ret = 1; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 68 | size_t i; |
| 69 | pk_context pk; |
| 70 | unsigned char hash[20]; |
| 71 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; |
| 72 | char filename[512]; |
| 73 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 74 | pk_init( &pk ); |
| 75 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 76 | if( argc != 3 ) |
| 77 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 78 | polarssl_printf( "usage: pk_verify <key_file> <filename>\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 79 | |
| 80 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 81 | polarssl_printf( "\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | goto exit; |
| 85 | } |
| 86 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 87 | polarssl_printf( "\n . Reading public key from '%s'", argv[1] ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 88 | fflush( stdout ); |
| 89 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 90 | if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) |
| 91 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 92 | polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 93 | goto exit; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Extract the signature from the text file |
| 98 | */ |
| 99 | ret = 1; |
| 100 | snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); |
| 101 | |
| 102 | if( ( f = fopen( filename, "rb" ) ) == NULL ) |
| 103 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 104 | polarssl_printf( "\n ! Could not open %s\n\n", filename ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 105 | goto exit; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | i = fread( buf, 1, sizeof(buf), f ); |
| 110 | |
| 111 | fclose( f ); |
| 112 | |
| 113 | /* |
| 114 | * Compute the SHA-1 hash of the input file and compare |
| 115 | * it with the hash decrypted from the signature. |
| 116 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 117 | polarssl_printf( "\n . Verifying the SHA-1 signature" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 118 | fflush( stdout ); |
| 119 | |
| 120 | if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) |
| 121 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 122 | polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 123 | goto exit; |
| 124 | } |
| 125 | |
| 126 | if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0, |
| 127 | buf, i ) ) != 0 ) |
| 128 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 129 | polarssl_printf( " failed\n ! pk_verify returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 130 | goto exit; |
| 131 | } |
| 132 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 133 | polarssl_printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 134 | |
| 135 | ret = 0; |
| 136 | |
| 137 | exit: |
| 138 | pk_free( &pk ); |
| 139 | |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 140 | #if defined(POLARSSL_ERROR_C) |
| 141 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 142 | polarssl_printf( " ! Last error was: %s\n", buf ); |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 143 | #endif |
| 144 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 145 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 146 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 147 | fflush( stdout ); getchar(); |
| 148 | #endif |
| 149 | |
| 150 | return( ret ); |
| 151 | } |
| 152 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA1_C && |
| 153 | POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */ |