tests: fix encoding of IP addresses in x509 certs

We need to encode just the address bytes, not the whole struct sockaddr
data. Add a test case to validate that we're matching on SAN IP
addresses correctly.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20220426160048.812266-2-berrange@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2022-04-26 17:00:40 +01:00 committed by Dr. David Alan Gilbert
parent 10c2a0c5e7
commit c98ce274db
2 changed files with 22 additions and 5 deletions

View File

@ -168,9 +168,19 @@ test_tls_get_ipaddr(const char *addrstr,
hints.ai_flags = AI_NUMERICHOST;
g_assert(getaddrinfo(addrstr, NULL, &hints, &res) == 0);
*datalen = res->ai_addrlen;
*data = g_new(char, *datalen);
memcpy(*data, res->ai_addr, *datalen);
if (res->ai_family == AF_INET) {
struct sockaddr_in *in = (struct sockaddr_in *)res->ai_addr;
*datalen = sizeof(in->sin_addr);
*data = g_new(char, *datalen);
memcpy(*data, &in->sin_addr, *datalen);
} else if (res->ai_family == AF_INET6) {
struct sockaddr_in6 *in = (struct sockaddr_in6 *)res->ai_addr;
*datalen = sizeof(in->sin6_addr);
*data = g_new(char, *datalen);
memcpy(*data, &in->sin6_addr, *datalen);
} else {
g_assert_not_reached();
}
freeaddrinfo(res);
}

View File

@ -512,12 +512,19 @@ int main(int argc, char **argv)
false, true, "wiki.qemu.org", NULL);
TEST_SESS_REG(altname4, cacertreq.filename,
servercertalt1req.filename, clientcertreq.filename,
false, false, "192.168.122.1", NULL);
TEST_SESS_REG(altname5, cacertreq.filename,
servercertalt1req.filename, clientcertreq.filename,
false, false, "fec0::dead:beaf", NULL);
TEST_SESS_REG(altname6, cacertreq.filename,
servercertalt2req.filename, clientcertreq.filename,
false, true, "qemu.org", NULL);
TEST_SESS_REG(altname5, cacertreq.filename,
TEST_SESS_REG(altname7, cacertreq.filename,
servercertalt2req.filename, clientcertreq.filename,
false, false, "www.qemu.org", NULL);
TEST_SESS_REG(altname6, cacertreq.filename,
TEST_SESS_REG(altname8, cacertreq.filename,
servercertalt2req.filename, clientcertreq.filename,
false, false, "wiki.qemu.org", NULL);