Question

I want to extract Issuer CN (Common name) from SSL certificate using openSSL in iOS. I am using the link http://pastebin.com/Vn797Sc0 for extracting the information from the certificate. I am getting the issuer name but could not extract Issuer Common name(Common name) in iOS. Thanks in advance

Was it helpful?

Solution

You can extract the Common Name like the Organization field in the code you linked (Not tested)

static NSString * CertificateGetIssuerCommonName(X509 *certificateX509) {
    NSString *issuerCN = nil;
    if (certificateX509 != NULL) {
        X509_NAME *issuerX509Name = X509_get_issuer_name(certificateX509);
        if (issuerX509Name != NULL) {
            int nid = OBJ_txt2nid("CN");
            int index = X509_NAME_get_index_by_NID(issuerX509Name, nid, -1);
            if (index != -1) {
                X509_NAME_ENTRY *issuerNameCommonName = X509_NAME_get_entry(issuerX509Name, index);

                if (issuerNameEntry) {
                    ASN1_STRING *issuerCNASN1 = X509_NAME_ENTRY_get_data(issuerNameEntry);

                    if (issuerCNASN1 != NULL) {
                        unsigned char *issuerCName = ASN1_STRING_data(issuerCNASN1);
                        issuerCN = [NSString stringWithUTF8String:(char *)issuerCName];
                    }
                }
            }
        }
    }

    return issuerCN;
}

OTHER TIPS

static NSString * CertificateGetIssuerName(X509 *certificateX509)
{
    NSString *issuer = nil;
    if (certificateX509 != NULL) {
        X509_NAME *issuerX509Name = X509_get_issuer_name(certificateX509);

        if (issuerX509Name != NULL) {
             //NID_commonName extract the common name of the issuer
            int index = X509_NAME_get_index_by_NID(issuerX509Name, NID_commonName, -1);

            X509_NAME_ENTRY *issuerNameEntry = X509_NAME_get_entry(issuerX509Name, index);

            if (issuerNameEntry) {
                ASN1_STRING *issuerNameASN1 = X509_NAME_ENTRY_get_data(issuerNameEntry);

                if (issuerNameASN1 != NULL) {
                    unsigned char *issuerName = ASN1_STRING_data(issuerNameASN1);
                    issuer = [NSString stringWithUTF8String:(char *)issuerName];
                }
            }
        }
    }

    return issuer;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top