Skip to content

Commit

Permalink
Add check of OpenPGP cert too.
Browse files Browse the repository at this point in the history
  • Loading branch information
jas4711 committed Aug 20, 2009
1 parent daca0c3 commit 177acbd
Showing 1 changed file with 96 additions and 41 deletions.
137 changes: 96 additions & 41 deletions tests/hostname-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <string.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/openpgp.h>

#include "utils.h"

Expand Down Expand Up @@ -550,7 +551,8 @@ char pem9[] =
"-----END CERTIFICATE-----\n";

/* Certificate with SAN and CN that match iff you truncate the SAN to
the embedded NUL. */
the embedded NUL.
See <http://thread.gmane.org/gmane.network.gnutls.general/1735>. */
char pem10[] =
"X.509 Certificate Information:\n"
" Version: 3\n"
Expand Down Expand Up @@ -633,30 +635,68 @@ char pem10[] =
"/yfcgJk0Zr3jMVTVtj/O1AijUihhXr0=\n"
"-----END CERTIFICATE-----\n";

/* Check basic OpenPGP comparison too.
<http://thread.gmane.org/gmane.comp.encryption.gpg.gnutls.devel/3812>. */
char pem11[] =
"-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
"Version: GnuPG v1.4.6 (GNU/Linux)\n"
"\n"
"mQGiBEXInlgRBAD0teb6ohIlchkHcFlmmvtVW1KXexlDfXExf8T+fOz5z354GPOX\n"
"sDq98ztCEE3hnPEOFj4NT0X3nEtrvLkhmZqrDHSbuJACB4qxeHwEbGFx7OIDW8+u\n"
"4sKxpaza1GVf1NQ7VIaQiXaGHy8Esn9SW7oNhK6z5l4TIRlm3OBt3cxU3wCgjnnO\n"
"jpGJeeo0OnZzSH+xsNLJQEcEAOmUc+7N9OhpT/gqddIgzYRr/FD0Ad6HBfABol6Q\n"
"wWCapzIxggnZJ9i+lHujpcA8idtrBU/DGhkGtW95QaHwQ8d5SvetM7Wc/xoHEP3o\n"
"HGvSGoXtfqlofastcC7eso39EBD10cpIB+gUmhe1MpaXm7A6m+KJO+2CkqE1vMkc\n"
"t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mo19jOzbnlRSGKRqrflOtO\n"
"YwhQXK9y/ohJBBgRAgAJBQJFyJ51AhsMAAoJEK5frT5dHRTYDDgAn2bLaS5n3Xy8\n"
"Z/V2Me1st/9pqPfZAJ4+9YBnyjCq/0vosIoZabi+s92m7g==\n"
"=NkXV\n"
"-----END PGP PUBLIC KEY BLOCK-----\n";

void
doit (void)
{
gnutls_x509_crt_t cert;
gnutls_x509_crt_t x509;
gnutls_openpgp_crt_t pgp;
gnutls_datum_t data;
int ret;

ret = gnutls_global_init ();
if (ret < 0)
fail ("gnutls_global_init: %d\n", ret);

ret = gnutls_x509_crt_init (&cert);
ret = gnutls_x509_crt_init (&x509);
if (ret < 0)
fail ("gnutls_x509_crt_init: %d\n", ret);

ret = gnutls_openpgp_crt_init (&pgp);
if (ret < 0)
fail ("gnutls_openpgp_crt_init: %d\n", ret);

success ("Testing pem1...\n");
data.data = pem1;
data.size = strlen (pem1);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo");
ret = gnutls_x509_crt_check_hostname (x509, "foo");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
Expand All @@ -666,23 +706,23 @@ doit (void)
data.data = pem2;
data.size = strlen (pem2);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo");
ret = gnutls_x509_crt_check_hostname (x509, "foo");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "*.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "*.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
Expand All @@ -692,23 +732,23 @@ doit (void)
data.data = pem3;
data.size = strlen (pem3);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo");
ret = gnutls_x509_crt_check_hostname (x509, "foo");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "*.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "*.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
Expand All @@ -718,29 +758,29 @@ doit (void)
data.data = pem4;
data.size = strlen (pem4);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo");
ret = gnutls_x509_crt_check_hostname (x509, "foo");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foo.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo.example.com");
ret = gnutls_x509_crt_check_hostname (x509, "foo.example.com");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
Expand All @@ -750,23 +790,23 @@ doit (void)
data.data = pem5;
data.size = strlen (pem5);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo");
ret = gnutls_x509_crt_check_hostname (x509, "foo");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "1.2.3.4");
ret = gnutls_x509_crt_check_hostname (x509, "1.2.3.4");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
Expand All @@ -776,17 +816,17 @@ doit (void)
data.data = pem6;
data.size = strlen (pem6);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foo.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "bar.foo.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "bar.foo.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
Expand All @@ -796,29 +836,29 @@ doit (void)
data.data = pem7;
data.size = strlen (pem7);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo.bar.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foo.bar.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foobar.bar.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foobar.bar.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foobar.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foobar.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foobazbar.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foobazbar.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
Expand All @@ -828,29 +868,29 @@ doit (void)
data.data = pem8;
data.size = strlen (pem8);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.com");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.com");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "www.example.foo.com");
ret = gnutls_x509_crt_check_hostname (x509, "www.example.foo.com");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
Expand All @@ -860,17 +900,17 @@ doit (void)
data.data = pem9;
data.size = strlen (pem9);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "foo.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "foo.example.org");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "bar.example.org");
ret = gnutls_x509_crt_check_hostname (x509, "bar.example.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
Expand All @@ -880,17 +920,32 @@ doit (void)
data.data = pem10;
data.size = strlen (pem10);

ret = gnutls_x509_crt_import (cert, &data, GNUTLS_X509_FMT_PEM);
ret = gnutls_x509_crt_import (x509, &data, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail ("gnutls_x509_crt_import: %d\n", ret);

ret = gnutls_x509_crt_check_hostname (cert, "localhost");
ret = gnutls_x509_crt_check_hostname (x509, "localhost");
if (ret)
fail ("Hostname incorrectly matches (%d)\n", ret);
else
success ("Hostname correctly does not match (%d)\n", ret);

gnutls_x509_crt_deinit (cert);
success ("Testing pem11...\n");
data.data = pem11;
data.size = strlen (pem11);

ret = gnutls_openpgp_crt_import (pgp, &data, GNUTLS_OPENPGP_FMT_BASE64);
if (ret < 0)
fail ("gnutls_openpgp_crt_import: %d\n", ret);

ret = gnutls_openpgp_crt_check_hostname (pgp, "test.gnutls.org");
if (ret)
success ("Hostname correctly matches (%d)\n", ret);
else
fail ("Hostname incorrectly does not match (%d)\n", ret);

gnutls_x509_crt_deinit (x509);
gnutls_openpgp_crt_deinit (pgp);

gnutls_global_deinit ();
}

0 comments on commit 177acbd

Please sign in to comment.