Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based signature verification program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 20 | #include "mbedtls/build_info.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 22 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 24 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \ |
| 26 | !defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 27 | int main(void) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 28 | { |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 29 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 30 | "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or " |
| 31 | "MBEDTLS_FS_IO not defined.\n"); |
| 32 | mbedtls_exit(0); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 33 | } |
| 34 | #else |
Manuel Pégourié-Gonnard | 06d5d61 | 2015-05-28 16:23:18 +0200 | [diff] [blame] | 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 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 43 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 44 | int main(int argc, char *argv[]) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 45 | { |
| 46 | FILE *f; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 47 | int ret = 1; |
Andres Amaya Garcia | 9f3379d | 2018-04-29 22:30:05 +0100 | [diff] [blame] | 48 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 49 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | mbedtls_pk_context pk; |
Manuel Pégourié-Gonnard | 102a620 | 2015-08-27 21:51:44 +0200 | [diff] [blame] | 51 | unsigned char hash[32]; |
Gilles Peskine | 96a7cd1 | 2019-11-08 19:22:35 +0100 | [diff] [blame] | 52 | unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 53 | char filename[512]; |
| 54 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 55 | mbedtls_pk_init(&pk); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 56 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 57 | if (argc != 3) { |
| 58 | mbedtls_printf("usage: mbedtls_pk_verify <key_file> <filename>\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 59 | |
| 60 | #if defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 61 | mbedtls_printf("\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
| 64 | goto exit; |
| 65 | } |
| 66 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 67 | mbedtls_printf("\n . Reading public key from '%s'", argv[1]); |
| 68 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 69 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 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); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 73 | goto exit; |
| 74 | } |
| 75 | |
| 76 | /* |
Manuel Pégourié-Gonnard | 1d8f2da | 2015-08-27 21:42:27 +0200 | [diff] [blame] | 77 | * Extract the signature from the file |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 78 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 79 | mbedtls_snprintf(filename, sizeof(filename), "%s.sig", argv[2]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 80 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 81 | if ((f = fopen(filename, "rb")) == NULL) { |
| 82 | mbedtls_printf("\n ! Could not open %s\n\n", filename); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 83 | goto exit; |
| 84 | } |
| 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 86 | i = fread(buf, 1, sizeof(buf), f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 87 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 88 | fclose(f); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 89 | |
| 90 | /* |
Manuel Pégourié-Gonnard | ce7a08b | 2015-08-27 21:59:58 +0200 | [diff] [blame] | 91 | * Compute the SHA-256 hash of the input file and |
| 92 | * verify the signature |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 93 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 94 | mbedtls_printf("\n . Verifying the SHA-256 signature"); |
| 95 | fflush(stdout); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 96 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 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]); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 101 | goto exit; |
| 102 | } |
| 103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 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); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 107 | goto exit; |
| 108 | } |
| 109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 110 | mbedtls_printf("\n . OK (the signature is valid)\n\n"); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 111 | |
Andres Amaya Garcia | 9f3379d | 2018-04-29 22:30:05 +0100 | [diff] [blame] | 112 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 113 | |
| 114 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 115 | mbedtls_pk_free(&pk); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 118 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { |
| 119 | mbedtls_strerror(ret, (char *) buf, sizeof(buf)); |
| 120 | mbedtls_printf(" ! Last error was: %s\n", buf); |
Manuel Pégourié-Gonnard | cf9ab63 | 2015-08-27 22:03:33 +0200 | [diff] [blame] | 121 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 122 | #endif |
| 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 124 | mbedtls_exit(exit_code); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 125 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C && |
| 127 | MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ |