【问题标题】:How to decode serial number,issuer information etc from certificate present in keychain under iOS?如何从 iOS 下钥匙串中存在的证书中解码序列号、发行者信息等?
【发布时间】:2014-06-04 11:25:12
【问题描述】:

我正在创建一个 iOS 应用程序,它可以检索钥匙串中存在的证书(.cer)信息。

参考链接: Link1, Link2

下面是代码:

const char *certLabelString = "Certificates";
    CFStringRef certLabel = CFStringCreateWithCString(
                                                      NULL, certLabelString,
                                                      kCFStringEncodingUTF8);

const void *keys[] =   { kSecClass, kSecAttrLabel, kSecReturnAttributes };
    const void *values[] = { kSecClassCertificate, certLabel, kCFBooleanTrue };

CFDictionaryRef dict = CFDictionaryCreate(NULL, keys,
                                              values, 3,
                                              NULL, NULL)
 if ((SecItemCopyMatching(dict, &myCertData)) == errSecSuccess){
            NSLog(@"Certificate found");

            CFDictionaryRef dictCertificateRef = (CFDictionaryRef)myCertData;

            NSDictionary *dictCertificate = (__bridge NSDictionary *)dictCertificateRef;
            NSLog(@"%@",dictCertificate);

        }

输出:

我获得了证书数据,但我可以看到编码形式的序列号或颁发者名称。

像这样: ISSR = ; P>

谁能告诉我如何解码这些信息?

【问题讨论】:

    标签: ios certificate keychain


    【解决方案1】:

    我也有兴趣知道这一点。到目前为止,浏览证书,我做了以下事情:

    id 数据 = [keychainDictionary objectForKey:@"issr"];

    然后你可以在这一行设置一个断点,当你越过它时,在调试窗口左侧面板中选择“数据”变量。选择“watch memory of *data”,你会看到一堆垃圾,里面有来自该数据的真实字符串。我不知道如何从那里开始。

    获取所有钥匙串项并将其加载到表格视图中的完整方法:

    -(void)loadDataSource
    {
    
        //enumerate all items in keychain http://stackoverflow.com/questions/10966969/enumerate-all-keychain-items-in-my-ios-application
        NSMutableDictionary *query = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                      (__bridge id)kCFBooleanTrue, (__bridge id)kSecReturnAttributes,
                                      (__bridge id)kSecMatchLimitAll, (__bridge id)kSecMatchLimit,
                                      nil];
    
        NSArray *secItemClasses = [NSArray arrayWithObjects:
                                   (__bridge id)kSecClassGenericPassword,
                                   (__bridge id)kSecClassInternetPassword,
                                   (__bridge id)kSecClassCertificate,
                                   (__bridge id)kSecClassKey,
                                   (__bridge id)kSecClassIdentity,
                                   nil];
    
    
        NSMutableArray* results = [NSMutableArray array];
    
        for(int i = 0; i < (int)secItemClasses.count;i++)
        {
            [results addObject:[NSMutableArray array]];
        }
    
    
    
        for (id secItemClass in secItemClasses) {
            [query setObject:secItemClass forKey:(__bridge id)kSecClass];
    
            CFTypeRef result = NULL;
            SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
    //        NSLog(@"%@", (__bridge id)result);
            if (result != NULL)
            {
                NSMutableArray* thisSection = results[[secItemClasses indexOfObject:secItemClass]];
                [thisSection addObject:(__bridge id)result];
    
    
    //            [results addObject:(__bridge id)result];
                CFRelease(result);
            }
    
            for(NSArray* object in results[[secItemClasses indexOfObject:secItemClass]])
            {
                DLog(@"object is of class: %@",[[object class] description]);
    
                for (NSDictionary* innerObject in object)
                {
                    DLog(@"object is of class: %@",[[innerObject class] description]);
    
    
                }
    
    
    
    
                }
    
            }
    
        self.datasource = results;
    
        [self.tableView reloadData];
    }
    

    //这是描述,您可以将其分配给表格视图单元格中的文本标签

    -(NSMutableString*)descriptionForObject:(NSDictionary*)object
    {
        NSMutableString* string = [[NSMutableString alloc] initWithCapacity:1024];
    
    //    https://developer.apple.com/library/mac/documentation/security/Reference/keychainservices/Reference/reference.html
        //search for kSecAlias for a list of codes
    
        if(object[@"labl"] != nil)
        {
        [string appendString:[NSString stringWithFormat:@"Label: %@\n",object[@"labl"]]];
        }
    
        [string appendString:[NSString stringWithFormat:@"Created at: %@\n",object[@"cdat"]]];
        if(object[@"agrp"] != nil)
        {
            [string appendString:[NSString stringWithFormat:@"Belongs to application: %@\n",object[@"agrp"]]];
        }
    
    
    
        for(NSString* key in @[@"issr",@"subj"])
        {
            id data = [object objectForKey:key];
    
    
            @try {
    
    
                if([data isKindOfClass:[NSData class]]==NO)
                {
                    continue;
                }
                NSString* stringAscii = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
    
                NSCharacterSet* alphaNumeric = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.@"];
    
    
                NSCharacterSet *doNotWant = [alphaNumeric invertedSet];
                NSString* cleanedUpString = [[stringAscii componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @" "];
    
                if(cleanedUpString.length>0)
                {
                    DLog(@" %@ Cleaned up: %@",key,cleanedUpString);
    
                    [string appendString:[NSString stringWithFormat:@" %@ Cleaned up: %@",key,cleanedUpString]];
                }
            }
            @catch (NSException *exception) {
    
            }
            @finally {
    
            }
        }
    
    
    //    [string appendString:[NSString stringWithFormat:@"Complete description:(%@)\n", [object description]]];
    
    
    
        return string;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2017-05-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多