Skip to content

Software License Dongle for OCaml

OCaml reaches the dongle through the SDK’s flat C API with ctypes-foreign, resolving the functions from the library at run time, so the package is pure OCaml with no C stubs to compile and nothing to link. keynub-licdongle from opam, OCaml 4.08 or later, on Windows, Linux and macOS. For a licensing component that is the right shape: nothing sits in the path of the check that a customer could substitute for something more agreeable.

opam version badge

opam install keynub-licdongle
; and in your dune file:
(libraries keynub-licdongle)Code language: OCaml (ocaml)

Reading a License

open Keynub_licdongle

let license =
  with_dongle (fun d ->                 (* first dongle, or ~serial:"..." *)
    ignore (verify_genuine d);          (* raises unless genuine *)
    with_session d (fun () ->
      read_record d "license"))Code language: OCaml (ocaml)

What You Are Protecting

OCaml software that is sold is usually the kind whose value is in what it knows rather than in its syntax: a static analyser, a verified compiler back end, a pricing or risk engine, a proof tool. The binary is native code, but a check that returns a bool is still one conditional branch in it, and patching one of those is a beginner exercise.

So the strong pattern is the one to reach for: the data the program needs only exists when the dongle is present.

(* Weak: one patched branch. *)
if not (is_genuine d) then exit 1

(* Strong: the parameters only exist with the dongle present. *)
let parameters =
  decode_parameters (with_session d (fun () -> app_decrypt d sealed_blob_shipped_with_your_program))Code language: OCaml (ocaml)

Failures raise Error with the status (No_device, Not_genuine, Auth_required, …), the operation and the library’s detail; is_genuine fails closed. with_dongle and with_session release the dongle and the session on every exit path. The package calls the SDK’s flat companion API, the one designed for foreign function interfaces: integer handles and buffers, no hand-written structure layouts.

Shipping the Native Library with an OCaml Application

The keynub-licdongle opam package is pure OCaml over the SDK’s flat API through ctypes-foreign: functions are resolved by name from the library opened at run time, nothing is linked, no C stubs. opam install keynub-licdongle and the library name in your dune file are the whole setup; the library is found through set_library_path, KEYNUB_LICDONGLE_FLAT_LIBRARY, an SDK clone’s natives/ folder, or the system search path.

A shipped executable takes keynub_licdongle_flat for its platform beside it or on the search path. The prebuilt libraries for every platform are in the SDK repository’s natives/<platform>/ folder, with a SHA-256 manifest: Windows x64, x86 and ARM64, Linux x86_64 and aarch64, and universal macOS binaries for Intel and Apple silicon. On Linux, install the udev rule from NATIVES.md once, so that ordinary users may open the device.

Questions OCaml Developers Ask

Which OCaml Versions Does the Binding Support?

OCaml 4.08 and later, including OCaml 5, with ctypes 0.20 and later, on Windows, Linux and macOS.

Does a OCaml Application Need Administrator Rights to Talk to the Dongle?

No, and no driver either: the dongle is a USB HID device that the operating systems handle with their built-in class drivers. On Linux, install the shipped udev rule once so that ordinary users may open it; without the rule the SDK reports access denied and names the cause in its error detail.

Does a OCaml License Check Need an Internet Connection?

No. Verification is a local exchange between your program and the dongle over USB: the SDK checks the dongle’s certificate chain to KeyNub’s root and runs a live challenge-response. There is no activation server and no account, so the check works on air-gapped machines.

Who Can Read the License Records on a Dongle?

Anyone holding the dongle: a program opens a session and reads records, and can decrypt data sealed for that dongle. Writing records, erasing them and incrementing counters need your write key. What the dongle guarantees is that none of it is available without the dongle present.

Can a License Written from OCaml Be Read by a Program in Another Language?

Yes. Every binding drives the same core library and the same dongle, and records and sealed data are language-neutral bytes. Your issuing tool can be written in one language and your product in another.

How Do I Test the OCaml Integration?

Against a dongle. The samples in the repository run against real hardware; with no dongle attached they print what to do and exit cleanly, which makes them a quick check that the library is found and the device is accessible.

Code

Runnable sample: ocaml/verify_and_read.ml. Binding source: bindings/ocaml. Both are Apache-2.0, in the public SDK repository; the prebuilt native libraries are in the repository’s natives/ folder, one per platform.

All supported languages · All industries · Buy a KeyNub · Ask us something