Used W port.h file operations for keyblob and added username argument so can append any username needed.

pull/754/head
aidan garske 2025-04-02 10:33:27 -07:00
parent 3775d1843a
commit a52c3b120e
2 changed files with 30 additions and 22 deletions

View File

@ -760,7 +760,7 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
{ {
int rc = 0; int rc = 0;
#if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES) #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
XFILE fp = NULL; WFILE* fp = NULL;
size_t fileSz = 0; size_t fileSz = 0;
size_t bytes_read = 0; size_t bytes_read = 0;
byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)]; byte pubAreaBuffer[sizeof(TPM2B_PUBLIC)];
@ -768,20 +768,25 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
WLOG(WS_LOG_DEBUG, "Entering readKeyBlob()"); WLOG(WS_LOG_DEBUG, "Entering readKeyBlob()");
XMEMSET(key, 0, sizeof(WOLFTPM2_KEYBLOB)); WMEMSET(key, 0, sizeof(WOLFTPM2_KEYBLOB));
if (WFOPEN(NULL, &fp, filename, "rb") != 0) {
printf("Failed to open file %s\n", filename);
rc = BUFFER_E; goto exit;
}
if (fp != WBADFILE) {
WFSEEK(NULL, fp, 0, WSEEK_END);
fileSz = WFTELL(NULL, fp);
WREWIND(NULL, fp);
fp = XFOPEN(filename, "rb");
if (fp != XBADFILE) {
XFSEEK(fp, 0, XSEEK_END);
fileSz = XFTELL(fp);
XREWIND(fp);
if (fileSz > sizeof(key->priv) + sizeof(key->pub)) { if (fileSz > sizeof(key->priv) + sizeof(key->pub)) {
printf("File size check failed\n"); printf("File size check failed\n");
rc = BUFFER_E; goto exit; rc = BUFFER_E; goto exit;
} }
printf("Reading %d bytes from %s\n", (int)fileSz, filename); printf("Reading %d bytes from %s\n", (int)fileSz, filename);
bytes_read = XFREAD(&key->pub.size, 1, sizeof(key->pub.size), fp); bytes_read = WFREAD(NULL, &key->pub.size, 1,
sizeof(key->pub.size), fp);
if (bytes_read != sizeof(key->pub.size)) { if (bytes_read != sizeof(key->pub.size)) {
printf("Read %zu, expected size marker of %zu bytes\n", printf("Read %zu, expected size marker of %zu bytes\n",
bytes_read, sizeof(key->pub.size)); bytes_read, sizeof(key->pub.size));
@ -789,7 +794,8 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
} }
fileSz -= bytes_read; fileSz -= bytes_read;
bytes_read = XFREAD(pubAreaBuffer, 1, sizeof(UINT16) + key->pub.size, fp); bytes_read = WFREAD(NULL, pubAreaBuffer, 1,
sizeof(UINT16) + key->pub.size, fp);
if (bytes_read != sizeof(UINT16) + key->pub.size) { if (bytes_read != sizeof(UINT16) + key->pub.size) {
printf("Read %zu, expected public blob %zu bytes\n", printf("Read %zu, expected public blob %zu bytes\n",
bytes_read, sizeof(UINT16) + key->pub.size); bytes_read, sizeof(UINT16) + key->pub.size);
@ -804,7 +810,7 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
if (fileSz > 0) { if (fileSz > 0) {
printf("Reading the private part of the key\n"); printf("Reading the private part of the key\n");
bytes_read = XFREAD(&key->priv, 1, fileSz, fp); bytes_read = WFREAD(NULL, &key->priv, 1, fileSz, fp);
if (bytes_read != fileSz) { if (bytes_read != fileSz) {
printf("Read %zu, expected private blob %zu bytes\n", printf("Read %zu, expected private blob %zu bytes\n",
bytes_read, fileSz); bytes_read, fileSz);
@ -824,14 +830,13 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key)
else { else {
rc = BUFFER_E; rc = BUFFER_E;
printf("File %s not found!\n", filename); printf("File %s not found!\n", filename);
printf("Keys can be generated by running:\n" printf("Key can be generated by running:\n"
" ./examples/keygen/keygen rsa_test_blob.raw -rsa -t\n" " ./examples/keygen/keygen keyblob.bin -rsa -t -pem -eh\n");
" ./examples/keygen/keygen ecc_test_blob.raw -ecc -t\n");
} }
exit: exit:
if (fp) if (fp)
XFCLOSE(fp); WFCLOSE(NULL, fp);
#else #else
(void)filename; (void)filename;
(void)key; (void)key;

View File

@ -2100,32 +2100,35 @@ static int LoadPubKeyList(StrList* strList, int format, PwMapList* mapList)
#endif #endif
#ifdef WOLFSSH_TPM #ifdef WOLFSSH_TPM
static char* LoadTpmSshKey(const char* keyFile) static char* LoadTpmSshKey(const char* keyFile, const char* username)
{ {
WFILE* file = NULL; WFILE* file = NULL;
char* buffer = NULL; char* buffer = NULL;
char* ret = NULL; char* ret = NULL;
long length; long length;
size_t usernameLen;
if (WFOPEN(NULL, &file, keyFile, "rb") != 0) { if (WFOPEN(NULL, &file, keyFile, "rb") != 0) {
fprintf(stderr, fprintf(stderr,
"Failed to open TPM key file: %s\n", keyFile); "Failed to open TPM key file: %s\n", keyFile);
return NULL; return NULL;
} }
WFSEEK(NULL, file, 0, WSEEK_END);
WFSEEK(NULL, file, 0, SEEK_END);
length = WFTELL(NULL, file); length = WFTELL(NULL, file);
WFSEEK(NULL, file, 0, SEEK_SET); WREWIND(NULL, file);
buffer = (char*)WMALLOC(length + 8 + 1, NULL, DYNTYPE_BUFFER); usernameLen = WSTRLEN(username);
buffer = (char*)WMALLOC(length + usernameLen + 2, NULL, DYNTYPE_BUFFER);
if (buffer) { if (buffer) {
if (WFREAD(NULL, buffer, 1, length, file) == (size_t)length) { if (WFREAD(NULL, buffer, 1, length, file) == (size_t)length) {
while (length > 0 && (buffer[length-1] == '\n' || while (length > 0 && (buffer[length-1] == '\n' ||
buffer[length-1] == '\r')) { buffer[length-1] == '\r')) {
length--; length--;
} }
WMEMCPY(buffer + length, " hansel\n", 8); buffer[length] = ' ';
buffer[length + 8] = '\0'; WMEMCPY(buffer + length + 1, username, usernameLen);
buffer[length + 1 + usernameLen] = '\n';
buffer[length + 2 + usernameLen] = '\0';
ret = buffer; ret = buffer;
} }
else { else {
@ -2634,7 +2637,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
/* Load custom TPM key if specified */ /* Load custom TPM key if specified */
#ifdef WOLFSSH_TPM #ifdef WOLFSSH_TPM
if (tpmKeyPath != NULL) { if (tpmKeyPath != NULL) {
const char* newBuffer = LoadTpmSshKey(tpmKeyPath); const char* newBuffer = LoadTpmSshKey(tpmKeyPath, "hansel");
if (newBuffer != NULL) { if (newBuffer != NULL) {
sampleTpmPublicKeyRsaBuffer = newBuffer; sampleTpmPublicKeyRsaBuffer = newBuffer;
} }