Giter Club home page Giter Club logo

swift-asn1's Introduction

SwiftASN1

An implementation of ASN.1 types and DER serialization.

Overview

ASN.1, and the DER encoding scheme, is a commonly used object serialization format. The most common use-cases for ASN.1 in general computing are in the cryptographic space, but there are a number of use-cases in a wide range of fields. This module provides an implementation of a number of ASN.1 types, as well as the DER serialization format for ASN.1.

ASN.1 can be used abstractly to describe essentially any kind of object. ASN.1 objects are made up of either primitive or composite (called "constructed") types. Individual scalar objects can be combined into aggregate types, and composed essentially arbitrarily to form abstract object formats.

Importantly, the ASN.1 object description does not define a specific encoding for these objects. Instead there are a wide range of possible ways to serialize or deserialize an ASN.1 object. Some of the most prominent are BER (the Basic Encoding Rules), CER (the Canonical Encoding Rules), DER (the Distinguished Encoding Rules), and XER (the XML Encoding Rules). For the cryptographic use-case DER is the standard choice, as a given ASN.1 object can be encoded in only one way under DER. This makes signing and verifying vastly easier, as it is at least in principle possible to perfectly reconstruct the serialization of a parsed object.

This module provides several moving pieces:

  1. A high-level representation of an ASN.1 object, in the form of a tree of object nodes (ASN1Node).
  2. A DER parser that can construct the ASN.1 tree from serialized bytes (DER.parse(_:)).
  3. A DER serializer that can construct serialized bytes from the ASN.1 tree (DER.Serializer).
  4. A number of built-in ASN.1 types, representing common constructs.

These moving pieces combine to provide support for the DER representation of ASN.1 suitable for a wide range of cryptographic uses.

Getting Started

To use swift-asn1, add the following dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/apple/swift-asn1.git", .upToNextMajor(from: "1.0.0"))
]

You can then add the specific product dependency to your target:

dependencies: [
    .product(name: "SwiftASN1", package: "swift-asn1"),
]

Consult the documentation for examples of how to use the code. A number of examples are also present in the repository itself.

swift-asn1's People

Contributors

ahoppen avatar aryan-25 avatar clintonpi avatar compnerd avatar davidzech avatar dnadoba avatar ekscrypto avatar futurejones avatar glbrntt avatar joey-gm avatar lukasa avatar rnro avatar tomerd avatar yim-lee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-asn1's Issues

PEMDocument struct internal

The PEMDocument struct, which is required in order to be able to read PEM formatted document from the outside, as found in the unit tests, is not tagged as public. Leaving us with no ability to load PEM documents even if this feature is present in the code.

Parsing fails for negative integer values

Hi,

in my current project we are generating certificates where the serial number can be negative. If that's the case, parsing the certificate fails with the following error:

backingCode	invalidASN1IntegerEncoding
reason	"INTEGER encoded with top bit set!"	

According to this specification negative integer values are valid though:

8.3 Encoding of an integer value
[...]
8.3.3 The contents octets shall be a two's complement binary number equal to the integer value, and consisting of
bits 8 to 1 of the first octet, followed by bits 8 to 1 of the second octet, followed by bits 8 to 1 of each octet in turn up to
and including the last octet of the contents octets.
NOTE – The value of a two's complement binary number is derived by numbering the bits in the contents octets, starting with bit1 of the last octet as bit zero and ending the numbering with bit 8 of the first octet. Each bit is assigned a numerical value of 2N, where N is its position in the above numbering sequence. The value of the two's complement binary number is obtained by summing the numerical values assigned to each bit for those bits which are set to one, excluding bit 8 of the first octet, and then reducing this value by the numerical value assigned to bit 8 of the first octet if that bit is set to one.

Is there a specific motivation to disallow negative integer values?

Thanks in advance!

How to get ASN1 Node tree of Curve25519 Public key

I am implementing encryption/decryption using ECDH and want to generate PEM of Public key using Curve25519. AFAIK I need to convert public key rawRepresentation data into ASN1 and then convert it to PEM.

I am struggling with creating DER from Public key and then ASN1 from DER. Please help if its possible using swift-asn1 package.

Feature request: Extend PEM parsing to support additional formats

The PEM parsing and generation implemented in PEMDocument doesn't fully support the PEM format as defined in RFC1421.

In particular, fields such as Proc-Type and DEK-INFO are not supported, which means that formats as defined in RFC5915 cannot be processed:

-----BEGIN EC PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,A4E9C3555C149C675E03B622A42500D2

<snip>
-----END EC PRIVATE KEY-----

It would be advantageous if the processing of these additional fields were supported by the PEMDocument, as otherwise some PEM files cannot be processed properly.

Convert RSA private key in PEM format to OpenSSH format

Hello,

I want to convert an RSA private key in PEM format to OpenSSH format.
I only got so far in converting the key to an ASN1 object:

import SwiftASN1
import CryptoKit

let key = """
-----BEGIN RSA PRIVATE KEY-----
MIIG4wIBAAKCAYEAnUx+3ufsDbhhsF5bE40YYhZdPI7UDLx6zWaC0Fa7ogEbTOkx
…
arMB2W/9HFE9zPvQbt5Dk0r1XUxnwnKhIQgLkPc67WSFMgEGM9/H
-----END RSA PRIVATE KEY-----
"""
let pemObject = try PEMDocument(pemString: key)
let asn1node = try DER.parse(pemObject.derBytes)

(the key was specifically generated for this example and deleted afterwards)

Is that possible by using this library? If this is the case, can you give me some hints?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.