CodeSampleX

Sample

yasna 0.5.2: yasna::parse_ber

Verified sample for cargo yasna 0.5.2: yasna::parse_ber. The contract ran on rust 1 · linux alpine/x64 · docker and passed: yasna::parse_ber decodes ASN.1…

sha256:b6072a610dabac63fddb92ca28eaa013efda46477edf2d8946eb0699fe20682c

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux x64 cargo

Verification-run environments

Environment Contract Stages Run
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-11-27

Case

HOW
Goal
verify yasna::parse_ber in pkg:cargo/yasna@0.5.2
Packages
Symbols
  • yasna::parse_ber
Created
2026-11-27T02:37:17Z

Contract

  1. yasna::parse_ber decodes ASN.1 SEQUENCE containing i64, bool, UTF8String, OCTET STRING, and optional explicit context-specific tagged u64 via BERReader
  2. yasna::parse_ber accepts BER indefinite-length SEQUENCE (0x80 ... 0x00 0x00) that yasna::parse_der rejects with ASN1ErrorKind::Invalid
  3. yasna::parse_ber accepts BER non-minimal long-form length and non-canonical BOOLEAN true (0x01) that yasna::parse_der rejects with ASN1ErrorKind::Invalid
  4. yasna::parse_ber returns ASN1ErrorKind::Extra for trailing unconsumed bytes
  5. yasna::parse_ber returns ASN1ErrorKind::Eof for truncated input and ASN1ErrorKind::Invalid for mismatched ASN.1 tags

Files

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • test/contract.rs

Download the source artifact (tar.gz)

Source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "sample-yasna-parse-ber"
version = "1.0.0"
dependencies = [
 "yasna",
]

[[package]]
name = "yasna"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17bb3549cc1321ae1296b9cdc2698e2b6cb1992adfa19a8c72e5b7a738f44cd"
Cargo.toml
[package]
name = "sample-yasna-parse-ber"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
yasna = "=0.5.2"

[[bin]]
name = "contract"
path = "test/contract.rs"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify yasna::parse_ber in pkg:cargo/yasna@0.5.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/yasna@0.5.2
Demonstrate these symbols/APIs:
  - yasna::parse_ber

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:dbff53867e3433746cb5239f495288cabbdfdfd7c3f374f2304a3f10fc14749f","contract":["yasna::parse_ber decodes ASN.1 SEQUENCE containing i64, bool, UTF8String, OCTET STRING, and optional explicit context-specific tagged u64 via BERReader","yasna::parse_ber accepts BER indefinite-length SEQUENCE (0x80 ... 0x00 0x00) that yasna::parse_der rejects with ASN1ErrorKind::Invalid","yasna::parse_ber accepts BER non-minimal long-form length and non-canonical BOOLEAN true (0x01) that yasna::parse_der rejects with ASN1ErrorKind::Invalid","yasna::parse_ber returns ASN1ErrorKind::Extra for trailing unconsumed bytes","yasna::parse_ber returns ASN1ErrorKind::Eof for truncated input and ASN1ErrorKind::Invalid for mismatched ASN.1 tags"],"goal":"verify yasna::parse_ber in pkg:cargo/yasna@0.5.2","kind":"HOW","packages":["pkg:cargo/yasna@0.5.2"],"schemaVersion":1,"symbols":["yasna::parse_ber"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/yasna@0.5.2"],"schemaVersion":1,"subject":"pkg:cargo/yasna@0.5.2","symbols":["yasna::parse_ber"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify yasna::parse_ber in pkg:cargo/yasna@0.5.2",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/yasna@0.5.2"
  ],
  "symbols": [
    "yasna::parse_ber"
  ]
}
src/lib.rs
//! Clean-room verification sample for `yasna::parse_ber`.

use yasna::{parse_ber, ASN1Result, Tag};

/// Structured record decoded from an ASN.1 BER sequence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Record {
    pub id: i64,
    pub active: bool,
    pub label: String,
    pub payload: Vec<u8>,
    pub tag_code: Option<u64>,
}

/// Decodes a `Record` from BER-encoded bytes using `yasna::parse_ber`.
pub fn decode_record_ber(input: &[u8]) -> ASN1Result<Record> {
    parse_ber(input, |reader| {
        reader.read_sequence(|seq| {
            let id = seq.next().read_i64()?;
            let active = seq.next().read_bool()?;
            let label = seq.next().read_utf8string()?;
            let payload = seq.next().read_bytes()?;
            let tag_code = seq.read_optional(|opt| {
                opt.read_tagged(Tag::context(0), |inner| inner.read_u64())
            })?;
            Ok(Record {
                id,
                active,
                label,
                payload,
                tag_code,
            })
        })
    })
}
test/contract.rs
use sample_yasna_parse_ber::{decode_record_ber, Record};
use yasna::{construct_der, parse_ber, parse_der, ASN1ErrorKind, Tag};

