<!--
{
  "documentType" : "article",
  "framework" : "Security",
  "identifier" : "/documentation/Security/parsing-an-identity",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Parsing an Identity"
}
-->

# Parsing an Identity

Extract the private key and certificate from an identity.

## Discussion

After you have an identity, you can extract the private key from it with a call to the [`SecIdentityCopyPrivateKey(_:_:)`](/documentation/Security/SecIdentityCopyPrivateKey(_:_:)) function:

```swift
var privateKey: SecKey?
let status = SecIdentityCopyPrivateKey(identity, &privateKey)
guard status == errSecSuccess else { throw <# an error #> }
```

Similarly, you can extract the certificate with a call to the [`SecIdentityCopyCertificate(_:_:)`](/documentation/Security/SecIdentityCopyCertificate(_:_:)) function:

```swift
var certificate: SecCertificate?
let status = SecIdentityCopyCertificate(identity, &certificate)
guard status == errSecSuccess else { throw <# an error #> }
```

In both cases, you inspect the returned status value to determine whether an error occurred during the extraction. In Objective-C, you are responsible for freeing the associated memory with a call to <doc://com.apple.documentation/documentation/CoreFoundation/CFRelease> when you’re done with these objects. In Swift, the system manages the memory automatically, releasing it when the object goes out of scope.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)