How to create a SecIdentityRef to be used with MultiPeerConnectivity?

Seddiki Anass

I need to implement an authentication part for MultiPeer Connectivity exchanges in a Swift iOS application. And so I need to create a SecIdentityRef object at the MCSession creation (like so MCSession(peer: myPeerId, securityIdentity: secIdentity, encryptionPreference: MCEncryptionPreference.Required)).

I already created a X509 certificate with Keychain access and saved it to a .p12 file. I also have a certificate in .cgi and .der formats that could be used.

I'd like to know if either of these certificates are worth to be used in my application, and how to use it if yes ? Is it possible to put the certificate directly in the project directory and import its data in the app or do I need to use a server to provide the certificate ?

Last but not least, I have no idea on how to create the SecIdentityRef from a given certificate. I've tried to browse Apple Developer's references for classes MCSession, SecIdentityRef, SecCertificateRef and even CFData but I found nothing that could possibly help me.

Seddiki Anass

I figured out how to import a certificate for Multipeer Connectivity session establishment.

In the example below we assume that we have the certificate under supportedFiles/certificate.p12. Moreover, your certificate has to be protected by a password as I've read that the certificate management API doesn't support unprotected certificates.

func getIdentity (password : String?) -> SecIdentityRef? {
    // Load certificate file
    let path = NSBundle.mainBundle().pathForResource("certificate", ofType : "p12")
    let p12KeyFileContent = NSData(contentsOfFile: path!)

    if (p12KeyFileContent == nil) {
        NSLog("Cannot read PKCS12 file from \(path)")
        return nil
    }

    // Setting options for the identity "query"
    let options = [String(kSecImportExportPassphrase):password ?? ""]
    var citems: CFArray? = nil
    let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in
        SecPKCS12Import(p12KeyFileContent!, options, citemsPtr)
    }
    if (resultPKCS12Import != errSecSuccess) {
        return nil
    }

    // Recover the identity
    let items = citems! as NSArray
    let myIdentityAndTrust = items.objectAtIndex(0) as! NSDictionary
    let identityKey = String(kSecImportItemIdentity)
    let identity = myIdentityAndTrust[identityKey] as! SecIdentity

    print("identity : ", identity)

    return identity as SecIdentityRef
}

However I still don't know whether having the certificate in the application files is a security threat.

edit : Thanks neilco, his snippet helped me build my solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MultipeerConnectivity Session management

From Dev

How to create objects that can be used by multiple methods in form

From Dev

How to retrieve the SQL used to create a view in Oracle?

From Dev

How do I delete an identity from the keychain using a SecIdentityRef or persistent reference?

From Dev

How to create a .sks file to used in spriteKit app

From Dev

How to create an overlay to be used under a pop up

From Dev

How to let Hibernate create tables in database automatically when used with JPA?

From Dev

How can a converter for TreeView be used to create a tree structure from a list?

From Dev

Lua How to create custom function that can be used on variables?

From Dev

Lua How to create custom function that can be used on variables follow up

From Dev

How to view query that was used to create a table?

From Dev

how can I get the sql statements used to create a specific table?

From Dev

how to create a cuda shared library used in python

From Dev

How to create a JavaScript wrapper to be used with JavaFx WebView? Is JSNI an option?

From Dev

Scala - how to create a single implicit that can be used for a type constructor

From Dev

C++ - How to create a property that can be used with _ inside the class?

From Dev

How to create custom color palette to be used by scale_fill_manual()

From Dev

How to specify python version used to create Virtual Environment through venv?

From Dev

MultipeerConnectivity Session management

From Dev

How can my argument be used to create a matlab file?

From Dev

How to create objects that can be used by multiple methods in form

From Dev

How to retrieve the SQL used to create a view in Oracle?

From Dev

How to create common method used in Web form as well as Windows form

From Dev

How do I retrieve the coordinates used to create an iOS MapKit MKPolygonView?

From Dev

How to create a Library/Framework that will be linked and used only if present at compile time

From Dev

Can you tell how or what was used to create an Android game?

From Dev

How to create a database that can be used by many users?

From Dev

How to used for loop to create table in django form

From Dev

How to get a SecIdentityRef from a SecCertificateRef and a SecKeyRef

Related Related

  1. 1

    MultipeerConnectivity Session management

  2. 2

    How to create objects that can be used by multiple methods in form

  3. 3

    How to retrieve the SQL used to create a view in Oracle?

  4. 4

    How do I delete an identity from the keychain using a SecIdentityRef or persistent reference?

  5. 5

    How to create a .sks file to used in spriteKit app

  6. 6

    How to create an overlay to be used under a pop up

  7. 7

    How to let Hibernate create tables in database automatically when used with JPA?

  8. 8

    How can a converter for TreeView be used to create a tree structure from a list?

  9. 9

    Lua How to create custom function that can be used on variables?

  10. 10

    Lua How to create custom function that can be used on variables follow up

  11. 11

    How to view query that was used to create a table?

  12. 12

    how can I get the sql statements used to create a specific table?

  13. 13

    how to create a cuda shared library used in python

  14. 14

    How to create a JavaScript wrapper to be used with JavaFx WebView? Is JSNI an option?

  15. 15

    Scala - how to create a single implicit that can be used for a type constructor

  16. 16

    C++ - How to create a property that can be used with _ inside the class?

  17. 17

    How to create custom color palette to be used by scale_fill_manual()

  18. 18

    How to specify python version used to create Virtual Environment through venv?

  19. 19

    MultipeerConnectivity Session management

  20. 20

    How can my argument be used to create a matlab file?

  21. 21

    How to create objects that can be used by multiple methods in form

  22. 22

    How to retrieve the SQL used to create a view in Oracle?

  23. 23

    How to create common method used in Web form as well as Windows form

  24. 24

    How do I retrieve the coordinates used to create an iOS MapKit MKPolygonView?

  25. 25

    How to create a Library/Framework that will be linked and used only if present at compile time

  26. 26

    Can you tell how or what was used to create an Android game?

  27. 27

    How to create a database that can be used by many users?

  28. 28

    How to used for loop to create table in django form

  29. 29

    How to get a SecIdentityRef from a SecCertificateRef and a SecKeyRef

HotTag

Archive