Skip to content

Commit ac4572f

Browse files
committed
Adds support so that we can pass a ssh-rsa BASE64 encoded public key created from keygen in wolfTPM and append to the samplePublicKeyRsaBuffer using the -s "key.ssh" argument on server side.
1 parent 3d99308 commit ac4572f

2 files changed

Lines changed: 98 additions & 14 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,15 @@ Custom password:
569569
$ ./examples/keygen/keygen keyblob.bin -rsa -t -pem -eh -auth=<custompassword>
570570

571571
This will produce a key.pem TPM public key which needs to be converted the to
572-
the ssh-rsa BASE64 username format using this command: `ssh-keygen -f key.pem -i -m PKCS8`
573-
Take this BASE64 encoded public key and update the `samplePublicKeyRsaBuffer`
574-
in `echoserver.c` with it. Make sure to the user is "hansel"'s public key.
572+
the ssh-rsa BASE64 username format using this command:
573+
574+
$ ssh-keygen -f key.pem -i -m PKCS8 > ../wolfssh/key.ssh
575575

576576
The directory `examples` contains an echoserver that any client should
577577
be able to connect to. From wolfSSH open two terminal instances and run the
578-
server:
578+
server with the key.ssh file you created in the previous step:
579579

580-
$ ./examples/echoserver/echoserver
580+
$ ./examples/echoserver/echoserver -s key.ssh
581581

582582
From another terminal run the client with the keyblob. Using primary endorsement key
583583
If you used the default password for keygen you must specify the password:

examples/echoserver/echoserver.c

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,21 +1784,84 @@ static const char samplePublicKeyEccBuffer[] =
17841784
#endif
17851785

17861786
#ifndef WOLFSSH_NO_RSA
1787-
static const char samplePublicKeyRsaBuffer[] =
1788-
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9P3ZFowOsONXHD5MwWiCciXytBRZGho"
1789-
"MNiisWSgUs5HdHcACuHYPi2W6Z1PBFmBWT9odOrGRjoZXJfDDoPi+j8SSfDGsc/hsCmc3G"
1790-
"p2yEhUZUEkDhtOXyqjns1ickC9Gh4u80aSVtwHRnJZh9xPhSq5tLOhId4eP61s+a5pwjTj"
1791-
"nEhBaIPUJO2C/M0pFnnbZxKgJlX7t1Doy7h5eXxviymOIvaCZKU+x5OopfzM/wFkey0EPW"
1792-
"NmzI5y/+pzU5afsdeEWdiQDIQc80H6Pz8fsoFPvYSG+s4/wz0duu7yeeV1Ypoho65Zr+pE"
1793-
"nIf7dO0B8EblgWt+ud+JI8wrAhfE4x hansel\n"
1787+
static const char* samplePublicKeyRsaBuffer =
17941788
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqDwRVTRVk/wjPhoo66+Mztrc31KsxDZ"
17951789
"+kAV0139PHQ+wsueNpba6jNn5o6mUTEOrxrz0LMsDJOBM7CmG0983kF4gRIihECpQ0rcjO"
17961790
"P6BSfbVTE9mfIK5IsUiZGd8SoE9kSV2pJ2FvZeBQENoAxEFk0zZL9tchPS+OCUGbK4SDjz"
17971791
"uNZl/30Mczs73N3MBzi6J1oPo7sFlqzB6ecBjK2Kpjus4Y1rYFphJnUxtKvB0s+hoaadru"
17981792
"biE57dK6BrH5iZwVLTQKux31uCJLPhiktI3iLbdlGZEctJkTasfVSsUizwVIyRjhVKmbdI"
1799-
"RGwkU38D043AR1h0mUoGCPIKuqcFMf gretel\n";
1793+
"RGwkU38D043AR1h0mUoGCPIKuqcFMf gretel\n"
1794+
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9P3ZFowOsONXHD5MwWiCciXytBRZGho"
1795+
"MNiisWSgUs5HdHcACuHYPi2W6Z1PBFmBWT9odOrGRjoZXJfDDoPi+j8SSfDGsc/hsCmc3G"
1796+
"p2yEhUZUEkDhtOXyqjns1ickC9Gh4u80aSVtwHRnJZh9xPhSq5tLOhId4eP61s+a5pwjTj"
1797+
"nEhBaIPUJO2C/M0pFnnbZxKgJlX7t1Doy7h5eXxviymOIvaCZKU+x5OopfzM/wFkey0EPW"
1798+
"NmzI5y/+pzU5afsdeEWdiQDIQc80H6Pz8fsoFPvYSG+s4/wz0duu7yeeV1Ypoho65Zr+pE"
1799+
"nIf7dO0B8EblgWt+ud+JI8wrAhfE4x hansel\n";
18001800
#endif
18011801

1802+
/* Loads a new key from a file and appends
1803+
* it to the samplePublicKeyRsaBuffer */
1804+
static char* LoadSshKey(const char* path)
1805+
{
1806+
FILE* file;
1807+
char* buffer = NULL;
1808+
char* ret = NULL;
1809+
long length;
1810+
const char* gretelKey = samplePublicKeyRsaBuffer;
1811+
const char* hanselKey;
1812+
1813+
/* Find where hansel's key starts (it's after gretel's key) */
1814+
hanselKey = strstr(gretelKey + 1, "ssh-rsa");
1815+
if (!hanselKey) {
1816+
fprintf(stderr, "Could not find hansel's key\n");
1817+
return NULL;
1818+
}
1819+
1820+
/* Calculate length of gretel's key portion */
1821+
long gretelLen = hanselKey - gretelKey;
1822+
1823+
/* Read new key from file */
1824+
file = fopen(path, "rb");
1825+
if (!file) {
1826+
fprintf(stderr, "Failed to open SSH key file: %s\n", path);
1827+
return NULL;
1828+
}
1829+
1830+
fseek(file, 0, SEEK_END);
1831+
length = ftell(file);
1832+
fseek(file, 0, SEEK_SET);
1833+
1834+
buffer = (char*)WMALLOC(length + 1, NULL, DYNTYPE_BUFFER);
1835+
if (buffer) {
1836+
if (fread(buffer, 1, length, file) != (size_t)length) {
1837+
WFREE(buffer, NULL, DYNTYPE_BUFFER);
1838+
buffer = NULL;
1839+
}
1840+
else {
1841+
/* Remove any trailing newlines */
1842+
while (length > 0 && (buffer[length-1] == '\n'
1843+
|| buffer[length-1] == '\r')) {
1844+
length--;
1845+
}
1846+
buffer[length] = '\0';
1847+
1848+
/* Allocate space for: gretel's key + new key + " hansel\n" */
1849+
ret = (char*)WMALLOC(gretelLen + length + 8, NULL, DYNTYPE_BUFFER);
1850+
if (ret) {
1851+
/* Copy gretel's key */
1852+
WMEMCPY(ret, gretelKey, gretelLen);
1853+
/* Copy new key */
1854+
WMEMCPY(ret + gretelLen, buffer, length);
1855+
/* Append hansel identifier */
1856+
WMEMCPY(ret + gretelLen + length, " hansel\n", 8);
1857+
}
1858+
WFREE(buffer, NULL, DYNTYPE_BUFFER);
1859+
}
1860+
}
1861+
1862+
fclose(file);
1863+
return ret;
1864+
}
18021865

18031866
#ifdef WOLFSSH_ALLOW_USERAUTH_NONE
18041867

@@ -2375,6 +2438,7 @@ static void ShowUsage(void)
23752438
"to use\n");
23762439
printf(" -m <list> set the comma separated list of mac algos to use\n");
23772440
printf(" -b <num> test user auth would block\n");
2441+
printf(" -s <file> load SSH public key file to replace default hansel key\n");
23782442
}
23792443

23802444

@@ -2420,6 +2484,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
24202484
const char* macList = NULL;
24212485
const char* cipherList = NULL;
24222486
ES_HEAP_HINT* heap = NULL;
2487+
static char* sshKeyPath = NULL;
24232488
int multipleConnections = 1;
24242489
int userEcc = 0;
24252490
int peerEcc = 0;
@@ -2442,7 +2507,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
24422507
kbAuthData.promptCount = 0;
24432508

24442509
if (argc > 0) {
2445-
const char* optlist = "?1a:d:efEp:R:Ni:j:i:I:J:K:P:k:b:x:m:c:";
2510+
const char* optlist = "?1a:d:efEp:R:Ni:j:i:I:J:K:P:k:b:x:m:c:s:";
24462511
myoptind = 0;
24472512
while ((ch = mygetopt(argc, argv, optlist)) != -1) {
24482513
switch (ch) {
@@ -2546,6 +2611,10 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
25462611
cipherList = myoptarg;
25472612
break;
25482613

2614+
case 's':
2615+
sshKeyPath = myoptarg;
2616+
break;
2617+
25492618
default:
25502619
ShowUsage();
25512620
serverArgs->return_code = MY_EX_USAGE;
@@ -2578,6 +2647,21 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
25782647
ES_ERROR("Couldn't initialize wolfSSH.\n");
25792648
}
25802649

2650+
/* Load custom SSH key if specified */
2651+
if (sshKeyPath != NULL) {
2652+
const char* newBuffer = LoadSshKey(sshKeyPath);
2653+
if (newBuffer != NULL) {
2654+
samplePublicKeyRsaBuffer = newBuffer;
2655+
}
2656+
else {
2657+
ES_ERROR("Failed to load SSH key from %s\n", sshKeyPath);
2658+
}
2659+
#ifdef WOLFSSH_DEBUG
2660+
printf("New samplePublicKeyRsaBuffer:\n%s\n",
2661+
samplePublicKeyRsaBuffer);
2662+
#endif
2663+
}
2664+
25812665
#ifdef WOLFSSH_STATIC_MEMORY
25822666
{
25832667
int ret;

0 commit comments

Comments
 (0)