how to get the Keyusage value from the X509 certificate?

后端 未结 3 1043
日久生厌
日久生厌 2020-12-21 14:22

I want to retrieve the Key usage value from the X509 structured certificate , i tried the following code

 X509* lcert=NULL;
 lCert=PEM_read(filename); // fun         


        
3条回答
  •  北海茫月
    2020-12-21 14:57

    I used the below code to get the Key usage value . Method 1;

       //iCertificate is in X509 format
       ASN1_BIT_STRING* lASN1UsageStr;
       lASN1UsageStr=(ASN1_BIT_STRING *)X509_get_ext_d2i(iCertificate,NID_key_usage,NULL,NULL);
        if(lASN1UsageStr == NULL)
        {
            cout<<" get ext_d2i function returns errors";
        }
        else if(lASN1UsageStr->length > 0) 
        {
            lKeyUsage = lASN1UsageStr->data[0];
            if(lASN1UsageStr->length > 1)
            { 
                   lKeyUsage |= lASN1UsageStr->data[1] << 8;
            }// else{}     
        } else 
        {
            lKeyUsage = -1;    //invalid keyusage
        }                
    

    method 2:

         X509_check_ca(lcert) ;       
         //need to call before the 
         unsigned long lKeyusage= lCert->ex_kusage;
    

提交回复
热议问题