iPhone: How to create a SecKeyRef from a public key file (PEM)

后端 未结 1 426
鱼传尺愫
鱼传尺愫 2020-12-13 01:08

In order to send and receive encrypted messages from/to the iPhone I need to read a public key (server\'s public key) PEM file and create a SecKeyRef (later I could even sto

相关标签:
1条回答
  • 2020-12-13 01:37

    You should be able to interpret a DER encoded pem and get a cert using SecCertificateCreateWithData() from which you can then extract a key;

    NSData *myCertData = ....;
    
    SecCertificateRef cert = SecCertificateCreateWithData (kCFAllocatorDefault, myCertData); 
    CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **) &cert, 1, NULL); 
    
    SecTrustRef trust;
    SecTrustCreateWithCertificates(certs, policy, &trust);
    SecTrustResultType trustResult;
    SecTrustEvaluate(trust, &trustResult);
    SecKeyRef pub_key_leaf = SecTrustCopyPublicKey(trust);
    
    0 讨论(0)
提交回复
热议问题