summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2020-01-25 12:34:04 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2020-01-25 12:34:04 +0000
commita963369e626b7fac7e86b9577570a065b885e053 (patch)
treef5cbea5561c0b1a5e981f3bd049d0532e532bcba
parentc74910abe050c268c8949473a58da7a295c9e3a4 (diff)
parentf38cc1ae4336ce10715b7b9719ce41e22fdd5349 (diff)
downloadgnutls-a963369e626b7fac7e86b9577570a065b885e053.tar.gz
Merge branch 'tmp-fuzzers-update' into 'master'
fuzz: update ed448 fuzzer traces and other fuzz improvements See merge request gnutls/gnutls!1177
-rw-r--r--fuzz/README-adding-traces.md17
-rw-r--r--fuzz/certs.h78
-rw-r--r--fuzz/gnutls_client_fuzzer.c2
-rw-r--r--fuzz/gnutls_client_fuzzer.in/96c552adcacf4108c319533ea61c33f4240ad0fd (renamed from fuzz/gnutls_server_fuzzer.in/96c552adcacf4108c319533ea61c33f4240ad0fd)bin1731 -> 1731 bytes
-rw-r--r--fuzz/gnutls_client_rawpk_fuzzer.c8
-rw-r--r--fuzz/gnutls_psk_server_fuzzer.c11
-rw-r--r--fuzz/gnutls_server_fuzzer.c13
-rw-r--r--fuzz/gnutls_server_fuzzer.in/5ef0df17445fb4098d15536a1195a47cb55b6845 (renamed from fuzz/gnutls_client_fuzzer.in/5ef0df17445fb4098d15536a1195a47cb55b6845)bin430 -> 430 bytes
-rw-r--r--fuzz/gnutls_server_rawpk_fuzzer.c8
-rw-r--r--fuzz/gnutls_srp_server_fuzzer.c1
-rw-r--r--fuzz/main.c64
11 files changed, 161 insertions, 41 deletions
diff --git a/fuzz/README-adding-traces.md b/fuzz/README-adding-traces.md
index 485c8bbb3d..20866f6ff3 100644
--- a/fuzz/README-adding-traces.md
+++ b/fuzz/README-adding-traces.md
@@ -1,10 +1,14 @@
# Generate and add new protocol traces
+## Step 1: compilation
+
Compile GnuTLS as:
```
./configure --enable-fuzzer-target --disable-doc
```
+## Step 2: Get the traces
+
Start the server with the necessary parameters (here TLS1.3 is assumed).
```
./gnutls-http-serv --priority NORMAL:-VERS-ALL:+VERS-TLS1.3
@@ -17,6 +21,19 @@ gnutls-cli localhost:5556 --priority NORMAL:-VERS-ALL:+VERS-TLS1.3:-KX-ALL:+PSK
gnutls-cli localhost:5556 --priority NORMAL:-VERS-ALL:+VERS-TLS1.3:-KX-ALL:+DHE-PSK --insecure --save-server-trace /tmp/server-trace-psk2 --save-client-trace /tmp/client-trace-psk2 --pskusername test --pskkey 8a7759b3f26983c453e448060bde8981
```
+## Step 3: Ensure server can read them
+
+If there is a new key type tested, ensure that `gnutls_server_fuzzer` and
+`gnutls_client_fuzzer` enable it, and set the appropriate keys.
+
+To verify that connection proceeds past to reading the first packet use:
+$ make gnutls_server_fuzzer gnutls_client_fuzzer
+$ GNUTLS_DEBUG_LEVEL=6 gnutls_server_fuzzer /tmp/server-trace-x509
+$ GNUTLS_DEBUG_LEVEL=6 gnutls_client_fuzzer /tmp/client-trace-x509
+
+
+## Step 4: Copy the traces
+
cp /tmp/server-trace-x509 fuzz/gnutls_client_fuzzer.in/$(sha1sum /tmp/server-trace-x509|cut -d ' ' -f 1)
cp /tmp/server-trace-psk1 fuzz/gnutls_psk_client_fuzzer.in/$(sha1sum /tmp/server-trace-psk1|cut -d ' ' -f 1)
cp /tmp/server-trace-psk2 fuzz/gnutls_psk_client_fuzzer.in/$(sha1sum /tmp/server-trace-psk2|cut -d ' ' -f 1)
diff --git a/fuzz/certs.h b/fuzz/certs.h
index c95f63b5dd..de57f9d4fe 100644
--- a/fuzz/certs.h
+++ b/fuzz/certs.h
@@ -300,6 +300,74 @@ static const uint8_t kEd25519PrivateKeyDER[] = {
0xe5, 0xb4, 0x5f, 0x9b, 0xd7, 0xa6, 0xf8, 0x36, 0x0c, 0x92, 0x59, 0x70
};
+static const uint8_t kEd448PrivateKeyDER[] = {
+ 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71,
+ 0x04, 0x3b, 0x04, 0x39, 0x0c, 0xf8, 0x7e, 0xb0, 0x94, 0xbf, 0x46, 0xd1,
+ 0x61, 0xbd, 0xe3, 0xb9, 0x9d, 0x1d, 0x32, 0x85, 0x6f, 0xec, 0xfa, 0xe0,
+ 0x14, 0x23, 0x92, 0xcd, 0x98, 0xc0, 0x91, 0xdb, 0x20, 0x6d, 0x17, 0x4b,
+ 0xbf, 0x8e, 0xf4, 0x76, 0xa9, 0xcf, 0x74, 0x6d, 0x94, 0x30, 0x6c, 0x56,
+ 0x5f, 0x97, 0xac, 0x50, 0x79, 0x6f, 0x02, 0x1e, 0xff, 0x8d, 0x77, 0x9c,
+ 0xa5
+};
+
+static const uint8_t kEd448CertificateDER[] = {
+ 0x30, 0x82, 0x02, 0x86, 0x30, 0x82, 0x01, 0x3e, 0xa0, 0x03, 0x02, 0x01,
+ 0x02, 0x02, 0x14, 0x71, 0x76, 0x43, 0x3c, 0xd1, 0x31, 0x93, 0x61, 0xdd,
+ 0xf7, 0x39, 0x0e, 0x77, 0xd7, 0x35, 0x41, 0x3b, 0x9d, 0xed, 0x8d, 0x30,
+ 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
+ 0x05, 0x00, 0x30, 0x19, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
+ 0x03, 0x13, 0x0e, 0x47, 0x6e, 0x75, 0x54, 0x4c, 0x53, 0x20, 0x54, 0x65,
+ 0x73, 0x74, 0x20, 0x43, 0x41, 0x30, 0x20, 0x17, 0x0d, 0x31, 0x39, 0x30,
+ 0x34, 0x32, 0x34, 0x31, 0x34, 0x35, 0x36, 0x35, 0x38, 0x5a, 0x18, 0x0f,
+ 0x39, 0x39, 0x39, 0x39, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, 0x39,
+ 0x35, 0x39, 0x5a, 0x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
+};
+
/* Raw public-key key material for testing */
static char rawpk_public_key_pem1[] =
"-----BEGIN PUBLIC KEY-----\n"
@@ -365,4 +433,14 @@ const gnutls_datum_t rawpk_private_key1 = {
sizeof(rawpk_private_key_pem1) - 1
};
+#define IGNORE_CERTS \
+ (void) kEd25519PrivateKeyDER; \
+ (void) kEd25519CertificateDER; \
+ (void) kEd448PrivateKeyDER; \
+ (void) kEd448CertificateDER; \
+ (void) kECDSAPrivateKeyDER; \
+ (void) kECDSACertificateDER; \
+ (void) kRSAPrivateKeyDER; \
+ (void) kRSACertificateDER
+
#endif
diff --git a/fuzz/gnutls_client_fuzzer.c b/fuzz/gnutls_client_fuzzer.c
index f5c969d990..b68cdb6d7c 100644
--- a/fuzz/gnutls_client_fuzzer.c
+++ b/fuzz/gnutls_client_fuzzer.c
@@ -46,7 +46,7 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
assert(res >= 0);
/*res = gnutls_set_default_priority(session);*/
- res = gnutls_priority_set_direct(session, "NORMAL:"VERS_STR, NULL);
+ res = gnutls_priority_set_direct(session, "NORMAL:+SIGN-EDDSA-ED448:"VERS_STR, NULL);
assert(res >= 0);
memdata.data = data;
diff --git a/fuzz/gnutls_server_fuzzer.in/96c552adcacf4108c319533ea61c33f4240ad0fd b/fuzz/gnutls_client_fuzzer.in/96c552adcacf4108c319533ea61c33f4240ad0fd
index feceb0a220..feceb0a220 100644
--- a/fuzz/gnutls_server_fuzzer.in/96c552adcacf4108c319533ea61c33f4240ad0fd
+++ b/fuzz/gnutls_client_fuzzer.in/96c552adcacf4108c319533ea61c33f4240ad0fd
Binary files differ
diff --git a/fuzz/gnutls_client_rawpk_fuzzer.c b/fuzz/gnutls_client_rawpk_fuzzer.c
index af4336363a..c20b885538 100644
--- a/fuzz/gnutls_client_rawpk_fuzzer.c
+++ b/fuzz/gnutls_client_rawpk_fuzzer.c
@@ -54,13 +54,7 @@
int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
{
- (void) kEd25519PrivateKeyDER;
- (void) kEd25519CertificateDER;
- (void) kECDSAPrivateKeyDER;
- (void) kECDSACertificateDER;
- (void) kRSAPrivateKeyDER;
- (void) kRSACertificateDER;
-
+ IGNORE_CERTS;
int res;
gnutls_session_t session;
gnutls_certificate_credentials_t rawpk_cred;
diff --git a/fuzz/gnutls_psk_server_fuzzer.c b/fuzz/gnutls_psk_server_fuzzer.c
index 3c1f62df19..d07e2eef60 100644
--- a/fuzz/gnutls_psk_server_fuzzer.c
+++ b/fuzz/gnutls_psk_server_fuzzer.c
@@ -51,6 +51,7 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
gnutls_datum_t rsa_cert, rsa_key;
gnutls_datum_t ecdsa_cert, ecdsa_key;
gnutls_datum_t ed25519_cert, ed25519_key;
+ gnutls_datum_t ed448_cert, ed448_key;
gnutls_session_t session;
gnutls_certificate_credentials_t xcred;
gnutls_psk_server_credentials_t pcred;
@@ -83,6 +84,11 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
ed25519_key.data = (unsigned char *)kEd25519PrivateKeyDER;
ed25519_key.size = sizeof(kEd25519PrivateKeyDER);
+ ed448_cert.data = (unsigned char *)kEd448CertificateDER;
+ ed448_cert.size = sizeof(kEd448CertificateDER);
+ ed448_key.data = (unsigned char *)kEd448PrivateKeyDER;
+ ed448_key.size = sizeof(kEd448PrivateKeyDER);
+
res =
gnutls_certificate_set_x509_key_mem(xcred, &rsa_cert, &rsa_key,
GNUTLS_X509_FMT_DER);
@@ -98,6 +104,11 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
GNUTLS_X509_FMT_DER);
assert(res >= 0);
+ res =
+ gnutls_certificate_set_x509_key_mem(xcred, &ed448_cert, &ed448_key,
+ GNUTLS_X509_FMT_DER);
+ assert(res >= 0);
+
gnutls_certificate_set_known_dh_params(xcred, GNUTLS_SEC_PARAM_MEDIUM);
res = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
diff --git a/fuzz/gnutls_server_fuzzer.c b/fuzz/gnutls_server_fuzzer.c
index 3ef02aec3c..c42ff21efa 100644
--- a/fuzz/gnutls_server_fuzzer.c
+++ b/fuzz/gnutls_server_fuzzer.c
@@ -34,6 +34,7 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
gnutls_datum_t rsa_cert, rsa_key;
gnutls_datum_t ecdsa_cert, ecdsa_key;
gnutls_datum_t ed25519_cert, ed25519_key;
+ gnutls_datum_t ed448_cert, ed448_key;
gnutls_session_t session;
gnutls_certificate_credentials_t xcred;
struct mem_st memdata;
@@ -59,6 +60,11 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
ed25519_key.data = (unsigned char *)kEd25519PrivateKeyDER;
ed25519_key.size = sizeof(kEd25519PrivateKeyDER);
+ ed448_cert.data = (unsigned char *)kEd448CertificateDER;
+ ed448_cert.size = sizeof(kEd448CertificateDER);
+ ed448_key.data = (unsigned char *)kEd448PrivateKeyDER;
+ ed448_key.size = sizeof(kEd448PrivateKeyDER);
+
res =
gnutls_certificate_set_x509_key_mem(xcred, &rsa_cert, &rsa_key,
GNUTLS_X509_FMT_DER);
@@ -74,13 +80,18 @@ int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
GNUTLS_X509_FMT_DER);
assert(res >= 0);
+ res =
+ gnutls_certificate_set_x509_key_mem(xcred, &ed448_cert, &ed448_key,
+ GNUTLS_X509_FMT_DER);
+ assert(res >= 0);
+
gnutls_certificate_set_known_dh_params(xcred, GNUTLS_SEC_PARAM_MEDIUM);
res = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
assert(res >= 0);
/*res = gnutls_set_default_priority(session);*/
- res = gnutls_priority_set_direct(session, "NORMAL:"VERS_STR, NULL);
+ res = gnutls_priority_set_direct(session, "NORMAL:+SIGN-EDDSA-ED448:"VERS_STR, NULL);
assert(res >= 0);
memdata.data = data;
diff --git a/fuzz/gnutls_client_fuzzer.in/5ef0df17445fb4098d15536a1195a47cb55b6845 b/fuzz/gnutls_server_fuzzer.in/5ef0df17445fb4098d15536a1195a47cb55b6845
index 45eede51d0..45eede51d0 100644
--- a/fuzz/gnutls_client_fuzzer.in/5ef0df17445fb4098d15536a1195a47cb55b6845
+++ b/fuzz/gnutls_server_fuzzer.in/5ef0df17445fb4098d15536a1195a47cb55b6845
Binary files differ
diff --git a/fuzz/gnutls_server_rawpk_fuzzer.c b/fuzz/gnutls_server_rawpk_fuzzer.c
index 854f5872fe..a1a6713869 100644
--- a/fuzz/gnutls_server_rawpk_fuzzer.c
+++ b/fuzz/gnutls_server_rawpk_fuzzer.c
@@ -50,13 +50,7 @@
int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
{
- (void) kEd25519PrivateKeyDER;
- (void) kEd25519CertificateDER;
- (void) kECDSAPrivateKeyDER;
- (void) kECDSACertificateDER;
- (void) kRSAPrivateKeyDER;
- (void) kRSACertificateDER;
-
+ IGNORE_CERTS;
int res;
gnutls_session_t session;
gnutls_certificate_credentials_t rawpk_cred;
diff --git a/fuzz/gnutls_srp_server_fuzzer.c b/fuzz/gnutls_srp_server_fuzzer.c
index 567b7d45c9..04301638e4 100644
--- a/fuzz/gnutls_srp_server_fuzzer.c
+++ b/fuzz/gnutls_srp_server_fuzzer.c
@@ -61,6 +61,7 @@ srp_cb(gnutls_session_t session, const char *username,
int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
{
+ IGNORE_CERTS;
int res;
gnutls_datum_t rsa_cert, rsa_key;
gnutls_datum_t ecdsa_cert, ecdsa_key;
diff --git a/fuzz/main.c b/fuzz/main.c
index cdeba17238..b2223d0fc7 100644
--- a/fuzz/main.c
+++ b/fuzz/main.c
@@ -43,6 +43,41 @@
# define SLASH '/'
#endif
+static int test_single_file(const char *fname)
+{
+ int fd, ret;
+ struct stat st;
+ uint8_t *data;
+ ssize_t n;
+
+ if ((fd = open(fname, O_RDONLY)) == -1) {
+ fprintf(stderr, "Failed to open %s (%d)\n", fname, errno);
+ return -1;
+ }
+
+ if (fstat(fd, &st) != 0) {
+ fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno);
+ close(fd);
+ return -1;
+ }
+
+ data = malloc(st.st_size);
+ if ((n = read(fd, data, st.st_size)) == st.st_size) {
+ printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname);
+ fflush(stdout);
+ LLVMFuzzerTestOneInput(data, st.st_size);
+ fflush(stderr);
+ ret = 0;
+ } else {
+ fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", (unsigned long long) st.st_size, fname, errno, n);
+ ret = -1;
+ }
+
+ free(data);
+ close(fd);
+ return ret;
+}
+
static int test_all_from(const char *dirname)
{
DIR *dirp;
@@ -55,31 +90,8 @@ static int test_all_from(const char *dirname)
char fname[strlen(dirname) + strlen(dp->d_name) + 2];
snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name);
- int fd;
- if ((fd = open(fname, O_RDONLY)) == -1) {
- fprintf(stderr, "Failed to open %s (%d)\n", fname, errno);
- continue;
- }
-
- struct stat st;
- if (fstat(fd, &st) != 0) {
- fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno);
- close(fd);
+ if (test_single_file(fname) < 0)
continue;
- }
-
- uint8_t *data = malloc(st.st_size);
- ssize_t n;
- if ((n = read(fd, data, st.st_size)) == st.st_size) {
- printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname);
- fflush(stdout);
- LLVMFuzzerTestOneInput(data, st.st_size);
- fflush(stderr);
- } else
- fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", (unsigned long long) st.st_size, fname, errno, n);
-
- free(data);
- close(fd);
}
closedir(dirp);
return 0;
@@ -108,7 +120,9 @@ int main(int argc, char **argv)
target_len -= 4; // ignore .exe
#endif
- {
+ if (argc > 1) { /* test a single file */
+ test_single_file(argv[1]);
+ } else { /* test the target directory */
int rc;
char corporadir[sizeof(SRCDIR) + 1 + target_len + 8];
snprintf(corporadir, sizeof(corporadir), SRCDIR "/%.*s.in", (int) target_len, target);