fn main() {
    // 1. Decode a standard SEQUENCE containing INTEGER, BOOLEAN, UTF8String, OCTET STRING, and an optional context-tagged u64.
    let der_with_opt = construct_der(|writer| {
        writer.write_sequence(|seq| {
            seq.next().write_i64(-42);
            seq.next().write_bool(true);
            seq.next().write_utf8_string("sample");
            seq.next().write_bytes(&[0xde, 0xad, 0xbe, 0xef]);
            seq.next().write_tagged(Tag::context(0), |w| w.write_u64(9001));
        });
    });

    let decoded = decode_record_ber(&der_with_opt)
        .expect("parse_ber should decode a DER-encoded SEQUENCE with optional tagged field");
    assert_eq!(
        decoded,
        Record {
            id: -42,
            active: true,
            label: "sample".to_string(),
            payload: vec![0xde, 0xad, 0xbe, 0xef],
            tag_code: Some(9001),
        }
    );

    // 2. Decode a SEQUENCE when the trailing optional context-tagged field is absent.
    let der_without_opt = construct_der(|writer| {
        writer.write_sequence(|seq| {
            seq.next().write_i64(7);
            seq.next().write_bool(false);
            seq.next().write_utf8_string("minimal");
            seq.next().write_bytes(&[0x01, 0x02]);
        });
    });

    let decoded_minimal = decode_record_ber(&der_without_opt)
        .expect("parse_ber should handle absent optional field");
    assert_eq!(
        decoded_minimal,
        Record {
            id: 7,
            active: false,
            label: "minimal".to_string(),
            payload: vec![0x01, 0x02],
            tag_code: None,
        }
    );

    // 3. Accept BER indefinite-length SEQUENCE (0x30 0x80 ... 0x00 0x00), which parse_der rejects.
    let indefinite_seq: &[u8] = &[
        0x30, 0x80, // SEQUENCE, indefinite length
        0x02, 0x01, 0x0a, // INTEGER 10
        0x01, 0x01, 0xff, // BOOLEAN true
        0x00, 0x00, // EOC
    ];
    let pair = parse_ber(indefinite_seq, |reader| {
        reader.read_sequence(|seq| {
            let n = seq.next().read_i64()?;
            let flag = seq.next().read_bool()?;
            Ok((n, flag))
        })
    })
    .expect("parse_ber should accept indefinite-length BER SEQUENCE");
    assert_eq!(pair, (10, true));

    let der_err = parse_der(indefinite_seq, |reader| {
        reader.read_sequence(|seq| {
            let n = seq.next().read_i64()?;
            let flag = seq.next().read_bool()?;
            Ok((n, flag))
        })
    })
    .expect_err("parse_der must reject indefinite-length SEQUENCE");
    assert_eq!(der_err.kind(), ASN1ErrorKind::Invalid);

    // 4. Accept BER non-minimal long-form length and non-canonical BOOLEAN true (0x01), which parse_der rejects.
    let non_canonical_ber: &[u8] = &[
        0x30, 0x81, 0x07, // SEQUENCE, long-form length 7
        0x02, 0x81, 0x01, 0x15, // INTEGER 21, long-form length 1
        0x01, 0x01, 0x01, // BOOLEAN true (0x01 instead of DER 0xff)
    ];
    let non_canon_pair = parse_ber(non_canonical_ber, |reader| {
        reader.read_sequence(|seq| {
            let n = seq.next().read_i64()?;
            let flag = seq.next().read_bool()?;
            Ok((n, flag))
        })
    })
    .expect("parse_ber should accept non-minimal length and non-canonical BOOLEAN true");
    assert_eq!(non_canon_pair, (21, true));

    let der_non_canon_err = parse_der(non_canonical_ber, |reader| {
        reader.read_sequence(|seq| {
            let n = seq.next().read_i64()?;
            let flag = seq.next().read_bool()?;
            Ok((n, flag))
        })
    })
    .expect_err("parse_der must reject non-canonical BER encodings");
    assert_eq!(der_non_canon_err.kind(), ASN1ErrorKind::Invalid);

    // 5. Reject trailing extra bytes with ASN1ErrorKind::Extra.
    let with_trailing: &[u8] = &[0x02, 0x01, 0x05, 0x00];
    let extra_err = parse_ber(with_trailing, |reader| reader.read_i64())
        .expect_err("parse_ber must reject trailing bytes");
    assert_eq!(extra_err.kind(), ASN1ErrorKind::Extra);

    // 6. Reject truncated input with ASN1ErrorKind::Eof.
    let truncated: &[u8] = &[0x02, 0x02, 0x01];
    let eof_err = parse_ber(truncated, |reader| reader.read_i64())
        .expect_err("parse_ber must reject truncated input");
    assert_eq!(eof_err.kind(), ASN1ErrorKind::Eof);

    // 7. Reject mismatched ASN.1 tag with ASN1ErrorKind::Invalid.
    let wrong_tag: &[u8] = &[0x02, 0x01, 0x01];
    let tag_err = parse_ber(wrong_tag, |reader| reader.read_bool())
        .expect_err("parse_ber must reject tag mismatch");
    assert_eq!(tag_err.kind(), ASN1ErrorKind::Invalid);

    println!("CONTRACT_OK");
}

Origin Seeder

anonymous