synta-python 0.3.0

Python extension module for the synta ASN.1 library
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Python bindings for RFC 5755 Attribute Certificate types.
//!
//! Exposes ``AttributeCertificate`` as a Python class and installs OID constants
//! into the ``synta.ac`` submodule.

use std::sync::OnceLock;

use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyString};

use synta::traits::Encode;
use synta::{Decoder, Encoding};

use crate::error::SyntaErr;
use crate::types::PyObjectIdentifier;

// ── helpers ───────────────────────────────────────────────────────────────────

/// Encode an arbitrary `T: Encode` value to DER bytes.
fn encode_to_der<T: Encode>(v: &T) -> Vec<u8> {
    let mut enc = synta::Encoder::new(Encoding::Der);
    if v.encode(&mut enc).is_err() {
        return Vec::new();
    }
    enc.finish().unwrap_or_default()
}

// ── PyAttributeCertificate ────────────────────────────────────────────────────

/// X.509 Attribute Certificate v2 (RFC 5755).
///
/// An Attribute Certificate (AC) binds a set of attributes (roles, clearances,
/// service-authentication information) to a holder identified by reference to
/// their Public Key Certificate (PKC), without requiring re-issuance of the PKC.
///
/// ```python,ignore
/// import synta.ac as ac
/// acer = ac.AttributeCertificate.from_der(open("attr.ac", "rb").read())
/// print(acer.serial_number.hex())
/// print(acer.not_before, "–", acer.not_after)
/// print(acer.signature_algorithm_oid)
/// ```
#[pyclass(frozen, name = "AttributeCertificate")]
pub struct PyAttributeCertificate {
    _data: Py<PyBytes>,
    raw: &'static [u8],
    inner: OnceLock<Box<synta_certificate::attribute_cert_types::AttributeCertificate<'static>>>,
    // Field caches
    serial_number_cache: OnceLock<Py<PyBytes>>,
    not_before_cache: OnceLock<Py<PyString>>,
    not_after_cache: OnceLock<Py<PyString>>,
    signature_algorithm_oid_cache: OnceLock<Py<PyObjectIdentifier>>,
    signature_cache: OnceLock<Py<PyBytes>>,
    holder_der_cache: OnceLock<Py<PyBytes>>,
    issuer_der_cache: OnceLock<Py<PyBytes>>,
    attributes_der_cache: OnceLock<Py<PyBytes>>,
}

impl PyAttributeCertificate {
    fn ac(
        &self,
    ) -> PyResult<&synta_certificate::attribute_cert_types::AttributeCertificate<'static>> {
        if let Some(v) = self.inner.get() {
            return Ok(v.as_ref());
        }
        let mut dec = Decoder::new(self.raw, Encoding::Der);
        let decoded = dec
            .decode::<synta_certificate::attribute_cert_types::AttributeCertificate<'static>>()
            .map_err(SyntaErr)?;
        let _ = self.inner.set(Box::new(decoded));
        Ok(self.inner.get().unwrap().as_ref())
    }
}

