int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; //DEFAULT_TPM_PORT; TPMI_RH_PROVISION auth = TPM_RH_NULL; TPMI_DH_OBJECT objectHandle; TPMI_DH_OBJECT persistentHandle; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvA:H:S:P:p:d:c:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"auth",1,NULL,'A'}, {"handle",1,NULL,'H'}, {"persistent",1,NULL,'S'}, {"pwda",1,NULL,'P'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {"context",1,NULL,'c'}, {0,0,0,0} }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, A_flag = 0, H_flag = 0, S_flag = 0, c_flag = 0, P_flag = 0; char *contextFile = NULL; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'A': if(strcmp(optarg,"o") == 0 || strcmp(optarg,"O") == 0) auth = TPM_RH_OWNER; else if(strcmp(optarg,"p") == 0 || strcmp(optarg,"P") == 0) auth = TPM_RH_PLATFORM; else { printf("ERROR: auth '%s' not supported!\n", optarg); returnVal = -1; break; } A_flag = 1; break; case 'H': if(getSizeUint32Hex(optarg, &objectHandle) != 0) { returnVal = -2; break; } printf("\nobjectHandle: 0x%x\n\n",objectHandle); H_flag = 1; break; case 'S': if(getSizeUint32Hex(optarg, &persistentHandle) != 0) { returnVal = -3; break; } printf("\npersistentHandle: 0x%x\n\n",persistentHandle); S_flag = 1; break; case 'P': if( optarg == NULL || (strlen(optarg) >= sizeof(TPMU_HA)) ) { printf("\nPlease input the authenticating password(optional,no more than %d characters).\n", (int)sizeof(TPMU_HA)-1); returnVal = -4; break; } if( strlen(optarg) > 0 ) { sessionData.hmac.t.size = strlen(optarg); safeStrNCpy( (char *)&sessionData.hmac.t.buffer[0], optarg, sizeof(sessionData.hmac.t.buffer) ); } P_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -5; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -6; } break; case 'c': contextFile = optarg; if(contextFile == NULL || contextFile[0] == '\0') { returnVal = -7; break; } printf("contextFile = %s\n", contextFile); c_flag = 1; break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -8; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -9; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; flagCnt = h_flag + v_flag + A_flag + H_flag + S_flag + c_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -10; } } else if(flagCnt == 3 && A_flag == 1 && (H_flag == 1 || c_flag) && S_flag == 1) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &objectHandle, contextFile); if (returnVal == 0) returnVal = evictControl(auth, objectHandle, persistentHandle, P_flag); finishTest(); if(returnVal) return -11; } else { showArgMismatch(argv[0]); return -12; } return 0; }
int main(int argc, char *argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvk:c:P:l:q:g:o:Xp:d:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"akHandle",1,NULL,'k'}, {"akContext",1,NULL,'c'}, {"akPassword",1,NULL,'P'}, //add ak auth {"idList",1,NULL,'l'}, {"algorithm",1,NULL,'g'}, {"qualifiedData",1,NULL,'q'}, {"outFile",1,NULL,'o'}, {"passwdInHex",0,NULL,'X'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {0,0,0,0} }; char *contextFilePath = NULL; TPM_HANDLE akHandle; TPMI_ALG_HASH algorithmId; PCR_LIST pcrList; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, k_flag = 0, c_flag = 0, P_flag = 0, l_flag = 0, g_flag = 0, q_flag = 0, o_flag = 0; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'k': if(getSizeUint32Hex(optarg,&akHandle) != 0) { showArgError(optarg, argv[0]); returnVal = -1; break; } k_flag = 1; break; case 'c': contextFilePath = optarg; if(contextFilePath == NULL || contextFilePath[0] == '\0') { returnVal = -2; break; } printf("contextFile = %s\n", contextFilePath); c_flag = 1; break; case 'P': sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2; if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0) { returnVal = -3; break; } P_flag = 1; break; case 'l': if(parseList(optarg, &pcrList) != 0) { returnVal = -4; break; } l_flag = 1; break; case 'g': if(getSizeUint16Hex(optarg,&algorithmId) != 0) { showArgError(optarg, argv[0]); returnVal = -5; break; } g_flag = 1; break; case 'q': qualifedData.t.size = sizeof(qualifedData.t.buffer); if(loadDataFromFile(optarg, qualifedData.t.buffer, &qualifedData.t.size) != 0) { returnVal = -6; break; } q_flag = 1; break; case 'o': safeStrNCpy(outFilePath, optarg, sizeof(outFilePath)); if(checkOutFile(outFilePath) != 0) { returnVal = -7; break; } o_flag = 1; break; case 'X': hexPasswd = true; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -8; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -9; } break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -10; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -11; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; flagCnt = h_flag + v_flag + k_flag + c_flag + l_flag + g_flag + o_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -12; } } else if(flagCnt == 4 && ((k_flag || c_flag) && l_flag && g_flag && o_flag)) { if(P_flag == 0) sessionData.hmac.t.size = 0; prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &akHandle, contextFilePath); if(returnVal == TPM_RC_SUCCESS) returnVal = quote(akHandle, pcrList, algorithmId); finishTest(); if(returnVal) return -13; } else { showArgMismatch(argv[0]); return -13; } return 0; }
int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; TPMI_DH_OBJECT keyHandle; TPM2B_PUBLIC_KEY_RSA cipherText; char outFilePath[PATH_MAX] = {0}; char *contextKeyFile = NULL; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvk:P:I:o:p:d:c:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"keyHandle",1,NULL,'k'}, {"pwdk",1,NULL,'P'}, {"inFile",1,NULL,'I'}, {"outFile",1,NULL,'o'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {"keyContext",1,NULL,'c'}, {0,0,0,0} }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, k_flag = 0, P_flag = 0, I_flag = 0, c_flag = 0, o_flag = 0; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'k': if(getSizeUint32Hex(optarg,&keyHandle) != 0) { returnVal = -1; break; } k_flag = 1; break; case 'P': sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2; if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0) { returnVal = -2; break; } P_flag = 1; break; case 'I': cipherText.t.size = sizeof(cipherText) - 2; if(loadDataFromFile(optarg, cipherText.t.buffer, &cipherText.t.size) != 0) { returnVal = -3; break; } I_flag = 1; break; case 'o': safeStrNCpy(outFilePath, optarg, sizeof(outFilePath)); if(checkOutFile(outFilePath) != 0) { returnVal = -4; break; } o_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -5; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -6; } break; case 'c': contextKeyFile = optarg; if(contextKeyFile == NULL || contextKeyFile[0] == '\0') { returnVal = -7; break; } printf("contextKeyFile = %s\n", contextKeyFile); c_flag = 1; break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -8; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -9; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; if(P_flag == 0) sessionData.hmac.t.size = 0; flagCnt = h_flag + v_flag + k_flag + I_flag + o_flag + c_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -10; } } else if((flagCnt == 3) && (k_flag == 1 || c_flag == 1) && (I_flag == 1) && (o_flag == 1)) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &keyHandle, contextKeyFile); if(returnVal == 0) returnVal = rsaDecrypt(keyHandle, &cipherText, outFilePath); finishTest(); if(returnVal) return -11; } else { showArgMismatch(argv[0]); return -12; } return 0; }
int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; TPMI_DH_OBJECT keyHandle; BYTE *msg = NULL; UINT16 length = 0; UINT16 size = 0; long fileSize = 0; TPMT_TK_HASHCHECK validation; TPMI_ALG_HASH halg; char outFilePath[PATH_MAX] = {0}; char inMsgFileName[PATH_MAX] = {0}; char *contextKeyFile = NULL; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvk:P:g:m:t:s:p:d:c:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"keyHandle",1,NULL,'k'}, {"pwdk",1,NULL,'P'}, {"halg",1,NULL,'g'}, {"msg",1,NULL,'m'}, {"sig",1,NULL,'s'}, {"ticket",1,NULL,'t'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {"keyContext",1,NULL,'c'}, {0,0,0,0} }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, k_flag = 0, P_flag = 0, g_flag = 0, m_flag = 0, t_flag = 0, c_flag = 0, s_flag = 0; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'k': if(getSizeUint32Hex(optarg,&keyHandle) != 0) { returnVal = -1; break; } k_flag = 1; break; case 'P': sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2; if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0) { returnVal = -2; break; } P_flag = 1; break; case 'g': if(getSizeUint16Hex(optarg,&halg) != 0) { showArgError(optarg, argv[0]); returnVal = -3; break; } printf("halg = 0x%4.4x\n", halg); g_flag = 1; break; case 'm': safeStrNCpy(inMsgFileName, optarg, sizeof(inMsgFileName)); m_flag = 1; break; case 't': size = sizeof(validation); if(loadDataFromFile(optarg, (UINT8 *)&validation, &size) != 0) { returnVal = -4; break; } t_flag = 1; break; case 's': safeStrNCpy(outFilePath, optarg, sizeof(outFilePath)); if(checkOutFile(outFilePath) != 0) { returnVal = -5; break; } s_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -6; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -7; } break; case 'c': contextKeyFile = optarg; if(contextKeyFile == NULL || contextKeyFile[0] == '\0') { returnVal = -8; break; } printf("contextKeyFile = %s\n", contextKeyFile); c_flag = 1; break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -9; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -10; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) goto end; if(m_flag) { if(getFileSize(inMsgFileName, &fileSize)) { returnVal = -11; goto end; } if(fileSize == 0) { printf("the message file is empty !\n"); returnVal = -12; goto end; } if(fileSize > 0xffff) { printf("the message file was too long !\n"); returnVal = -13; goto end; } msg = (BYTE*)malloc(fileSize); if(msg == NULL) { returnVal = -14; goto end; } memset(msg, 0, fileSize); length = fileSize; if(loadDataFromFile(inMsgFileName, msg, &length) != 0) { returnVal = -15; goto end; } #if 0 printf("\nmsg length: %d\n",length); printf("msg content: "); for(int i = 0; i < length; i++) { printf("%02x ", msg[i]); } printf("\n"); return -1; #endif } if(P_flag == 0) sessionData.hmac.t.size = 0; if(t_flag == 0) { validation.tag = TPM_ST_HASHCHECK; validation.hierarchy = TPM_RH_NULL; validation.digest.t.size = 0; } flagCnt = h_flag + v_flag + k_flag + g_flag + m_flag + s_flag + c_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); returnVal = -16; } } else if((flagCnt == 4) && (k_flag == 1 || c_flag == 1) && (g_flag == 1) && (m_flag == 1) && (s_flag == 1)) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &keyHandle, contextKeyFile); if(returnVal == 0) returnVal = sign(keyHandle, halg, msg, length, &validation, outFilePath); finishTest(); if(returnVal) returnVal = -17; } else { showArgMismatch(argv[0]); returnVal = -18; } end: if(msg) free(msg); return returnVal; }
int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; TPM2B_SENSITIVE_CREATE inSensitive; inSensitive.t.sensitive.data.t.size = 0; TPM2B_PUBLIC inPublic; TPMI_ALG_PUBLIC type; TPMI_ALG_HASH nameAlg; TPMI_DH_OBJECT parentHandle; UINT32 objectAttributes = 0; char opuFilePath[PATH_MAX] = {0}; char oprFilePath[PATH_MAX] = {0}; char *contextParentFilePath = NULL; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvH:P:K:g:G:A:I:L:o:O:p:d:c:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"parent",1,NULL,'H'}, {"pwdp",1,NULL,'P'}, {"pwdk",1,NULL,'K'}, {"halg",1,NULL,'g'}, {"kalg",1,NULL,'G'}, {"objectAttributes",1,NULL,'A'}, {"inFile",1,NULL,'I'}, {"policyFile",1,NULL,'L'}, {"opu",1,NULL,'o'}, {"opr",1,NULL,'O'}, {"contextParent",1,NULL,'c'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {0,0,0,0} }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, H_flag = 0, P_flag = 0, K_flag = 0, g_flag = 0, G_flag = 0, A_flag = 0, I_flag = 0, L_flag = 0, o_flag = 0, c_flag = 0, O_flag = 0/*, f_flag = 0*/; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'H': if(getSizeUint32Hex(optarg,&parentHandle) != 0) { showArgError(optarg, argv[0]); returnVal = -1; break; } H_flag = 1; break; case 'P': sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2; if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0) { returnVal = -2; break; } P_flag = 1; break; case 'K': inSensitive.t.sensitive.userAuth.t.size = sizeof(inSensitive.t.sensitive.userAuth.t) - 2; if(str2ByteStructure(optarg,&inSensitive.t.sensitive.userAuth.t.size, inSensitive.t.sensitive.userAuth.t.buffer) != 0) { returnVal = -3; break; } K_flag = 1; break; case 'g': if(getSizeUint16Hex(optarg,&nameAlg) != 0) { showArgError(optarg, argv[0]); returnVal = -4; break; } printf("nameAlg = 0x%4.4x\n", nameAlg); g_flag = 1; break; case 'G': if(getSizeUint16Hex(optarg,&type) != 0) { showArgError(optarg, argv[0]); returnVal = -5; break; } printf("type = 0x%4.4x\n", type); G_flag = 1; break; case 'A': if(getSizeUint32Hex(optarg,&objectAttributes) != 0) { showArgError(optarg, argv[0]); returnVal = -6; break; } A_flag = 1;//H_flag = 1; break; case 'I': inSensitive.t.sensitive.data.t.size = sizeof(inSensitive.t.sensitive.data) - 2; if(loadDataFromFile(optarg, inSensitive.t.sensitive.data.t.buffer, &inSensitive.t.sensitive.data.t.size) != 0) { returnVal = -7; break; } I_flag = 1; printf("inSensitive.t.sensitive.data.t.size = %d\n",inSensitive.t.sensitive.data.t.size); break; case 'L': inPublic.t.publicArea.authPolicy.t.size = sizeof(inPublic.t.publicArea.authPolicy) - 2; if(loadDataFromFile(optarg, inPublic.t.publicArea.authPolicy.t.buffer, &inPublic.t.publicArea.authPolicy.t.size) != 0) { returnVal = -8; break; } L_flag = 1; break; case 'o': safeStrNCpy(opuFilePath, optarg, sizeof(opuFilePath)); if(checkOutFile(opuFilePath) != 0) { returnVal = -9; break; } o_flag = 1; break; case 'O': safeStrNCpy(oprFilePath, optarg, sizeof(oprFilePath)); if(checkOutFile(oprFilePath) != 0) { returnVal = -10; break; } O_flag = 1; break; case 'c': contextParentFilePath = optarg; if(contextParentFilePath == NULL || contextParentFilePath[0] == '\0') { returnVal = -11; break; } printf("contextParentFile = %s\n", contextParentFilePath); c_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -12; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -13; } break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -14; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -15; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; if(P_flag == 0) sessionData.hmac.t.size = 0; if(I_flag == 0) inSensitive.t.sensitive.data.t.size = 0; if(K_flag == 0) inSensitive.t.sensitive.userAuth.t.size = 0; if(L_flag == 0) inPublic.t.publicArea.authPolicy.t.size = 0; *((UINT8 *)((void *)&sessionData.sessionAttributes)) = 0; flagCnt = h_flag + v_flag + H_flag + g_flag + G_flag + c_flag ; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -16; } } else if(flagCnt == 3 && (H_flag == 1 || c_flag == 1) && g_flag == 1 && G_flag == 1) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &parentHandle, contextParentFilePath); if(returnVal == 0) returnVal = create(parentHandle, &inPublic, &inSensitive, type, nameAlg, opuFilePath, oprFilePath, o_flag, O_flag, I_flag, A_flag, objectAttributes); finishTest(); if(returnVal) return -17; } else { showArgMismatch(argv[0]); return -18; } return 0; }
int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; //DEFAULT_TPM_PORT; char *contextFilePath = NULL; char *keyContextFilePath = NULL; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvH:c:k:C:P:K:f:o:p:d:"; struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"handle",1,NULL,'H'}, {"context",1,NULL,'c'}, {"keyHandle",1,NULL,'k'}, {"keyContext",1,NULL,'C'}, {"Password",1,NULL,'P'}, {"keyPassword",1,NULL,'K'}, {"inFile",1,NULL,'f'}, {"outFile",1,NULL,'o'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {0,0,0,0}, }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, H_flag = 0, c_flag = 0, k_flag = 0, C_flag = 0, K_flag = 0, P_flag = 0, f_flag = 0, o_flag = 0; if(argc == 1) { showHelp(argv[0]); return 0; } cmdAuth.hmac.t.size = 0; cmdAuth2.hmac.t.size = 0; while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'H': if(getSizeUint32Hex(optarg,&activateHandle) != 0) { returnVal = -1; break; } H_flag = 1; break; case 'c': contextFilePath = optarg; if(contextFilePath == NULL || contextFilePath[0] == '\0') { returnVal = -2; break; } printf("contextFile = %s\n", contextFilePath); c_flag = 1; break; case 'k': if(getSizeUint32Hex(optarg,&keyHandle) != 0) { returnVal = -3; break; } k_flag = 1; break; case 'C': keyContextFilePath = optarg; if(keyContextFilePath == NULL || keyContextFilePath[0] == '\0') { returnVal = -4; break; } printf("keyContextFile = %s\n", keyContextFilePath); C_flag = 1; break; case 'P': cmdAuth.hmac.t.size = sizeof(cmdAuth.hmac.t) - 2; if(str2ByteStructure(optarg,&cmdAuth.hmac.t.size,cmdAuth.hmac.t.buffer) != 0) { returnVal = -5; break; } P_flag = 1; break; case 'K': cmdAuth2.hmac.t.size = sizeof(cmdAuth2.hmac.t) - 2; if(str2ByteStructure(optarg,&cmdAuth2.hmac.t.size,cmdAuth2.hmac.t.buffer) != 0) { returnVal = -6; break; } K_flag = 1; break; case 'f': if(readCrtSecFromFile(optarg,&credentialBlob,&secret) != 0) { returnVal = -7; break; } f_flag = 1; break; case 'o': safeStrNCpy(outFilePath, optarg, sizeof(outFilePath)); #if 0 if(checkOutFile(outFilePath) != 0) { returnVal = -1; break; } #endif o_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -8; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -9; } break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -10; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -11; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; flagCnt = h_flag + v_flag + H_flag + c_flag + k_flag + C_flag + f_flag + o_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -12; } } else if((flagCnt == 4) && (H_flag == 1 || c_flag == 1) && (k_flag == 1 || C_flag == 1) && (f_flag == 1) && (o_flag == 1)) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &activateHandle, contextFilePath); if(C_flag && returnVal == 0) returnVal = loadTpmContextFromFile(sysContext, &keyHandle, keyContextFilePath); if(returnVal == 0) returnVal = activateCredential(); finishTest(); if(returnVal) return -13; } else { showArgMismatch(argv[0]); return -14; } return 0; }
int main(int argc, char* argv[]) { char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; TPMI_DH_OBJECT objectHandle; char outFilePath[PATH_MAX] = {0}; char *contextFile = NULL; setbuf(stdout, NULL); setvbuf (stdout, NULL, _IONBF, BUFSIZ); int opt = -1; const char *optstring = "hvH:o:p:d:c:"; static struct option long_options[] = { {"help",0,NULL,'h'}, {"version",0,NULL,'v'}, {"object",1,NULL,'H'}, {"opu",1,NULL,'o'}, {"port",1,NULL,'p'}, {"debugLevel",1,NULL,'d'}, {"contextObject",1,NULL,'c'}, {0,0,0,0} }; int returnVal = 0; int flagCnt = 0; int h_flag = 0, v_flag = 0, H_flag = 0, c_flag = 0, o_flag = 0; if(argc == 1) { showHelp(argv[0]); return 0; } while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1) { switch(opt) { case 'h': h_flag = 1; break; case 'v': v_flag = 1; break; case 'H': if(getSizeUint32Hex(optarg,&objectHandle) != 0) { returnVal = -1; break; } printf("\nobject handle: 0x%x\n\n",objectHandle); H_flag = 1; break; case 'o': safeStrNCpy(outFilePath, optarg, sizeof(outFilePath)); if(checkOutFile(outFilePath) != 0) { returnVal = -2; break; } o_flag = 1; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); returnVal = -3; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); returnVal = -4; } break; case 'c': contextFile = optarg; if(contextFile == NULL || contextFile[0] == '\0') { returnVal = -5; break; } printf("contextFile = %s\n", contextFile); c_flag = 1; break; case ':': // printf("Argument %c needs a value!\n",optopt); returnVal = -6; break; case '?': // printf("Unknown Argument: %c\n",optopt); returnVal = -7; break; //default: // break; } if(returnVal) break; }; if(returnVal != 0) return returnVal; flagCnt = h_flag + v_flag + H_flag + o_flag + c_flag; if(flagCnt == 1) { if(h_flag == 1) showHelp(argv[0]); else if(v_flag == 1) showVersion(argv[0]); else { showArgMismatch(argv[0]); return -8; } } else if(flagCnt == 2 && (H_flag == 1 || c_flag) && o_flag == 1) { prepareTest(hostName, port, debugLevel); if(c_flag) returnVal = loadTpmContextFromFile(sysContext, &objectHandle, contextFile); if(returnVal == 0) returnVal = readPublic(objectHandle, outFilePath); finishTest(); if(returnVal) return -9; } else { showArgMismatch(argv[0]); return -10; } return 0; }