summaryrefslogtreecommitdiff
path: root/tests/set_x509_key.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2016-09-12 10:23:48 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2016-09-12 10:25:28 +0200
commitb3c508908e78455cd5588c2cb0663d0386182a96 (patch)
tree3de87105c781312b5ed466ffdcbd7acde6c46479 /tests/set_x509_key.c
parent767c893992ffec5664f74a69ff76d2835903e975 (diff)
downloadgnutls-b3c508908e78455cd5588c2cb0663d0386182a96.tar.gz
tests: check key mismatch on gnutls_certificate_set_*key
That is, check whether these functions can successfully recover from such condition, without leaks or double freeing.
Diffstat (limited to 'tests/set_x509_key.c')
-rw-r--r--tests/set_x509_key.c45
1 files changed, 41 insertions, 4 deletions
diff --git a/tests/set_x509_key.c b/tests/set_x509_key.c
index aa540144a8..306bcd0809 100644
--- a/tests/set_x509_key.c
+++ b/tests/set_x509_key.c
@@ -84,7 +84,7 @@ static void compare(const gnutls_datum_t *der, const void *ipem)
return;
}
-static unsigned import_key(gnutls_certificate_credentials_t xcred, const gnutls_datum_t *skey, const gnutls_datum_t *cert)
+static int import_key(gnutls_certificate_credentials_t xcred, const gnutls_datum_t *skey, const gnutls_datum_t *cert)
{
gnutls_x509_privkey_t key;
gnutls_x509_crt_t *crt_list;
@@ -107,8 +107,9 @@ static unsigned import_key(gnutls_certificate_credentials_t xcred, const gnutls_
ret = gnutls_certificate_set_x509_key(xcred, crt_list,
crt_list_size, key);
if (ret < 0) {
- fail("error in gnutls_certificate_set_x509_key: %s\n", gnutls_strerror(ret));
- exit(1);
+ success("error in gnutls_certificate_set_x509_key: %s\n", gnutls_strerror(ret));
+ idx = ret;
+ goto cleanup;
}
/* return index */
@@ -125,6 +126,7 @@ static unsigned import_key(gnutls_certificate_credentials_t xcred, const gnutls_
compare(&tcert, cert->data+i);
}
+ cleanup:
gnutls_x509_privkey_deinit(key);
for (i=0;i<crt_list_size;i++) {
gnutls_x509_crt_deinit(crt_list[i]);
@@ -134,7 +136,7 @@ static unsigned import_key(gnutls_certificate_credentials_t xcred, const gnutls_
return idx;
}
-void doit(void)
+static void basic(void)
{
gnutls_certificate_credentials_t x509_cred;
gnutls_certificate_credentials_t clicred;
@@ -178,3 +180,38 @@ void doit(void)
success("success");
}
+static void failure_mode(void)
+{
+ gnutls_certificate_credentials_t x509_cred;
+ int ret;
+
+ /* this must be called once in the program
+ */
+ global_init();
+
+ gnutls_global_set_time_function(mytime);
+
+ gnutls_global_set_log_function(tls_log_func);
+ if (debug)
+ gnutls_global_set_log_level(6);
+
+ assert(gnutls_certificate_allocate_credentials(&x509_cred)>=0);
+
+ ret = import_key(x509_cred, &server_key, &server_ecc_cert);
+ if (ret >= 0) {
+ fail("gnutls_certificate_set_x509_key: succeeded!\n");
+ }
+
+ gnutls_certificate_free_credentials(x509_cred);
+
+ gnutls_global_deinit();
+
+ if (debug)
+ success("success");
+}
+
+void doit(void)
+{
+ basic();
+ failure_mode();
+}