blob: 5969488fc633c8330588b7ca1b4e445287ff7aaa [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature verification program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker940f9ce2013-09-18 15:34:57 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker940f9ce2013-09-18 15:34:57 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010057#include <stdlib.h>
58#define mbedtls_snprintf snprintf
59#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010060#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010061#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010062#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000064
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020065#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
67 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000068int main( void )
Paul Bakker940f9ce2013-09-18 15:34:57 +020069{
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020070 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
72 "MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020073 mbedtls_exit( 0 );
Paul Bakker940f9ce2013-09-18 15:34:57 +020074}
75#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020076
77#include "mbedtls/error.h"
78#include "mbedtls/md.h"
79#include "mbedtls/pk.h"
80
81#include <stdio.h>
82#include <string.h>
83
Simon Butcher63cb97e2018-12-06 17:43:31 +000084
Paul Bakker940f9ce2013-09-18 15:34:57 +020085int main( int argc, char *argv[] )
86{
87 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020088 int ret = 1;
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010089 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020090 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_pk_context pk;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020092 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020094 char filename[512];
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_pk_init( &pk );
Paul Bakker0c226102014-04-17 16:02:36 +020097
Paul Bakker940f9ce2013-09-18 15:34:57 +020098 if( argc != 3 )
99 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200101
102#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104#endif
105
106 goto exit;
107 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200110 fflush( stdout );
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115 goto exit;
116 }
117
118 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200119 * Extract the signature from the file
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122
123 if( ( f = fopen( filename, "rb" ) ) == NULL )
124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200126 goto exit;
127 }
128
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129 i = fread( buf, 1, sizeof(buf), f );
130
131 fclose( f );
132
133 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200134 * Compute the SHA-256 hash of the input file and
135 * verify the signature
Paul Bakker940f9ce2013-09-18 15:34:57 +0200136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( "\n . Verifying the SHA-256 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200138 fflush( stdout );
139
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200140 if( ( ret = mbedtls_md_file(
141 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
142 argv[2], hash ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145 goto exit;
146 }
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200149 buf, i ) ) != 0 )
150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " failed\n ! mbedtls_pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200152 goto exit;
153 }
154
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200155 mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200156
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +0100157 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200158
159exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_pk_free( &pk );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +0100163 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200164 {
165 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
166 mbedtls_printf( " ! Last error was: %s\n", buf );
167 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200168#endif
169
Paul Bakker940f9ce2013-09-18 15:34:57 +0200170#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200172 fflush( stdout ); getchar();
173#endif
174
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200175 mbedtls_exit( exit_code );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
178 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */