Working with certificates

This sample demonstrates certificate parsing and field access (including extension access).

Supported Languages

C++

void WorkingWithCertificates()
{
      //This function demonstrates certificate parsing and field access (including
      //extension access).

      CPKIFCertificate cert;
      cert.Decode(userCertBuf, userCertBufLen); 

      CPKIFNamePtr subjectName = cert.Subject();
      cout << "Certificate issued to: " << subjectName->ToString() << endl;     

      CPKIFNamePtr issuerName = cert.Issuer();
      cout << "Certificate issued by: " << issuerName->ToString() << endl; 

      bool isCA = false;
      CPKIFBasicConstraintsPtr bc = cert.GetExtension<CPKIFBasicConstraints>();
      if(bc != (CPKIFBasicConstraints*)NULL)
      {
            //certificate has basic constraints extension
            if(bc->isCA())
                  isCA = true;                 
     
} 

      if(isCA)
            cout << "Subject is a CA" << endl;
      else
            cout << "Subject is not a CA" << endl; 

      CPKIFSubjectKeyIdentifierPtr skid = cert.GetExtension<CPKIFSubjectKeyIdentifier>();
      if(skid != (CPKIFSubjectKeyIdentifier*)NULL)
      {
            //certificate has subject key identifier extension
            CPKIFBufferPtr skidBuf = skid->KeyIdentifier();
      }
}

C#

public void WorkingWithCertificates()
{
    //This function demonstrates certificate parsing and field access (including
    //extension access).
 

    CPKIFCertificatePtr cert = pkif_module.make_CPKIFCertificatePtr();
    cert.Decode(userCertBuf, userCertBuf.Length);
 

    CPKIFNamePtr subjectName = cert.Subject();
    Console.WriteLine("Certificate issued to: " + subjectName.ToString());    

    CPKIFNamePtr issuerName = cert.Issuer();
    Console.WriteLine("Certificate issued by: " + issuerName.ToString()); 

    bool isCA = false;
    CPKIFBasicConstraintsPtr bc = pkif_module.Get_BasicConstraintsExtensionFromCert(cert);
    if(bc.get() != null)
    {
        //certificate has basic constraints extension
        if(bc.isCA())
              isCA = true;               
   
} 

    if(isCA)
        Console.WriteLine("Subject is a CA");
    else
        Console.WriteLine("Subject is not a CA"); 

    CPKIFSubjectKeyIdentifierPtr skid = pkif_module.Get_SubjectKeyIdentifierExtensionFromCert(cert);
    if(skid.get() != null)
    {
        //certificate has subject key identifier extension
        CPKIFBufferPtr skidBuf = skid.KeyIdentifier();
    }
}

JAVA

    
public void WorkingWithCertificates()
{
   
//This function demonstrates certificate parsing and field access (including
    //extension access). 

    CPKIFCertificatePtr cert = pkif_module.make_CPKIFCertificatePtr();
    cert.Decode(IntArrayToByteArray(
userCertBuf), userCertBuf.length); 

    CPKIFNamePtr subjectName = cert.Subject();
    System.
out.println("Certificate issued to: " + subjectName.ToString());     

    CPKIFNamePtr issuerName = cert.Issuer();
    System.
out.println("Certificate issued by: " + issuerName.ToString()); 

    boolean isCA = false;
    CPKIFBasicConstraintsPtr bc = pkif_module.Get_BasicConstraintsExtensionFromCert(cert);
   
if(bc.get() != null)
    {
       
//certificate has basic constraints extension
        if(bc.isCA())
              isCA =
true;               
   
}
 

    if(isCA)
        System.
out.println("Subject is a CA");
   
else
        System.out.println("Subject is not a CA"); 

    CPKIFSubjectKeyIdentifierPtr skid = pkif_module.Get_SubjectKeyIdentifierExtensionFromCert(cert);
   
if(skid.get() != null)
    {
       
//certificate has subject key identifier extension
        CPKIFBufferPtr skidBuf = skid.KeyIdentifier();
    }
}