Creating signed messages


The following code samples demonstrate creation of a signed CMS message using functionality from PKIFv2 

Supported Languages

C++

void CreatingSignedMessages()
{
      //Create a mediator object
      IPKIFMediatorPtr mediator = MakeDefaultMediator(); 

      //Create a SignedData object and pass the mediator to it
      CPKIFSignedData signedData;
      signedData.AddMediator(mediator); 

      //Create a signer info object and populate it with the selected credential
      //(the hash alg will be set to sha1 by default).

      IPKIFCryptoKeyIDOperations* iKIDO = mediator->GetMediator<IPKIFCryptoKeyIDOperations>();
      CPKIFCredentialList creds;
      std::bitset<9> ku = DigitalSignature | NonRepudiation;
      iKIDO->GetKeyList(creds, &ku); 

      bool addAnother = false;
      do
      {
            cout << "The following " << creds.size() << " credentials are available for signature generation purposes" << endl;
            CPKIFCredentialList::iterator pos = creds.begin();
            CPKIFCredentialList::iterator end = creds.end();
            for(int slot = 0; pos != end; ++pos)
            {
                  cout << slot++ << ": Name - " << (*pos)->Name() << "  ID: " << (*pos)->ID() << endl;
            } 

            int selection = -1;
            do
            {
                  cout << "Enter the number corresponding to the credential with which you would like to sign: ";
                  cin >> selection;
                  if(selection > creds.size())
                        cout << "ERROR: invalid selection" << endl;
                  else
                  {
                        CPKIFSignerInfoPtr si(new CPKIFSignerInfo);
                        si->SetCredential(creds[selection]);
                        signedData.AddSignerInfo(si); 

                        if(SolicitBool("Include signer's certificate in message"))
                              signedData.AddCertificate(creds[selection]->GetCertificate());
                        break;
                  }
            }while(1);
            addAnother = SolicitBool("Would you like to add another signer");
      }while(addAnother); 

      //Create an encapsulated info object and populate it with the buffer to sign
      //(the content type oid will be set to g_data by default).
      CPKIFEncapsulatedContentInfoPtr ecip(new CPKIFEncapsulatedContentInfo);
      CPKIFBufferPtr dataToSign(new CPKIFBuffer((unsigned char*)g_buf, g_bufSize));
      ecip->SetContent(dataToSign); 

      //Pass the encapsulated data and signer info to the signed data object then
      //include the certificate from the selected credential in the message.
      signedData.SetEncapsulatedContent(ecip);

      //Generate the SignedData message
      CPKIFBufferPtr tmpBufSignedData;

      try
      {
            //The Encode function will generate the DER encoded SignedData
            //object (including generation of signatures for each SignerInfo)
            tmpBufSignedData = signedData.Encode(); 

            //Prepare an outer ContentInfo wrapper then store the encoded result
            //in the global g_signedDataBuffer variable for use in the
            //VerifyingSignedMessages function.  Set the content type using the
            //global g_signedData declared in ToolkitUtils.h.
            CPKIFContentInfo contentInfo;
            contentInfo.SetContentType(g_signedData);
            contentInfo.SetContent(tmpBufSignedData);
            g_signedDataBuffer = contentInfo.Encode();
      }
      catch(CPKIFException& e)
      {
            cout << "Unexpected exception thrown by CreatingSignedMessages: ";
            cout << e.print()->c_str() << endl; 

            return;
      }
}

C#

public void CreatingSignedMessages()
{
    //Create a mediator object
    IPKIFColleaguePtr mediator = pkif_module.MakeDefaultMediator(); 

    //Create a SignedData object and pass the mediator to it
    CPKIFSignedData signedData = new CPKIFSignedData();
    signedData.AddMediator(mediator); 

    //Create a signer info object and populate it with the selected credential
    //(the hash alg will be set to sha1 by default).
 

    IPKIFCryptoKeyIDOperations iKIDO = pkif_module.Get_IPKIFCryptoKeyIDOperations(mediator);
    CPKIFCredentialList creds = new CPKIFCredentialList();
    CPKIFKeyUsagePtr keyUsage = pkif_module.make_CPKIFKeyUsagePtr();
    keyUsage.SetNonRepudiation();
    keyUsage.SetDigitalSignature();
    iKIDO.GetKeyList(creds, keyUsage); 

    bool addAnother = false;
    do
    {
        Console.WriteLine("The following " + creds.Count + " credentials are available for signature generation purposes");
        for (int ii = 0; ii < creds.Count; ii++)
        {
            Console.WriteLine(ii + ": Name - " + creds[ii].Name() + "  ID: " + creds[ii].ID());
        }

        int selection = -1;
        do
        {
            Console.Write("Enter the number corresponding to the credential with which you would like to sign: ");

            string selectionStr = Console.ReadLine();
            selection = Convert.ToInt32(selectionStr);

            if (selection > creds.Count)
                Console.WriteLine("ERROR: invalid selection");
            else
            {
                CPKIFSignerInfoPtr si = pkif_module.make_CPKIFSignerInfoPtr();
                si.SetCredential(creds[selection]);
                signedData.AddSignerInfo(si); 

                if (SolicitBool("Include signer's certificate in message"))
                    signedData.AddCertificate(creds[selection].GetCertificate());
                break;
            }
        } while (true); 

        addAnother = SolicitBool("Would you like to add another signer");
    } while (addAnother);

    CPKIFEncapsulatedContentInfoPtr ecip = pkif_module.make_CPKIFEncapsulatedContentInfoPtr();
    CPKIFBufferPtr dataToSign = pkif_module.StringToBuffer(g_buf);
    ecip.SetContent(dataToSign); 

    //Pass the encapsulated data and signer info to the signed data object then
    //include the certificate from the selected credential in the message.
    signedData.SetEncapsulatedContent(ecip); 

    //Generate the SignedData message
    CPKIFBufferPtr tmpBufSignedData = pkif_module.make_CPKIFBufferPtr();

    try
    {
        //The Encode function will generate the DER encoded SignedData
        //object (including generation of signatures for each SignerInfo)
        tmpBufSignedData = signedData.Encode(); 

        //Prepare an outer ContentInfo wrapper then store the encoded result
        //in the global g_signedDataBuffer variable for use in the
        //VerifyingSignedMessages function.  Set the content type using the
        //global g_signedData declared in ToolkitUtils.h.
        CPKIFContentInfo contentInfo = new CPKIFContentInfo();
        contentInfo.SetContentType(pkif_module.g_signedData);
        contentInfo.SetContent(tmpBufSignedData);
        g_signedDataBuffer = contentInfo.Encode();
    }
    catch (Exception e)
    {
        Console.WriteLine("Unexpected exception thrown by CreatingSignedMessages: ");
        Console.WriteLine(e.Message);
        return;
    }
}

JAVA

public void CreatingSignedMessages()
{
   
//Create a mediator object
    IPKIFColleaguePtr mediator = pkif_module.MakeDefaultMediator(); 

    //Create a SignedData object and pass the mediator to it
    CPKIFSignedData signedData = new CPKIFSignedData();
    signedData.AddMediator(mediator);

    //Create a signer info object and populate it with the selected credential 
    //(the hash alg will be set to sha1 by default). 

    IPKIFCryptoKeyIDOperations iKIDO = pkif_module.Get_IPKIFCryptoKeyIDOperations(mediator);
    CPKIFCredentialList creds =
new CPKIFCredentialList();
    CPKIFKeyUsagePtr keyUsage = pkif_module.make_CPKIFKeyUsagePtr();
    keyUsage.SetNonRepudiation();
    keyUsage.SetDigitalSignature();
    iKIDO.GetKeyList(creds, keyUsage);
 

    boolean addAnother = false;
   
do
    {
        System.
out.println("The following " + creds.size() + " credentials are available for signature generation purposes"); 

        for (int ii = 0; ii < creds.size(); ii++)
        {
            System.
out.println(ii + ": Name - " + creds.get(ii).Name() + "  ID: " + creds.get(ii).ID());
        }
 

        int selection = -1;
       
do
        {
            System.
out.print("Enter the number corresponding to the credential with which you would like to sign: "); 

            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String selectionStr =
"";
           
try
            {
                  selectionStr = in.readLine();

            }catch(IOException e)
            {
                  System.
out.println("Error reading user input");
                 
try
                {
                  in.close();                 

                }catch(IOException k)
                {
                  System.
out.println("Error closing BufferedReader");

                }
            }
            selection = Integer.parseInt(selectionStr);
           
if (selection > creds.size())
                System.
out.println("ERROR: invalid selection");
           
else
            {
                CPKIFSignerInfoPtr si = pkif_module.make_CPKIFSignerInfoPtr();
                si.SetCredential(creds.get(selection));
                signedData.AddSignerInfo(si);
 

                if (SolicitBool("Include signer's certificate in message"))
                    signedData.AddCertificate(creds.get(selection).GetCertificate());
 

                break;
            }
        }
while (true); 

        addAnother = SolicitBool("Would you like to add another signer");
    }
while (addAnother); 

    CPKIFEncapsulatedContentInfoPtr ecip = pkif_module.make_CPKIFEncapsulatedContentInfoPtr();
    CPKIFBufferPtr dataToSign = pkif_module.StringToBuffer(
g_buf);
    ecip.SetContent(dataToSign);
 

    //Pass the encapsulated data and signer info to the signed data object then
    //include the certificate from the selected credential in the message.
    signedData.SetEncapsulatedContent(ecip); 

    //Generate the SignedData message
    CPKIFBufferPtr tmpBufSignedData = pkif_module.make_CPKIFBufferPtr();

    try
    {
       
//The Encode function will generate the DER encoded SignedData
        //object (including generation of signatures for each SignerInfo)
        tmpBufSignedData = signedData.Encode();
 

        //Prepare an outer ContentInfo wrapper then store the encoded result
        //in the global g_signedDataBuffer variable for use in the
        //VerifyingSignedMessages function.  Set the content type using the
        //global g_signedData declared in ToolkitUtils.h.

        CPKIFContentInfo contentInfo = new CPKIFContentInfo();
        contentInfo.SetContentType(pkif_module.getG_signedData());
        contentInfo.SetContent(tmpBufSignedData);
       
g_signedDataBuffer = contentInfo.Encode();
    }
   
catch (Exception e)
    {
        System.
out.println("Unexpected exception thrown by CreatingSignedMessages: ");
        System.
out.println(e.getMessage());

        return;
    }
}