7 changed files with 479 additions and 71 deletions
@ -0,0 +1,318 @@ |
|||||
|
#include "member-tpm.h" |
||||
|
|
||||
|
typedef enum memberstate { |
||||
|
ON, |
||||
|
ISSUERPUB, |
||||
|
RCVPUBLIC, |
||||
|
JOIN, |
||||
|
APPEND, |
||||
|
JOINPROCEED, |
||||
|
JOINED, |
||||
|
} memberstate_e; |
||||
|
|
||||
|
typedef struct member { |
||||
|
struct ecdaa_member_public_key_FP256BN mpk; |
||||
|
memberstate_e state; |
||||
|
uint8_t nonce[NONCE_SIZE]; |
||||
|
struct ecdaa_credential_FP256BN cred; |
||||
|
struct ecdaa_issuer_public_key_FP256BN ipk; |
||||
|
uint8_t bsn[MAX_BSNSIZE]; |
||||
|
size_t bsn_len; |
||||
|
struct ecdaa_tpm_context ctx; |
||||
|
} member_t; |
||||
|
|
||||
|
member_t member; |
||||
|
|
||||
|
int init_tpm(); |
||||
|
int free_tpm(); |
||||
|
int member_join(char *buffer); |
||||
|
int member_attest(char *buffer); |
||||
|
int member_publish(char *buffer); |
||||
|
int member_getpublic(char *buffer); |
||||
|
int member_joinappend(char *buffer); |
||||
|
int member_joinfinish(char *buffer); |
||||
|
|
||||
|
int main() { |
||||
|
|
||||
|
init_tpm(); |
||||
|
if (2 != server_start(&process_member, MEMBERPORT)) { |
||||
|
printf("server failed\n"); |
||||
|
} |
||||
|
free_tpm(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int init_tpm() { |
||||
|
TSS2_TCTI_CONTEXT *tctiContext = NULL; |
||||
|
TPM_HANDLE handle = 0; |
||||
|
const char* passwd = "1234"; |
||||
|
uint16_t passwdlen = strlen(passwd); |
||||
|
TSS2_RC retval = 0; |
||||
|
size_t bufsize = tss2_tcti_getsize_device(); |
||||
|
uint8_t tctiBuffer[bufsize]; |
||||
|
bzero(tctiBuffer, bufsize); |
||||
|
const char* devicePath = "/dev/tpm0"; |
||||
|
|
||||
|
tctiContext = tctiBuffer; |
||||
|
retval = tss2_tcti_init_device(devicePath, strlen(devicePath), tctiContext); |
||||
|
switch (retval & 0xFF) { |
||||
|
case TSS2_RC_SUCCESS: |
||||
|
printf("tcti context established\n"); |
||||
|
break; |
||||
|
default: |
||||
|
printf("tcti context failed\n"); |
||||
|
break; |
||||
|
} |
||||
|
//initialize ecdaa tpm context
|
||||
|
if(0 != ecdaa_tpm_context_init(&member.ctx, handle, NULL, 0, tctiContext)) { |
||||
|
printf("\necdaa context failed\n"); |
||||
|
return -1; |
||||
|
} |
||||
|
printf("\necdaa context initialized\n"); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int free_tpm() { |
||||
|
ecdaa_tpm_context_free(&member.ctx); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int process_member(char *buffer) { |
||||
|
int ret = 0; |
||||
|
|
||||
|
bzero(member.bsn, MAX_BSNSIZE); |
||||
|
strncpy((char *) member.bsn, "Biometric Sensor", 16); |
||||
|
member.bsn_len = 16; |
||||
|
|
||||
|
printf("> MEMBER: %s\n", buffer); |
||||
|
|
||||
|
if (member.state == JOINED && 0 == strncasecmp("ATTEST", buffer, 6)) { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "ATTEST ", 7); |
||||
|
member_attest(buffer); |
||||
|
} else if (member.state == ON && 0 == strncasecmp("GETPUBLIC", buffer, 9)) { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
ret = client_connect(&member_getpublic, ISSUERIP, ISSUERPORT); |
||||
|
if (0 >= ret || RCVPUBLIC != member.state) { |
||||
|
printf("process_member: issuer connection failed\n"); |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "ERR\n", 4); |
||||
|
} else { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "OK\n", 3); |
||||
|
} |
||||
|
ret = 0; |
||||
|
} else if (0 == strncasecmp("PUBLISH", buffer, 7)) { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
member_publish(buffer); |
||||
|
} else if (member.state == RCVPUBLIC && 0 == strncasecmp("JOIN", buffer, 4)) { |
||||
|
member.state = JOIN; |
||||
|
ret = client_connect(&member_join, ISSUERIP, ISSUERPORT); |
||||
|
if (0 >= ret || JOINED != member.state) { |
||||
|
printf("process_member: issuer connection failed\n"); |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "ERR\n", 4); |
||||
|
} else { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "OK\n", 3); |
||||
|
} |
||||
|
ret = 0; |
||||
|
} else if (0 == strncasecmp("EXIT", buffer, 4)) { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "OK\n", 3); |
||||
|
ret = 1; |
||||
|
} else if (0 == strncasecmp("SHUTDOWN", buffer, 8)) { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "OK\n", 3); |
||||
|
ret = 2; |
||||
|
} else { |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "ERR\n", 4); |
||||
|
ret = 0; |
||||
|
} |
||||
|
|
||||
|
printf("< MEMBER: %s\n", buffer); |
||||
|
return ret; |
||||
|
} |
||||
|
|
||||
|
int member_join(char *buffer) { |
||||
|
int ret = 0; |
||||
|
|
||||
|
switch (member.state) { |
||||
|
case JOIN: |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "JOIN\n", 5); |
||||
|
member.state = APPEND; |
||||
|
break; |
||||
|
case APPEND: |
||||
|
if (0 == strncasecmp("JOINSTART", buffer, 9)) { |
||||
|
printf("ISSUER > MEMBER: %s", buffer); |
||||
|
member_joinappend(buffer); |
||||
|
member.state = JOINPROCEED; |
||||
|
} else { |
||||
|
printf("member_join: did not get nonce from issuer\n"); |
||||
|
member.state = RCVPUBLIC; |
||||
|
ret = -1; |
||||
|
} |
||||
|
break; |
||||
|
case JOINPROCEED: |
||||
|
if (0 == strncasecmp("JOINPROCEED", buffer, 11)) { |
||||
|
printf("ISSUER > MEMBER: %s", buffer); |
||||
|
member_joinfinish(buffer); |
||||
|
member.state = JOINED; |
||||
|
ret = 1; |
||||
|
} else { |
||||
|
printf("member_getpublic: did not get credentials from issuer\n"); |
||||
|
member.state = RCVPUBLIC; |
||||
|
ret = -1; |
||||
|
} |
||||
|
break; |
||||
|
default: |
||||
|
ret = -1; |
||||
|
} |
||||
|
if (0 == ret) { |
||||
|
printf("ISSUER < MEMBER: %s", buffer); |
||||
|
} |
||||
|
return ret; |
||||
|
} |
||||
|
|
||||
|
int member_getpublic(char *buffer) { |
||||
|
int ret = 0; |
||||
|
|
||||
|
switch (member.state) { |
||||
|
case ON: |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "PUBLISH\n", 8); |
||||
|
member.state = ISSUERPUB; |
||||
|
break; |
||||
|
case ISSUERPUB: |
||||
|
if (0 == strncasecmp("PUBLISH", buffer, 7)) { |
||||
|
printf("ISSUER > MEMBER: %s", buffer); |
||||
|
uint8_t binbuf[MAX_BUFSIZE]; |
||||
|
char *current = &buffer[8]; |
||||
|
ecdaa_hextobin(current, binbuf, ECDAA_ISSUER_PUBLIC_KEY_FP256BN_LENGTH); |
||||
|
ret = ecdaa_issuer_public_key_FP256BN_deserialize(&member.ipk, binbuf); |
||||
|
if (-1 == ret) { |
||||
|
printf("member_getpublic: issuer public key is malformed!\n"); |
||||
|
ret = -1; |
||||
|
} else if (-2 == ret) { |
||||
|
printf("member_getpublic: signature of issuer public key is invalid\n"); |
||||
|
ret = -1; |
||||
|
} else { |
||||
|
member.state = RCVPUBLIC; |
||||
|
ret = 1; |
||||
|
} |
||||
|
} else { |
||||
|
printf("member_getpublic: did not get public key from issuer\n"); |
||||
|
member.state = ON; |
||||
|
ret = -1; |
||||
|
} |
||||
|
break; |
||||
|
default: |
||||
|
printf("member_getpublic: did not get public key from issuer\n"); |
||||
|
member.state = ON; |
||||
|
ret = -1; |
||||
|
break; |
||||
|
} |
||||
|
if (0 == ret) { |
||||
|
printf("ISSUER < MEMBER: %s", buffer); |
||||
|
} |
||||
|
return ret; |
||||
|
} |
||||
|
|
||||
|
//"ATTEST" > "ATTEST <attestval>"
|
||||
|
int member_attest(char *buffer) { |
||||
|
char *current = buffer; |
||||
|
uint8_t binbuf[MAX_BUFSIZE]; |
||||
|
uint8_t msg[MAX_MSGSIZE] = "I am the real host"; |
||||
|
size_t msg_len = strlen((char*)msg); |
||||
|
uint8_t has_nym = member.bsn_len != 0 ? 1 : 0; |
||||
|
struct ecdaa_signature_FP256BN sig; |
||||
|
size_t sig_len = has_nym ? ECDAA_SIGNATURE_FP256BN_WITH_NYM_LENGTH : ECDAA_SIGNATURE_FP256BN_LENGTH; |
||||
|
|
||||
|
ecdaa_signature_TPM_FP256BN_sign(&sig, msg, msg_len, member.bsn, member.bsn_len, &member.cred, ecdaa_rand, &member.ctx); |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
bzero(binbuf, MAX_BUFSIZE); |
||||
|
strncpy(current, "ATTEST ", 7); |
||||
|
current = ¤t[7]; |
||||
|
|
||||
|
strncpy(current, (char*) msg, MAX_MSGSIZE); |
||||
|
current[MAX_MSGSIZE] = has_nym == 1 ? '1' : '0'; |
||||
|
current = ¤t[MAX_MSGSIZE + 1]; |
||||
|
if(has_nym) { |
||||
|
strncpy(current, (char *)member.bsn, MAX_BSNSIZE); |
||||
|
current = ¤t[MAX_BSNSIZE]; |
||||
|
} |
||||
|
|
||||
|
ecdaa_signature_FP256BN_serialize(binbuf, &sig, has_nym); |
||||
|
ecdaa_bintohex(binbuf, sig_len, current); |
||||
|
current[2 * sig_len] = '\n'; |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
//"PUBLISH" > "PUBLISH <member.mpk>"
|
||||
|
int member_publish(char *buffer) { |
||||
|
char *current; |
||||
|
uint8_t binbuf[MAX_BUFSIZE]; |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
|
||||
|
strncpy(buffer, "PUBLISH ", 8); |
||||
|
|
||||
|
current = &buffer[8]; |
||||
|
bzero(binbuf, MAX_BUFSIZE); |
||||
|
ecdaa_member_public_key_FP256BN_serialize(binbuf, &member.mpk); |
||||
|
ecdaa_bintohex(binbuf, ECDAA_MEMBER_PUBLIC_KEY_FP256BN_LENGTH, current); |
||||
|
|
||||
|
current[2 * ECDAA_MEMBER_PUBLIC_KEY_FP256BN_LENGTH] = '\n'; |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
//"JOINSTART <issuer.nonce>" > "APPEND <member.mpk>"
|
||||
|
int member_joinappend(char *buffer) { |
||||
|
char *current = &buffer[10]; |
||||
|
uint8_t binbuf[MAX_BUFSIZE]; |
||||
|
ecdaa_hextobin(current, member.nonce, NONCE_SIZE); |
||||
|
|
||||
|
uint8_t serial_gpk[ECDAA_GROUP_PUBLIC_KEY_FP256BN_LENGTH]; |
||||
|
bzero(serial_gpk, ECDAA_GROUP_PUBLIC_KEY_FP256BN_LENGTH); |
||||
|
ecdaa_issuer_public_key_FP256BN_serialize(serial_gpk, &member.ipk); |
||||
|
|
||||
|
if (0 != ecdaa_member_key_pair_TPM_FP256BN_generate(&member.mpk, serial_gpk, member.nonce, NONCE_SIZE, &member.ctx)) { |
||||
|
fprintf(stderr, "Error generating member key-pair\n"); |
||||
|
return -1; |
||||
|
} |
||||
|
bzero(buffer, MAX_BUFSIZE); |
||||
|
strncpy(buffer, "APPEND ", 7); |
||||
|
|
||||
|
current = &buffer[7]; |
||||
|
bzero(binbuf, MAX_BUFSIZE); |
||||
|
ecdaa_member_public_key_FP256BN_serialize(binbuf, &member.mpk); |
||||
|
ecdaa_bintohex(binbuf, ECDAA_MEMBER_PUBLIC_KEY_FP256BN_LENGTH, current); |
||||
|
current[2 * ECDAA_MEMBER_PUBLIC_KEY_FP256BN_LENGTH] = '\n'; |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
//"JOINPROCEED <member.cred><member.cred_sig>" > ""
|
||||
|
int member_joinfinish(char *buffer) { |
||||
|
char *current = &buffer[12]; |
||||
|
uint8_t *bincur; |
||||
|
uint8_t binbuf[MAX_BUFSIZE]; |
||||
|
int ret = 0; |
||||
|
bzero(binbuf, MAX_BUFSIZE); |
||||
|
ecdaa_hextobin(current, binbuf, ECDAA_CREDENTIAL_FP256BN_LENGTH); |
||||
|
|
||||
|
current = &buffer[12 + 2 * ECDAA_CREDENTIAL_FP256BN_LENGTH + 1]; |
||||
|
bincur = &binbuf[ECDAA_CREDENTIAL_FP256BN_LENGTH]; |
||||
|
ecdaa_hextobin(current, bincur, ECDAA_CREDENTIAL_FP256BN_SIGNATURE_LENGTH); |
||||
|
ret = ecdaa_credential_FP256BN_deserialize_with_signature(&member.cred, &member.mpk, &member.ipk.gpk, binbuf, bincur); |
||||
|
if(-1 == ret) { |
||||
|
printf("member_joinfinish: credential is malformed!\n"); |
||||
|
ret = -1; |
||||
|
} else if(-2 == ret) { |
||||
|
printf("member_joinfinish: siganture of credential is invalid"); |
||||
|
ret = -1; |
||||
|
} |
||||
|
|
||||
|
return ret; |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
//
|
||||
|
// Created by root on 11/5/19.
|
||||
|
//
|
||||
|
|
||||
|
#ifndef ECDAA_ISSUER_MEMBER_H |
||||
|
#define ECDAA_ISSUER_MEMBER_H |
||||
|
#include <tss2/tss2_sys.h> |
||||
|
#include <tss2/tss2_tcti.h> |
||||
|
#include <tss2/tss2_tcti_device.h> |
||||
|
#include <ecdaa.h> |
||||
|
#include <ecdaa-tpm.h> |
||||
|
#include <ecdaa.h> |
||||
|
#include "server.h" |
||||
|
#include "client.h" |
||||
|
#include "common.h" |
||||
|
|
||||
|
int process_member(char *buffer); |
||||
|
|
||||
|
#endif //ECDAA_ISSUER_ISSUER_H
|
||||
Loading…
Reference in new issue