#[pymethods]
impl PyAttributeCertificate {
    /// Parse a DER-encoded ``AttributeCertificate`` SEQUENCE.
    ///
    /// :param data: DER bytes of the ``AttributeCertificate``.
    /// :raises ValueError: if the bytes cannot be decoded.
    #[staticmethod]
    fn from_der(py: Python<'_>, data: Bound<'_, PyBytes>) -> PyResult<Self> {
        let py_bytes = data.unbind();
        {
            let raw = py_bytes.as_bytes(py);
            Decoder::new(raw, Encoding::Der)
                .decode::<synta_certificate::attribute_cert_types::AttributeCertificate<'_>>()
                .map_err(SyntaErr)?;
        }
        let raw: &'static [u8] = unsafe {
            let s = py_bytes.bind(py).as_bytes();
            std::slice::from_raw_parts(s.as_ptr(), s.len())
        };
        Ok(Self {
            _data: py_bytes,
            raw,
            inner: OnceLock::new(),
            serial_number_cache: OnceLock::new(),
            not_before_cache: OnceLock::new(),
            not_after_cache: OnceLock::new(),
            signature_algorithm_oid_cache: OnceLock::new(),
            signature_cache: OnceLock::new(),
            holder_der_cache: OnceLock::new(),
            issuer_der_cache: OnceLock::new(),
            attributes_der_cache: OnceLock::new(),
        })
    }

    /// Return the DER encoding of this ``AttributeCertificate``.
    fn to_der<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        Ok(PyBytes::new(py, &self.ac()?.to_der().map_err(SyntaErr)?))
    }

    /// Parse the first ``ATTRIBUTE CERTIFICATE`` PEM block from ``data``.
    ///
    /// ```python,ignore
    /// import synta.ac as ac
    /// acer = ac.AttributeCertificate.from_pem(open("attr.pem", "rb").read())
    /// print(acer.serial_number.hex())
    /// ```
    ///
    /// :raises ValueError: if no valid PEM block is found or the DER is invalid.
    #[staticmethod]
    fn from_pem(py: Python<'_>, data: &[u8]) -> PyResult<Self> {
        let blocks = synta_certificate::pem_blocks(data);
        let der = match blocks.as_slice() {
            [] => {
                return Err(pyo3::exceptions::PyValueError::new_err(
                    "no PEM block found in input",
                ))
            }
            [(_, first), ..] => first,
        };
        let py_bytes = pyo3::types::PyBytes::new(py, der).unbind();
        {
            let raw = py_bytes.as_bytes(py);
            synta::Decoder::new(raw, Encoding::Der)
                .decode::<synta_certificate::attribute_cert_types::AttributeCertificate<'_>>()
                .map_err(SyntaErr)?;
        }
        let raw: &'static [u8] = unsafe {
            let s = py_bytes.bind(py).as_bytes();
            std::slice::from_raw_parts(s.as_ptr(), s.len())
        };
        Ok(Self {
            _data: py_bytes,
            raw,
            inner: OnceLock::new(),
            serial_number_cache: OnceLock::new(),
            not_before_cache: OnceLock::new(),
            not_after_cache: OnceLock::new(),
            signature_algorithm_oid_cache: OnceLock::new(),
            signature_cache: OnceLock::new(),
            holder_der_cache: OnceLock::new(),
            issuer_der_cache: OnceLock::new(),
            attributes_der_cache: OnceLock::new(),
        })
    }

    /// Return the PEM encoding of this ``AttributeCertificate``.
    ///
    /// ```python,ignore
    /// import synta.ac as ac
    /// acer = ac.AttributeCertificate.from_der(open("attr.ac", "rb").read())
    /// open("attr.pem", "wb").write(acer.to_pem())
    /// ```
    fn to_pem<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
        let pem = synta_certificate::der_to_pem("ATTRIBUTE CERTIFICATE", self.raw);
        PyBytes::new(py, &pem)
    }

    /// ``AttCertVersion`` integer (always ``1`` for v2 per RFC 5755).
    #[getter]
    fn version(&self) -> PyResult<i64> {
        Ok(self.ac()?.acinfo.version.as_i64().unwrap_or(1))
    }

    /// Certificate serial number as big-endian bytes.
    #[getter]
    fn serial_number<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        if let Some(c) = self.serial_number_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let b = PyBytes::new(py, self.ac()?.acinfo.serial_number.as_bytes());
        let _ = self.serial_number_cache.set(b.as_unbound().clone_ref(py));
        Ok(b)
    }

    /// Validity period start time (GeneralizedTime string, e.g. ``"20240101120000Z"``).
    #[getter]
    fn not_before<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyString>> {
        if let Some(c) = self.not_before_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let s = self
            .ac()?
            .acinfo
            .attr_cert_validity_period
            .not_before_time
            .to_string();
        let ps = PyString::new(py, &s);
        let _ = self.not_before_cache.set(ps.as_unbound().clone_ref(py));
        Ok(ps)
    }

    /// Validity period end time (GeneralizedTime string).
    #[getter]
    fn not_after<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyString>> {
        if let Some(c) = self.not_after_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let s = self
            .ac()?
            .acinfo
            .attr_cert_validity_period
            .not_after_time
            .to_string();
        let ps = PyString::new(py, &s);
        let _ = self.not_after_cache.set(ps.as_unbound().clone_ref(py));
        Ok(ps)
    }

    /// Signature algorithm OID.
    #[getter]
    fn signature_algorithm_oid(&self, py: Python<'_>) -> PyResult<Py<PyObjectIdentifier>> {
        if let Some(c) = self.signature_algorithm_oid_cache.get() {
            return Ok(c.clone_ref(py));
        }
        let oid = self.ac()?.acinfo.signature.algorithm.clone();
        let obj = Py::new(py, PyObjectIdentifier::from_oid(oid))?;
        let _ = self.signature_algorithm_oid_cache.set(obj.clone_ref(py));
        Ok(obj)
    }

    /// Raw signature bytes (the bit-string value, zero-byte padding stripped).
    #[getter]
    fn signature<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        if let Some(c) = self.signature_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let b = PyBytes::new(py, self.ac()?.signature.as_bytes());
        let _ = self.signature_cache.set(b.as_unbound().clone_ref(py));
        Ok(b)
    }

    /// Raw DER bytes of the ``Holder`` SEQUENCE (for re-decoding or inspection).
    #[getter]
    fn holder_der<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        if let Some(c) = self.holder_der_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let der = encode_to_der(&self.ac()?.acinfo.holder);
        let b = PyBytes::new(py, &der);
        let _ = self.holder_der_cache.set(b.as_unbound().clone_ref(py));
        Ok(b)
    }

    /// Raw DER bytes of the ``AttCertIssuer`` CHOICE (for re-decoding or inspection).
    #[getter]
    fn issuer_der<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        if let Some(c) = self.issuer_der_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let der = encode_to_der(&self.ac()?.acinfo.issuer);
        let b = PyBytes::new(py, &der);
        let _ = self.issuer_der_cache.set(b.as_unbound().clone_ref(py));
        Ok(b)
    }

    /// Raw DER bytes of the ``SEQUENCE OF Attribute`` attributes field.
    ///
    /// Each ``Attribute`` in the sequence can carry roles, clearances,
    /// or service-authentication information.  Re-decode with a ``Decoder``
    /// to inspect individual attributes.
    #[getter]
    fn attributes_der<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        if let Some(c) = self.attributes_der_cache.get() {
            return Ok(c.clone_ref(py).into_bound(py));
        }
        let der = encode_to_der(&self.ac()?.acinfo.attributes);
        let b = PyBytes::new(py, &der);
        let _ = self.attributes_der_cache.set(b.as_unbound().clone_ref(py));
        Ok(b)
    }

    /// Verify that this Attribute Certificate was signed by ``issuer``.
    ///
    /// Verifies the outer signature against the issuer's public key.  Note that
    /// AC issuers are represented as ``AttCertIssuer`` (a GeneralNames CHOICE),
    /// not a plain Name, so no issuer-to-subject name matching is performed
    /// here — only the cryptographic signature is checked.
    ///
    /// :raises ValueError: if the AC carries no ``responseBytes``, or the
    ///     signature is invalid.
    ///
    /// ```python,ignore
    /// import synta.ac as ac
    /// ca_cert = synta.Certificate.from_pem(open("ca.pem", "rb").read())
    /// acer = ac.AttributeCertificate.from_der(open("attr.ac", "rb").read())
    /// acer.verify_issued_by(ca_cert)   # raises ValueError if not valid
    /// ```
    fn verify_issued_by(&self, issuer: &super::cert::PyCertificate) -> PyResult<()> {
        use synta_certificate::{default_signature_verifier, SignatureVerifier};

        let ac = self.ac()?;
        let issuer_cert = issuer.cert()?;

        // Re-encode AttributeCertificateInfo (TBS).
        let tbs_der = encode_to_der(&ac.acinfo);
        if tbs_der.is_empty() {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "failed to encode AttributeCertificateInfo",
            ));
        }

        // Re-encode outer signatureAlgorithm.
        let sig_alg_der = encode_to_der(&ac.signature_algorithm);
        if sig_alg_der.is_empty() {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "failed to encode AC signatureAlgorithm",
            ));
        }

        // Re-encode issuer SPKI.
        let spki_der = encode_to_der(&issuer_cert.tbs_certificate.subject_public_key_info);
        if spki_der.is_empty() {
            return Err(pyo3::exceptions::PyValueError::new_err(
                "failed to encode issuer SubjectPublicKeyInfo",
            ));
        }

        // Verify using the backend-agnostic verifier.
        default_signature_verifier()
            .verify_certificate_signature(
                &tbs_der,
                &sig_alg_der,
                ac.signature.as_bytes(),
                &spki_der,
            )
            .map_err(|e| {
                pyo3::exceptions::PyValueError::new_err(format!("AC signature invalid: {e}"))
            })
    }

    fn __repr__(&self) -> PyResult<String> {
        let ac = self.ac()?;
        Ok(format!(
            "AttributeCertificate(serial={})",
            ac.acinfo
                .serial_number
                .as_bytes()
                .iter()
                .map(|b| format!("{b:02x}"))
                .collect::<String>(),
        ))
    }
}

