use of buffer keys in examples with no filesystem

pull/47/head
Jacob Barthelmeh 2018-03-30 10:45:24 -06:00
parent 48590f6896
commit bb668fe743
4 changed files with 215 additions and 9 deletions

View File

@ -22,17 +22,16 @@
#include <wolfssh/ssh.h>
#include <wolfssh/test.h>
#include "examples/client/client.h"
#ifndef USE_WINDOWS_API
#if !defined(USE_WINDOWS_API) && !defined(MICROCHIP_PIC32)
#include <termios.h>
#endif
const char testString[] = "Hello, wolfSSH!";
static int SetEcho(int on)
{
#ifndef USE_WINDOWS_API
#if !defined(USE_WINDOWS_API) && !defined(MICROCHIP_PIC32)
static int echoInit = 0;
static struct termios originalTerm;
if (!echoInit) {

View File

@ -32,6 +32,11 @@
#include "examples/echoserver/echoserver.h"
#ifdef NO_FILESYSTEM
#include <wolfssh/certs_test.h>
#endif
static const char echoserverBanner[] = "wolfSSH Example Echo Server\n";
@ -165,7 +170,7 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
return 0;
}
#ifndef NO_FILESYSTEM
static int load_file(const char* fileName, byte* buf, word32 bufSz)
{
FILE* file;
@ -195,7 +200,38 @@ static int load_file(const char* fileName, byte* buf, word32 bufSz)
return fileSz;
}
#endif /* NO_FILESYSTEM */
/* returns buffer size on success */
static int load_key(byte isEcc, byte* buf, word32 bufSz)
{
word32 sz = 0;
#ifndef NO_FILESYSTEM
const char* bufName;
bufName = isEcc ? "./keys/server-key-ecc.der" :
"./keys/server-key-rsa.der" ;
sz = load_file(bufName, buf, bufSz);
#else
/* using buffers instead */
if (isEcc) {
if (sizeof_ecc_key_der_256 > bufSz) {
return 0;
}
WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
sz = sizeof_ecc_key_der_256;
}
else {
if (sizeof_rsa_key_der_2048 > bufSz) {
return 0;
}
WMEMCPY(buf, (byte*)rsa_key_der_2048, sizeof_rsa_key_der_2048);
sz = sizeof_rsa_key_der_2048;
}
#endif
return sz;
}
static INLINE void c32toa(word32 u32, byte* c)
{
@ -555,7 +591,7 @@ THREAD_RETURN WOLFSSH_THREAD echoserver_test(void* args)
bufName = useEcc ? "./keys/server-key-ecc.der" :
"./keys/server-key-rsa.der" ;
bufSz = load_file(bufName, buf, SCRATCH_BUFFER_SZ);
bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
if (bufSz == 0) {
fprintf(stderr, "Couldn't load key file.\n");
exit(EXIT_FAILURE);

View File

@ -31,6 +31,10 @@
#include <wolfssh/test.h>
#include "examples/server/server.h"
#ifdef NO_FILESYSTEM
#include <wolfssh/certs_test.h>
#endif
static const char serverBanner[] = "wolfSSH Example Server\n";
@ -165,7 +169,7 @@ static THREAD_RETURN WOLFSSH_THREAD server_worker(void* vArgs)
return 0;
}
#ifndef NO_FILESYSTEM
static int load_file(const char* fileName, byte* buf, word32 bufSz)
{
FILE* file;
@ -195,6 +199,38 @@ static int load_file(const char* fileName, byte* buf, word32 bufSz)
return fileSz;
}
#endif /* !NO_FILESYSTEM */
/* returns buffer size on success */
static int load_key(byte isEcc, byte* buf, word32 bufSz)
{
word32 sz = 0;
#ifndef NO_FILESYSTEM
const char* bufName;
bufName = isEcc ? "./keys/server-key-ecc.der" :
"./keys/server-key-rsa.der" ;
sz = load_file(bufName, buf, bufSz);
#else
/* using buffers instead */
if (isEcc) {
if (sizeof_ecc_key_der_256 > bufSz) {
return 0;
}
WMEMCPY(buf, ecc_key_der_256, sizeof_ecc_key_der_256);
sz = sizeof_ecc_key_der_256;
}
else {
if (sizeof_rsa_key_der_2048 > bufSz) {
return 0;
}
WMEMCPY(buf, rsa_key_der_2048, sizeof_rsa_key_der_2048);
sz = sizeof_rsa_key_der_2048;
}
#endif
return sz;
}
static INLINE void c32toa(word32 u32, byte* c)
@ -526,9 +562,9 @@ THREAD_RETURN WOLFSSH_THREAD server_test(void* args)
bufName = useEcc ? "./keys/server-key-ecc.der" :
"./keys/server-key-rsa.der" ;
bufSz = load_file(bufName, buf, SCRATCH_BUFFER_SZ);
bufSz = load_key(useEcc, buf, SCRATCH_BUFFER_SZ);
if (bufSz == 0) {
fprintf(stderr, "Couldn't load key file.\n");
fprintf(stderr, "Couldn't load key.\n");
exit(EXIT_FAILURE);
}
if (wolfSSH_CTX_UsePrivateKey_buffer(ctx, buf, bufSz,
@ -589,13 +625,17 @@ THREAD_RETURN WOLFSSH_THREAD server_test(void* args)
threadCtx->fd = clientFd;
threadCtx->id = threadCount++;
#ifndef SINGLE_THREADED
start_thread(server_worker, threadCtx, &thread);
if (multipleConnections)
detach_thread(thread);
else
join_thread(thread);
#else
server_worker(threadCtx);
(void)thread;
#endif /* SINGLE_THREADED */
} while (multipleConnections);
PwMapListDelete(&pwMapList);

131
gencertbuf.pl 100755
View File

@ -0,0 +1,131 @@
#!/usr/bin/perl
# gencertbuf.pl
# version 1.1
# Updated 07/01/2014
#
# Copyright (C) 2006-2018 wolfSSL Inc.
#
use strict;
use warnings;
# ---- SCRIPT SETTINGS -------------------------------------------------------
# output C header file to write key buffers to
my $outputFile = "./wolfssh/certs_test.h";
# ecc keys to be converted
my @fileList_ecc = (
[ "./keys/server-key-ecc.der", "ecc_key_der_256" ],
[ "./keys/server-key-ecc-384.der", "ecc_key_der_384" ],
[ "./keys/server-key-ecc-521.der", "ecc_key_der_521" ]
);
# 2048-bit keys to be converted
my @fileList_2048 = (
[ "./keys/server-key-rsa.der", "rsa_key_der_2048" ],
);
# ----------------------------------------------------------------------------
my $num_ecc = @fileList_ecc;
my $num_2048 = @fileList_2048;
# open our output file, "+>" creates and/or truncates
open OUT_FILE, "+>", $outputFile or die $!;
print OUT_FILE "/* certs_test.h\n";
print OUT_FILE "*\n";
print OUT_FILE "* Copyright (C) 2014-2018 wolfSSL Inc.\n";
print OUT_FILE "*\n";
print OUT_FILE "* This file is part of wolfSSH.\n";
print OUT_FILE "*\n";
print OUT_FILE "* wolfSSH is free software; you can redistribute it and/or modify\n";
print OUT_FILE "* it under the terms of the GNU General Public License as published by\n";
print OUT_FILE "* the Free Software Foundation; either version 3 of the License, or\n";
print OUT_FILE "* (at your option) any later version.\n";
print OUT_FILE "*\n";
print OUT_FILE "* wolfSSH is distributed in the hope that it will be useful,\n";
print OUT_FILE "* but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
print OUT_FILE "* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n";
print OUT_FILE "* GNU General Public License for more details.\n";
print OUT_FILE "*\n";
print OUT_FILE "* You should have received a copy of the GNU General Public License\n";
print OUT_FILE "* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.\n";
print OUT_FILE "*/\n\n";
print OUT_FILE "#ifndef WOLFSSL_CERTS_TEST_H\n";
print OUT_FILE "#define WOLFSSL_CERTS_TEST_H\n\n";
# convert and print 2048-bit certs/keys
print OUT_FILE "#ifdef NO_FILESYSTEM\n\n";
for (my $i = 0; $i < $num_2048; $i++) {
my $fname = $fileList_2048[$i][0];
my $sname = $fileList_2048[$i][1];
print OUT_FILE "/* $fname, 2048-bit */\n";
print OUT_FILE "static const unsigned char $sname\[] =\n";
print OUT_FILE "{\n";
file_to_hex($fname);
print OUT_FILE "};\n";
print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
}
# convert and print ECC cert/keys
for (my $i = 0; $i < $num_ecc; $i++) {
my $fname = $fileList_ecc[$i][0];
my $sname = $fileList_ecc[$i][1];
print OUT_FILE "/* $fname, ECC */\n";
print OUT_FILE "static const unsigned char $sname\[] =\n";
print OUT_FILE "{\n";
file_to_hex($fname);
print OUT_FILE "};\n";
print OUT_FILE "static const int sizeof_$sname = sizeof($sname);\n\n";
}
print OUT_FILE "#endif /* NO_FILESYSTEM */\n\n";
print OUT_FILE "#endif /* WOLFSSL_CERTS_TEST_H */\n\n";
# close certs_test.h file
close OUT_FILE or die $!;
# print file as hex, comma-separated, as needed by C buffer
sub file_to_hex {
my $fileName = $_[0];
open my $fp, "<", $fileName or die $!;
binmode($fp);
my $fileLen = -s $fileName;
my $byte;
for (my $i = 0, my $j = 1; $i < $fileLen; $i++, $j++)
{
if ($j == 1) {
print OUT_FILE "\t";
}
read($fp, $byte, 1) or die "Error reading $fileName";
my $output = sprintf("0x%02X", ord($byte));
print OUT_FILE $output;
if ($i != ($fileLen - 1)) {
print OUT_FILE ", ";
}
if ($j == 10) {
$j = 0;
print OUT_FILE "\n";
}
}
print OUT_FILE "\n";
close($fp);
}