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