blob: 5e6c16e8649006c76977190f6ca9121aec66b7a5 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based signature verification program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker940f9ce2013-09-18 15:34:57 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010059#include <stdlib.h>
60#define mbedtls_snprintf snprintf
61#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010062#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010063#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010064#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
65#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000066
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020067#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
69 !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000070int main( void )
Paul Bakker940f9ce2013-09-18 15:34:57 +020071{
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020072 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
74 "MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Paul Bakker940f9ce2013-09-18 15:34:57 +020076}
77#else
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +020078
79#include "mbedtls/error.h"
80#include "mbedtls/md.h"
81#include "mbedtls/pk.h"
82
83#include <stdio.h>
84#include <string.h>
85
Simon Butcher63cb97e2018-12-06 17:43:31 +000086
Paul Bakker940f9ce2013-09-18 15:34:57 +020087int main( int argc, char *argv[] )
88{
89 FILE *f;
Paul Bakker0c226102014-04-17 16:02:36 +020090 int ret = 1;
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +010091 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020092 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_pk_context pk;
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020094 unsigned char hash[32];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker940f9ce2013-09-18 15:34:57 +020096 char filename[512];
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_pk_init( &pk );
Paul Bakker0c226102014-04-17 16:02:36 +020099
Paul Bakker940f9ce2013-09-18 15:34:57 +0200100 if( argc != 3 )
101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200103
104#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200106#endif
107
108 goto exit;
109 }
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112 fflush( stdout );
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200117 goto exit;
118 }
119
120 /*
Manuel Pégourié-Gonnard1d8f2da2015-08-27 21:42:27 +0200121 * Extract the signature from the file
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200124
125 if( ( f = fopen( filename, "rb" ) ) == NULL )
126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "\n ! Could not open %s\n\n", filename );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200128 goto exit;
129 }
130
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131 i = fread( buf, 1, sizeof(buf), f );
132
133 fclose( f );
134
135 /*
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200136 * Compute the SHA-256 hash of the input file and
137 * verify the signature
Paul Bakker940f9ce2013-09-18 15:34:57 +0200138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_printf( "\n . Verifying the SHA-256 signature" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200140 fflush( stdout );
141
Manuel Pégourié-Gonnard06d5d612015-05-28 16:23:18 +0200142 if( ( ret = mbedtls_md_file(
143 mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
144 argv[2], hash ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200147 goto exit;
148 }
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
Paul Bakker940f9ce2013-09-18 15:34:57 +0200151 buf, i ) ) != 0 )
152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_printf( " failed\n ! mbedtls_pk_verify returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200154 goto exit;
155 }
156
Manuel Pégourié-Gonnardce7a08b2015-08-27 21:59:58 +0200157 mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200158
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +0100159 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200160
161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_pk_free( &pk );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia9f3379d2018-04-29 22:30:05 +0100165 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200166 {
167 mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
168 mbedtls_printf( " ! Last error was: %s\n", buf );
169 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200170#endif
171
Paul Bakker940f9ce2013-09-18 15:34:57 +0200172#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200174 fflush( stdout ); getchar();
175#endif
176
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200177 mbedtls_exit( exit_code );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
180 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */