structured-pkix 0.21.1

Borrowed X.509 certificates and provider-neutral certification path validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Original `DSTU4145Params`, read without group arithmetic or inheritance.

use super::{Basis, ByteOrder, Octets, Profile, identify};
use crate::algorithm::{AlgorithmIdentifier, Producer};
use crate::error::Located;
use crate::{Error, ErrorCode, Result};
use structured_asn1::memory::{Execution, Work};
use structured_asn1::{Element, Encoding, Integer, Oid, ScopedReader, Span, Tag, universal};

const FIELD: &str = "dstu4145.parameters";

/// Original nonnegative ASN.1 INTEGER with its complete encoded range.
#[derive(Debug, Clone, Copy)]
pub struct IntegerField<'a> {
    span: Span,
    value: Integer<'a>,
}

impl<'a> IntegerField<'a> {
    /// Original INTEGER value, not a narrowed backend scalar.
    #[must_use]
    pub const fn value(self) -> Integer<'a> {
        self.value
    }

    /// Original complete INTEGER range, including its tag and header.
    #[must_use]
    pub const fn span(self) -> Span {
        self.span
    }
}

/// Field representation, not a primality or irreducibility assertion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reduction {
    /// Polynomial x^m + x^k + 1.
    Trinomial(u32),
    /// Polynomial x^m + x^k + x^j + x^l + 1, with k < j < l.
    Pentanomial {
        /// Smallest nonzero reduction exponent.
        k: u32,
        /// Middle reduction exponent.
        j: u32,
        /// Largest reduction exponent, strictly below m.
        l: u32,
    },
    /// Omitted reduction polynomial for an optimal normal basis.
    OptimalNormal,
}

/// A written named-curve OID, including unknown names.
#[derive(Debug, Clone, Copy)]
pub struct NamedCurve<'a> {
    element: Element<'a>,
    identifier: Oid<'a>,
    known: Option<(Basis, u32)>,
}

impl<'a> NamedCurve<'a> {
    /// Exact written identity, never inferred from point width.
    #[must_use]
    pub const fn identifier(self) -> Oid<'a> {
        self.identifier
    }

    /// Original OID element and its source coordinates.
    #[must_use]
    pub const fn element(self) -> Element<'a> {
        self.element
    }

    /// Known field basis and degree, or None for an unsupported curve name.
    /// No mathematical parameters are synthesized for unknown names.
    #[must_use]
    pub const fn known(self) -> Option<(Basis, u32)> {
        self.known
    }
}

/// Written explicit curve components. No point, order or field math is done.
#[derive(Debug, Clone, Copy)]
pub struct ExplicitCurve<'a> {
    element: Element<'a>,
    degree: IntegerField<'a>,
    degree_value: u32,
    field: Span,
    reduction: Reduction,
    a: IntegerField<'a>,
    b: Octets<'a>,
    order: IntegerField<'a>,
    base_point: Octets<'a>,
}

impl<'a> ExplicitCurve<'a> {
    /// Whole original `ECBinary` SEQUENCE.
    #[must_use]
    pub const fn element(self) -> Element<'a> {
        self.element
    }
    /// Original complete `BinaryField` range, including reduction exponents.
    #[must_use]
    pub const fn field_span(self) -> Span {
        self.field
    }
    /// Original degree INTEGER.
    #[must_use]
    pub const fn degree(self) -> IntegerField<'a> {
        self.degree
    }
    /// Structural field degree, not a supported-backend assertion.
    #[must_use]
    pub const fn degree_value(self) -> u32 {
        self.degree_value
    }
    /// Polynomial exponents or optimal-normal-basis omission.
    #[must_use]
    pub const fn reduction(self) -> Reduction {
        self.reduction
    }
    /// Coefficient A, constrained to zero or one.
    #[must_use]
    pub const fn a(self) -> IntegerField<'a> {
        self.a
    }
    /// Coefficient B in the selected byte order, still unvalidated mathematically.
    #[must_use]
    pub const fn b(self) -> Octets<'a> {
        self.b
    }
    /// Positive subgroup-order candidate, not a primality assertion.
    #[must_use]
    pub const fn order(self) -> IntegerField<'a> {
        self.order
    }
    /// Compressed original base point; no decompression is attempted.
    #[must_use]
    pub const fn base_point(self) -> Octets<'a> {
        self.base_point
    }
}

/// The actual named or explicit CHOICE, without a guessed fallback.
#[derive(Debug, Clone, Copy)]
pub enum Curve<'a> {
    /// A named curve, possibly unknown to the bounded metadata table.
    Named(NamedCurve<'a>),
    /// Explicit original components.
    Explicit(ExplicitCurve<'a>),
}

impl Curve<'_> {
    /// Field degree when written explicitly or known from the exact curve OID.
    #[must_use]
    pub const fn degree(&self) -> Option<u32> {
        match self {
            Self::Named(curve) => match curve.known {
                Some((_, degree)) => Some(degree),
                None => None,
            },
            Self::Explicit(curve) => Some(curve.degree_value),
        }
    }
}

/// Borrowed original `DSTU4145Params`. Storage and arithmetic belong to callers.
#[derive(Debug, Clone, Copy)]
pub struct Parameters<'a> {
    element: Element<'a>,
    curve: Curve<'a>,
    dke: Option<Element<'a>>,
    producer: Producer<'a>,
}

impl<'a> Parameters<'a> {
    /// Retains the borrowed producer for deferred, explicitly funded backends.
    /// This is a resource origin, not a parameter or document identity.
    #[must_use]
    pub const fn execution_origin(self) -> Producer<'a> {
        self.producer
    }
    /// Whole original parameter SEQUENCE, without canonicalization.
    #[must_use]
    pub const fn element(self) -> Element<'a> {
        self.element
    }
    /// Original named or explicit curve.
    #[must_use]
    pub const fn curve(self) -> Curve<'a> {
        self.curve
    }
    /// Written packed 64-octet DKE, or None for normative DKE number one.
    /// Absence never inherits an authority's DKE. Provider selection of the
    /// default is separate from parsing (§§3.11.1 and 3.12 of the profile).
    #[must_use]
    pub const fn dke(self) -> Option<Element<'a>> {
        self.dke
    }
    /// Funds deferred interpretation from the original and current ceilings.
    #[must_use]
    pub fn execution_in<'q, W: Work>(&self, current: &'q W) -> Execution<'q>
    where
        'a: 'q,
    {
        self.producer.execution_in(current)
    }
}

/// Reads key-role parameters under the producing parser and current Work.
///
/// Returns None for actual absence, not NULL and not inherited EC parameters.
/// The pinned profile §§3.10–3.12 requires DER and a 64-octet packed DKE.
/// Unknown named curves remain inspectable with `known() == None`; neither
/// that fact nor readable explicit components permits a provider operation.
/// <https://zakon.rada.gov.ua/laws/show/z1398-12>
///
/// # Errors
/// Another algorithm, malformed/extra fields, unsupported explicit version,
/// written DER DEFAULT, or the original located resource/processing failure.
pub fn read_parameters<'a, W: Work>(
    identifier: AlgorithmIdentifier<'a>,
    current: &W,
) -> Result<Option<Parameters<'a>>> {
    let profile = identify(identifier, current)?
        .ok_or_else(|| Error::new(ErrorCode::UnexpectedField, FIELD, identifier.offset()))?;
    read_known(identifier, profile, current)
}

pub(crate) fn read_known<'a, W: Work>(
    identifier: AlgorithmIdentifier<'a>,
    profile: Profile,
    current: &W,
) -> Result<Option<Parameters<'a>>> {
    let Some(source) = identifier.parameters_nested() else {
        return Ok(None);
    };
    let (reader, element) = source.open(current, FIELD)?;
    let value = if reader.encoding().is_der() {
        fields(&reader, element, profile, identifier.execution_origin())?
    } else {
        let mut strict = reader.reread(&element, Encoding::Der).field(FIELD)?;
        let element = strict
            .next_element()
            .field(FIELD)?
            .ok_or_else(|| Error::new(ErrorCode::MissingField, FIELD, strict.offset()))?;
        fields(&strict, element, profile, identifier.execution_origin())?
    };
    Ok(Some(value))
}

fn fields<'a>(
    reader: &ScopedReader<'a, '_>,
    element: Element<'a>,
    profile: Profile,
    producer: Producer<'a>,
) -> Result<Parameters<'a>> {
    require_tag(element, Tag::constructed(universal::SEQUENCE))?;
    let mut inside = reader.enter(&element).field(FIELD)?;
    let first = next(&mut inside)?;
    let curve = if first.tag() == Tag::primitive(universal::OID) {
        let identifier = inside.oid(&first).field(FIELD)?;
        inside
            .charge(identifier.as_bytes().len() as u64 + 1)
            .field(FIELD)?;
        let known = named(identifier);
        if let Some((basis, _)) = known
            && basis != profile.basis()
        {
            return Err(Error::new(
                ErrorCode::MalformedValue,
                FIELD,
                first.span().start,
            ));
        }
        Curve::Named(NamedCurve {
            element: first,
            identifier,
            known,
        })
    } else {
        Curve::Explicit(explicit(&inside, first, profile)?)
    };
    let dke = inside.next_element().field(FIELD)?;
    if let Some(dke) = dke {
        require_tag(dke, Tag::primitive(universal::OCTET_STRING))?;
        if dke.contents().len() != 64 {
            return Err(Error::new(
                ErrorCode::MalformedValue,
                FIELD,
                dke.span().start,
            ));
        }
    }
    exhausted(&inside)?;
    Ok(Parameters {
        element,
        curve,
        dke,
        producer,
    })
}

fn explicit<'a>(
    reader: &ScopedReader<'a, '_>,
    element: Element<'a>,
    profile: Profile,
) -> Result<ExplicitCurve<'a>> {
    require_tag(element, Tag::constructed(universal::SEQUENCE))?;
    let mut inside = reader.enter(&element).field(FIELD)?;
    let field = next(&mut inside)?;
    if field.tag() == Tag::context(true, 0) {
        let mut version = inside.enter(&field).field(FIELD)?;
        let value = integer(&mut version)?;
        exhausted(&version)?;
        let value = number(&version, value)?;
        // X.690 §11.5: DEFAULT zero must not be explicitly written in DER.
        // https://www.itu.int/rec/T-REC-X.690-202102-I/en
        let code = if value == 0 {
            ErrorCode::DefaultWritten
        } else {
            ErrorCode::UnsupportedVersion
        };
        return Err(Error::new(code, FIELD, field.span().start));
    }
    require_tag(field, Tag::constructed(universal::SEQUENCE))?;
    let mut binary = inside.enter(&field).field(FIELD)?;
    let degree = integer(&mut binary)?;
    let degree_value = number(&binary, degree)?;
    if degree_value == 0 {
        return Err(Error::new(
            ErrorCode::MalformedValue,
            FIELD,
            degree.span.start,
        ));
    }
    let reduction = if profile.basis() == Basis::OptimalNormal {
        exhausted(&binary)?;
        Reduction::OptimalNormal
    } else {
        let written = next(&mut binary)?;
        let reduction = if written.tag() == Tag::primitive(universal::INTEGER) {
            let k = integer_element(&binary, written)?;
            let k = number(&binary, k)?;
            if k == 0 || k >= degree_value {
                return Err(Error::new(
                    ErrorCode::MalformedValue,
                    FIELD,
                    written.span().start,
                ));
            }
            Reduction::Trinomial(k)
        } else {
            require_tag(written, Tag::constructed(universal::SEQUENCE))?;
            let mut terms = binary.enter(&written).field(FIELD)?;
            let k = integer(&mut terms)?;
            let k = number(&terms, k)?;
            let j = integer(&mut terms)?;
            let j = number(&terms, j)?;
            let l = integer(&mut terms)?;
            let l = number(&terms, l)?;
            exhausted(&terms)?;
            if k == 0 || k >= j || j >= l || l >= degree_value {
                return Err(Error::new(
                    ErrorCode::MalformedValue,
                    FIELD,
                    written.span().start,
                ));
            }
            Reduction::Pentanomial { k, j, l }
        };
        exhausted(&binary)?;
        reduction
    };
    let a = integer(&mut inside)?;
    if number(&inside, a)? > 1 {
        return Err(Error::new(ErrorCode::MalformedValue, FIELD, a.span.start));
    }
    let b_element = next(&mut inside)?;
    let b = field_octets(&inside, b_element, degree_value, profile.byte_order())?;
    let order = integer(&mut inside)?;
    inside.charge(1).field(FIELD)?;
    if order.value.as_bytes() == [0] {
        return Err(Error::new(
            ErrorCode::MalformedValue,
            FIELD,
            order.span.start,
        ));
    }
    let point_element = next(&mut inside)?;
    let base_point = field_octets(&inside, point_element, degree_value, profile.byte_order())?;
    exhausted(&inside)?;
    Ok(ExplicitCurve {
        element,
        degree,
        degree_value,
        field: field.span(),
        reduction,
        a,
        b,
        order,
        base_point,
    })
}

