home *** CD-ROM | disk | FTP | other *** search
- /* Xceed Encryption Library - RSAVerify Sample Application
- * Copyright (c) 2001 Xceed Software Inc.
- *
- * [RSAVerify.cpp]
- *
- * This console application shows how to verify a file or a message.
- * It specifically demonstrates:
- * - The ReadFile and Verify methods.
- * - The Signature, PublicKey and SigningMethod properties.
- *
- * [Example]
- *
- * To verify a signature "HELLO.SIG" against the allegedly source
- * file "HELLO.BMP" using the public key stored in hexadecimal
- * in file "PUBLIC.TXT":
- * RSAVERIFY HELLO.BMP PUBLIC.TXT HELLO.SIG
- *
- * This file is part of the Xceed Encryption Library sample
- * applications. The source code in this file is only intended as
- * a supplement to Xceed Encryption Library's documentation,
- * and is provided "as is", without warranty of any kind, either
- * expressed or implied.
- */
-
- #include "stdafx.h"
- #include "RSAVerify.h"
-
- #include "utility.h"
-
- //
- // Entry point of the application
- //
-
- int main(int argc, char* argv[])
- {
- CoInitialize( NULL );
-
- try
- {
- // Create an instance of the XceedSigning coclass, and
- // use the "I" interface which manipulates byte arrays instead of
- // Variants as the "D" interface does.
- IXceedSigningPtr piSign;
- piSign.CreateInstance( CLSID_XceedSigning );
-
- // Two BSTR variables that will contain the Input and Signature file names.
- _bstr_t bstrInputFileName;
-
- // Extract the command line parameters and initialize the Signing
- // instance according to the user specification. After this call
- // the Signing instance piSign is ready to verify.
- if( !ExtractParameters( argc, argv,
- piSign,
- bstrInputFileName ) )
- {
- // There's been an error extracting the command-line parameters or the
- // user requested some help.
- ShowHelp();
- return 1;
- }
-
- if( bstrInputFileName.length() == 0 )
- {
- // An input file name was not provided by the user.
- // Get the source from the console
-
- printf( "Verifying from the console input. Press Ctrl-Z and Enter when done.\n\n" );
-
- char pcBuffer[ BUFFER_SIZE ];
-
- while( !feof( stdin ) )
- {
- // Read from the console BUFFER_SIZE characters at a time.
- int nRead = fread( pcBuffer, sizeof( char ), BUFFER_SIZE, stdin );
-
- if( nRead )
- {
- // Verify the buffer read so far.
- // We specify :
- // - The source buffer, with the size, since we want
- // to verify all the source buffer.
- // - The bEndOfData parameter set to FALSE, since we do the
- // processing in multiple block.
- piSign->Verify( ( BYTE* )pcBuffer, nRead, FALSE, NULL );
- }
- }
-
- long lIdentical;
-
- // Since we always called with bEndOfData to FALSE, we must
- // make sure to flush the remaining data.
- piSign->Verify( NULL, 0, TRUE, &lIdentical );
-
- if( lIdentical == FALSE)
- {
- printf( "Verify failed\n" );
- }
- else
- {
- printf( "Verify passed\n" );
- }
- }
- else
- {
- // An input file name was provided by the user.
- // Read the input file and verify
-
- DWORD dwBytesRead = 0;
- DWORD dwBytesWritten = 0;
-
- // Verify by reading a file and sending the Signature to another file.
- // We specify :
- // - The source filename, without any offset or size, since we want
- // to verify all the source file.
- // - The processing we want to perform, in this case efpVerify.
- // - The bEndOfData parameter set to TRUE, since we do all the
- // processing in a single block.
- // - The address of a DWORD that will receive the number of bytes
- // actually read from the source.
- piSign->ReadFile( bstrInputFileName, 0, 0, efpVerify, TRUE, &dwBytesRead );
-
- // A failed verification will throw an exception. So, if we are here, it
- // means the verification passed.
- printf( "Verify passed\n" );
- }
- }
- catch( const _com_error& err )
- {
- // When using the "#import" directive, the compiler generates wrapper classes
- // around all interface types. These wrapper classes throw exceptions when
- // a method call returns an HRESULT which is a failure code.
- printf( "Error %08x: %s\n", err.Error(), ( const char* )err.Description() );
- }
- catch( ... )
- {
- // Catch any other exceptions
- printf( "An unknown error occured.\n" );
- }
-
- // Close the COM library for the current thread
- CoUninitialize();
-
- return 0;
- }
-
- //--------------------------------------------------------------------------
- // Display usage information
- //--------------------------------------------------------------------------
- void ShowHelp()
- {
- // "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
- printf( "Usage: RSAVerify [options] [input_file] public_key_file Signature_file\n\n"
- " input_file: the file to verify\n"
- " public_key_file: the file containing the public key in hexadecimal that\n"
- " will be used to verify\n"
- " Signature_file: the Signature source file\n\n"
- " options: /h or /? : Show this help\n\n" );
- }
-
- //--------------------------------------------------------------------------
- // Extract commands from the parameters
- //
- // In this function, we call the piSign interface and let exceptions
- // be caught by the caller.
- // This function returns false if an error occured parsing the command
- // line parameters or if the user requested help.
- //--------------------------------------------------------------------------
- bool ExtractParameters( int argc, char* argv[],
- IXceedSigningPtr piSign,
- _bstr_t& bstrInputFileName )
- {
- _bstr_t bstrPublicKeyFileName;
- _bstr_t bstrSignatureFileName;
- BYTE* pcBuffer = NULL;
- short nBufferSize = 0;
-
- // We parse each command line parameter
- int i = 0;
- while( ++i < argc )
- {
- if( argv[ i ][ 0 ] == '/' )
- {
- // The parameter starts with a /
- // Meaning it's an option parameter
- switch( argv[ i ][ 1 ] )
- {
- case 'h':
- case 'H':
- case '?':
- return false;
- break;
-
- default:
- printf( "Unknown command '%s'\n\n", argv[ i ] );
- return false;
- break;
- }
- }
- }
-
- // Check if the user provided an output file name
- if( argc < 3 || argv[ argc - 1 ][ 0 ] == '/' || argv[ argc - 2 ][ 0 ] == '/' )
- {
- printf( "You did not specify both public key filename and signature filename\n\n" );
- return false;
- }
- else if( argc < 4 || argv[ argc - 3 ][ 0 ] == '/' )
- {
- // Only a public key and signature file were specified
- bstrPublicKeyFileName = argv[ argc - 2 ];
- bstrSignatureFileName = argv[ argc - 1 ];
- }
- else
- {
- // Input, public key and Signature file were specified
- bstrInputFileName = argv[ argc - 3 ];
- bstrPublicKeyFileName = argv[ argc - 2 ];
- bstrSignatureFileName = argv[ argc - 1 ];
- }
-
- IXceedRSASigningMethodPtr piRSA;
- piRSA.CreateInstance( CLSID_XceedRSASigningMethod );
-
- bool bSuccess = true;
-
- // Read the public key from the specified file and initialize the
- // XceedRSASigningMethod's PublicKey property with it.
- bSuccess = ReadHexValueFromFile( bstrPublicKeyFileName, &pcBuffer, &nBufferSize );
- if( bSuccess )
- {
- piRSA->SetPublicKey( pcBuffer, nBufferSize );
- delete [] pcBuffer;
- bSuccess = ReadHexValueFromFile( bstrSignatureFileName, &pcBuffer, &nBufferSize);
- if( bSuccess )
- {
- piRSA->SetSignature( pcBuffer, nBufferSize );
- delete [] pcBuffer;
- piSign->SigningMethod = IXceedSignDataPtr( piRSA );
- }
- }
-
- return bSuccess;
- }
-
- //
- // END_OF_FILE
- //
-