gpt4 book ai didi

ios - 使用 openssl/x509.h 解析证书

转载 作者:行者123 更新时间:2023-11-29 04:09:27 26 4
gpt4 key购买 nike

我正在使用 #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 来满足我的下一个请求,我必须将这些名称附加到我的请求中。

提前致谢

最佳答案

我不知道 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);

关于ios - 使用 openssl/x509.h 解析证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14645779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com