Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RSASSA-PSS/SHA-1 signature verification program |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 30 | #include "polarssl/config.h" |
| 31 | |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 32 | #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 Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 38 | #if defined _MSC_VER && !defined snprintf |
| 39 | #define snprintf _snprintf |
| 40 | #endif |
| 41 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 42 | #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 Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 45 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 47 | ((void) argc); |
| 48 | ((void) argv); |
| 49 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 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 Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 56 | int main( int argc, char *argv[] ) |
| 57 | { |
| 58 | FILE *f; |
Paul Bakker | 3914840 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 59 | int ret = 1; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 60 | size_t i; |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 61 | rsa_context rsa; |
| 62 | unsigned char hash[20]; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 63 | unsigned char buf[POLARSSL_MPI_MAX_SIZE]; |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 64 | char filename[512]; |
| 65 | |
Paul Bakker | 3914840 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 66 | rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 ); |
| 67 | |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 68 | if( argc != 3 ) |
| 69 | { |
| 70 | printf( "usage: rsa_verify_pss <key_file> <filename>\n" ); |
| 71 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 72 | #if defined(_WIN32) |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 73 | 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 Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 82 | 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 Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame] | 123 | if( ( ret = rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, SIG_RSA_SHA1, |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 124 | 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 | |
| 134 | exit: |
| 135 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 136 | #if defined(_WIN32) |
Paul Bakker | 2291f6c | 2011-03-25 14:07:53 +0000 | [diff] [blame] | 137 | printf( " + Press Enter to exit this program.\n" ); |
| 138 | fflush( stdout ); getchar(); |
| 139 | #endif |
| 140 | |
| 141 | return( ret ); |
| 142 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 143 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C && |
| 144 | POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */ |