blob: f2e5eb3d4941359a97266b4d971ebf417fef1415 [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
29#include <string.h>
30#include <stdio.h>
31
Paul Bakker940f9ce2013-09-18 15:34:57 +020032#include "polarssl/error.h"
33#include "polarssl/md.h"
34#include "polarssl/pk.h"
35#include "polarssl/sha1.h"
36
37#if defined _MSC_VER && !defined snprintf
38#define snprintf _snprintf
39#endif
40
41#if !defined(POLARSSL_BIGNUM_C) || \
42 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_PK_PARSE_C) || \
43 !defined(POLARSSL_FS_IO)
44int main( int argc, char *argv[] )
45{
46 ((void) argc);
47 ((void) argv);
48
49 printf("POLARSSL_BIGNUM_C and/or "
50 "POLARSSL_SHA1_C and/or POLARSSL_PK_PARSE_C and/or "
51 "POLARSSL_FS_IO not defined.\n");
52 return( 0 );
53}
54#else
55int main( int argc, char *argv[] )
56{
57 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020058 int ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +020059 size_t i;
60 pk_context pk;
61 unsigned char hash[20];
62 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
63 char filename[512];
64
Paul Bakker0c226102014-04-17 16:02:36 +020065 pk_init( &pk );
66
Paul Bakker940f9ce2013-09-18 15:34:57 +020067 if( argc != 3 )
68 {
69 printf( "usage: pk_verify <key_file> <filename>\n" );
70
71#if defined(_WIN32)
72 printf( "\n" );
73#endif
74
75 goto exit;
76 }
77
78 printf( "\n . Reading public key from '%s'", argv[1] );
79 fflush( stdout );
80
Paul Bakker940f9ce2013-09-18 15:34:57 +020081 if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
82 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020083 printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020084 goto exit;
85 }
86
87 /*
88 * Extract the signature from the text file
89 */
90 ret = 1;
91 snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
92
93 if( ( f = fopen( filename, "rb" ) ) == NULL )
94 {
95 printf( "\n ! Could not open %s\n\n", filename );
96 goto exit;
97 }
98
99
100 i = fread( buf, 1, sizeof(buf), f );
101
102 fclose( f );
103
104 /*
105 * Compute the SHA-1 hash of the input file and compare
106 * it with the hash decrypted from the signature.
107 */
108 printf( "\n . Verifying the SHA-1 signature" );
109 fflush( stdout );
110
111 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
112 {
113 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
114 goto exit;
115 }
116
117 if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0,
118 buf, i ) ) != 0 )
119 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200120 printf( " failed\n ! pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200121 goto exit;
122 }
123
124 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
125
126 ret = 0;
127
128exit:
129 pk_free( &pk );
130
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200131#if defined(POLARSSL_ERROR_C)
132 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
133 printf( " ! Last error was: %s\n", buf );
134#endif
135
Paul Bakker940f9ce2013-09-18 15:34:57 +0200136#if defined(_WIN32)
137 printf( " + Press Enter to exit this program.\n" );
138 fflush( stdout ); getchar();
139#endif
140
141 return( ret );
142}
143#endif /* POLARSSL_BIGNUM_C && POLARSSL_SHA1_C &&
144 POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */