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