Path: blob/main/crypto/krb5/src/tests/gssapi/t_oid.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/gssapi/t_oid.c - Test OID manipulation functions */2/*3* Copyright (C) 2012 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#include <stdio.h>33#include <stdlib.h>34#include <string.h>3536#include "common.h"3738static struct {39char *canonical;40char *variant;41gss_OID_desc oid;42} tests[] = {43/* GSS_C_NT_USER_NAME */44{ "{ 1 2 840 113554 1 2 1 1 }", "1.2.840.113554.1.2.1.1",45{ 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x01\x01" } },46/* GSS_C_NT_MACHINE_UID_NAME */47{ "{ 1 2 840 113554 1 2 1 2 }", "1 2 840 113554 1 2 1 2",48{ 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x01\x02" } },49/* GSS_C_NT_STRING_UID_NAME */50{ "{ 1 2 840 113554 1 2 1 3 }", "{1 2 840 113554 1 2 1 3}",51{ 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x01\x03" } },52/* GSS_C_NT_HOSTBASED_SERVICE_X */53{ "{ 1 3 6 1 5 6 2 }", "{ 1 3 6 1 5 6 2 }",54{ 6, "\x2B\x06\x01\x05\x06\x02" } },55/* GSS_C_NT_ANONYMOUS */56{ "{ 1 3 6 1 5 6 3 }", "{ 01 03 06 01 05 06 03 }",57{ 6, "\x2B\x06\x01\x05\x06\x03" } },58/* GSS_KRB5_NT_PRINCIPAL_NAME */59{ "{ 1 2 840 113554 1 2 2 1 }", " {01 2 840 113554 1 2 2 1 } ",60{ 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02\x01" } },61/* GSS_KRB5_NT_ENTERPRISE_NAME */62{ "{ 1 2 840 113554 1 2 2 6 }", " {1.2.840.113554.1.2.2.6} ",63{ 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02\x06" } },64/* gss_krb5_nt_principal */65{ "{ 1 2 840 113554 1 2 2 2 }", "{1.2.840.113554.1.2.2.2}",66{ 10, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02\x02" } },67/* gss_mech_krb5 */68{ "{ 1 2 840 113554 1 2 2 }", "{ 1.2.840.113554.1.2.2 }",69{ 9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02" } },70/* gss_mech_krb5_old */71{ "{ 1 3 5 1 5 2 }", "001 . 003 . 005 . 001 . 005 . 002",72{ 5, "\x2B\x05\x01\x05\x02" } },73/* gss_mech_krb5_wrong */74{ "{ 1 2 840 48018 1 2 2 }", "1.2.840.48018.1.2.2 trailing garbage",75{ 9, "\x2A\x86\x48\x82\xF7\x12\x01\x02\x02" } },76/* gss_mech_iakerb */77{ "{ 1 3 6 1 5 2 5 }", "{ 1 3 6 1 5 2 5 } trailing garbage",78{ 6, "\x2B\x06\x01\x05\x02\x05" } },79/* SPNEGO */80{ "{ 1 3 6 1 5 5 2 }", "{1 3 6 1 5 5 2} trailing garbage",81{ 6, "\x2B\x06\x01\x05\x05\x02" } },82/* Edge cases for the first two arcs */83{ "{ 0 0 }", NULL, { 1, "\x00" } },84{ "{ 0 39 }", NULL, { 1, "\x27" } },85{ "{ 1 0 }", NULL, { 1, "\x28" } },86{ "{ 1 39 }", NULL, { 1, "\x4F" } },87{ "{ 2 0 }", NULL, { 1, "\x50" } },88{ "{ 2 40 }", NULL, { 1, "\x78" } },89{ "{ 2 47 }", NULL, { 1, "\x7F" } },90{ "{ 2 48 }", NULL, { 2, "\x81\x00" } },91{ "{ 2 16304 }", NULL, { 3, "\x81\x80\x00" } },92/* Zero-valued arcs */93{ "{ 0 0 0 }", NULL, { 2, "\x00\x00" } },94{ "{ 0 0 1 0 }", NULL, { 3, "\x00\x01\x00" } },95{ "{ 0 0 128 0 }", NULL, { 4, "\x00\x81\x00\x00 " } },96{ "{ 0 0 0 1 }", NULL, { 3, "\x00\x00\x01" } },97{ "{ 0 0 128 0 1 0 128 }", NULL,98{ 8, "\x00\x81\x00\x00\x01\x00\x81\x00 " } }99};100101static char *invalid_strings[] = {102"",103"{}",104"{",105"}",106" ",107" { } ",108"x",109"+1 1",110"-1.1",111"1.+0",112"+0.1",113"{ 1 garbage }",114"{ 1 }",115"{ 0 40 }",116"{ 1 40 }",117"{ 1 128 }",118"{ 1 1",119"{ 1 2 3 4 +5 }",120"{ 1.2.-3.4.5 }"121};122123static int124oid_equal(gss_OID o1, gss_OID o2)125{126return o1->length == o2->length &&127memcmp(o1->elements, o2->elements, o1->length) == 0;128}129130int131main(void)132{133size_t i;134OM_uint32 major, minor;135gss_buffer_desc buf;136gss_OID oid;137gss_OID_set set;138int status = 0, present;139140for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {141/* Check that this test's OID converts to its canonical string form. */142major = gss_oid_to_str(&minor, &tests[i].oid, &buf);143check_gsserr("gss_oid_to_str", major, minor);144if (buf.length != strlen(tests[i].canonical) + 1 ||145memcmp(buf.value, tests[i].canonical, buf.length) != 0) {146status = 1;147printf("test %d: OID converts to %.*s, wanted %s\n", (int)i,148(int)buf.length, (char *)buf.value, tests[i].canonical);149}150(void)gss_release_buffer(&minor, &buf);151152/* Check that this test's canonical string form converts to its OID. */153buf.value = tests[i].canonical;154buf.length = strlen(tests[i].canonical);155major = gss_str_to_oid(&minor, &buf, &oid);156check_gsserr("gss_str_to_oid", major, minor);157if (!oid_equal(oid, &tests[i].oid)) {158status = 1;159printf("test %d: %s converts to wrong OID\n", (int)i,160tests[i].canonical);161display_oid("wanted", &tests[i].oid);162display_oid("actual", oid);163}164(void)gss_release_oid(&minor, &oid);165166/* Check that this test's variant string form converts to its OID. */167if (tests[i].variant == NULL)168continue;169buf.value = tests[i].variant;170buf.length = strlen(tests[i].variant);171major = gss_str_to_oid(&minor, &buf, &oid);172check_gsserr("gss_str_to_oid", major, minor);173if (!oid_equal(oid, &tests[i].oid)) {174status = 1;175printf("test %d: %s converts to wrong OID\n", (int)i,176tests[i].variant);177display_oid("wanted", &tests[i].oid);178display_oid("actual", oid);179}180(void)gss_release_oid(&minor, &oid);181}182183for (i = 0; i < sizeof(invalid_strings) / sizeof(*invalid_strings); i++) {184buf.value = invalid_strings[i];185buf.length = strlen(invalid_strings[i]);186major = gss_str_to_oid(&minor, &buf, &oid);187if (major == GSS_S_COMPLETE) {188status = 1;189printf("invalid %d: %s converted when it should not have\n",190(int)i, invalid_strings[i]);191(void)gss_release_oid(&minor, &oid);192}193}194195major = gss_create_empty_oid_set(&minor, &set);196check_gsserr("gss_create_empty_oid_set", major, minor);197for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {198major = gss_add_oid_set_member(&minor, &tests[i].oid, &set);199check_gsserr("gss_add_oid_set_member", major, minor);200}201if (set->count != i) {202status = 1;203printf("oid set has wrong size: wanted %d, actual %d\n", (int)i,204(int)set->count);205}206for (i = 0; i < set->count; i++) {207if (!oid_equal(&set->elements[i], &tests[i].oid)) {208status = 1;209printf("oid set has wrong element %d\n", (int)i);210display_oid("wanted", &tests[i].oid);211display_oid("actual", &set->elements[i]);212}213major = gss_test_oid_set_member(&minor, &tests[i].oid, set, &present);214check_gsserr("gss_test_oid_set_member", major, minor);215if (!present) {216status = 1;217printf("oid set does not contain OID %d\n", (int)i);218display_oid("wanted", &tests[i].oid);219}220}221(void)gss_release_oid_set(&minor, &set);222return status;223}224225226