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