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
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be
// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

use App;
use errors::AppError;
use ffi::helper::send_sync;
use ffi_utils::{FFI_RESULT_OK, FfiResult, OpaqueCtx, SafePtr, catch_unwind_cb,
                vec_clone_from_raw_parts};
use maidsafe_utilities::serialisation::{deserialise, serialise};
use object_cache::MDataInfoHandle;
use routing::XorName;
use rust_sodium::crypto::secretbox;
use safe_core::MDataInfo;
use safe_core::crypto::shared_secretbox;
use safe_core::ffi::arrays::{SymNonce, SymSecretKey, XorNameArray};
use std::os::raw::c_void;

/// Create non-encrypted mdata info with explicit data name.
///
/// Callback parameters: user data, error code, mdata info handle
#[no_mangle]
pub unsafe extern "C" fn mdata_info_new_public(
    app: *const App,
    name: *const XorNameArray,
    type_tag: u64,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        mdata_info_h: MDataInfoHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let name = XorName(*name);

        send_sync(app, user_data, o_cb, move |_, context| {
            let info = MDataInfo::new_public(name, type_tag);
            Ok(context.object_cache().insert_mdata_info(info))
        })
    })
}

/// Create encrypted mdata info with explicit data name and a
/// provided private key.
///
/// Callback parameters: user data, error code, mdata info handle
#[no_mangle]
pub unsafe extern "C" fn mdata_info_new_private(
    app: *const App,
    name: *const XorNameArray,
    type_tag: u64,
    secret_key: *const SymSecretKey,
    nonce: *const SymNonce,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        mdata_info_h: MDataInfoHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let name = XorName(*name);

        let sk = shared_secretbox::Key::from_raw(&*secret_key);
        let nonce = secretbox::Nonce(*nonce);

        send_sync(app, user_data, o_cb, move |_, context| {
            let info = MDataInfo::new_private(name, type_tag, (sk, nonce));
            Ok(context.object_cache().insert_mdata_info(info))
        })
    })
}

/// Create random, non-encrypted mdata info.
///
/// Callback parameters: user data, error code, mdata info handle
#[no_mangle]
pub unsafe extern "C" fn mdata_info_random_public(
    app: *const App,
    type_tag: u64,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        mdata_info_h: MDataInfoHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let info = MDataInfo::random_public(type_tag)?;
            Ok(context.object_cache().insert_mdata_info(info))
        })
    })
}

/// Create random, encrypted mdata info.
///
/// Callback parameters: user data, error code, mdata info handle
#[no_mangle]
pub unsafe extern "C" fn mdata_info_random_private(
    app: *const App,
    type_tag: u64,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        mdata_info_h: MDataInfoHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let info = MDataInfo::random_private(type_tag)?;
            Ok(context.object_cache().insert_mdata_info(info))
        })
    })
}

/// Encrypt mdata entry key using the corresponding mdata info.
///
/// Callback parameters: user data, error code, encrypted entry key vector, vector size
#[no_mangle]
pub unsafe extern "C" fn mdata_info_encrypt_entry_key(
    app: *const App,
    info_h: MDataInfoHandle,
    input_ptr: *const u8,
    input_len: usize,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        enc_entry_key_ptr: *const u8,
                        enc_entry_key_len: usize),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);
        let input = vec_clone_from_raw_parts(input_ptr, input_len);

        (*app).send(move |_, context| {
            let info = try_cb!(
                context.object_cache().get_mdata_info(info_h),
                user_data,
                o_cb
            );
            let vec = try_cb!(
                info.enc_entry_key(&input).map_err(AppError::from),
                user_data,
                o_cb
            );

            o_cb(user_data.0, FFI_RESULT_OK, vec.as_safe_ptr(), vec.len());

            None
        })
    })
}

/// Encrypt mdata entry value using the corresponding mdata info.
///
/// Callback parameters: user data, error code, encrypted entry value vector, vector size
#[no_mangle]
pub unsafe extern "C" fn mdata_info_encrypt_entry_value(
    app: *const App,
    info_h: MDataInfoHandle,
    input_ptr: *const u8,
    input_len: usize,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        enc_entry_value_ptr: *const u8,
                        enc_entry_value_len: usize),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);
        let input = vec_clone_from_raw_parts(input_ptr, input_len);

        (*app).send(move |_, context| {
            let info = try_cb!(
                context.object_cache().get_mdata_info(info_h),
                user_data,
                o_cb
            );
            let vec = try_cb!(
                info.enc_entry_value(&input).map_err(AppError::from),
                user_data,
                o_cb
            );

            o_cb(user_data.0, FFI_RESULT_OK, vec.as_safe_ptr(), vec.len());

            None
        })
    })
}

/// Decrypt mdata entry value or a key using the corresponding mdata info.
///
/// Callback parameters: user data, error code, decrypted mdata info vector, vector size
#[no_mangle]
pub unsafe extern "C" fn mdata_info_decrypt(
    app: *const App,
    info_h: MDataInfoHandle,
    input_ptr: *const u8,
    input_len: usize,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        mdata_info_decrypt_ptr: *const u8,
                        mdata_info_decrypt_len: usize),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);
        let encoded = vec_clone_from_raw_parts(input_ptr, input_len);

        (*app).send(move |_, context| {
            let info = try_cb!(
                context.object_cache().get_mdata_info(info_h),
                user_data,
                o_cb
            );
            let decrypted = try_cb!(
                info.decrypt(&encoded).map_err(AppError::from),
                user_data,
                o_cb
            );

            o_cb(
                user_data.0,
                FFI_RESULT_OK,
                decrypted.as_safe_ptr(),
                decrypted.len(),
            );

            None
        })
    })
}

