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
#![deny(warnings)]
use std::mem::MaybeUninit;
use std::ffi::{CString, CStr};
use std::marker::PhantomData;
use std::ptr;
pub mod ffi;
pub struct Nfc {
context: *mut ffi::context_t,
}
impl Nfc {
pub fn new() -> Option<Self> {
let mut context_uninit = MaybeUninit::<*mut ffi::context_t>::uninit();
let context = unsafe {
ffi::nfc_init(context_uninit.as_mut_ptr());
if context_uninit.as_mut_ptr() == std::ptr::null_mut() {
return None;
}
context_uninit.assume_init()
};
Some(Nfc { context })
}
pub fn gatekeeper_device(&mut self, conn_str: String) -> Option<NfcDevice> {
let device_string = CString::new(conn_str).unwrap();
let device = unsafe {
let device_ptr = ffi::nfc_open(self.context, device_string.as_ptr());
if device_ptr == std::ptr::null_mut() {
return None;
}
device_ptr
};
Some(NfcDevice { device, _context: self })
}
}
impl Drop for Nfc {
fn drop(&mut self) {
unsafe {
ffi::nfc_exit(self.context);
}
}
}
pub struct NfcDevice<'a> {
device: *mut ffi::device_t,
_context: &'a Nfc,
}
impl NfcDevice<'_> {
pub fn first_tag(&mut self) -> Option<NfcTag> {
let (tags, tag) = unsafe {
let tags = ffi::freefare_get_tags(self.device);
if tags == std::ptr::null_mut() { return None; }
let tag = *tags;
if tag == std::ptr::null_mut() { return None; }
(tags, tag)
};
Some(NfcTag { tags, tag, _device_lifetime: PhantomData })
}
}
impl Drop for NfcDevice<'_> {
fn drop(&mut self) {
unsafe {
ffi::nfc_close(self.device);
}
}
}
pub struct NfcTag <'a> {
tags: *mut *mut ffi::mifare_t,
tag: *mut ffi::mifare_t,
_device_lifetime: std::marker::PhantomData<&'a ()>,
}
impl NfcTag<'_> {
pub fn get_uid(&mut self) -> Option<String> {
unsafe {
let tag_uid = ffi::freefare_get_tag_uid(self.tag);
if tag_uid == std::ptr::null_mut() { return None; }
let tag_uid_string = CString::from_raw(tag_uid);
Some(tag_uid_string.to_string_lossy().to_string())
}
}
pub fn get_friendly_name(&mut self) -> Option<&str> {
unsafe {
let tag_name = ffi::freefare_get_tag_friendly_name(self.tag);
let tag_name_string = CStr::from_ptr(tag_name);
tag_name_string.to_str().ok()
}
}
pub fn format(&mut self, uid: Option<&str>, system_secret: Option<&str>) -> Result<(), ()> {
let uid_opt = match uid {
Some(uid) => CString::new(uid).ok(),
None => None,
};
let uid = match &uid_opt {
Some(uid) => uid.as_ptr(),
None => ptr::null(),
};
let system_secret_opt = match system_secret {
Some(system_secret) => CString::new(system_secret).ok(),
None => None,
};
let system_secret = match &system_secret_opt {
Some(system_secret) => system_secret.as_ptr(),
None => ptr::null(),
};
unsafe {
let format_result = ffi::format_tag(
self.tag,
uid,
system_secret,
);
if format_result != 0 { return Err(()); }
return Ok(());
}
}
pub fn issue(&mut self, system_secret: &str, uid: Option<&str>, in_realms: Vec<&mut Realm>) -> Result<(), ()> {
let system_secret = CString::new(system_secret).unwrap();
let uid_opt = match uid {
Some(uid) => CString::new(uid).ok(),
None => None,
};
let uid = match &uid_opt {
Some(uid) => uid.as_ptr(),
None => ptr::null(),
};
let mut realms: Vec<*mut ffi::realm_t> =
Vec::with_capacity(in_realms.len());
for realm in in_realms {
realms.push(realm.realm);
}
unsafe {
let issue_result = ffi::issue_tag(self.tag, system_secret.as_ptr(),
uid,
realms.as_mut_ptr(), realms.len());
if issue_result != 0 { return Err(()); }
return Ok(());
}
}
pub fn authenticate(&mut self, realm: &mut Realm) -> Result<String, ()> {
let mut association_id = [0u8; 37];
let auth_result = unsafe {
ffi::authenticate_tag(self.tag, realm.realm, association_id.as_mut_ptr())
};
if auth_result == 0 { return Err(()); }
let mut association_id = association_id.to_vec();
association_id.pop();
Ok(String::from_utf8(association_id).unwrap())
}
}
impl Drop for NfcTag<'_> {
fn drop(&mut self) {
unsafe {
ffi::freefare_free_tags(self.tags);
}
}
}
pub struct Realm {
realm: *mut ffi::realm_t,
}
impl Realm {
pub fn new(
slot: u8,
name: &str,
association: &str,
auth_key: &str,
read_key: &str,
update_key: &str,
public_key: &str,
private_key: &str,
) -> Option<Self> {
let ffi_name = CString::new(name).ok()?;
let ffi_association = CString::new(association).ok()?;
let ffi_auth_key = CString::new(auth_key).ok()?;
let ffi_read_key = CString::new(read_key).ok()?;
let ffi_update_key = CString::new(update_key).ok()?;
let ffi_public_key = CString::new(public_key).ok()?;
let ffi_private_key = CString::new(private_key).ok()?;
let realm = unsafe {
ffi::realm_create(slot,
ffi_name.as_ptr(),
ffi_association.as_ptr(),
ffi_auth_key.as_ptr(),
ffi_read_key.as_ptr(),
ffi_update_key.as_ptr(),
ffi_public_key.as_ptr(),
ffi_private_key.as_ptr(),
)
};
Some(Realm { realm })
}
}
impl Drop for Realm {
fn drop(&mut self) {
unsafe {
ffi::realm_free(self.realm);
}
}
}