blob: 31cd0df7e423b00b213bdee8d66a5f8c04052f0f [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
Paul Bakker5690efc2011-05-26 13:16:06 +000033#include "polarssl/config.h"
34
Paul Bakker2291f6c2011-03-25 14:07:53 +000035#include "polarssl/md.h"
36#include "polarssl/pem.h"
37#include "polarssl/rsa.h"
38#include "polarssl/sha1.h"
39#include "polarssl/x509.h"
40
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000041#if defined _MSC_VER && !defined snprintf
42#define snprintf _snprintf
43#endif
44
Paul Bakker5690efc2011-05-26 13:16:06 +000045#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
46 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_X509_PARSE_C) || \
47 !defined(POLARSSL_FS_IO)
48int main( void )
49{
50 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
51 "POLARSSL_SHA1_C and/or POLARSSL_X509_PARSE_C and/or "
52 "POLARSSL_FS_IO not defined.\n");
53 return( 0 );
54}
55#else
Paul Bakker2291f6c2011-03-25 14:07:53 +000056int main( int argc, char *argv[] )
57{
58 FILE *f;
Paul Bakker23986e52011-04-24 08:57:21 +000059 int ret;
60 size_t i;
Paul Bakker2291f6c2011-03-25 14:07:53 +000061 rsa_context rsa;
62 unsigned char hash[20];
63 unsigned char buf[512];
64 char filename[512];
65
66 ret = 1;
67 if( argc != 3 )
68 {
69 printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
70
71#ifdef 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
81 rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
82
83 if( ( ret = x509parse_public_keyfile( &rsa, argv[1] ) ) != 0 )
84 {
85 printf( " failed\n ! x509parse_public_key returned %d\n\n", ret );
86 goto exit;
87 }
88
89 /*
90 * Extract the RSA signature from the text file
91 */
92 ret = 1;
93 snprintf( filename, 512, "%s.sig", argv[2] );
94
95 if( ( f = fopen( filename, "rb" ) ) == NULL )
96 {
97 printf( "\n ! Could not open %s\n\n", filename );
98 goto exit;
99 }
100
101 i = fread( buf, 1, rsa.len, f );
102
103 fclose( f );
104
105 if( i != rsa.len )
106 {
107 printf( "\n ! Invalid RSA signature format\n\n" );
108 goto exit;
109 }
110
111 /*
112 * Compute the SHA-1 hash of the input file and compare
113 * it with the hash decrypted from the RSA signature.
114 */
115 printf( "\n . Verifying the RSA/SHA-1 signature" );
116 fflush( stdout );
117
118 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
119 {
120 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
121 goto exit;
122 }
123
124 if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1,
125 20, hash, buf ) ) != 0 )
126 {
127 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
128 goto exit;
129 }
130
131 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
132
133 ret = 0;
134
135exit:
136
137#ifdef WIN32
138 printf( " + Press Enter to exit this program.\n" );
139 fflush( stdout ); getchar();
140#endif
141
142 return( ret );
143}
Paul Bakker5690efc2011-05-26 13:16:06 +0000144#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
145 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */