0

我正在使用#import 类来解析证书并获取到期日期和开始日期,我正在使用以下函数

static NSDate *CertificateGetExpiryDate(X509 *certificateX509)
{
    NSDate *expiryDate = nil;

    if (certificateX509 != NULL) {
        ASN1_TIME *certificateExpiryASN1 = X509_get_notAfter(certificateX509);
        if (certificateExpiryASN1 != NULL) {
            ASN1_GENERALIZEDTIME *certificateExpiryASN1Generalized = ASN1_TIME_to_generalizedtime(certificateExpiryASN1, NULL);
            if (certificateExpiryASN1Generalized != NULL) {
                unsigned char *certificateExpiryData = ASN1_STRING_data(certificateExpiryASN1Generalized);

                // ASN1 generalized times look like this: "20131114230046Z"
                //                                format:  YYYYMMDDHHMMSS
                //                               indices:  01234567890123
                //                                                   1111
                // There are other formats (e.g. specifying partial seconds or
                // time zones) but this is good enough for our purposes since
                // we only use the date and not the time.
                //
                // (Source: http://www.obj-sys.com/asn1tutorial/node14.html)

                NSString *expiryTimeStr = [NSString stringWithUTF8String:(char *)certificateExpiryData];
                NSDateComponents *expiryDateComponents = [[NSDateComponents alloc] init];

                expiryDateComponents.year   = [[expiryTimeStr substringWithRange:NSMakeRange(0, 4)] intValue];
                expiryDateComponents.month  = [[expiryTimeStr substringWithRange:NSMakeRange(4, 2)] intValue];
                expiryDateComponents.day    = [[expiryTimeStr substringWithRange:NSMakeRange(6, 2)] intValue];
                expiryDateComponents.hour   = [[expiryTimeStr substringWithRange:NSMakeRange(8, 2)] intValue];
                expiryDateComponents.minute = [[expiryTimeStr substringWithRange:NSMakeRange(10, 2)] intValue];
                expiryDateComponents.second = [[expiryTimeStr substringWithRange:NSMakeRange(12, 2)] intValue];

                NSCalendar *calendar = [NSCalendar currentCalendar];
                expiryDate = [calendar dateFromComponents:expiryDateComponents];

                [expiryDateComponents release];
            }
        }
    }

    return expiryDate;
}

但我想解析证书的全部细节,如公用名、版本、私钥等。

任何人都可以告诉我我怎么能得到那个东西

X509_NAME *issuerX509Name = X509_get_issuer_name(certificateX509); X509_NAME *subjectX509Name = X509_get_subject_name(certificateX509);

使用上述两个函数,我得到了问题名称和主题,但我想将这个东西转换为 Nsstring 格式。

任何人都可以帮助我如何将 X509_NAme 转换为 Nsstring sing 以便我的下一个请求我必须将这些名称附加到我的请求中。

提前致谢

4

1 回答 1

1

我不知道 NSstring,但是您可以通过以下方式从 X509_NAME * 获取 C 样式的字符串:

int const nid = OBJ_txt2nid("commonName");

if (nid != NID_undef)
{
    int result = X509_NAME_get_text_by_NID(issuerX509Name, nid, NULL, 0);

    if (result > 0)
    {
        *cn = malloc((size_t)result + 1);

        if (*cn != NULL)
        {
            result = X509_NAME_get_text_by_NID(issuerX509Name, nid, cn, result+1);
于 2013-02-01T13:51:14.727 回答