Giter Site home page Giter Site logo

fast_ber's Introduction

fast_ber version Appveyor status Travis status C++11 C++14 C++17 C++20

A performant ASN.1 BER encoding and decoding library written in C++11

Introduction

fast_ber is a small, lightweight library for BER encoding and decoding. Fast ber forgoes some tight ASN.1 specification conformance to provide fast encoding and decoding performance in common use cases

Design Decisions

  • Simple, modern C++ interface
  • ASN.1 sequences are represented as POD structs - no private members or complex getters and setters
  • No exceptions, no RTTI and limited memory allocations (everything is small buffer optimised)
  • Header only
  • View classes are provided for zero copy decoding
  • Interfaces mimic STL types such as std::string, std::vector and std::optional

Read more about the decisions made writing this library

Limitations

  • No circular data structures
  • Size and value constraints are not implemented

Call for Test Data

Test data is wanted to improve this project! If you have any test ASN.1 specs or BER files please share them. More test data will improve parsing and help find any issues with the library.

Example Project

fast_ber is designed to be easy to consume via CMake. This is demonstrated in fast_ber_ldap3, an example project using fast_ber to create and dissect LDAP3 certificates.

Usage

  1. Build the compiler:
git submodule update --init
mkdir build_cmake
cd build_cmake
cmake ..
make
ctest
  1. Define an ASN.1 file - Example: pokemon.asn
Pokemon DEFINITIONS EXPLICIT TAGS ::= BEGIN

Team ::= SEQUENCE {
    team-name OCTET STRING,
    members SEQUENCE OF Pokemon
}

Pokemon ::= SEQUENCE {
    name OCTET STRING,
    category OCTET STRING,
    type Type,
    ability OCTET STRING,
    weakness OCTET STRING,
    weight INTEGER
}

Type ::= ENUMERATED {
    normal,
    fire,
    fighting,
    water,
    flying,
    grass
}

END
  1. Compile an asn file into a header file defining the ber structure
cd build_cmake
./src/fast_ber_compiler pokemon.asn pokemon

output:

#pragma once

#include "fast_ber/ber_types/All.hpp"
#include "pokemon.fwd.hpp"


namespace fast_ber {
using namespace abbreviations;

namespace Pokemon {

enum class TypeValues {
    normal,
    fire,
    fighting,
    water,
    flying,
    grass,
};

template <typename Identifier>
FAST_BER_ALIAS(Type, Enumerated<TypeValues>);

template <typename Identifier>
struct Pokemon {
    OctetString<> name;
    OctetString<> category;
    Type<> type;
    OctetString<> ability;
    OctetString<> weakness;
    Integer<> weight;
};

template <typename Identifier>
struct Team {
    OctetString<> team_name;
    SequenceOf<Pokemon<>> members;
};

} // End namespace Pokemon
} // End namespace fast_ber
  1. Include the header file into your application. Fields in the generated structure can be assigned to standard types. Encode and decode functions are used to serialize and deserialize the data
#include "pokemon.hpp"

int main()
{
    fast_ber::Pokemon::Team<>    team    = {"Sam's Team"};
    fast_ber::Pokemon::Pokemon<> muchlax = { "Munchlax",
                                             "Big Eater",
                                             fast_ber::Pokemon::Type<>::Values::normal,
                                             "Thick Fat, Pickup",
                                             "Fighting",
                                             105 };
    fast_ber::Pokemon::Pokemon<> piplup  = { "Piplup",
                                             "Penguin",
                                             fast_ber::Pokemon::Type<>::Values::water,
                                             Torrent",
                                             "Electric, Grass",
                                             12 };
    team.members.push_back(muchlax);
    team.members.push_back(piplup);

    std::array<uint8_t, 2000> buffer{};
    const auto                encode_result = fast_ber::encode(absl::Span<uint8_t>(buffer), team);
    if (!encode_result.success)
    {
        return -1;
    }

    return 0;
}

Take a look at fast_ber_ldap3 for an example of these steps in action.

Features

fast_ber is in development. The following table documents what has been implemented and what features are coming soon

Type Implemented
Integer Yes
Boolean Yes
Strings Yes
Sequence Yes
Sequence Of Yes
Choice Yes
Set No
Set Of Yes
Enumerated Yes
Object Identifier Yes
Dates / Times No
Null Yes
Any No
Feature Implemented
Explicit tags Yes
Implicit tags Yes
Indefinitive length No
Arbitrary top level structure Yes
Arbritary definition order Yes
Embedded Types Yes
Imports Yes

Possible Future Extensions

The following features may be added in the future

Partial decoding
Partial encode at compile time (templated / constexpr)
High / low conformance modes
DER mode

Benchmarks

fast_ber includes benchmarks against asn1c, an ASN library written in C. Here is an example of the output:

-------------------------------------------------------------------------------
Benchmark: Decode Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x decode 2107B pdu      1    516381442   516.381 ms
asn1c           - 1,000,000 x decode 2107B pdu      1   4996255249    4.99626 s
fast_ber        - 1,000,000 x decode 64B pdu        1    192230063    192.23 ms
asn1c           - 1,000,000 x decode 64B pdu        1   2581069031    2.58107 s

-------------------------------------------------------------------------------
Benchmark: Encode Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x encode 2107B pdu      1    191266512   191.267 ms
asn1c           - 1,000,000 x encode 2107B pdu      1   7349946740    7.34995 s

-------------------------------------------------------------------------------
Benchmark: Object Construction Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x construct data        1   1005938231    1.00594 s
asn1c           - 1,000,000 x construct data        1    511881940   511.882 ms

-------------------------------------------------------------------------------
Benchmark: Calculate Encoded Length Performance
-------------------------------------------------------------------------------
...............................................................................

benchmark name                                  iters   elapsed ns      average
-------------------------------------------------------------------------------
fast_ber        - 1,000,000 x encoded length        1     17084558   17.0846 ms

===============================================================================
All tests passed (31 assertions in 8 test cases)

fast_ber's People

Contributors

samuel-tyler avatar

Watchers

James Cloos avatar

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.