Skip to content

gematik/ASN1Kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASN1Kit

About The Project

ASN1Kit is an ASN.1 (Abstract Syntax Notation One) Decoder for Swift.

This library can be used for ASN.1 encoding/decoding using distinguished encoding rules (DER) according to the ITU-T X.690 specification.

For more information, please find the complete X.690-0207.pdf specification.

Release Notes

See ReleaseNotes for all information regarding the (newest) releases.

Getting Started

ASN1Kit requires Swift 5.5.

Prerequisites

For development, you will need:

Installation

Swift Package Manager (SPM)

Put this in your Package.swift:

.package(url: "https://github.com/gematik/ASN1Kit.git", from: "1.2.0")

Development Setup

Checkout (and build) dependencies and generate the xcodeproject:

$ make setup

Usage

Decoding Serialized Data

Use ASN1Decoder.decode(asn1:) to decode serialized data:

let expected = Data([0xB, 0xB, 0x0])
let serialized = Data([0x23, 0x0C,
                       0x03, 0x02, 0x00, 0x0B,
                       0x03, 0x02, 0x00, 0x0B,
                       0x03, 0x02, 0x04, 0x0F])
expect(try Data(from: ASN1Decoder.decode(asn1: serialized))) == expected

Constructing and Encoding ASN1Objects

Construct an ASN1Object of your choice and serialize it:

let data = Data([0x0, 0x1, 0x2, 0x4]) as ASN1EncodableType
let data2 = Data([0x4, 0x3, 0x2, 0x1]) as ASN1EncodableType
let array = [data, data2]

let expected = Data([0x30, 0xC, 0x4, 0x4, 0x0, 0x1, 0x2, 0x4, 0x4, 0x4, 0x4, 0x3, 0x2, 0x1])
expect(try array.asn1encode().serialize()) == expected

Encoding Swift Primitives

ASN.1-encode Swift primitives and extract the encoded value(s):

// 0x0080 = 128
let expected = Data([0x00, 0x80])
expect(try 128.asn1encode(tag: nil).data.primitive) == expected
// 0x0080 = 128
let expected = Data([0x00, 0x80])
expect(try UInt(128).asn1encode(tag: nil).data.primitive) == expected

Extracting Tags

Extract the tag of the first element of a constructed ASN1Object:

let data = Data([0x1, 0x2, 0x3, 0x4, 0x8])
let tag1 = ASN1Primitive(data: .primitive(data), tag: .taggedTag(3)) // context-specific class tag
let tag2 = ASN1Primitive(data: .primitive(data), tag: .universal(ASN1Tag.octetString))
let implicitTag = ASN1Primitive(data: .constructed([tag1, tag2]), tag: .taggedTag(83))

expect(implicitTag.data.items?.first?.tag) == .taggedTag(3)

For more examples and in-depth documentation, please refer to the API Documentation.

License

Copyright 2019-2025 gematik GmbH

Apache License, Version 2.0

See the LICENSE for the specific language governing permissions and limitations under the License.

Additional Notes and Disclaimer from gematik GmbH

  1. Copyright notice: Each published work result is accompanied by an explicit statement of the license conditions for use. These are regularly typical conditions in connection with open source or free software. Programs described/provided/linked here are free software, unless otherwise stated.

  2. Permission notice: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    1. The copyright notice (Item 1) and the permission notice (Item 2) shall be included in all copies or substantial portions of the Software.

    2. The software is provided "as is" without warranty of any kind, either express or implied, including, but not limited to, the warranties of fitness for a particular purpose, merchantability, and/or non-infringement. The authors or copyright holders shall not be liable in any manner whatsoever for any damages or other claims arising from, out of or in connection with the software or the use or other dealings with the software, whether in an action of contract, tort, or otherwise.

    3. The software is the result of research and development activities, therefore not necessarily quality assured and without the character of a liable product. For this reason, gematik does not provide any support or other user assistance (unless otherwise stated in individual cases and without justification of a legal obligation). Furthermore, there is no claim to further development and adaptation of the results to a more current state of the art.

  3. Gematik may remove published results temporarily or permanently from the place of publication at any time without prior notice or justification.

  4. Please note: Parts of this code may have been generated using AI-supported technology. Please take this into account, especially when troubleshooting, for security analyses and possible adjustments.

Contact

This software is currently being tested to ensure its technical quality and legal compliance. Your feedback is highly valued. If you find any issues or have any suggestions or comments, or if you see any other ways in which we can improve, please reach out to: [email protected].

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •