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 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 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 | |
| 26 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 27 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 28 | #endif |
| 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | #include "polarssl/config.h" |
| 34 | |
| 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; |
| 61 | int ret; |
| 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 | |
| 68 | ret = 1; |
| 69 | if( argc != 3 ) |
| 70 | { |
| 71 | printf( "usage: pk_verify <key_file> <filename>\n" ); |
| 72 | |
| 73 | #if defined(_WIN32) |
| 74 | printf( "\n" ); |
| 75 | #endif |
| 76 | |
| 77 | goto exit; |
| 78 | } |
| 79 | |
| 80 | printf( "\n . Reading public key from '%s'", argv[1] ); |
| 81 | fflush( stdout ); |
| 82 | |
| 83 | pk_init( &pk ); |
| 84 | |
| 85 | if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) |
| 86 | { |
| 87 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
| 88 | printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x - %s\n\n", -ret, buf ); |
| 89 | goto exit; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Extract the signature from the text file |
| 94 | */ |
| 95 | ret = 1; |
| 96 | snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); |
| 97 | |
| 98 | if( ( f = fopen( filename, "rb" ) ) == NULL ) |
| 99 | { |
| 100 | printf( "\n ! Could not open %s\n\n", filename ); |
| 101 | goto exit; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | i = fread( buf, 1, sizeof(buf), f ); |
| 106 | |
| 107 | fclose( f ); |
| 108 | |
| 109 | /* |
| 110 | * Compute the SHA-1 hash of the input file and compare |
| 111 | * it with the hash decrypted from the signature. |
| 112 | */ |
| 113 | printf( "\n . Verifying the SHA-1 signature" ); |
| 114 | fflush( stdout ); |
| 115 | |
| 116 | if( ( ret = sha1_file( argv[2], hash ) ) != 0 ) |
| 117 | { |
| 118 | printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); |
| 119 | goto exit; |
| 120 | } |
| 121 | |
| 122 | if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0, |
| 123 | buf, i ) ) != 0 ) |
| 124 | { |
| 125 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
| 126 | printf( " failed\n ! pk_verify returned -0x%04x - %s\n\n", -ret, buf ); |
| 127 | goto exit; |
| 128 | } |
| 129 | |
| 130 | printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" ); |
| 131 | |
| 132 | ret = 0; |
| 133 | |
| 134 | exit: |
| 135 | pk_free( &pk ); |
| 136 | |
| 137 | #if defined(_WIN32) |
| 138 | printf( " + Press Enter to exit this program.\n" ); |
| 139 | fflush( stdout ); getchar(); |
| 140 | #endif |
| 141 | |
| 142 | return( ret ); |
| 143 | } |
| 144 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA1_C && |
| 145 | POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */ |