blob: 1fb20d8ed70e2dd667abad037e0f8b87525c4892 [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
2 * RSASSA-PSS/SHA-1 signature verification program
3 *
4 * Copyright (C) 2006-2010, 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/md.h"
34#include "polarssl/pem.h"
35#include "polarssl/rsa.h"
36#include "polarssl/sha1.h"
37#include "polarssl/x509.h"
38
39int main( int argc, char *argv[] )
40{
41 FILE *f;
42 int ret, i;
43 rsa_context rsa;
44 unsigned char hash[20];
45 unsigned char buf[512];
46 char filename[512];
47
48 ret = 1;
49 if( argc != 3 )
50 {
51 printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
52
53#ifdef WIN32
54 printf( "\n" );
55#endif
56
57 goto exit;
58 }
59
60 printf( "\n . Reading public key from '%s'", argv[1] );
61 fflush( stdout );
62
63 rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
64
65 if( ( ret = x509parse_public_keyfile( &rsa, argv[1] ) ) != 0 )
66 {
67 printf( " failed\n ! x509parse_public_key returned %d\n\n", ret );
68 goto exit;
69 }
70
71 /*
72 * Extract the RSA signature from the text file
73 */
74 ret = 1;
75 snprintf( filename, 512, "%s.sig", argv[2] );
76
77 if( ( f = fopen( filename, "rb" ) ) == NULL )
78 {
79 printf( "\n ! Could not open %s\n\n", filename );
80 goto exit;
81 }
82
83 i = fread( buf, 1, rsa.len, f );
84
85 fclose( f );
86
87 if( i != rsa.len )
88 {
89 printf( "\n ! Invalid RSA signature format\n\n" );
90 goto exit;
91 }
92
93 /*
94 * Compute the SHA-1 hash of the input file and compare
95 * it with the hash decrypted from the RSA signature.
96 */
97 printf( "\n . Verifying the RSA/SHA-1 signature" );
98 fflush( stdout );
99
100 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
101 {
102 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
103 goto exit;
104 }
105
106 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
107 20, hash, buf ) ) != 0 )
108 {
109 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
110 goto exit;
111 }
112
113 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
114
115 ret = 0;
116
117exit:
118
119#ifdef WIN32
120 printf( " + Press Enter to exit this program.\n" );
121 fflush( stdout ); getchar();
122#endif
123
124 return( ret );
125}