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
macro_rules! const_assert {
($bool:expr, $msg:expr) => {
[$msg][!$bool as usize]
};
}
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Aid {
bytes: [u8; Self::MAX_LEN],
len: u8,
truncated_len: u8,
}
#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub enum Category {
International,
National,
Standard,
Proprietary,
Other,
}
impl core::fmt::Debug for Aid {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.len <= self.truncated_len {
f.write_fmt(format_args!("'{} {}'",
hexstr!(&self.bytes[..5]),
hexstr!(&self.bytes[5..self.len as _])))
} else {
f.write_fmt(format_args!("'{} {} {}'",
hexstr!(&self.bytes[..5]),
hexstr!(&self.bytes[5..self.truncated_len as _]),
hexstr!(&self.bytes[self.truncated_len as _..self.len as _])))
}
}
}
pub trait App {
fn aid(&self) -> Aid;
}
impl core::ops::Deref for Aid {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.as_bytes()
}
}
impl Aid {
const MAX_LEN: usize = 16;
pub fn as_bytes(&self) -> &[u8] {
&self.bytes[..self.len as usize]
}
pub fn truncated(&self) -> &[u8] {
&self.bytes[..self.truncated_len as usize]
}
pub fn matches(&self, aid: &[u8]) -> bool {
aid.starts_with(self.truncated())
}
pub const fn new(aid: &[u8]) -> Self {
Self::new_truncatable(aid, aid.len())
}
pub const fn new_truncatable(aid: &[u8], truncated_len: usize) -> Self {
const_assert!(!aid.is_empty(), "AID needs at least a category identifier");
const_assert!(aid.len() <= Self::MAX_LEN, "AID too long");
const_assert!(truncated_len <= aid.len(), "truncated length too long");
let mut s = Self { bytes: [0u8; Self::MAX_LEN], len: aid.len() as u8, truncated_len: truncated_len as u8 };
s = s.fill(aid, 0);
const_assert!(!s.is_national() || aid.len() >= 5, "National RID must have length 5");
const_assert!(!s.is_international() || aid.len() >= 5, "International RID must have length 5");
s
}
const fn fill(mut self, bytes: &[u8], i: usize) -> Self {
match i == bytes.len() {
true => self,
false => {
self.bytes[i] = bytes[i];
self.fill(bytes, i + 1)
}
}
}
pub const fn category(&self) -> Category {
match self.bytes[0] >> 4 {
b'A' => Category::International,
b'D' => Category::National,
b'E' => Category::Standard,
b'F' => Category::Proprietary,
_ => Category::Other,
}
}
pub const fn is_international(&self) -> bool {
match self.category() {
Category::International => true,
_ => false,
}
}
pub const fn is_national(&self) -> bool {
match self.category() {
Category::National => true,
_ => false,
}
}
pub const fn is_standard(&self) -> bool {
match self.category() {
Category::Standard => true,
_ => false,
}
}
pub const fn is_proprietary(&self) -> bool {
match self.category() {
Category::Proprietary => true,
_ => false,
}
}
const fn has_rid_pix(&self) -> bool {
self.is_national() || self.is_international()
}
pub fn rid(&self) -> Option<&[u8]> {
self.has_rid_pix().then(|| &self.bytes[..5])
}
pub fn pix(&self) -> Option<&[u8]> {
self.has_rid_pix().then(|| &self.bytes[5..])
}
}
#[cfg(test)]
mod test {
use super::Aid;
use hex_literal::hex;
#[allow(dead_code)]
const PIV_AID: Aid = Aid::new_truncatable(&hex!("A000000308 00001000 0100"), 9);
#[test]
fn aid() {
let piv_aid = Aid::new(&hex!("A000000308 00001000 0100"));
assert!(piv_aid.matches(&*PIV_AID));
assert!(PIV_AID.matches(&*piv_aid));
}
}