Discovering available credentials

The following sample demonstrates usage of PKIF to obtain a list of available credentials.

The key identifier available from a credential can be harvested, saved and used to establish a default key for a specific cryptographic operation (see the CPKIFCryptoMediator2::SetDefaultKey function)

Supported Languages

C++

void DiscoveringAvailableCredentials()
{

      //Create a mediator.  This can be done several ways:
      //    - by calling MakeDefaultMediator,
      //    - by deserializing settings from the registry using
      //          CPathProcessingSettings (from PKIFRESOURCES),
      //    - by creating a mediator/colleague collection manually
      //For purposes of credential discovery, the mediator MUST
      //include a colleague that implements IPKIFCryptoKeyIDOperations.
      IPKIFMediatorPtr mediator = MakeDefaultMediator(); 

      //Query for the IPKIFCryptoKeyIDOperations interface to perform
      //the search for available credentials.
      IPKIFCryptoKeyIDOperations* cKeyID =
            mediator->GetMediator<IPKIFCryptoKeyIDOperations>();

      //Declare a vector to hold the credentials and a bitset
      //to filter credentials based on key usage.  To query for
      //encryption keys set the keyUsage bitset = KeyEncipherment
      CPKIFCredentialList allSignatureKeys;
      bitset<9> keyUsage = DigitalSignature | NonRepudiation; 

      try
      {
            //Invoke the GetKeyList function to retrieve list
            //of credentials with the request key usage.  Alternatively,
            //use the ShowSelectCertDialog from PKIFRESOURCES to solicit
            //a user selection via a dialog box.
            cKeyID->GetKeyList(allSignatureKeys, &keyUsage);
      }
      catch(CPKIFException& e)
      {
            cout << "Unexpected exception thrown by GetKeyList: ";
            cout << e.print()->c_str() << endl;
            return;
      } 

      //Iterate over the list of credentials returned by GetKeyList and
      //output information about each available signature credential
      cout << "Listing available signature credentials:" << endl;

      CPKIFCredentialList::iterator pos;
      CPKIFCredentialList::iterator end = allSignatureKeys.end();
      for(pos = allSignatureKeys.begin(); pos != end; ++pos)
      {
            //output credential and key identifier
            cout << endl << "\tName  : " << (*pos)->Name();
            cout << endl << "\tKey ID: " << (*pos)->ID() << endl;
      }
}

C#

public void DiscoveringAvailableCredentials()
{
    //Create a mediator.  This can be done several ways:
    //      - by calling MakeDefaultMediator,
    //      - by creating a mediator/colleague collection manually
    //For purposes of credential discovery, the mediator MUST
    //include a colleague that implements IPKIFCryptoKeyIDOperations.
    IPKIFColleaguePtr mediator = pkif_module.MakeDefaultMediator(); 

    //Query for the IPKIFCryptoKeyIDOperations interface to perform
    //the search for available credentials.
    IPKIFCryptoKeyIDOperations cKeyID = pkif_module.Get_IPKIFCryptoKeyIDOperations(mediator); 

    //Declare a vector to hold the credentials and a CPKIFKeyUsage
    //to filter credentials based on key usage.  To query for
    //encryption keys set the keyUsage bitset = KeyEncipherment
    CPKIFCredentialList allSignatureKeys = new CPKIFCredentialList();
    CPKIFKeyUsagePtr keyUsage = pkif_module.make_CPKIFKeyUsagePtr();
    keyUsage.SetNonRepudiation();
    keyUsage.SetDigitalSignature(); 

    try
    {
        //Invoke the GetKeyList function to retrieve list
        //of credentials with the request key usage.
        cKeyID.GetKeyList(allSignatureKeys, keyUsage);
    }
    catch (Exception e)
    {
        Console.WriteLine("Unexpected exception thrown by GetKeyList: ");
        Console.WriteLine(e.Message);
        return;
    }

    //Iterate over the list of credentials returned by GetKeyList and
    //output information about each available signature credential
    Console.WriteLine("Listing available signature credentials:"); 

    for (int ii = 0; ii < allSignatureKeys.Count; ii++)
    {
        Console.WriteLine("\tName  : " + allSignatureKeys[ii].Name());
        Console.WriteLine("\tKey ID: " + allSignatureKeys[ii].ID());
    }
}

JAVA

public void DiscoveringAvailableCredentials()
{
   
//Create a mediator.  This can be done several ways:
    //      - by calling MakeDefaultMediator,
    //      - by creating a mediator/colleague collection manually
    //For purposes of credential discovery, the mediator MUST
    //include a colleague that implements IPKIFCryptoKeyIDOperations.
    IPKIFColleaguePtr mediator = pkif_module.MakeDefaultMediator(); 

    //Query for the IPKIFCryptoKeyIDOperations interface to perform
    //the search for available credentials.
    IPKIFCryptoKeyIDOperations cKeyID = pkif_module.Get_IPKIFCryptoKeyIDOperations(mediator); 

    //Declare a vector to hold the credentials and a CPKIFKeyUsage
    //to filter credentials based on key usage.  To query for
    //encryption keys set the keyUsage bitset = KeyEncipherment
    CPKIFCredentialList allSignatureKeys = new CPKIFCredentialList();
    CPKIFKeyUsagePtr keyUsage = pkif_module.make_CPKIFKeyUsagePtr();
    keyUsage.SetNonRepudiation();
    keyUsage.SetDigitalSignature();
 

    try
    {
       
//Invoke the GetKeyList function to retrieve list
        //of credentials with the request key usage.
        cKeyID.GetKeyList(allSignatureKeys, keyUsage);
    }
   
catch (Exception e)
    {
        System.
out.println("Unexpected exception thrown by GetKeyList: ");
        System.
out.println(e.getMessage());
       
return;
    }
 

    //Iterate over the list of credentials returned by GetKeyList and
    //output information about each available signature credential
    System.out.println("Listing available signature credentials:"); 

    for (int ii = 0; ii < allSignatureKeys.size(); ii++)
    {
        System.
out.println("\tName  : " + allSignatureKeys.get(ii).Name());
        System.
out.println("\tKey ID: " + allSignatureKeys.get(ii).ID());
    }
}