1 /*
2  *  Public key-based signature verification program
3  *
4  *  Copyright The Mbed TLS Contributors
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 
20 #include "mbedtls/build_info.h"
21 
22 #include "mbedtls/platform.h"
23 
24 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
25     !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) ||   \
26     !defined(MBEDTLS_FS_IO)
main(void)27 int main( void )
28 {
29     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
30            "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
31            "MBEDTLS_FS_IO not defined.\n");
32     mbedtls_exit( 0 );
33 }
34 #else
35 
36 #include "mbedtls/error.h"
37 #include "mbedtls/md.h"
38 #include "mbedtls/pk.h"
39 
40 #include <stdio.h>
41 #include <string.h>
42 
43 
main(int argc,char * argv[])44 int main( int argc, char *argv[] )
45 {
46     FILE *f;
47     int ret = 1;
48     int exit_code = MBEDTLS_EXIT_FAILURE;
49     size_t i;
50     mbedtls_pk_context pk;
51     unsigned char hash[32];
52     unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
53     char filename[512];
54 
55     mbedtls_pk_init( &pk );
56 
57     if( argc != 3 )
58     {
59         mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
60 
61 #if defined(_WIN32)
62         mbedtls_printf( "\n" );
63 #endif
64 
65         goto exit;
66     }
67 
68     mbedtls_printf( "\n  . Reading public key from '%s'", argv[1] );
69     fflush( stdout );
70 
71     if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
72     {
73         mbedtls_printf( " failed\n  ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", (unsigned int) -ret );
74         goto exit;
75     }
76 
77     /*
78      * Extract the signature from the file
79      */
80     mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
81 
82     if( ( f = fopen( filename, "rb" ) ) == NULL )
83     {
84         mbedtls_printf( "\n  ! Could not open %s\n\n", filename );
85         goto exit;
86     }
87 
88     i = fread( buf, 1, sizeof(buf), f );
89 
90     fclose( f );
91 
92     /*
93      * Compute the SHA-256 hash of the input file and
94      * verify the signature
95      */
96     mbedtls_printf( "\n  . Verifying the SHA-256 signature" );
97     fflush( stdout );
98 
99     if( ( ret = mbedtls_md_file(
100                     mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
101                     argv[2], hash ) ) != 0 )
102     {
103         mbedtls_printf( " failed\n  ! Could not open or read %s\n\n", argv[2] );
104         goto exit;
105     }
106 
107     if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
108                            buf, i ) ) != 0 )
109     {
110         mbedtls_printf( " failed\n  ! mbedtls_pk_verify returned -0x%04x\n", (unsigned int) -ret );
111         goto exit;
112     }
113 
114     mbedtls_printf( "\n  . OK (the signature is valid)\n\n" );
115 
116     exit_code = MBEDTLS_EXIT_SUCCESS;
117 
118 exit:
119     mbedtls_pk_free( &pk );
120 
121 #if defined(MBEDTLS_ERROR_C)
122     if( exit_code != MBEDTLS_EXIT_SUCCESS )
123     {
124         mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
125         mbedtls_printf( "  !  Last error was: %s\n", buf );
126     }
127 #endif
128 
129     mbedtls_exit( exit_code );
130 }
131 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
132           MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
133