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
use crate::{
    attestation::FieldKind,
    hash::{HashAlgId, DEFAULT_SUPPORTED_HASH_ALGS},
    signing::SignatureAlgId,
};

const DEFAULT_SUPPORTED_FIELDS: &[FieldKind] = &[
    FieldKind::ConnectionInfo,
    FieldKind::ServerEphemKey,
    FieldKind::ServerIdentityCommitment,
    FieldKind::EncodingCommitment,
];

#[derive(Debug)]
#[allow(dead_code)]
enum ErrorKind {
    Builder,
}

impl std::fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorKind::Builder => write!(f, "builder"),
        }
    }
}

/// Error for [`AttestationConfig`].
#[derive(Debug, thiserror::Error)]
#[error("attestation config error: kind: {kind}, reason: {reason}")]
pub struct AttestationConfigError {
    kind: ErrorKind,
    reason: String,
}

impl AttestationConfigError {
    #[allow(dead_code)]
    fn builder(reason: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Builder,
            reason: reason.into(),
        }
    }
}

/// Attestation configuration.
#[derive(Debug, Clone)]
pub struct AttestationConfig {
    supported_signature_algs: Vec<SignatureAlgId>,
    supported_hash_algs: Vec<HashAlgId>,
    supported_fields: Vec<FieldKind>,
}

impl AttestationConfig {
    /// Creates a new builder.
    pub fn builder() -> AttestationConfigBuilder {
        AttestationConfigBuilder::default()
    }

    pub(crate) fn supported_signature_algs(&self) -> &[SignatureAlgId] {
        &self.supported_signature_algs
    }

    pub(crate) fn supported_hash_algs(&self) -> &[HashAlgId] {
        &self.supported_hash_algs
    }

    pub(crate) fn supported_fields(&self) -> &[FieldKind] {
        &self.supported_fields
    }
}

/// Builder for [`AttestationConfig`].
#[derive(Debug)]
pub struct AttestationConfigBuilder {
    supported_signature_algs: Vec<SignatureAlgId>,
    supported_hash_algs: Vec<HashAlgId>,
    supported_fields: Vec<FieldKind>,
}

impl Default for AttestationConfigBuilder {
    fn default() -> Self {
        Self {
            supported_signature_algs: Vec::default(),
            supported_hash_algs: DEFAULT_SUPPORTED_HASH_ALGS.to_vec(),
            supported_fields: DEFAULT_SUPPORTED_FIELDS.to_vec(),
        }
    }
}

impl AttestationConfigBuilder {
    /// Sets the supported signature algorithms.
    pub fn supported_signature_algs(
        &mut self,
        supported_signature_algs: impl Into<Vec<SignatureAlgId>>,
    ) -> &mut Self {
        self.supported_signature_algs = supported_signature_algs.into();
        self
    }

    /// Sets the supported hash algorithms.
    pub fn supported_hash_algs(
        &mut self,
        supported_hash_algs: impl Into<Vec<HashAlgId>>,
    ) -> &mut Self {
        self.supported_hash_algs = supported_hash_algs.into();
        self
    }

    /// Sets the supported attestation fields.
    pub fn supported_fields(&mut self, supported_fields: impl Into<Vec<FieldKind>>) -> &mut Self {
        self.supported_fields = supported_fields.into();
        self
    }

    /// Builds the configuration.
    pub fn build(&self) -> Result<AttestationConfig, AttestationConfigError> {
        Ok(AttestationConfig {
            supported_signature_algs: self.supported_signature_algs.clone(),
            supported_hash_algs: self.supported_hash_algs.clone(),
            supported_fields: self.supported_fields.clone(),
        })
    }
}