/// Extract name and type tag from the mdata info.
///
/// Callback parameters: user data, error code, xor name, type type
#[no_mangle]
pub unsafe extern "C" fn mdata_info_extract_name_and_type_tag(
    app: *const App,
    info_h: MDataInfoHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        name: *const XorNameArray,
                        tag: u64),
) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let info = context.object_cache().get_mdata_info(info_h)?;
            Ok((&info.name.0, info.type_tag))
        })
    })
}

/// Serialise `MDataInfo`.
///
/// Callback parameters: user data, error code, serialised mdata info
#[no_mangle]
pub unsafe extern "C" fn mdata_info_serialise(
    app: *const App,
    info_h: MDataInfoHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        encoded_ptr: *const u8,
                        encoded_len: usize),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        (*app).send(move |_, context| {
            let info = try_cb!(
                context.object_cache().get_mdata_info(info_h),
                user_data,
                o_cb
            );
            let encoded = try_cb!(serialise(&*info).map_err(AppError::from), user_data, o_cb);

            o_cb(
                user_data.0,
                FFI_RESULT_OK,
                encoded.as_safe_ptr(),
                encoded.len(),
            );
            None
        })
    })
}

/// Deserialise `MDataInfo`.
///
/// Callback parameters: user data, error code, mdata info handle
#[no_mangle]
pub unsafe extern "C" fn mdata_info_deserialise(
    app: *const App,
    ptr: *const u8,
    len: usize,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void,
                        result: FfiResult,
                        mdata_info_h: MDataInfoHandle),
) {
    catch_unwind_cb(user_data, o_cb, || {
        let encoded = vec_clone_from_raw_parts(ptr, len);

        send_sync(app, user_data, o_cb, move |_, context| {
            let info = deserialise(&encoded)?;
            Ok(context.object_cache().insert_mdata_info(info))
        })
    })
}

/// Free `MDataInfo` from memory.
///
/// Callback parameters: user data, error code
#[no_mangle]
pub unsafe extern "C" fn mdata_info_free(
    app: *const App,
    info_h: MDataInfoHandle,
    user_data: *mut c_void,
    o_cb: extern "C" fn(user_data: *mut c_void, result: FfiResult),
) {

    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_info(info_h);
            Ok(())
        })
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use ffi_utils::test_utils::{call_0, call_1, call_vec_u8};
    use rand;
    use routing::XOR_NAME_LEN;
    use rust_sodium::crypto::secretbox;
    use safe_core::MDataInfo;
    use test_utils::{create_app, run_now};

    // Test creating non-encrypted mdata info.
    #[test]
    fn create_public() {
        let app = create_app();
        let type_tag: u64 = rand::random();

        let info_h = unsafe {
            unwrap!(call_1(
                |ud, cb| mdata_info_random_public(&app, type_tag, ud, cb),
            ))
        };

        run_now(&app, move |_, context| {
            let info = unwrap!(context.object_cache().get_mdata_info(info_h));
            assert_eq!(info.type_tag, type_tag);
            assert!(info.enc_info.is_none());
        })
    }

    // Test creating encrypted mdata info.
    #[test]
    fn create_private() {
        let app = create_app();
        let type_tag: u64 = rand::random();

        let rand_info_h = unsafe {
            unwrap!(call_1(
                |ud, cb| mdata_info_random_private(&app, type_tag, ud, cb),
            ))
        };

        let key = shared_secretbox::gen_key();
        let nonce = secretbox::gen_nonce();
        let new_info_h = unsafe {
            unwrap!(call_1(|ud, cb| {
                mdata_info_new_private(&app, &[2; XOR_NAME_LEN], type_tag, &key.0, &nonce.0, ud, cb)
            }))
        };

        run_now(&app, move |_, context| {
            {
                let rand_info = unwrap!(context.object_cache().get_mdata_info(rand_info_h));
                assert_eq!(rand_info.type_tag, type_tag);
                assert!(rand_info.enc_info.is_some());
            }

            {
                let new_info = unwrap!(context.object_cache().get_mdata_info(new_info_h));
                assert_eq!(new_info.type_tag, type_tag);
                match new_info.enc_info {
                    Some((ref got_key, ref got_nonce)) => {
                        assert_eq!(*got_key, key);
                        assert_eq!(*got_nonce, nonce);
                    }
                    None => panic!("Unexpected result: no enc_info in private MDataInfo"),
                }
            }
        });
    }

    // Test serialising and deserialising mdata_info.
    #[test]
    fn serialise_deserialise() {
        let app = create_app();
        let info1 = unwrap!(MDataInfo::random_private(1000));

        let info1_h = {
            let info = info1.clone();
            run_now(&app, move |_, context| {
                context.object_cache().insert_mdata_info(info)
            })
        };

        let encoded = unsafe {
            unwrap!(call_vec_u8(
                |ud, cb| mdata_info_serialise(&app, info1_h, ud, cb),
            ))
        };

        let info2_h = unsafe {
            let res = call_1(|ud, cb| {
                mdata_info_deserialise(&app, encoded.as_ptr(), encoded.len(), ud, cb)
            });

            unwrap!(res)
        };

        run_now(&app, move |_, context| {
            let info2 = unwrap!(context.object_cache().get_mdata_info(info2_h));
            assert_eq!(info1, *info2);
        });

        unsafe {
            unwrap!(call_0(|ud, cb| mdata_info_free(&app, info1_h, ud, cb)));
            unwrap!(call_0(|ud, cb| mdata_info_free(&app, info2_h, ud, cb)));
        }
    }
}