SOL9 Sample: MessageSignVerify

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * MessageSignVerify.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/03/13

#include <sol/crypt/CryptServiceProvider.h>
#include <sol/crypt/CertStore.h>
#include <sol/crypt/MessageSigner.h>
#include <sol/crypt/MessageVerifier.h>
#include <sol/crypt/CertContext.h>

#define ENCODING_TYPE  (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING)


// Signer name of a certificate. You have to modify this name to fit for your environment. 
#define SIGNER_NAME  L"Antillia"

// Store name for a certificate
#define CERT_STORE_NAME  L"CA"


void _tmain(int argc, TCHAR** argv)
{
  try {
    int rc = 0;

    CryptServiceProvider csp;

    printf("Try to certStore.open(L\"%S\")\n", CERT_STORE_NAME);

    CertStore certStore;
    rc = certStore.open(csp,
      CERT_STORE_PROV_SYSTEM, 
      0, 
      CERT_SYSTEM_STORE_CURRENT_USER, 
      CERT_STORE_NAME);
    if (rc == NO_ERROR) {
      printf("OK, certStore.open(\"%S\")\n", CERT_STORE_NAME);

    } else {
      printf("Failed to open store:%S\n", CERT_STORE_NAME);
      throw rc;
    }

    printf("Find a certificate by subject name:%S\n", SIGNER_NAME);    
      
    PCCERT_CONTEXT pContext = certStore.findSubject(ENCODING_TYPE, 
        0,
        SIGNER_NAME);
    if (pContext) {
      printf("OK, certStore.findSubject(L\"%S\")\n", SIGNER_NAME);
      
    } else {
      printf("Failed to find a certificate of subject:%S\n", SIGNER_NAME);
      throw -1;
    }

    CertContext certContext(pContext);

    TCHAR sbuffer[256];
    certContext.getName(sbuffer, SizeOf(sbuffer)); //2012/06/21
    _tprintf(_T("Cert Subject:%s\n"), sbuffer);

    // Sign the message, and get encoded (signed) messsage    
    unsigned char* encodedBytes = NULL;
    size_t   encodedBytesSize = 0;

    // The message to be signed

    BYTE* message = (BYTE*)"SOL9 MessageSigner and MessageVerifier classes sample program";
    size_t messageSize = strlen((char*)message)+1;    
        
    printf("Try to signer.sign():%s\n", message);

    MessageSigner signer;
    
    rc = signer.sign(pContext, message, messageSize,
      &encodedBytes, &encodedBytesSize);
 
    if (rc == NO_ERROR) {
      printf("OK, singner.sign()\n");
    } else {
      printf("Failed to sign\n");
      throw rc;
    }     

    printf("Try to verifier.verify()\n");

    MessageVerifier verifier;
    unsigned char* decodedBytes = NULL;
    unsigned int   decodedBytesSize = 0;

    rc = verifier.verify(encodedBytes,
        encodedBytesSize,
        &decodedBytes,
        &decodedBytesSize);

    if (rc == NO_ERROR) {
      printf("OK, verifier.verify():%s\n", decodedBytes);
    } else {
      printf("Failed to verifer\\n");
      throw rc;
    }
  } catch (int err) {
    printf("Exception:%x\n", err);    
  }
} 



Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.