<!--
{
  "documentType" : "article",
  "framework" : "Security",
  "identifier" : "/documentation/Security/asn-1",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "ASN.1"
}
-->

# ASN.1

Encode and decode Distinguished Encoding Rules (DER) and Basic Encoding Rules (BER) data streams.

## Overview

You use an ASN.1 coder to encode and decode both DER and BER data streams based on templates that you supply, which in turn are based upon ASN.1 specifications. You must import this API explicitly:

```swift
import Security.SecAsn1Coder
import Security.SecAsn1Templates
```

## Topics

### Coder Creation

[`SecAsn1CoderCreate`](/documentation/Security/SecAsn1CoderCreate)

Creates an ASN.1 coder object.

[`SecAsn1CoderRelease`](/documentation/Security/SecAsn1CoderRelease)

Destroys an ASN.1 coder object and releases all of its memory.

[`SecAsn1CoderRef`](/documentation/Security/SecAsn1CoderRef)

An object that represents an ASN.1 coder.

### Encoding

[`SecAsn1EncodeItem`](/documentation/Security/SecAsn1EncodeItem)

Encodes data in DER format.

[`SecAsn1Item`](/documentation/Security/SecAsn1Item)

A structure holding DER encoded data.

[`SecAsn1Template_struct`](/documentation/Security/SecAsn1Template_struct)

A structure that defines one element of a BER or DER encoding.

[`SecAsn1Template_struct`](/documentation/Security/SecAsn1Template_struct)

A structure that defines one element of a BER or DER encoding.

[`SecAsn1TemplateChooser`](/documentation/Security/SecAsn1TemplateChooser)

Dynamically provides the sub-template to use during encode or decode.

[`SecAsn1TemplateChooserPtr`](/documentation/Security/SecAsn1TemplateChooserPtr)

A pointer to the template chooser function.

[Type Tags](/documentation/Security/type-tags)

Recognize BER and DER values for ASN.1 identifier octets.

### Encoding

[`SecAsn1EncodeItem`](/documentation/Security/SecAsn1EncodeItem)

Encodes data in DER format.

[`SecAsn1Item`](/documentation/Security/SecAsn1Item)

A structure holding DER encoded data.

[`SecAsn1Template_struct`](/documentation/Security/SecAsn1Template_struct)

A structure that defines one element of a BER or DER encoding.

[`SecAsn1Template_struct`](/documentation/Security/SecAsn1Template_struct)

A structure that defines one element of a BER or DER encoding.

[`SecAsn1TemplateChooser`](/documentation/Security/SecAsn1TemplateChooser)

Dynamically provides the sub-template to use during encode or decode.

[`SecAsn1TemplateChooserPtr`](/documentation/Security/SecAsn1TemplateChooserPtr)

A pointer to the template chooser function.

[Generic Templates](/documentation/Security/generic-templates)

Use templates for each of the simple types.

[Type Tags](/documentation/Security/type-tags)

Recognize BER and DER values for ASN.1 identifier octets.

### Decoding

[`SecAsn1Decode`](/documentation/Security/SecAsn1Decode)

Decodes untyped DER data.

[`SecAsn1DecodeData`](/documentation/Security/SecAsn1DecodeData)

Decodes an ASN.1 item in DER format.

### Memory Management

You don’t explicitly free any of the memory allocated in a coder object’s memory pool using these functions. Instead, that memory is automatically freed when you destroy the coder with a call to the [`SecAsn1CoderRelease`](/documentation/Security/SecAsn1CoderRelease)

Destroys an ASN.1 coder object and releases all of its memory. function.

[`SecAsn1Malloc`](/documentation/Security/SecAsn1Malloc)

Allocates memory in the coder object’s memory pool.

[`SecAsn1AllocItem`](/documentation/Security/SecAsn1AllocItem)

Allocates memory for an item’s data field in the coder object’s memory pool.

[`SecAsn1AllocCopy`](/documentation/Security/SecAsn1AllocCopy)

Allocates memory for an item’s data field in the coder object’s memory pool and copies in a block of data.

[`SecAsn1AllocCopyItem`](/documentation/Security/SecAsn1AllocCopyItem)

Allocates memory for an item’s data field in the coder object’s memory pool and copies in a block of data from another item.

### OID Comparison

[`SecAsn1OidCompare`](/documentation/Security/SecAsn1OidCompare)

Compares two decoded object identifiers.

[`SecAsn1Oid`](/documentation/Security/SecAsn1Oid)

An object identifier.

### Public Key Info

[`SecAsn1AlgId`](/documentation/Security/SecAsn1AlgId)

A structure identifying an ASN.1 algorithm by its OID, and its corresponding parameters.

[`SecAsn1PubKeyInfo`](/documentation/Security/SecAsn1PubKeyInfo)

A structure containing a public key and its associated algorithm.



---

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)