title: Programmatically read root CA certificates in iOS
body: <p>The following code reads out the root certificates in macOS.</p>
  
  <p>I just wonder what are the equivalent code in iOS?</p>
  
  <p><a href="https://github.com/HaxeFoundation/hxcpp/blob/7bd5ff3/src/hx/libs/ssl/SSL.cpp#L455-L491" rel="noreferrer">https://github.com/HaxeFoundation/hxcpp/blob/7bd5ff3/src/hx/libs/ssl/SSL.cpp#L455-L491</a></p>
  
  <pre><code>CFMutableDictionaryRef search;
  CFArrayRef result;
  SecKeychainRef keychain;
  SecCertificateRef item;
  CFDataRef dat;
  sslcert *chain = NULL;
  
  // Load keychain
  if( SecKeychainOpen("/System/Library/Keychains/SystemRootCertificates.keychain",&amp;keychain) != errSecSuccess )
      return null();
  
  // Search for certificates
  search = CFDictionaryCreateMutable( NULL, 0, NULL, NULL );
  CFDictionarySetValue( search, kSecClass, kSecClassCertificate );
  CFDictionarySetValue( search, kSecMatchLimit, kSecMatchLimitAll );
  CFDictionarySetValue( search, kSecReturnRef, kCFBooleanTrue );
  CFDictionarySetValue( search, kSecMatchSearchList, CFArrayCreate(NULL, (const void **)&amp;keychain, 1, NULL) );
  if( SecItemCopyMatching( search, (CFTypeRef *)&amp;result ) == errSecSuccess ){
      CFIndex n = CFArrayGetCount( result );
      for( CFIndex i = 0; i &lt; n; i++ ){
          item = (SecCertificateRef)CFArrayGetValueAtIndex( result, i );
  
          // Get certificate in DER format
          dat = SecCertificateCopyData( item );
          if( dat ){
              if( chain == NULL ){
                  chain = new sslcert();
                  chain-&gt;create( NULL );
              }
              mbedtls_x509_crt_parse_der( chain-&gt;c, (unsigned char *)CFDataGetBytePtr(dat), CFDataGetLength(dat) );
              CFRelease( dat );
          }
      }
  }
  CFRelease(keychain);
  if( chain != NULL )
      return chain;
  </code></pre>