문제

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

도움이 되었습니까?

해결책

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;
}

다른 팁

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top