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