// ── register_ac_submodule ─────────────────────────────────────────────────────

/// Build and register the ``synta.ac`` submodule.
pub(super) fn register_ac_submodule(parent: &Bound<'_, PyModule>) -> PyResult<()> {
    let py = parent.py();
    let m = PyModule::new(py, "ac")?;

    m.add_class::<PyAttributeCertificate>()?;
    m.add_class::<PyAttributeCertificateBuilder>()?;

    // ── RFC 5755 OIDs ─────────────────────────────────────────────────────────
    m.add(
        "ID_PE_AC_AUDIT_IDENTITY",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_PE_AC_AUDIT_IDENTITY,
        ),
    )?;
    m.add(
        "ID_PE_AA_CONTROLS",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_PE_AA_CONTROLS,
        ),
    )?;
    m.add(
        "ID_PE_AC_PROXYING",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_PE_AC_PROXYING,
        ),
    )?;
    m.add(
        "ID_CE_TARGET_INFORMATION",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_CE_TARGET_INFORMATION,
        ),
    )?;
    m.add(
        "ID_ACA_AUTHENTICATION_INFO",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_ACA_AUTHENTICATION_INFO,
        ),
    )?;
    m.add(
        "ID_ACA_ACCESS_IDENTITY",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_ACA_ACCESS_IDENTITY,
        ),
    )?;
    m.add(
        "ID_ACA_CHARGING_IDENTITY",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_ACA_CHARGING_IDENTITY,
        ),
    )?;
    m.add(
        "ID_ACA_GROUP",
        super::oid_const(py, synta_certificate::attribute_cert_types::ID_ACA_GROUP),
    )?;
    m.add(
        "ID_ACA_ENC_ATTRS",
        super::oid_const(
            py,
            synta_certificate::attribute_cert_types::ID_ACA_ENC_ATTRS,
        ),
    )?;
    m.add(
        "ID_AT_ROLE",
        super::oid_const(py, synta_certificate::attribute_cert_types::ID_AT_ROLE),
    )?;
    m.add(
        "ID_AT_CLEARANCE",
        super::oid_const(py, synta_certificate::attribute_cert_types::ID_AT_CLEARANCE),
    )?;

    crate::install_submodule(
        parent,
        &m,
        "synta.ac",
        Some(concat!(
            "synta.ac — RFC 5755 Attribute Certificate v2 types.\n\n",
            "Provides AttributeCertificate for decoding X.509 Attribute\n",
            "Certificates that bind roles, clearances, or service-auth\n",
            "attributes to a holder's PKC, along with OID constants for\n",
            "RFC 5755 extensions and attribute types.",
        )),
    )
}

