Skip to content

Commit 56a8d55

Browse files
committed
Fix compile warning and store user account id to phone number mapping
1 parent 7c96064 commit 56a8d55

File tree

17 files changed

+516
-94
lines changed

17 files changed

+516
-94
lines changed

Client/client.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ const getMsg2 = require('./msg2').getMsg2
1212
const getMsg4 = require('./msg4').getMsg4
1313
const msg0 = {
1414
"type": "msg0",
15-
"name" : "yaoz",
16-
"age" : 44,
1715
};
1816

1917
async function main() {
2018
var session = {}
2119
/* Send msg0 and get msg1 */
22-
const res1 = await httpSend(srvurl,null,msg0)
20+
const res1 = await httpSend(srvurl,"POST",null,msg0)
2321
const msg1 = res1.body
2422
if (res1.statusCode != 200 || msg1.status == 'failed')
2523
{
@@ -30,15 +28,15 @@ async function main() {
3028
console.log("\n===== Msg1 Detail =====")
3129
console.log(msg1)
3230
// Get msg2 from msg1
33-
const msg2 = getMsg2({
31+
const msg2 = await getMsg2({
3432
X : msg1.gax,
3533
Y : msg1.gay,
3634
}, session)
3735
console.log("\n===== Msg2 Detail =====")
3836
console.log(msg2)
3937

4038
/* Send msg2 and get msg3 */
41-
const res2 = await httpSend(srvurl,null,msg2)
39+
const res2 = await httpSend(srvurl,"POST",null,msg2)
4240
const msg3 = res2.body
4341
if (res2.statusCode != 200 || msg3.status == 'failed')
4442
{
@@ -52,10 +50,10 @@ async function main() {
5250
const msg4 = await getMsg4(msg3,session)
5351
console.log("\n===== Msg4 Detail =====")
5452
console.log(msg4)
55-
const res = await httpSend(srvurl,null,msg4)
53+
const res3 = await httpSend(srvurl,"POST",null,msg4)
5654

5755
console.log("\n===== Response Detail =====")
58-
console.log(res.body)
56+
console.log(res3.body)
5957
}
6058

6159
main()

Client/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ const srvurl = "http://localhost:12345";
22
const iasBaseUrl = "https://api.trustedservices.intel.com/sgx/dev/attestation/v3"
33
const iasHeader = { "Ocp-Apim-Subscription-Key": "e2e08166ca0f41ef88af2797f007c7cd"}
44
const signPriKey = "018C03D1533457ADEAAEB6653B6A861FEC879C4311DE663BCEA1522DBB6CE790"
5+
const SPID = "FEF23C7E73A379823CE71FF289CFBC07"
6+
const AES_CMAC_KDF_ID = 0x0001
7+
const SAMPLE_QUOTE_LINKABLE_SIGNATURE = 1
58

69
module.exports = {
710
srvurl : srvurl,
811
iasBaseUrl : iasBaseUrl,
912
iasHeader : iasHeader,
1013
signPriKey : signPriKey,
14+
SPID : SPID,
15+
AES_CMAC_KDF_ID : AES_CMAC_KDF_ID,
16+
SAMPLE_QUOTE_LINKABLE_SIGNATURE : SAMPLE_QUOTE_LINKABLE_SIGNATURE,
1117
}

Client/msg2.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
const SPID = "FEF23C7E73A379823CE71FF289CFBC07";
21
const SIGRL = 0;
32
const SIZE_SIGRL = 0;
4-
const AES_CMAC_KDF_ID = 0x0001;
5-
const SAMPLE_QUOTE_LINKABLE_SIGNATURE = 1;
63
const {
74
switchEndian,
85
toHex,
96
hexStringToArray,
107
buf2hexString,
118
hexString2Buffer,
9+
httpSend,
1210
} = require("./utils");
1311
const {
1412
signPriKey,
13+
SPID,
14+
iasBaseUrl,
15+
iasHeader,
1516
} = require("./config")
1617

1718
const crypto = require("crypto")
1819
const aesCmac = require("node-aes-cmac").aesCmac;
1920
const EC = require('elliptic').ec
2021
const ec = new EC('p256');
21-
const ecUtils = require('eckey-utils')
22-
const eccrypto = require("eccrypto")
23-
const bigInt = require("big-integer");
2422

2523

26-
function handleEcdhParam(decArray) {
27-
const hexStrArray = decArray.map(num => {
28-
const hex = num.toString(16);
29-
return (hex.length < 2) ? '0' + hex : hex;
30-
});
31-
const hexString = hexStrArray.join("");
32-
const switchedHexString = switchEndian(hexString);
33-
const decimalString = bigInt(switchedHexString, 16).toString();
34-
return decimalString;
35-
}
3624

25+
async function getMsg2(ecPublicKey, session) {
26+
/* Get sigrl */
27+
console.log("\n===== Requesting SigRL from IAS... ======")
28+
var sigrl = ""
29+
var sigrlSize = 0
30+
const sigRet = await httpSend(iasBaseUrl+"/sigrl/"+switchEndian(session.gid),"GET",iasHeader,"")
31+
if (sigRet.statusCode != 200)
32+
{
33+
console.log("Request IAS server failed!")
34+
return null
35+
}
36+
if (sigRet.body != undefined && sigRet.body != "")
37+
{
38+
sigrl = sigRet.body
39+
sigrlSize = sigrl.length
40+
}
3741

38-
function getMsg2(ecPublicKey, session) {
3942
/* Get GAX */
4043
const gax = ecPublicKey.X
4144
const gay = ecPublicKey.Y
@@ -60,10 +63,15 @@ function getMsg2(ecPublicKey, session) {
6063
const iv = Buffer.alloc(16, 0)
6164
const kdk = aesCmac(iv, hexString2Buffer(sharedKey))
6265
//console.log("kdk = ",kdk)
63-
// derive smk
66+
// Derive smk
6467
const message = [0x01,'S'.charCodeAt(0),'M'.charCodeAt(0),'K'.charCodeAt(0),0x00,0x80,0x00]
6568
const smk = aesCmac(hexString2Buffer(kdk), Buffer.from(message))
6669
//console.log("smk = ",smk)
70+
// Derive sk and mk
71+
const skMsg = [0x01,'S'.charCodeAt(0),'K'.charCodeAt(0),0x00,0x80,0x00]
72+
const mkMsg = [0x01,'M'.charCodeAt(0),'K'.charCodeAt(0),0x00,0x80,0x00]
73+
const sk = aesCmac(hexString2Buffer(kdk), Buffer.from(skMsg))
74+
const mk = aesCmac(hexString2Buffer(kdk), Buffer.from(mkMsg))
6775

6876
/**
6977
* @desc get signature: sign publck keys with my private key
@@ -100,8 +108,10 @@ function getMsg2(ecPublicKey, session) {
100108
gax: gax,
101109
gay: gay
102110
}
103-
session["smk"] = smk
104111
session["kdk"] = kdk
112+
session["smk"] = smk
113+
session["sk"] = sk
114+
session["mk"] = mk
105115
session["sharedKey"] = sharedKey
106116

107117
/**
@@ -118,8 +128,8 @@ function getMsg2(ecPublicKey, session) {
118128
SigSPX: SigSPX,
119129
SigSPY: SigSPY,
120130
CMACsmk: CMACsmk,
121-
sizeSigrl: SIZE_SIGRL,
122-
sigrl: SIGRL
131+
sizeSigrl: sigrlSize,
132+
sigrl: sigrl,
123133
}
124134
}
125135

Client/msg4.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,27 @@ async function getMsg4(msg3, session) {
4141
}
4242

4343
// Send quote to IAS
44-
/*
4544
const b64quote = Base64.encode(hexString2Buffer(msg3.quote))
4645
const body = {
4746
"isvEnclaveQuote": b64quote
4847
}
49-
console.log("===== Sending quote to IAS... ======")
50-
const iasResponse = await httpSend(iasBaseUrl+"/report",iasHeader,body)
48+
console.log("\n===== Sending quote to IAS... ======")
49+
const iasResponse = await httpSend(iasBaseUrl+"/report","POST",iasHeader,body)
5150
if (iasResponse.statusCode != 200)
5251
{
53-
console.log("Request IAS service failed!")
52+
console.log("Request IAS server failed!")
5453
return null
5554
}
5655
console.log("\n===== Verify Quote successfully =====")
5756
console.log(iasResponse.body)
58-
*/
5957

60-
61-
const skMsg = [0x01,'S'.charCodeAt(0),'K'.charCodeAt(0),0x00,0x80,0x00]
62-
const sk = aesCmac(hexString2Buffer(session.kdk), Buffer.from(skMsg))
63-
console.log("===== sk", sk)
58+
// Encypt data
59+
console.log("===== sk", session.sk)
60+
console.log("===== mk", session.mk)
6461
const iv = Buffer.alloc(12,0)
6562
const emptyBuffer = Buffer.alloc(0,0)
66-
const plainText = hexString2Buffer(sk)
67-
const cipher = gcm.encrypt(hexString2Buffer(sk), iv, plainText, emptyBuffer)
63+
const plainText = hexString2Buffer("15021128363")
64+
const cipher = gcm.encrypt(hexString2Buffer(session.sk), iv, plainText, emptyBuffer)
6865
console.log("===== cipher text ===== ")
6966
console.log(cipher)
7067

Client/utils.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,31 @@ function buf2hexString(buffer) { // buffer is an ArrayBuffer
6666
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
6767
}
6868

69-
function httpSend(url,header,data) {
69+
function httpSend(url,method,header,data) {
7070
if (header == null)
7171
{
7272
header = {"content-type": "application/json"}
7373
}
74+
if (method != "POST" && method != "GET")
75+
{
76+
console.log("Wrong http method!Should be POST or GET!")
77+
return
78+
}
7479
return new Promise(function(resolve, reject) {
7580
request({
7681
url: url,
77-
method: "POST",
82+
method: method,
7883
json: true,
7984
headers: header,
8085
body: data
8186
}, function(error, response, body) {
8287
if (!error && response.statusCode == 200) {
83-
//console.log("successful:",response)
8488
resolve({
8589
body: body,
8690
statusCode: response.statusCode,
8791
response: response,
8892
})
8993
} else {
90-
//console.log("failed:",response)
9194
reject({
9295
statusCode: response.statusCode,
9396
})

Server/App/App.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
extern FILE *felog;
66

7-
int main(int argc, char** args)
7+
//int main(int argc, char** args)
8+
int main()
89
{
910
// Create log file
1011
if ((felog = create_logfile("./server.log")) == NULL)

Server/Enclave/EUtils/EUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int eprintf(const char *fmt, ...)
2323
* @description: use ocall_eprint_string to print format string
2424
* @return: the length of printed string
2525
*/
26-
int cfeprintf(const char *fmt, ...)
26+
int feprintf(const char *fmt, ...)
2727
{
2828
char buf[BUFSIZE] = {'\0'};
2929
va_list ap;

Server/Enclave/EUtils/EUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern "C"
2020
#endif
2121

2222
int eprintf(const char* fmt, ...);
23-
int cfeprintf(const char* fmt, ...);
23+
int feprintf(const char* fmt, ...);
2424
char *hexstring(const void *vsrc, size_t len);
2525
uint8_t *hex_string_to_bytes(const char *src, size_t len);
2626

Server/Enclave/Enclave.cpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
#include <stdio.h>
33

44
#include <assert.h>
5+
#include <map>
6+
#include <string>
57
#include "sgx_tkey_exchange.h"
68
#include "sgx_tcrypto.h"
79
#include "sgx_tseal.h"
810
#include "string.h"
911
#include "EUtils.h"
1012
#include "Enclave_t.h"
1113

14+
using namespace std;
15+
1216
// This is the public EC key of the SP. The corresponding private EC key is
1317
// used by the SP to sign data used in the remote attestation SIGMA protocol
1418
// to sign channel binding data in MSG2. A successful verification of the
@@ -36,7 +40,17 @@ static const sgx_ec256_public_t g_sp_pub_key = {
3640

3741
};
3842

43+
// Store user account id to user phone num mapping
44+
map<vector<uint8_t>, string> accid_phone_map;
45+
// Store context id to user account id mapping
46+
map<sgx_ra_context_t, vector<uint8_t>> contextid_accid_map;
3947

48+
/**
49+
* @description: Initialize remote attestation context
50+
* @param b_pse -> Indicate whether create pse session
51+
* @param p_context -> RA session
52+
* @return: Initialize status
53+
* */
4054
sgx_status_t ecall_init_ra(int b_pse, sgx_ra_context_t *p_context)
4155
{
4256
// isv enclave call to trusted key exchange library.
@@ -61,32 +75,84 @@ sgx_status_t ecall_init_ra(int b_pse, sgx_ra_context_t *p_context)
6175
return ret;
6276
}
6377

78+
/**
79+
* @description: Close remote attestation
80+
* @param context -> Indicate to be closed context
81+
* @return: Close status
82+
* */
6483
sgx_status_t SGXAPI ecall_ra_close(sgx_ra_context_t context)
6584
{
6685
sgx_status_t ret;
6786
ret = sgx_ra_close(context);
87+
contextid_accid_map.erase(context);
6888
return ret;
6989
}
7090

71-
sgx_status_t ecall_verify_secret(sgx_ra_context_t context,
91+
/**
92+
* @description: Store user account id
93+
* @param context -> Corresponding context
94+
* @param p_Gb -> Pointer to user account id
95+
* @param Gb_size -> User account id size
96+
* */
97+
void ecall_store_account_id(sgx_ra_context_t context, uint8_t* p_Gb, uint32_t Gb_size)
98+
{
99+
vector<uint8_t> Gb_v(p_Gb, p_Gb + Gb_size);
100+
contextid_accid_map[context] = Gb_v;
101+
}
102+
103+
/**
104+
* @description: Decrypt user passed data
105+
* @param context -> Corresponding context
106+
* @param p_src -> Encrypted data
107+
* @param src_len -> Encrypted data size
108+
* @param p_in_mac -> Encrypted data mac
109+
* @return: Decrypted status
110+
* */
111+
sgx_status_t ecall_decrypt_secret(sgx_ra_context_t context,
72112
const uint8_t *p_src, uint32_t src_len,
73-
uint8_t *p_dst, const sgx_aes_gcm_128bit_tag_t *p_in_mac)
113+
const sgx_aes_gcm_128bit_tag_t *p_in_mac)
74114
{
115+
if (contextid_accid_map.find(context) == contextid_accid_map.end())
116+
{
117+
return SGX_ERROR_UNEXPECTED;
118+
}
119+
75120
sgx_status_t sgx_status = SGX_SUCCESS;
76121
sgx_ra_key_128_t ra_key;
122+
string phone_num;
77123

78124
sgx_status = sgx_ra_get_keys(context, SGX_RA_KEY_SK, &ra_key);
79125
if (SGX_SUCCESS != sgx_status)
80126
{
81127
return sgx_status;
82128
}
83129

84-
cfeprintf("ra key:%s\n", hexstring(&ra_key, sizeof(ra_key)));
130+
feprintf("ra key:%s\n", hexstring(&ra_key, sizeof(ra_key)));
85131

86132
uint8_t *p_iv = (uint8_t*)malloc(SGX_AESGCM_IV_SIZE);
87133
memset(p_iv, 0, SGX_AESGCM_IV_SIZE);
134+
uint8_t *p_dst = (uint8_t*)malloc(src_len);
135+
memset(p_dst, 0, src_len);
88136
sgx_status = sgx_rijndael128GCM_decrypt(&ra_key, p_src,
89137
src_len, p_dst, p_iv, SGX_AESGCM_IV_SIZE, NULL, 0, p_in_mac);
90138

139+
if (SGX_SUCCESS != sgx_status)
140+
{
141+
sgx_status = SGX_ERROR_UNEXPECTED;
142+
goto cleanup;
143+
}
144+
145+
phone_num = string(hexstring(p_dst, src_len));
146+
phone_num = phone_num.substr(0, 11);
147+
accid_phone_map[contextid_accid_map[context]] = phone_num;
148+
149+
feprintf("Phone number:%s\n", accid_phone_map[contextid_accid_map[context]]);
150+
151+
152+
cleanup:
153+
154+
free(p_iv);
155+
free(p_dst);
156+
91157
return sgx_status;
92158
}

0 commit comments

Comments
 (0)