pk_verify.c 3.4 KB

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