/pCloudCC/lib/mbedtls/programs/pkey/rsa_verify_pss.c

https://github.com/pcloudcom/console-client · C · 156 lines · 99 code · 28 blank · 29 comment · 16 complexity · 64ce888357eeea2f023c3c57ea32b85d MD5 · raw file

  1. /*
  2. * RSASSA-PSS/SHA-1 signature verification program
  3. *
  4. * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
  5. *
  6. * This file is part of mbed TLS (https://polarssl.org)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #if !defined(POLARSSL_CONFIG_FILE)
  23. #include "polarssl/config.h"
  24. #else
  25. #include POLARSSL_CONFIG_FILE
  26. #endif
  27. #if defined(POLARSSL_PLATFORM_C)
  28. #include "polarssl/platform.h"
  29. #else
  30. #define polarssl_printf printf
  31. #endif
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include "polarssl/md.h"
  35. #include "polarssl/pem.h"
  36. #include "polarssl/pk.h"
  37. #include "polarssl/sha1.h"
  38. #include "polarssl/x509.h"
  39. #if defined _MSC_VER && !defined snprintf
  40. #define snprintf _snprintf
  41. #endif
  42. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  43. !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_PK_PARSE_C) || \
  44. !defined(POLARSSL_FS_IO)
  45. int main( int argc, char *argv[] )
  46. {
  47. ((void) argc);
  48. ((void) argv);
  49. polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  50. "POLARSSL_SHA1_C and/or POLARSSL_PK_PARSE_C and/or "
  51. "POLARSSL_FS_IO not defined.\n");
  52. return( 0 );
  53. }
  54. #else
  55. int main( int argc, char *argv[] )
  56. {
  57. FILE *f;
  58. int ret = 1;
  59. size_t i;
  60. pk_context pk;
  61. unsigned char hash[20];
  62. unsigned char buf[POLARSSL_MPI_MAX_SIZE];
  63. char filename[512];
  64. pk_init( &pk );
  65. if( argc != 3 )
  66. {
  67. polarssl_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
  68. #if defined(_WIN32)
  69. polarssl_printf( "\n" );
  70. #endif
  71. goto exit;
  72. }
  73. polarssl_printf( "\n . Reading public key from '%s'", argv[1] );
  74. fflush( stdout );
  75. if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
  76. {
  77. polarssl_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
  78. polarssl_printf( " ! pk_parse_public_keyfile returned %d\n\n", ret );
  79. goto exit;
  80. }
  81. if( !pk_can_do( &pk, POLARSSL_PK_RSA ) )
  82. {
  83. ret = 1;
  84. polarssl_printf( " failed\n ! Key is not an RSA key\n" );
  85. goto exit;
  86. }
  87. rsa_set_padding( pk_rsa( pk ), RSA_PKCS_V21, POLARSSL_MD_SHA1 );
  88. /*
  89. * Extract the RSA signature from the text file
  90. */
  91. ret = 1;
  92. snprintf( filename, 512, "%s.sig", argv[2] );
  93. if( ( f = fopen( filename, "rb" ) ) == NULL )
  94. {
  95. polarssl_printf( "\n ! Could not open %s\n\n", filename );
  96. goto exit;
  97. }
  98. i = fread( buf, 1, POLARSSL_MPI_MAX_SIZE, f );
  99. fclose( f );
  100. /*
  101. * Compute the SHA-1 hash of the input file and compare
  102. * it with the hash decrypted from the RSA signature.
  103. */
  104. polarssl_printf( "\n . Verifying the RSA/SHA-1 signature" );
  105. fflush( stdout );
  106. if( ( ret = sha1_file( argv[2], hash ) ) != 0 )
  107. {
  108. polarssl_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
  109. goto exit;
  110. }
  111. if( ( ret = pk_verify( &pk, POLARSSL_MD_SHA1, hash, 0,
  112. buf, i ) ) != 0 )
  113. {
  114. polarssl_printf( " failed\n ! pk_verify returned %d\n\n", ret );
  115. goto exit;
  116. }
  117. polarssl_printf( "\n . OK (the decrypted SHA-1 hash matches)\n\n" );
  118. ret = 0;
  119. exit:
  120. pk_free( &pk );
  121. #if defined(_WIN32)
  122. polarssl_printf( " + Press Enter to exit this program.\n" );
  123. fflush( stdout ); getchar();
  124. #endif
  125. return( ret );
  126. }
  127. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
  128. POLARSSL_PK_PARSE_C && POLARSSL_FS_IO */