2013-02-01 181 views
0

我正在使用#import類分析證書,並獲得到期日期和現在用的是下面的函數解析證書使用OpenSSL的/ x509.h

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:
       //             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爲我的下一個請求唱歌,我必須將這些名稱附加到我的請求中。

在此先感謝

回答

0

我不知道有關的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); 
+0

感謝您的更新,我得到它如何通過你的代碼片段來做到這一點 – user564963