// ── PyAttributeCertificateBuilder ────────────────────────────────────────────

/// Builder for RFC 5755 Attribute Certificate TBS encoding.
///
/// Use the fluent setter methods to configure the certificate, then call
/// :meth:`build` to obtain the DER-encoded ``AttributeCertificateInfo``
/// (TBS) SEQUENCE.
///
/// ```python,ignore
/// import synta.ac as ac
///
/// tbs_der = (
///     ac.AttributeCertificateBuilder()
///     .serial_number(42)
///     .not_before("20240101120000Z")
///     .not_after("20250101120000Z")
///     .issuer_rfc822("ca@example.com")
///     .holder_entity_name_rfc822("user@example.com")
///     .build()
/// )
/// ```
#[pyclass(name = "AttributeCertificateBuilder")]
pub struct PyAttributeCertificateBuilder {
    inner: synta_certificate::AttributeCertificateBuilder,
}

#[pymethods]
impl PyAttributeCertificateBuilder {
    /// Create a new, empty ``AttributeCertificateBuilder``.
    #[new]
    fn new() -> Self {
        Self {
            inner: synta_certificate::AttributeCertificateBuilder::new(),
        }
    }

    /// Set the AC serial number.
    ///
    /// ```python,ignore
    /// builder.serial_number(42)
    /// ```
    fn serial_number<'py>(slf: Bound<'py, Self>, n: i64) -> Bound<'py, Self> {
        let old = std::mem::replace(
            &mut slf.borrow_mut().inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        slf.borrow_mut().inner = old.serial_number(n);
        slf
    }

    /// Set the validity start time as a GeneralizedTime string (``"YYYYMMDDHHmmssZ"``).
    ///
    /// :raises ValueError: if the string is not a valid GeneralizedTime.
    ///
    /// ```python,ignore
    /// builder.not_before("20240101120000Z")
    /// ```
    fn not_before<'py>(slf: Bound<'py, Self>, s: &str) -> Bound<'py, Self> {
        let old = std::mem::replace(
            &mut slf.borrow_mut().inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        slf.borrow_mut().inner = old.not_before(s);
        slf
    }

    /// Set the validity end time as a GeneralizedTime string (``"YYYYMMDDHHmmssZ"``).
    ///
    /// :raises ValueError: if the string is not a valid GeneralizedTime.
    ///
    /// ```python,ignore
    /// builder.not_after("20250101120000Z")
    /// ```
    fn not_after<'py>(slf: Bound<'py, Self>, s: &str) -> Bound<'py, Self> {
        let old = std::mem::replace(
            &mut slf.borrow_mut().inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        slf.borrow_mut().inner = old.not_after(s);
        slf
    }

    /// Add an ``rfc822Name`` GeneralName to the ``AttCertIssuer`` ``v1Form``.
    ///
    /// ```python,ignore
    /// builder.issuer_rfc822("ca@example.com")
    /// ```
    fn issuer_rfc822<'py>(slf: Bound<'py, Self>, email: &str) -> Bound<'py, Self> {
        let old = std::mem::replace(
            &mut slf.borrow_mut().inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        slf.borrow_mut().inner = old.issuer_rfc822(email);
        slf
    }

    /// Add a ``dNSName`` GeneralName to the ``AttCertIssuer`` ``v1Form``.
    ///
    /// ```python,ignore
    /// builder.issuer_dns("ca.example.com")
    /// ```
    fn issuer_dns<'py>(slf: Bound<'py, Self>, name: &str) -> Bound<'py, Self> {
        let old = std::mem::replace(
            &mut slf.borrow_mut().inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        slf.borrow_mut().inner = old.issuer_dns(name);
        slf
    }

    /// Add an ``rfc822Name`` GeneralName to the ``Holder.entityName``.
    ///
    /// ```python,ignore
    /// builder.holder_entity_name_rfc822("user@example.com")
    /// ```
    fn holder_entity_name_rfc822<'py>(slf: Bound<'py, Self>, email: &str) -> Bound<'py, Self> {
        let old = std::mem::replace(
            &mut slf.borrow_mut().inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        slf.borrow_mut().inner = old.holder_entity_name_rfc822(email);
        slf
    }

    /// Encode the ``AttributeCertificateInfo`` SEQUENCE to DER bytes.
    ///
    /// :raises ValueError: if any required field is missing or encoding fails.
    ///
    /// ```python,ignore
    /// tbs_der = builder.build()
    /// ```
    fn build<'py>(&mut self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
        let inner = std::mem::replace(
            &mut self.inner,
            synta_certificate::AttributeCertificateBuilder::new(),
        );
        let der = inner
            .build()
            .map_err(pyo3::exceptions::PyValueError::new_err)?;
        Ok(PyBytes::new(py, &der))
    }

    fn __repr__(&self) -> String {
        "AttributeCertificateBuilder()".to_string()
    }
}