blob: 2b52b38b0c8434a270383524efe50451c4cf751d [file] [log] [blame]
Paul Bakker2291f6c2011-03-25 14:07:53 +00001/*
2 * RSASSA-PSS/SHA-1 signature verification program
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker2291f6c2011-03-25 14:07:53 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker2291f6c2011-03-25 14:07:53 +00007 *
8 * 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
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdio.h>
29
Paul Bakker5690efc2011-05-26 13:16:06 +000030#include "polarssl/config.h"
31
Paul Bakker2291f6c2011-03-25 14:07:53 +000032#include "polarssl/md.h"
33#include "polarssl/pem.h"
34#include "polarssl/rsa.h"
35#include "polarssl/sha1.h"
36#include "polarssl/x509.h"
37
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000038#if defined _MSC_VER && !defined snprintf
39#define snprintf _snprintf
40#endif
41
Paul Bakker5690efc2011-05-26 13:16:06 +000042#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
43 !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_X509_PARSE_C) || \
44 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Paul Bakker5690efc2011-05-26 13:16:06 +000050 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 Bakker39148402014-04-17 16:02:36 +020059 int ret = 1;
Paul Bakker23986e52011-04-24 08:57:21 +000060 size_t i;
Paul Bakker2291f6c2011-03-25 14:07:53 +000061 rsa_context rsa;
62 unsigned char hash[20];
Paul Bakker1d569582012-10-03 20:35:44 +000063 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker2291f6c2011-03-25 14:07:53 +000064 char filename[512];
65
Paul Bakker39148402014-04-17 16:02:36 +020066 rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
67
Paul Bakker2291f6c2011-03-25 14:07:53 +000068 if( argc != 3 )
69 {
70 printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
71
Paul Bakkercce9d772011-11-18 14:26:47 +000072#if defined(_WIN32)
Paul Bakker2291f6c2011-03-25 14:07:53 +000073 printf( "\n" );
74#endif
75
76 goto exit;
77 }
78
79 printf( "\n . Reading public key from '%s'", argv[1] );
80 fflush( stdout );
81
Paul Bakker2291f6c2011-03-25 14:07:53 +000082 if( ( ret = x509parse_public_keyfile( &rsa, argv[1] ) ) != 0 )
83 {
84 printf( " failed\n ! x509parse_public_key returned %d\n\n", ret );
85 goto exit;
86 }
87
88 /*
89 * Extract the RSA signature from the text file
90 */
91 ret = 1;
92 snprintf( filename, 512, "%s.sig", argv[2] );
93
94 if( ( f = fopen( filename, "rb" ) ) == NULL )
95 {
96 printf( "\n ! Could not open %s\n\n", filename );
97 goto exit;
98 }
99
100 i = fread( buf, 1, rsa.len, f );
101
102 fclose( f );
103
104 if( i != rsa.len )
105 {
106 printf( "\n ! Invalid RSA signature format\n\n" );
107 goto exit;
108 }
109
110 /*
111 * Compute the SHA-1 hash of the input file and compare
112 * it with the hash decrypted from the RSA signature.
113 */
114 printf( "\n . Verifying the RSA/SHA-1 signature" );
115 fflush( stdout );
116
117 if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
118 {
119 printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
120 goto exit;
121 }
122
Paul Bakker43f97992013-09-23 11:23:31 +0200123 if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, SIG_RSA_SHA1,
Paul Bakker2291f6c2011-03-25 14:07:53 +0000124 20, hash, buf ) ) != 0 )
125 {
126 printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret );
127 goto exit;
128 }
129
130 printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
131
132 ret = 0;
133
134exit:
135
Paul Bakkercce9d772011-11-18 14:26:47 +0000136#if defined(_WIN32)
Paul Bakker2291f6c2011-03-25 14:07:53 +0000137 printf( " + Press Enter to exit this program.\n" );
138 fflush( stdout ); getchar();
139#endif
140
141 return( ret );
142}
Paul Bakker5690efc2011-05-26 13:16:06 +0000143#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
144 POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */