pk_verify.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Public key-based signature verification program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_snprintf snprintf
  57. #define mbedtls_printf printf
  58. #define mbedtls_exit exit
  59. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  60. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
  63. !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
  64. !defined(MBEDTLS_FS_IO)
  65. int main( void )
  66. {
  67. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
  68. "MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
  69. "MBEDTLS_FS_IO not defined.\n");
  70. mbedtls_exit( 0 );
  71. }
  72. #else
  73. #include "mbedtls/error.h"
  74. #include "mbedtls/md.h"
  75. #include "mbedtls/pk.h"
  76. #include <stdio.h>
  77. #include <string.h>
  78. int main( int argc, char *argv[] )
  79. {
  80. FILE *f;
  81. int ret = 1;
  82. int exit_code = MBEDTLS_EXIT_FAILURE;
  83. size_t i;
  84. mbedtls_pk_context pk;
  85. unsigned char hash[32];
  86. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  87. char filename[512];
  88. mbedtls_pk_init( &pk );
  89. if( argc != 3 )
  90. {
  91. mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
  92. #if defined(_WIN32)
  93. mbedtls_printf( "\n" );
  94. #endif
  95. goto exit;
  96. }
  97. mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
  98. fflush( stdout );
  99. if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
  100. {
  101. mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
  102. goto exit;
  103. }
  104. /*
  105. * Extract the signature from the file
  106. */
  107. mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
  108. if( ( f = fopen( filename, "rb" ) ) == NULL )
  109. {
  110. mbedtls_printf( "\n ! Could not open %s\n\n", filename );
  111. goto exit;
  112. }
  113. i = fread( buf, 1, sizeof(buf), f );
  114. fclose( f );
  115. /*
  116. * Compute the SHA-256 hash of the input file and
  117. * verify the signature
  118. */
  119. mbedtls_printf( "\n . Verifying the SHA-256 signature" );
  120. fflush( stdout );
  121. if( ( ret = mbedtls_md_file(
  122. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  123. argv[2], hash ) ) != 0 )
  124. {
  125. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  126. goto exit;
  127. }
  128. if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
  129. buf, i ) ) != 0 )
  130. {
  131. mbedtls_printf( " failed\n ! mbedtls_pk_verify returned -0x%04x\n", -ret );
  132. goto exit;
  133. }
  134. mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
  135. exit_code = MBEDTLS_EXIT_SUCCESS;
  136. exit:
  137. mbedtls_pk_free( &pk );
  138. #if defined(MBEDTLS_ERROR_C)
  139. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  140. {
  141. mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
  142. mbedtls_printf( " ! Last error was: %s\n", buf );
  143. }
  144. #endif
  145. #if defined(_WIN32)
  146. mbedtls_printf( " + Press Enter to exit this program.\n" );
  147. fflush( stdout ); getchar();
  148. #endif
  149. mbedtls_exit( exit_code );
  150. }
  151. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
  152. MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */