blob: 89e9cc1be206a025fb593da22bbcf5513253694f [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature verification program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker940f9ce2013-09-18 15:34:57 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker940f9ce2013-09-18 15:34:57 +02007 *
Paul Bakker940f9ce2013-09-18 15:34:57 +02008 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#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 Bakker940f9ce2013-09-18 15:34:57 +020038#include <string.h>
39#include <stdio.h>
40
Paul Bakker940f9ce2013-09-18 15:34:57 +020041#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)
53int main( int argc, char *argv[] )
54{
55 ((void) argc);
56 ((void) argv);
57
Rich Evansf90016a2015-01-19 14:26:37 +000058 polarssl_printf("POLARSSL_BIGNUM_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020059 "POLARSSL_SHA1_C and/or POLARSSL_PK_PARSE_C and/or "
60 "POLARSSL_FS_IO not defined.\n");
61 return( 0 );
62}
63#else
64int main( int argc, char *argv[] )
65{
66 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020067 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020068 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 Bakker0c226102014-04-17 16:02:36 +020074 pk_init( &pk );
75
Paul Bakker940f9ce2013-09-18 15:34:57 +020076 if( argc != 3 )
77 {
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( "usage: pk_verify <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020079
80#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000081 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020082#endif
83
84 goto exit;
85 }
86
Rich Evansf90016a2015-01-19 14:26:37 +000087 polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020088 fflush( stdout );
89
Paul Bakker940f9ce2013-09-18 15:34:57 +020090 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
91 {
Rich Evansf90016a2015-01-19 14:26:37 +000092 polarssl_printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020093 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 Evansf90016a2015-01-19 14:26:37 +0000104 polarssl_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200105 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 Evansf90016a2015-01-19 14:26:37 +0000117 polarssl_printf( "\n . Verifying the SHA-1 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200118 fflush( stdout );
119
120 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
121 {
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200123 goto exit;
124 }
125
126 if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0,
127 buf, i ) ) != 0 )
128 {
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( " failed\n ! pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130 goto exit;
131 }
132
Rich Evansf90016a2015-01-19 14:26:37 +0000133 polarssl_printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134
135 ret = 0;
136
137exit:
138 pk_free( &pk );
139
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200140#if defined(POLARSSL_ERROR_C)
141 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000142 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200143#endif
144
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000146 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200147 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 */