1 /*
2  *  Public key-based signature verification program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_snprintf        snprintf
34 #define mbedtls_printf          printf
35 #define mbedtls_exit            exit
36 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39 
40 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
41     !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) ||   \
42     !defined(MBEDTLS_FS_IO)
main(void)43 int main( void )
44 {
45     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
46            "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
47            "MBEDTLS_FS_IO not defined.\n");
48     return( 0 );
49 }
50 #else
51 
52 #include "mbedtls/error.h"
53 #include "mbedtls/md.h"
54 #include "mbedtls/pk.h"
55 
56 #include <stdio.h>
57 #include <string.h>
58 
59 #if defined(MBEDTLS_CHECK_PARAMS)
60 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)61 void mbedtls_param_failed( const char *failure_condition,
62                            const char *file,
63                            int line )
64 {
65     mbedtls_printf( "%s:%i: Input param failed - %s\n",
66                     file, line, failure_condition );
67     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
68 }
69 #endif
70 
main(int argc,char * argv[])71 int main( int argc, char *argv[] )
72 {
73     FILE *f;
74     int ret = 1;
75     int exit_code = MBEDTLS_EXIT_FAILURE;
76     size_t i;
77     mbedtls_pk_context pk;
78     unsigned char hash[32];
79     unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
80     char filename[512];
81 
82     mbedtls_pk_init( &pk );
83 
84     if( argc != 3 )
85     {
86         mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
87 
88 #if defined(_WIN32)
89         mbedtls_printf( "\n" );
90 #endif
91 
92         goto exit;
93     }
94 
95     mbedtls_printf( "\n  . Reading public key from '%s'", argv[1] );
96     fflush( stdout );
97 
98     if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
99     {
100         mbedtls_printf( " failed\n  ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
101         goto exit;
102     }
103 
104     /*
105      * Extract the signature from the file
106      */
107     mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
108 
109     if( ( f = fopen( filename, "rb" ) ) == NULL )
110     {
111         mbedtls_printf( "\n  ! Could not open %s\n\n", filename );
112         goto exit;
113     }
114 
115 
116     i = fread( buf, 1, sizeof(buf), f );
117 
118     fclose( f );
119 
120     /*
121      * Compute the SHA-256 hash of the input file and
122      * verify the signature
123      */
124     mbedtls_printf( "\n  . Verifying the SHA-256 signature" );
125     fflush( stdout );
126 
127     if( ( ret = mbedtls_md_file(
128                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
129                     argv[2], hash ) ) != 0 )
130     {
131         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[2] );
132         goto exit;
133     }
134 
135     if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
136                            buf, i ) ) != 0 )
137     {
138         mbedtls_printf( " failed\n  ! mbedtls_pk_verify returned -0x%04x\n", -ret );
139         goto exit;
140     }
141 
142     mbedtls_printf( "\n  . OK (the signature is valid)\n\n" );
143 
144     exit_code = MBEDTLS_EXIT_SUCCESS;
145 
146 exit:
147     mbedtls_pk_free( &pk );
148 
149 #if defined(MBEDTLS_ERROR_C)
150     if( exit_code != MBEDTLS_EXIT_SUCCESS )
151     {
152         mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
153         mbedtls_printf( "  !  Last error was: %s\n", buf );
154     }
155 #endif
156 
157 #if defined(_WIN32)
158     mbedtls_printf( "  + Press Enter to exit this program.\n" );
159     fflush( stdout ); getchar();
160 #endif
161 
162     return( exit_code );
163 }
164 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
165           MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
166