FreeBSD pkg

repository·main·Indexed 21 days ago

https://github.com/freebsd/pkg

The official package manager for FreeBSD, providing tools for installing, managing, and updating software packages via the libpkg library. This repository also includes external libraries: libder, a lightweight library for encoding and decoding DER-encoded objects; and libecc, a C99 library for Elliptic Curve Cryptography (ECC) supporting various signature algorithms (ISO 14888-3, EdDSA, BIP0340), ECDH, and multiple cryptographic primitives.

Tokens
62.3K
Snippets
170
Records
289
Agent score
73%

What's inside pkg

  1. Overview of libder

    main

    libder is a lightweight library designed for encoding and decoding DER-encoded objects. Its primary use case is decoding and encoding ECC keys to ensure interoperability with OpenSSL.

    Key characteristics:

    • BER Compatibility: It is capable of decoding any BER-encoded buffer.
    • Normalization: When re-encoding a decoded tree, libder applies DER normalization rules.
    • Opaque Data Handling: For complex data types, libder expects the consumer to provide a type/payload/size triple, treating the payload as relatively opaque while still applying encoding normalization.
    • Validation Model: The library does not perform strict validation of input. For example, a boolean might occupy multiple bytes in the input; libder will present it as-is, but will normalize it to 0xff or 0x00 upon re-encoding based on whether any bits are set.
  2. Introduction to libucl

    main

    libucl is a C library for parsing configurations written in the Universal Configuration Language (UCL). It is designed to be a more convenient and powerful alternative to JSON, heavily inspired by the nginx configuration syntax.

    In addition to UCL, the library provides functions to read, write, and format other data formats:

    • JSON: Read, write, and pretty format.
    • Messagepack: Read and write.
    • S-Expressions: Read-only (canonical form).
    • Yaml: Limited write support (primarily for compatibility).
  3. Overview of libecc capabilities

    main

    libecc is a C99 library for Elliptic Curve Cryptography (ECC) designed for readability, auditability, and portability. It is suitable for embedded targets as it uses no dynamic allocation and has no external dependencies (not even libc).

    Supported Features

    • Signature Algorithms:

      • ISO 14888-3:2018: ECDSA, ECKCDSA, ECGDSA, ECRDSA, EC{,O}SDSA, ECFSDSA, SM2.
      • EdDSA: Ed25519 and Ed448 (RFC 8032).
      • BIGN: Standardized in STB 34.101.45-2013.
      • BIP0340: Schnorr Bitcoin proposal.
      • Batch Verification: Supported for ECFSDSA, EdDSA, and BIP0340, providing 2x to 6.5x speedups for batches of $\ge 10$ signatures.
    • ECDH (Diffie-Hellman):

      • ECC-CDH: NIST SP 800-56A Rev. 3.
      • Montgomery curves: X25519 and X448 (RFC 7748).
    • Cryptographic Primitives:

      • Curves: SECP{192,224,256,384,521}R1, SECP{192,224,256}K1, BRAINPOOL, FRP256V1, GOST, SM2, WEI, BIGN, etc.
      • Hash Functions: SHA-2, SHA-3, SM3, RIPEMD-160, GOST 34.11-2012 (Streebog), SHAKE256, BELT-HASH, and BASH.
      • HMAC: Based on any supported hash function.
    • Advanced Implementations (Examples):

      • RSA (PKCS#1), DSA, SDSA, KCDSA, and Shamir Secret Sharing (SSS).
  4. Overview of libpkg

    main

    The pkg command-line tool is built on top of libpkg. This library provides the interface for package registration backends and abstracts core package management tasks, including:

    • Package registration
    • Remote repository management
    • Package creation
    • Package updating
  5. UCL Syntax: Basic Structure and JSON Compatibility

    main

    UCL is fully compatible with the JSON format and can parse any valid JSON file. However, it offers a more ergonomic syntax similar to nginx.

    Key differences in syntax:

    • Nginx-style: Uses = for assignment and {} for blocks. Supports suffixes for numbers (e.g., 10k, 0.2s).
    • JSON-style: Standard key-value pairs with quotes and colons.

    UCL allows you to mix these styles or use the more concise Nginx-like notation for easier manual editing.

    # Nginx-like UCL
    param = value;
    section {
        param1 = value1;
        flag = true;
        number = 10k;
        time = 0.2s;
        string = "something";
        subsection {
            host = { host = "hostname"; port = 900; }
        }
    }
  6. UCL Syntax: Improvements over JSON

    main

    UCL provides several syntactic improvements to make configuration files easier to write and maintain than strict JSON:

    General Syntax Sugar

    • Implicit Top-Level Object: Braces {} are not required for the top-level object.
    • Flexible Assignment: You can use = or : for assignment, or even skip the separator entirely for objects.
    • No Trailing Comma Errors: You can safely include trailing commas or semicolons in arrays and objects.
    • Unquoted Keys/Strings: Quotes are not strictly required for keys and simple strings.

    Automatic Array Creation

    If you define non-unique keys within an object, UCL automatically converts them into an array internally.

    Named Keys Hierarchy

    UCL allows you to define hierarchical objects using named keys. For example, section "name" { ... } creates a nested structure: section { name { ... } }.

    # Automatic array creation via non-unique keys
    key = "value1"
    key = "value2"
    # Results in: {"key": ["value1", "value2"]}
    
    # Named keys hierarchy
    section "blah" {
        key = value;
    }
    # Results in: {"section": {"blah": {"key": "value"}}}
  7. Configure libecc SCA countermeasures

    main

    libecc implements several approaches to limit Side Channel Attacks (SCA):

    • SPA (Simple Power Analysis): Thwarted using the Montgomery Ladder. You can optionally use the ADALWAYS=1 switch to use 'Double and Add Always' to avoid leaking the point at infinity.
    • DDPA (Data DPA): Thwarted using blinding of the point (projective coordinates) and the scalar. This must be manually enabled via BLINDING=1 due to performance costs.
    • ADPA (Address-bit DPA): Limited using the Itoh et al. Double and Add Always masked variant.

    Note: All countermeasures should be validated on your specific target hardware using leakage assessments, as C code behavior can vary based on compiler optimizations and CPU microarchitecture.

  8. Use Lua scripts for sandboxed package automation

    main

    Lua scripts can be used for package automation and always run before shell scripts. They are designed for security and portability:

    • Sandboxing: They run in a Capsicum sandbox, restricting them to filesystem access only.
    • No External Execution: They are prevented from executing external programs, making them cross-installation friendly.
    • Rootdir Friendly: They run in a modified Lua environment where all I/O operations are seamlessly compatible with rootdir operations.

    Supported Phases:

    • post-install
    • pre-install
    • post-deinstall
    • pre-deinstall

    For the full API specification, refer to pkg-lua-script(5).

  9. Use Variables in UCL

    main

    UCL supports variable expansion in input strings using the following formats:

    • ${VARIABLE}
    • $VARIABLE

    Note: Nested variables are not currently supported. To escape a dollar sign, use double dollar signs:

    • $${VARIABLE} $\rightarrow$ ${VARIABLE}
    • $$VARIABLE $\rightarrow$ $VARIABLE
  10. Use shell scripts in pkg lifecycle phases

    main

    pkg supports shell scripts that run via /bin/sh during specific package lifecycle phases. To prevent scripts from spawning persistent daemons, pkg acts as a 'reaper' and will kill any remaining child processes once the script finishes.

    Supported Phases:

    • post-install
    • pre-install
    • post-deinstall
    • pre-deinstall

    Available Environment Variables:

    • PKG_PREFIX
    • PKG_ROOTDIR
    • PKG_MSGFD
    • PKG_UPGRADE

    For detailed documentation, refer to pkg-script(5).

  11. Validate UCL objects using JSON Schema v4

    main

    UCL supports object validation using the JSON Schema v4 standard. You can use UCL syntax to define your schemas, which is often simpler than using JSON.

    Key validation features include:

    • Full JSON Schema support: Supports the standard set of JSON schema features (excluding remote references).
    • Multi-value constraints: Because UCL allows multiple values for a single key, you can use maxValues and minValues to define constraints on the number of values allowed for that key.

    Note: To ensure reliable validation, always provide schemas that strictly adhere to the JSON Schema draft v4 specification.

  12. Choosing between deterministic and non-deterministic ECDSA/BIGN

    main

    libecc provides both classical (non-deterministic) and deterministic variants for ECDSA and BIGN. The choice depends heavily on your attack model and usage context:

    • Deterministic Variants (e.g., RFC 6979 for ECDSA):

      • Pros: Suitable for environments with no high-quality RNG or weak entropy sources.
      • Cons: Susceptible to fault attacks. If a fault is injected during computation, the private key can be leaked.
    • Non-deterministic Variants:

      • Pros: Resistant to fault attacks.
      • Cons: Requires a high-quality, reliable RNG. Any leak of the nonce bits can lead to devastating attacks (e.g., via the Hidden Number Problem).

    Recommendation: Carefully evaluate whether your platform is more susceptible to side-channel/fault attacks or entropy exhaustion before selecting a variant.