pk_verify.c 3.8 KB

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