【问题标题】:Get email address (part of Subject Alternative Name) from a X509_Extension with openSSL使用 openSSL 从 X509_Extension 获取电子邮件地址(主题备用名称的一部分)
【发布时间】:2015-07-03 22:47:56
【问题描述】:

我有一个 iOS 应用程序尝试使用 openSSL 从证书中检索 SAN。我被困在我可以正确获取 X509_Extension 的部分(我可以检查 X509_Extension 中的数据,它包含电子邮件地址,混合在一堆其他数据中)。我不知道如何从 X509_Extension 中提取电子邮件地址。

有人可以帮忙吗?谢谢。以下是我的代码:

int loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
if (loc >= 0) {
    X509_EXTENSION * ext = X509_get_ext(cert, loc);
    //How to extract the email address from the ext?
}

【问题讨论】:

    标签: ios iphone openssl


    【解决方案1】:

    this thread启发,我终于解决了。飞:

    int loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
    
    if (loc >= 0) {
    
        X509_EXTENSION * ext = X509_get_ext(cert, loc);
    
        BUF_MEM *bptr = NULL;
    
        char *buf = NULL;
    
        BIO *bio = BIO_new(BIO_s_mem());
    
        if(!X509V3_EXT_print(bio, ext, 0, 0)){
    
            // error handling...
    
        }
    
        BIO_flush(bio);
    
        BIO_get_mem_ptr(bio, &bptr);
    
    
    
        // now bptr contains the strings of the key_usage, take
    
        // care that bptr->data is NOT NULL terminated, so
    
        // to print it well, let's do something..
    
        buf = (char *)malloc( (bptr->length + 1)*sizeof(char) );
    
    
    
        memcpy(buf, bptr->data, bptr->length);
    
        buf[bptr->length] = '\0';
    
    
    
        NSString *email = [NSString stringWithUTF8String:buf];
    
        NSRange r1 = [email rangeOfString:@"email:"];
    
        NSRange r2 = [email rangeOfString:@","];
    
        NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
    
        NSString *email = [email substringWithRange:rSub];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2012-07-24
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多