pk_verify.c 4.0 KB

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