pub(crate) fn field_octets<'a>(
    reader: &ScopedReader<'a, '_>,
    element: Element<'a>,
    degree: u32,
    byte_order: ByteOrder,
) -> Result<Octets<'a>> {
    require_tag(element, Tag::primitive(universal::OCTET_STRING))?;
    let bytes = element.contents();
    reader.charge(1).field(FIELD)?;
    if degree == 0 || bytes.len() as u64 != u64::from(degree).div_ceil(8) {
        return Err(Error::new(
            ErrorCode::MalformedValue,
            FIELD,
            element.span().start,
        ));
    }
    let unused = (8 - degree % 8) % 8;
    let top = match byte_order {
        ByteOrder::LittleEndian => bytes[bytes.len() - 1],
        ByteOrder::BigEndian => bytes[0],
    };
    if unused != 0 && top >> (8 - unused) != 0 {
        return Err(Error::new(
            ErrorCode::MalformedValue,
            FIELD,
            element.contents_span().start,
        ));
    }
    Ok(Octets::new(
        bytes,
        byte_order,
        element.contents_span().start,
    ))
}

fn named(identifier: Oid<'_>) -> Option<(Basis, u32)> {
    let (basis, index) = match identifier.as_bytes() {
        [0x2a, 0x86, 0x24, 2, 1, 1, 1, 1, 3, 1, basis, 2, index]
        | [
            0x2a,
            0x86,
            0x24,
            2,
            1,
            1,
            1,
            1,
            3,
            6,
            1..=3,
            basis,
            2,
            index,
        ] => (*basis, *index),
        _ => return None,
    };
    // Pinned OID table, named polynomial and optimal-normal-basis branches.
    // https://zakon.rada.gov.ua/laws/show/z1399-12
    let (basis, degrees): (_, &[u32]) = match basis {
        1 => (
            Basis::Polynomial,
            &[163, 167, 173, 179, 191, 233, 257, 307, 367, 431],
        ),
        2 => (Basis::OptimalNormal, &[173, 179, 191, 233, 431]),
        _ => return None,
    };
    degrees
        .get(usize::from(index))
        .map(|degree| (basis, *degree))
}

fn require_tag(element: Element<'_>, tag: Tag) -> Result<()> {
    if element.tag() != tag {
        return Err(Error::new(
            ErrorCode::Encoding(structured_asn1::ErrorCode::UnexpectedTag),
            FIELD,
            element.span().start,
        ));
    }
    Ok(())
}
fn next<'a>(reader: &mut ScopedReader<'a, '_>) -> Result<Element<'a>> {
    reader
        .next_element()
        .field(FIELD)?
        .ok_or_else(|| Error::new(ErrorCode::MissingField, FIELD, reader.offset()))
}
fn exhausted(reader: &ScopedReader<'_, '_>) -> Result<()> {
    if !reader.is_empty() {
        return Err(Error::new(ErrorCode::TrailingData, FIELD, reader.offset()));
    }
    Ok(())
}
fn integer<'a>(reader: &mut ScopedReader<'a, '_>) -> Result<IntegerField<'a>> {
    let element = next(reader)?;
    integer_element(reader, element)
}
fn integer_element<'a>(
    reader: &ScopedReader<'a, '_>,
    element: Element<'a>,
) -> Result<IntegerField<'a>> {
    require_tag(element, Tag::primitive(universal::INTEGER))?;
    let value = reader.integer(&element).field(FIELD)?;
    reader.charge(1).field(FIELD)?;
    if value.is_negative() {
        return Err(Error::new(
            ErrorCode::MalformedValue,
            FIELD,
            element.span().start,
        ));
    }
    Ok(IntegerField {
        span: element.span(),
        value,
    })
}
fn number(reader: &ScopedReader<'_, '_>, value: IntegerField<'_>) -> Result<u32> {
    reader
        .charge(value.value.as_bytes().len() as u64)
        .field(FIELD)?;
    value
        .value
        .as_u32()
        .ok_or_else(|| Error::new(ErrorCode::ValueTooLarge, FIELD, value.span.start))
}