/* * Copyright (C) 2016 Red Hat, Inc. * * Author: Nikos Mavrogiannopoulos * * This file is part of GnuTLS. * * GnuTLS is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GnuTLS is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GnuTLS; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ /* This checks the low level DN encoding and decoding routines */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "utils.h" static void decode(const char *test_name, const gnutls_datum_t *raw, const char *expected, const char *expected_compat) { int ret; gnutls_datum_t out; gnutls_x509_dn_t dn; ret = gnutls_x509_dn_init(&dn); if (ret < 0) { test_fail("%s\n", gnutls_strerror(ret)); } ret = gnutls_x509_dn_import(dn, raw); if (ret < 0) { test_fail("%s\n", gnutls_strerror(ret)); } ret = gnutls_x509_dn_get_str2(dn, &out, 0); if (ret < 0) { test_fail("%s\n", gnutls_strerror(ret)); } if (out.size != strlen(expected)) { test_fail("The length of the output (%d) doesn't match the expected (%d)\n", (int)out.size, (int)strlen(expected)); } if (memcmp(out.data, expected, out.size) != 0) { test_fail("The string output (%s) doesn't match the expected (%s)\n", (char*)out.data, expected); } if (out.data[out.size] != 0) { test_fail("The string output isn't null terminated\n"); } gnutls_free(out.data); /* compat mode */ ret = gnutls_x509_dn_get_str(dn, &out); if (ret < 0) { test_fail("%s\n", gnutls_strerror(ret)); } if (out.size != strlen(expected_compat)) { test_fail("The length of the output (%d) doesn't match the expected (%d)\n", (int)out.size, (int)strlen(expected_compat)); } if (memcmp(out.data, expected_compat, out.size) != 0) { test_fail("The string output (%s) doesn't match the expected (%s)\n", (char*)out.data, expected_compat); } if (out.data[out.size] != 0) { test_fail("The string output isn't null terminated\n"); } gnutls_free(out.data); gnutls_x509_dn_deinit(dn); return; } static void encode(const char *test_name, const gnutls_datum_t *raw, const char *str, int exp_error) { int ret; gnutls_datum_t out; gnutls_x509_dn_t dn; const char *err; ret = gnutls_x509_dn_init(&dn); if (ret < 0) { test_fail("%s\n", gnutls_strerror(ret)); } ret = gnutls_x509_dn_set_str(dn, str, &err); if (ret < 0) { if (ret == exp_error) goto cleanup; if (ret == GNUTLS_E_PARSING_ERROR) test_fail("error: %s: %s\n", gnutls_strerror(ret), err); else test_fail("%s\n", gnutls_strerror(ret)); } if (ret != exp_error) { test_fail("unexpected success in encoding (got: %d, exp: %d)\n", ret, exp_error); } ret = gnutls_x509_dn_export2(dn, GNUTLS_X509_FMT_DER, &out); if (ret < 0) { test_fail("%s\n", gnutls_strerror(ret)); } if (out.size != raw->size) { { unsigned i; fprintf(stderr, "got:\n"); for (i=0;isize); } if (memcmp(out.data, raw->data, out.size) != 0) { { unsigned i; fprintf(stderr, "got:\n"); for (i=0;i