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 mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n");
59
60 #if defined(_WIN32)
61 mbedtls_printf("\n");
62 #endif
63
64 goto exit;
65 }
66
67 mbedtls_printf("\n . Reading public key from '%s'", argv[1]);
68 fflush(stdout);
69
70 if ((ret = mbedtls_pk_parse_public_keyfile(&pk, argv[1])) != 0) {
71 mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n",
72 (unsigned int) -ret);
73 goto exit;
74 }
75
76 /*
77 * Extract the signature from the file
78 */
79 mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]);
80
81 if ((f = fopen(filename, "rb")) == NULL) {
82 mbedtls_printf("\n ! Could not open %s\n\n", filename);
83 goto exit;
84 }
85
86 i = fread(buf, 1, sizeof(buf), f);
87
88 fclose(f);
89
90 /*
91 * Compute the SHA-256 hash of the input file and
92 * verify the signature
93 */
94 mbedtls_printf("\n . Verifying the SHA-256 signature");
95 fflush(stdout);
96
97 if ((ret = mbedtls_md_file(
98 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
99 argv[2], hash)) != 0) {
100 mbedtls_printf(" failed\n ! Could not open or read %s\n\n", argv[2]);
101 goto exit;
102 }
103
104 if ((ret = mbedtls_pk_verify(&pk, MBEDTLS_MD_SHA256, hash, 0,
105 buf, i)) != 0) {
106 mbedtls_printf(" failed\n ! mbedtls_pk_verify returned -0x%04x\n", (unsigned int) -ret);
107 goto exit;
108 }
109
110 mbedtls_printf("\n . OK (the signature is valid)\n\n");
111
112 exit_code = MBEDTLS_EXIT_SUCCESS;
113
114 exit:
115 mbedtls_pk_free(&pk);
116
117 #if defined(MBEDTLS_ERROR_C)
118 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
119 mbedtls_strerror(ret, (char *) buf, sizeof(buf));
120 mbedtls_printf(" ! Last error was: %s\n", buf);
121 }
122 #endif
123
124 mbedtls_exit(exit_code);
125 }
126 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
127 MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
128