priore

Get informations of the certificate (OS X)

Feb 22nd, 2015
1,445
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // openssl x509 -inform der -outform pem -in public_key.der -out public_key.pem
  2. // openssl pkcs12 -export -in public_key.pem -inkey private_key.pem -out private_key.p12
  3.  
  4. NSDictionary *info = [NSDictionary new];
  5. [self getX509InfoWithName:@"your-cert.p12" password:@"cert-password" info:&info];
  6. NSLog(@"X509 Infos: %@", info);
  7.  
  8. #pragma mark - X509
  9.  
  10. - (void)getX509InfoWithName:(NSString*)certificateName password:(NSString*)password info:(NSDictionary**)info
  11. {
  12.     NSString *p12Path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:certificateName];
  13.     NSData *p12Data = [[NSData alloc] initWithContentsOfFile:p12Path];
  14.    
  15.     CFStringRef p12Password = (__bridge CFStringRef)password;
  16.     const void *keys[] = { kSecImportExportPassphrase };
  17.     const void *values[] = { p12Password };
  18.     CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
  19.     CFArrayRef p12Items;
  20.     OSStatus result = SecPKCS12Import((__bridge CFDataRef)p12Data, optionsDictionary, &p12Items);
  21.     CFRelease(optionsDictionary);
  22.    
  23.     if(result == noErr) {
  24.         NSDictionary *item = (NSDictionary*)CFArrayGetValueAtIndex(p12Items, 0);
  25.         SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex((__bridge CFArrayRef)[item objectForKey:(__bridge NSString*)kSecImportItemCertChain], 0);
  26.        
  27.         *info = [self dictionaryFromCerificateWithLongDescription:cert];
  28.     }
  29. }
  30.  
  31. - (NSDictionary*)dictionaryFromCerificateWithLongDescription:(SecCertificateRef)certificateRef {
  32.    
  33.     NSMutableDictionary *dict = [NSMutableDictionary new];
  34.    
  35.     if (certificateRef == NULL)
  36.         return dict;
  37.  
  38. #if TARGET_OS_MAC
  39.    
  40.     const void *keys[] = { kSecOIDX509V1SubjectName, kSecOIDX509V1IssuerName, kSecOIDX509V1SerialNumber, kSecOIDX509V1Signature };
  41.     CFArrayRef keySelection = CFArrayCreate(NULL, keys , sizeof(keys)/sizeof(keys[0]), &kCFTypeArrayCallBacks);
  42.    
  43.     CFErrorRef error;
  44.     CFDictionaryRef vals = SecCertificateCopyValues(certificateRef, keySelection, &error);
  45.    
  46.     for(int i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
  47.         CFDictionaryRef dict_values = CFDictionaryGetValue(vals, keys[i]);
  48.         CFStringRef label = CFDictionaryGetValue(dict_values, kSecPropertyKeyLabel);
  49.         if (CFEqual(label, CFSTR("Serial Number"))) {
  50.             CFStringRef value = CFDictionaryGetValue(dict_values, kSecPropertyKeyValue);
  51.             if (value == NULL)
  52.                 continue;
  53.             [dict setObject:(__bridge NSString*)(value) forKey:(__bridge NSString*)label];
  54.         } else if (CFEqual(label, CFSTR("Signature"))) {
  55.             CFDataRef value = CFDictionaryGetValue(dict_values, kSecPropertyKeyValue);
  56.             if (value == NULL)
  57.                 continue;
  58.             [dict setObject:(__bridge NSData*)(value) forKey:(__bridge NSString*)label];
  59.         } else {
  60.             CFArrayRef values = CFDictionaryGetValue(dict_values, kSecPropertyKeyValue);
  61.             if (values == NULL)
  62.                 continue;
  63.             [dict setObject:[self dictionaryFromDNwithSubjectName:values] forKey:(__bridge NSString*)label];
  64.         }
  65.     }
  66.    
  67.     CFRelease(vals);
  68.    
  69. #endif
  70.    
  71.     return dict;
  72. }
  73.  
  74. - (NSDictionary*)dictionaryFromDNwithSubjectName:(CFArrayRef)array {
  75.    
  76.     NSMutableDictionary *dict = [NSMutableDictionary new];
  77.    
  78. #if TARGET_OS_MAC
  79.    
  80.     const void *keys[] = { kSecOIDCommonName, kSecOIDEmailAddress, kSecOIDOrganizationalUnitName, kSecOIDOrganizationName, kSecOIDLocalityName, kSecOIDStateProvinceName, kSecOIDCountryName };
  81.     NSArray *labels = [NSArray arrayWithObjects:@"CN", @"E", @"OU", @"O", @"L", @"S", @"C", @"E", nil];
  82.    
  83.     for(int i = 0; i < sizeof(keys)/sizeof(keys[0]);  i++) {
  84.         for (CFIndex n = 0 ; n < CFArrayGetCount(array); n++) {
  85.             CFDictionaryRef dict_values = CFArrayGetValueAtIndex(array, n);
  86.             if (CFGetTypeID(dict_values) != CFDictionaryGetTypeID())
  87.                 continue;
  88.             CFTypeRef dictkey = CFDictionaryGetValue(dict_values, kSecPropertyKeyLabel);
  89.             if (!CFEqual(dictkey, keys[i]))
  90.                 continue;
  91.             CFStringRef str = (CFStringRef) CFDictionaryGetValue(dict_values, kSecPropertyKeyValue);
  92.             [dict setObject:(__bridge  NSString*)str forKey:labels[i]];
  93.         }
  94.     }
  95.    
  96. #endif
  97.    
  98.     return dict;
  99. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×