pk_verify.c 4.0 KB

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