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