-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlib.rs
executable file
·355 lines (281 loc) · 10.9 KB
/
lib.rs
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
/*
Copyright 2019 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#![crate_name = "sealedkeyenclave"]
#![crate_type = "staticlib"]
#![cfg_attr(not(target_env = "sgx"), no_std)]
#![cfg_attr(target_env = "sgx", feature(rustc_private))]
extern crate sgx_types;
extern crate sgx_tseal;
#[cfg(not(target_env = "sgx"))]
#[macro_use]
extern crate sgx_tstd as std;
extern crate sgx_rand;
extern crate crypto;
extern crate rust_base58;
extern crate serde_json;
extern crate sgx_crypto_helper;
extern crate sgx_serialize;
extern crate log;
extern crate primitives;
use primitives::{ed25519};
extern crate my_node_runtime;
use my_node_runtime::{UncheckedExtrinsic, Call, Hash, SubstraTEEProxyCall};
extern crate runtime_primitives;
use runtime_primitives::generic::Era;
extern crate parity_codec;
use parity_codec::{Decode, Encode, Compact};
extern crate primitive_types;
use primitive_types::U256;
use sgx_types::{sgx_status_t, sgx_sealed_data_t};
use sgx_types::marker::ContiguousMemory;
use sgx_tseal::{SgxSealedData};
use sgx_rand::{Rng, StdRng};
use sgx_serialize::{SerializeHelper, DeSerializeHelper};
#[macro_use]
extern crate sgx_serialize_derive;
use log::*;
use std::sgxfs::SgxFile;
use std::slice;
use std::string::String;
use std::vec::Vec;
use std::collections::HashMap;
use std::string::ToString;
use crypto::ed25519::{keypair, signature};
use rust_base58::{ToBase58};
use sgx_crypto_helper::RsaKeyPair;
use sgx_crypto_helper::rsa3072::{Rsa3072KeyPair};
type Index = u64;
mod constants;
mod utils;
use constants::{RSA3072_SEALED_KEY_FILE, ED25519_SEALED_KEY_FILE, COUNTERSTATE};
#[no_mangle]
pub extern "C" fn get_rsa_encryption_pubkey(pubkey: *mut u8, pubkey_size: u32) -> sgx_status_t {
let mut retval = sgx_status_t::SGX_SUCCESS;
match SgxFile::open(RSA3072_SEALED_KEY_FILE) {
Err(x) => {
info!("[Enclave] Keyfile not found, creating new! {}", x);
retval = create_sealed_rsa3072_keypair();
},
_ => ()
}
if retval != sgx_status_t::SGX_SUCCESS {
// detailed error msgs are already printed in utils::write file
return retval;
}
let rsa_keypair = utils::read_rsa_keypair(&mut retval);
let rsa_pubkey = rsa_keypair.export_pubkey().unwrap();
// println!("rsa_pubkey = {:?}", rsa_pubkey);
let rsa_pubkey_json = match serde_json::to_string(&rsa_pubkey) {
Ok(k) => k,
Err(x) => {
println!("[Enclave] can't serialize rsa_pubkey {:?} {}", rsa_pubkey, x);
return sgx_status_t::SGX_ERROR_UNEXPECTED;
}
};
let pubkey_slice = unsafe { slice::from_raw_parts_mut(pubkey, pubkey_size as usize) };
// split the pubkey_slice at the length of the rsa_pubkey_json
// and fill the right side with whitespace so that the json can be decoded later on
let (left, right) = pubkey_slice.split_at_mut(rsa_pubkey_json.len());
left.clone_from_slice(rsa_pubkey_json.as_bytes());
right.iter_mut().for_each(|x| *x = 0x20);
sgx_status_t::SGX_SUCCESS
}
fn create_sealed_rsa3072_keypair() -> sgx_status_t {
let rsa_keypair = Rsa3072KeyPair::new().unwrap();
let rsa_key_json = serde_json::to_string(&rsa_keypair).unwrap();
// println!("[Enclave] generated RSA3072 key pair. Cleartext: {}", rsa_key_json);
utils::write_file(rsa_key_json.as_bytes(), RSA3072_SEALED_KEY_FILE)
}
#[no_mangle]
pub extern "C" fn get_ecc_signing_pubkey(pubkey: * mut u8, pubkey_size: u32) -> sgx_status_t {
let mut retval = sgx_status_t::SGX_SUCCESS;
match SgxFile::open(ED25519_SEALED_KEY_FILE) {
Ok(_k) => (),
Err(x) => {
info!("[Enclave] Keyfile not found, creating new! {}", x);
retval = create_sealed_ed25519_seed();
},
}
if retval != sgx_status_t::SGX_SUCCESS {
// detailed error msgs are already printed in utils::write file
return retval;
}
let _seed = _get_ecc_seed_file(&mut retval);
let (_privkey, _pubkey) = keypair(&_seed);
info!("[Enclave] Restored ECC pubkey: {:?}", _pubkey.to_base58());
let pubkey_slice = unsafe { slice::from_raw_parts_mut(pubkey, pubkey_size as usize) };
pubkey_slice.clone_from_slice(&_pubkey);
sgx_status_t::SGX_SUCCESS
}
fn _get_ecc_seed_file(status: &mut sgx_status_t) -> (Vec<u8>) {
let mut seed_vec: Vec<u8> = Vec::new();
*status = utils::read_file(&mut seed_vec, ED25519_SEALED_KEY_FILE);
seed_vec
}
fn create_sealed_ed25519_seed() -> sgx_status_t {
let mut seed = [0u8; 32];
let mut rand = match StdRng::new() {
Ok(rng) => rng,
Err(_) => { return sgx_status_t::SGX_ERROR_UNEXPECTED; },
};
rand.fill_bytes(&mut seed);
utils::write_file(&seed, ED25519_SEALED_KEY_FILE)
}
#[no_mangle]
pub extern "C" fn call_counter(ciphertext: * mut u8,
ciphertext_size: u32,
hash: * const u8,
hash_size: u32,
nonce: * const u8,
nonce_size: u32,
unchechecked_extrinsic: * mut u8,
unchecked_extrinsic_size: u32) -> sgx_status_t {
let ciphertext_slice = unsafe { slice::from_raw_parts(ciphertext, ciphertext_size as usize) };
let hash_slice = unsafe { slice::from_raw_parts(hash, hash_size as usize) };
let mut nonce_slice = unsafe {slice::from_raw_parts(nonce, nonce_size as usize)};
let extrinsic_slize = unsafe { slice::from_raw_parts_mut(unchechecked_extrinsic, unchecked_extrinsic_size as usize) };
let mut retval = sgx_status_t::SGX_SUCCESS;
let rsa_keypair = utils::read_rsa_keypair(&mut retval);
if retval != sgx_status_t::SGX_SUCCESS {
return retval;
}
// decode the message
let plaintext = utils::get_plaintext_from_encrypted_data(&ciphertext_slice, &rsa_keypair);
let (account, increment) = utils::get_account_and_increment_from_plaintext(plaintext.clone());
// read the counter state
let mut state_vec: Vec<u8> = Vec::new();
retval = utils::read_counterstate(&mut state_vec, COUNTERSTATE);
if retval != sgx_status_t::SGX_SUCCESS {
return retval;
}
let helper = DeSerializeHelper::<AllCounts>::new(state_vec);
let mut counter = helper.decode().unwrap();
// increment or create the counter and write state
increment_or_insert_counter(&mut counter, &account, increment);
retval = write_counter_state(counter);
// get information for composing the extrinsic
let nonce = U256::decode(&mut nonce_slice).unwrap();
let _seed = _get_ecc_seed_file(&mut retval);
let genesis_hash = utils::hash_from_slice(hash_slice);
let call_hash = utils::blake2s(&plaintext);
debug!("[Enclave]: Call hash {:?}", call_hash);
let ex = compose_extrinsic(_seed, &call_hash, nonce, genesis_hash);
let encoded = ex.encode();
extrinsic_slize.clone_from_slice(&encoded);
retval
}
#[no_mangle]
pub extern "C" fn get_counter(account: *const u8, account_size: u32, value: *mut u32) -> sgx_status_t {
let mut state_vec: Vec<u8> = Vec::new();
let account_slice = unsafe { slice::from_raw_parts(account, account_size as usize) };
let acc_str = std::str::from_utf8(account_slice).unwrap();
let retval = utils::read_counterstate(&mut state_vec, COUNTERSTATE);
if retval != sgx_status_t::SGX_SUCCESS {
return retval;
}
let helper = DeSerializeHelper::<AllCounts>::new(state_vec);
let mut counter = helper.decode().unwrap();
unsafe {
let ref_mut = &mut *value;
*ref_mut = *counter.entries.entry(acc_str.to_string()).or_insert(0);
}
retval
}
fn increment_or_insert_counter(counter: &mut AllCounts, name: &str, value: u32) {
{
let c = counter.entries.entry(name.to_string()).or_insert(0);
*c += value;
}
if counter.entries.get(name).unwrap() == &value {
info!("[Enclave] No counter found for '{}', adding new with initial value {}", name, value);
} else {
info!("[Enclave] Incremented counter for '{}'. New value: {:?}", name, counter.entries.get(name).unwrap());
}
}
fn write_counter_state(value: AllCounts) -> sgx_status_t {
let helper = SerializeHelper::new();
let c = helper.encode(value).unwrap();
utils::write_file( &c, COUNTERSTATE)
}
#[no_mangle]
pub extern "C" fn sign(sealed_seed: * mut u8, sealed_seed_size: u32,
msg: * mut u8, msg_size: u32,
sig: * mut u8, sig_size: u32) -> sgx_status_t {
// runseal seed
let opt = from_sealed_log::<[u8; 32]>(sealed_seed, sealed_seed_size);
let sealed_data = match opt {
Some(x) => x,
None => {
return sgx_status_t::SGX_ERROR_INVALID_PARAMETER;
},
};
let result = sealed_data.unseal_data();
let unsealed_data = match result {
Ok(x) => x,
Err(ret) => {
return ret;
},
};
let seed = unsealed_data.get_decrypt_txt();
//restore ed25519 keypair from seed
let (_privkey, _pubkey) = keypair(seed);
info!("[Enclave]: Restored sealed key pair with pubkey: {:?}", _pubkey.to_base58());
// sign message
let msg_slice = unsafe {
slice::from_raw_parts_mut(msg, msg_size as usize)
};
let sig_slice = unsafe {
slice::from_raw_parts_mut(sig, sig_size as usize)
};
let _sig = signature(&msg_slice, &_privkey);
sig_slice.clone_from_slice(&_sig);
sgx_status_t::SGX_SUCCESS
}
#[derive(Serializable, DeSerializable, Debug)]
struct AllCounts {
entries: HashMap<String, u32>
}
// fn to_sealed_log<T: Copy + ContiguousMemory>(sealed_data: &SgxSealedData<T>, sealed_log: * mut u8, sealed_log_size: u32) -> Option<* mut sgx_sealed_data_t> {
// unsafe {
// sealed_data.to_raw_sealed_data_t(sealed_log as * mut sgx_sealed_data_t, sealed_log_size)
// }
// }
fn from_sealed_log<'a, T: Copy + ContiguousMemory>(sealed_log: * mut u8, sealed_log_size: u32) -> Option<SgxSealedData<'a, T>> {
unsafe {
SgxSealedData::<T>::from_raw_sealed_data_t(sealed_log as * mut sgx_sealed_data_t, sealed_log_size)
}
}
pub fn compose_extrinsic(seed: Vec<u8>, call_hash: &[u8], nonce: U256, genesis_hash: Hash) -> UncheckedExtrinsic {
let (_privkey, _pubkey) = keypair(&seed);
let era = Era::immortal();
let function = Call::SubstraTEEProxy(SubstraTEEProxyCall::confirm_call(call_hash.to_vec()));
let index = Index::from(nonce.low_u64());
let raw_payload = (Compact(index), function, era, genesis_hash);
let sign = raw_payload.using_encoded(|payload| if payload.len() > 256 {
// should not be thrown as we calculate a 32 byte hash ourselves
error!("unsupported payload size");
signature(&[0u8; 64], &_privkey)
} else {
//println!("signing {}", HexDisplay::from(&payload));
signature(payload, &_privkey)
});
let signerpub = ed25519::Public::from_raw(_pubkey);
let signature = ed25519::Signature::from_raw(sign);
UncheckedExtrinsic::new_signed(
index,
raw_payload.1,
signerpub.into(),
signature.into(),
era,
)
}