rsa_verify_pss.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * RSASSA-PSS/SHA-256 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_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
  63. !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
  64. !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  65. !defined(MBEDTLS_CTR_DRBG_C)
  66. int main( void )
  67. {
  68. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
  69. "MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
  70. "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
  71. "MBEDTLS_CTR_DRBG_C not defined.\n");
  72. mbedtls_exit( 0 );
  73. }
  74. #else
  75. #include "mbedtls/md.h"
  76. #include "mbedtls/pem.h"
  77. #include "mbedtls/pk.h"
  78. #include "mbedtls/md.h"
  79. #include "mbedtls/x509.h"
  80. #include <stdio.h>
  81. #include <string.h>
  82. int main( int argc, char *argv[] )
  83. {
  84. FILE *f;
  85. int ret = 1;
  86. int exit_code = MBEDTLS_EXIT_FAILURE;
  87. size_t i;
  88. mbedtls_pk_context pk;
  89. unsigned char hash[32];
  90. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  91. char filename[512];
  92. mbedtls_pk_init( &pk );
  93. if( argc != 3 )
  94. {
  95. mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
  96. #if defined(_WIN32)
  97. mbedtls_printf( "\n" );
  98. #endif
  99. goto exit;
  100. }
  101. mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
  102. fflush( stdout );
  103. if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
  104. {
  105. mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
  106. mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
  107. goto exit;
  108. }
  109. if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
  110. {
  111. mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
  112. goto exit;
  113. }
  114. mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
  115. /*
  116. * Extract the RSA signature from the file
  117. */
  118. mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
  119. if( ( f = fopen( filename, "rb" ) ) == NULL )
  120. {
  121. mbedtls_printf( "\n ! Could not open %s\n\n", filename );
  122. goto exit;
  123. }
  124. i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
  125. fclose( f );
  126. /*
  127. * Compute the SHA-256 hash of the input file and
  128. * verify the signature
  129. */
  130. mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
  131. fflush( stdout );
  132. if( ( ret = mbedtls_md_file(
  133. mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
  134. argv[2], hash ) ) != 0 )
  135. {
  136. mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  137. goto exit;
  138. }
  139. if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
  140. buf, i ) ) != 0 )
  141. {
  142. mbedtls_printf( " failed\n ! mbedtls_pk_verify returned %d\n\n", ret );
  143. goto exit;
  144. }
  145. mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
  146. exit_code = MBEDTLS_EXIT_SUCCESS;
  147. exit:
  148. mbedtls_pk_free( &pk );
  149. #if defined(_WIN32)
  150. mbedtls_printf( " + Press Enter to exit this program.\n" );
  151. fflush( stdout ); getchar();
  152. #endif
  153. mbedtls_exit( exit_code );
  154. }
  155. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
  156. MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */