I'm trying to programmatically install SSL certificate on the iOS(iPhone/iPhone simulator). I have successfully create and install .crt using this small guide! But when I try to convert obtained server.crt to server.der and install it programmatically I get CFNetwork SSLHandshake failed (-9807). Note that I use .der version of this cert on the iOS and .crt version on the OS X openssl server.
Converting:
openssl x509 -outform der -in server.crt -out server.der
Receiving on the OS X:
openssl s_server -key server.key -cert server.crt -accept 1678
Installing and sending on the iOS:
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSData *iosTrustedCertDerData =
    [NSData dataWithContentsOfFile:[bundle pathForResource:@"certificate"
                                                    ofType:@"der"]];
    OSStatus            err = noErr;
    SecCertificateRef   cert;
    cert = SecCertificateCreateWithData(NULL, (CFDataRef) iosTrustedCertDerData);
    assert(cert != NULL);
    CFTypeRef result;
    NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          (id)kSecClassCertificate, kSecClass,
                          cert, kSecValueRef,
                          nil];
    err = SecItemAdd((CFDictionaryRef)dict, &result);
    assert(err == noErr || err == errSecDuplicateItem);
    printf("adding finished \n");
    if ((err == noErr) ||
        (err == errSecDuplicateItem)) {
        printf("success \n");
        CFReadStreamRef readStream;
        CFWriteStreamRef writeStream;
        CFStreamCreatePairWithSocketToHost(NULL,
                                           (CFStringRef)@"localhost",
                                           1678,
                                           &readStream,
                                           &writeStream);
        CFReadStreamSetProperty(readStream,
                                kCFStreamPropertySocketSecurityLevel,
                                kCFStreamSocketSecurityLevelTLSv1);
        CFReadStreamOpen(readStream);
        CFWriteStreamOpen(writeStream);
        UInt8 buf[] = "Hello from iOS\n";
        int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
}
So, how can I convert and install .der certificate on the iOS properly?
UPD:
 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSString *resourcePath = [bundle pathForResource:@"certificate" ofType:@"der"];
    NSData *certData = [NSData dataWithContentsOfFile:resourcePath];
    NSString* publickKeyRef = @"certificate_der";
    NSData* headerStrippedData = [self stripPublicKeyHeader:certData];
    [self addPeerPublicKey:publickKeyRef keyBits:headerStrippedData];
    SecKeyRef ref= [self getPublicKeyReference:publickKeyRef];
    printf("adding finished \n");
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL,
                                       (CFStringRef)@"localhost",
                                       1678,
                                       &readStream,
                                       &writeStream);
    CFReadStreamSetProperty(readStream,
                            kCFStreamPropertySocketSecurityLevel,
                            kCFStreamSocketSecurityLevelTLSv1);
    CFReadStreamOpen(readStream);
    CFWriteStreamOpen(writeStream);
Exception occurs on this line:
[self addPeerPublicKey:publickKeyRef keyBits:headerStrippedData];
Exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: v_Data)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00a81a14 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00540e02 objc_exception_throw + 50
    2   CoreFoundation                      0x0096a174 -[__NSDictionaryM setObject:forKey:] + 948
    3   ssl2                                0x0006d8eb -[ViewController addPeerPublicKey:keyBits:] + 491
    4   ssl2                                0x0006e2dd -[ViewController viewDidLoad] + 397
    5   UIKit                               0x010932ae -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44
    6   UIKit                               0x01097dce -[UIViewController loadViewIfRequired] + 1384
    7   UIKit                               0x010981ed -[UIViewController view] + 35
    8   UIKit                               0x00f45f94 -[UIWindow addRootViewControllerViewIfPossible] + 69
    9   UIKit                               0x00f466b1 -[UIWindow _setHidden:forced:] + 304
    10  UIKit                               0x00f46a67 -[UIWindow _orderFrontWithoutMakingKey] + 49
    11  UIKit                               0x00f5a118 -[UIWindow makeKeyAndVisible] + 80
    12  UIKit                               0x00ec26e7 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4190
    13  UIKit                               0x00ec9cd6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1989
    14  UIKit                               0x00eeeee5 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3218 + 68
    15  UIKit                               0x00ec6966 -[UIApplication workspaceDidEndTransaction:] + 163
    16  FrontBoardServices                  0x0373fc76 __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
    17  FrontBoardServices                  0x0373f74d __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
    18  FrontBoardServices                  0x0375d173 -[FBSSerialQueue _performNext] + 184
    19  FrontBoardServices                  0x0375d5aa -[FBSSerialQueue _performNextFromRunLoopSource] + 52
    20  FrontBoardServices                  0x0375c8a6 FBSSerialQueueRunLoopSourceHandler + 33
    21  CoreFoundation                      0x0099b6ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    22  CoreFoundation                      0x0099138b __CFRunLoopDoSources0 + 523
    23  CoreFoundation                      0x009907a8 __CFRunLoopRun + 1032
    24  CoreFoundation                      0x009900e6 CFRunLoopRunSpecific + 470
    25  CoreFoundation                      0x0098fefb CFRunLoopRunInMode + 123
    26  UIKit                               0x00ec6206 -[UIApplication _run] + 540
    27  UIKit                               0x00ecbbfa UIApplicationMain + 160
    28  ssl2                                0x0006e91a main + 138
    29  libdyld.dylib                       0x0310ba21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
 
    