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
//! [ISO/IEC DIS 18013-5](https://www.iso.org/standard/69084.html) `mDL` implementation in Rust.
//!
//! It is intended
//! to be used
//! in creating apps for `Devices` and `Readers` that can interact with each other to exchange `mDL`
//! data.
//!
//! ## Simulated `Device` and `Reader` interaction
//!
//! Here are examples of how to use the library. You can see more in [examples](../examples) folder and read about in the dedicated [README](../examples/README.md).
//!
//! This example demonstrates a simulated device and reader interaction.
//! The reader requests the `age_over_21` element, and the device responds with that value.
//! The flow is something like this:
//!
//! ```text
//! +---------------------+ +----------------------+
//! | | | |
//! | | | |
//! | Device | | Reader |
//! | | | |
//! | | | |
//! +---------+-----------+ +----------+-----------+
//! | |
//! Initialize session |
//! | |
//! | |
//! Create QR code engagement |
//! | |
//! +-------------+ |
//! | | |
//! | | |
//! <-------------+ |
//! | |
//! | Send QR code |
//! +----------------------------------------------------------------->
//! | |
//! | | Establish session
//! | +-----------+
//! | | |
//! | | |
//! | +-----------+
//! | Request age_over_21 |
//! <-----------------------------------------------------------------+
//! | |
//! | |
//! | |
//! | Send age_over_21 |
//! +----------------------------------------------------------------->
//! | |
//! | |
//! | | Process age_over_21
//! | +-----------+
//! | | |
//! | | |
//! | +-----------+
//! | |
//! | |
//! | |
//! | Session finished |
//! | |
//! ```
//!
//! 1. **Device initialization and engagement:**
//! - The device creates a `QR code` containing `DeviceEngagement` data, which includes its public key.
//! - Internally:
//! - The device initializes with the `mDL` data, private key, and public key.
//! 2. **Reader processing `QR code` and requesting the necessary fields:**
//! - The reader processes the QR code and creates a request for the `age_over_21` element.
//! - Internally:
//! - Generates its private and public keys.
//! - Initiates a key exchange, and generates the session keys.
//! - The request is encrypted with the reader's session key.
//! 3. **Device accepting request and responding:**
//! - The device receives the request and creates a response with the `age_over_21` element.
//! - Internally:
//! - Initiates the key exchange, and generates the session keys.
//! - Decrypts the request with the reader's session key.
//! - Parse and validate it creating error response if needed.
//! - The response is encrypted with the device's session key.
//! 4. **Reader Processing mDL data:**
//! - The reader processes the response and prints the value of the `age_over_21` element.
//!
//! ### Device perspective
//!
//! There are several states through which the device goes during the interaction:
//!
//! ```text
//!
//! +---------+
//! | |
//! | |
//! | User |
//! | |
//! | |
//! +---+-----+
//! |
//! |
//! +----------------------------------------------------v--------------------------------------------------------------+
//! | Device |
//! | |
//! | +-------------------+ |
//! | | | |
//! | |SessionManagerInit | |
//! | | | |
//! | +---------------------------------+ +-------------+ |
//! | | | | | |
//! | | | | | |
//! | | +-------------------+ | |
//! | | | |
//! | | qr_engagement |
//! | | | |
//! | | | |
//! | qr_engagement | |
//! | | | |
//! | | +----------v------------+ |
//! | | | | |
//! | | | SessionManagerEngaged <-------------+ |
//! | | +----------+ | | |
//! | | | | | | |
//! | | | +-----------------------+ | |
//! | | | | |
//! | | | | |
//! | | | | |
//! | | | | |
//! | | | | |
//! | | | | |
//! | | | | |
//! | | process_session_establishment | |
//! | | | | |
//! | | +--------------------------------------v----------------------------------+ | |
//! | | | SessionManager | | |
//! | | | | | |
//! | | | +--------------------+ | | |
//! | | | | | | | |
//! | | | +-----------------------+ AwaitingRequest <----------------+ | | |
//! | | | | | | | | | |
//! | | | | +-----------+ | | | | |
//! | | |prepare_response | +--------------------+ | | | |
//! | | | | | | | | |
//! | | | | | | | establish_session |
//! | | | | | | | | |
//! | | | | handle_request | | | |
//! | | | | | | | | |
//! | | | | | | | | |
//! | | | +--v-----------v--------+ retrieve_response | | |
//! | | | | +--------- | | | |
//! | | | | Signing | get_next_signature_payload | | | |
//! | | | | <--------- | | | |
//! | | | +---------+-------------+ | | | |
//! | | | | | | | |
//! | | | | | | | |
//! | | | | | | | |
//! | | | submit_next_signature | | | |
//! | | | | | | | |
//! | | | | +----------------------+ | | | |
//! | | | | | | | | | |
//! | | | | | ReadyToRespond | | | | |
//! | | | +-----------------> +-------------+ | | |
//! | | | | | | | |
//! | | | +----------+-----------+ | | |
//! | | | | | | |
//! | | +-----------------------------------------+-------------------------------+ | |
//! | | handle_response | |
//! +-------+------------------------------------------------+---------------------------------------------+------------+
//! | | |
//! | +-------v-------+ |
//! | | Reader | |
//! | | | |
//! | | | |
//! +----------------------------------------> +-------------------------------------+
//! | |
//! | |
//! | |
//! +---------------+
//! ```
//!
//! ### Reader perspective
//!
//! From the reader's perspective, the flow is simpler:
//!
//! ```text
//! +--------+
//! | |
//! | User |
//! | |
//! +---+----+
//! |
//! |
//! +---v-----+
//! | |
//! | Device |
//! +---------+ |<----------------+
//! | | | |
//! | | | |
//! | +--+--^---+ |
//! | | | |
//! | | | establish_session |
//! |qr_engagement | | |
//! | | | | new_request
//! | | | |
//! +-v--------------v--+-------------------+---+
//! | Reader |
//! | |
//! | +--------------------+ handle_response |
//! | | +---------------+ |
//! | | SessionManager | | |
//! | | |<--------------+ |
//! | +--------------------+ |
//! | |
//! +-------------------------------------------+
//! ```
//!
//!
//! The reader is simulated in `common`
//! module (you can find the complete code in `examples` directory), here we focus on the code from the
//! device perspective.
//!
//! ## Example
//!
//! Basic example.
//!
//! ```ignore
//! ```
//!
//! ## Example
//!
//! An example that uses `State` pattern, `Arc` and `Mutex`.
//!
//! ```ignore
//! ```