diff options
author | Jouni Malinen <j@w1.fi> | 2009-12-06 16:45:36 +0200 |
---|---|---|
committer | Jouni Malinen <j@w1.fi> | 2009-12-06 16:45:36 +0200 |
commit | ab7ddc74ad39ab11de2b3082fc78a3549781aa24 (patch) | |
tree | 5dbb629b43a63d9cc5f3353fa50eabed5a53448d /tests | |
parent | 0e574b07f83c0595a71267e273de28c64187b1b6 (diff) | |
download | external_wpa_supplicant_8_ti-ab7ddc74ad39ab11de2b3082fc78a3549781aa24.zip external_wpa_supplicant_8_ti-ab7ddc74ad39ab11de2b3082fc78a3549781aa24.tar.gz external_wpa_supplicant_8_ti-ab7ddc74ad39ab11de2b3082fc78a3549781aa24.tar.bz2 |
Move asn1_test.c into tests subdirectory and split it in two
The new test-asn1 and test-x509 tools are built using libraries
from src/{utils,crypto,tls}. Currently, cross dependencies between
crypto and tls are still preventing the test-x509 from being linked
properly.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/.gitignore | 2 | ||||
-rw-r--r-- | tests/Makefile | 14 | ||||
-rw-r--r-- | tests/test-asn1.c | 203 | ||||
-rw-r--r-- | tests/test-x509.c | 44 |
4 files changed, 261 insertions, 2 deletions
diff --git a/tests/.gitignore b/tests/.gitignore index 0710c37..6d6d575 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,4 +1,5 @@ test-aes +test-asn1 test-base64 test-md4 test-md5 @@ -6,3 +7,4 @@ test-milenage test-ms_funcs test-sha1 test-sha256 +test-x509 diff --git a/tests/Makefile b/tests/Makefile index 7c0a6ff..b40ff1c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,5 @@ TESTS=test-base64 test-md4 test-md5 test-milenage test-ms_funcs test-sha1 \ - test-sha256 test-aes + test-sha256 test-aes test-asn1 test-x509 all: $(TESTS) @@ -19,7 +19,8 @@ CFLAGS += -I../src CFLAGS += -I../src/utils LIBS = ../src/utils/libutils.a \ - ../src/crypto/libcrypto.a + ../src/crypto/libcrypto.a \ + ../src/tls/libtls.a ../src/utils/libutils.a: $(MAKE) -C ../src/utils @@ -27,10 +28,16 @@ LIBS = ../src/utils/libutils.a \ ../src/crypto/libcrypto.a: $(MAKE) -C ../src/crypto +../src/tls/libtls.a: + $(MAKE) -C ../src/tls + test-aes: test-aes.o $(LIBS) $(LDO) $(LDFLAGS) -o $@ $^ +test-asn1: test-asn1.o $(LIBS) + $(LDO) $(LDFLAGS) -o $@ $^ + test-base64: test-base64.o $(LIBS) $(LDO) $(LDFLAGS) -o $@ $^ @@ -52,6 +59,9 @@ test-sha1: test-sha1.o $(LIBS) test-sha256: test-sha256.o $(LIBS) $(LDO) $(LDFLAGS) -o $@ $^ +test-x509: test-x509.o $(LIBS) + $(LDO) $(LDFLAGS) -o $@ $^ + run-tests: $(TESTS) ./test-aes diff --git a/tests/test-asn1.c b/tests/test-asn1.c new file mode 100644 index 0000000..e59aa49 --- /dev/null +++ b/tests/test-asn1.c @@ -0,0 +1,203 @@ +/* + * Testing tool for ASN.1 routines + * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Alternatively, this software may be distributed under the terms of BSD + * license. + * + * See README and COPYING for more details. + */ + +#include "includes.h" + +#include "common.h" +#include "tls/asn1.h" + +extern int wpa_debug_level; + + +static const char * asn1_class_str(int class) +{ + switch (class) { + case ASN1_CLASS_UNIVERSAL: + return "Universal"; + case ASN1_CLASS_APPLICATION: + return "Application"; + case ASN1_CLASS_CONTEXT_SPECIFIC: + return "Context-specific"; + case ASN1_CLASS_PRIVATE: + return "Private"; + default: + return "?"; + } +} + + +int asn1_parse(const u8 *buf, size_t len, int level) +{ + const u8 *pos, *prev, *end; + char prefix[10], str[100]; + int _level; + struct asn1_hdr hdr; + struct asn1_oid oid; + u8 tmp; + + _level = level; + if ((size_t) _level > sizeof(prefix) - 1) + _level = sizeof(prefix) - 1; + memset(prefix, ' ', _level); + prefix[_level] = '\0'; + + pos = buf; + end = buf + len; + + while (pos < end) { + if (asn1_get_next(pos, end - pos, &hdr) < 0) + return -1; + + prev = pos; + pos = hdr.payload; + + wpa_printf(MSG_MSGDUMP, "ASN.1:%s Class %d(%s) P/C %d(%s) " + "Tag %u Length %u", + prefix, hdr.class, asn1_class_str(hdr.class), + hdr.constructed, + hdr.constructed ? "Constructed" : "Primitive", + hdr.tag, hdr.length); + + if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC && + hdr.constructed) { + if (asn1_parse(pos, hdr.length, level + 1) < 0) + return -1; + pos += hdr.length; + } + + if (hdr.class != ASN1_CLASS_UNIVERSAL) + continue; + + switch (hdr.tag) { + case ASN1_TAG_EOC: + if (hdr.length) { + wpa_printf(MSG_DEBUG, "ASN.1: Non-zero " + "end-of-contents length (%u)", + hdr.length); + return -1; + } + wpa_printf(MSG_MSGDUMP, "ASN.1:%s EOC", prefix); + break; + case ASN1_TAG_BOOLEAN: + if (hdr.length != 1) { + wpa_printf(MSG_DEBUG, "ASN.1: Unexpected " + "Boolean length (%u)", hdr.length); + return -1; + } + tmp = *pos++; + wpa_printf(MSG_MSGDUMP, "ASN.1:%s Boolean %s", + prefix, tmp ? "TRUE" : "FALSE"); + break; + case ASN1_TAG_INTEGER: + wpa_hexdump(MSG_MSGDUMP, "ASN.1: INTEGER", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_BITSTRING: + wpa_hexdump(MSG_MSGDUMP, "ASN.1: BitString", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_OCTETSTRING: + wpa_hexdump(MSG_MSGDUMP, "ASN.1: OctetString", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_NULL: + if (hdr.length) { + wpa_printf(MSG_DEBUG, "ASN.1: Non-zero Null " + "length (%u)", hdr.length); + return -1; + } + wpa_printf(MSG_MSGDUMP, "ASN.1:%s Null", prefix); + break; + case ASN1_TAG_OID: + if (asn1_get_oid(prev, end - prev, &oid, &prev) < 0) { + wpa_printf(MSG_DEBUG, "ASN.1: Invalid OID"); + return -1; + } + asn1_oid_to_str(&oid, str, sizeof(str)); + wpa_printf(MSG_DEBUG, "ASN.1:%s OID %s", prefix, str); + pos += hdr.length; + break; + case ANS1_TAG_RELATIVE_OID: + wpa_hexdump(MSG_MSGDUMP, "ASN.1: Relative OID", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_SEQUENCE: + wpa_printf(MSG_MSGDUMP, "ASN.1:%s SEQUENCE", prefix); + if (asn1_parse(pos, hdr.length, level + 1) < 0) + return -1; + pos += hdr.length; + break; + case ASN1_TAG_SET: + wpa_printf(MSG_MSGDUMP, "ASN.1:%s SET", prefix); + if (asn1_parse(pos, hdr.length, level + 1) < 0) + return -1; + pos += hdr.length; + break; + case ASN1_TAG_PRINTABLESTRING: + wpa_hexdump_ascii(MSG_MSGDUMP, + "ASN.1: PrintableString", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_IA5STRING: + wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: IA5String", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_UTCTIME: + wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: UTCTIME", + pos, hdr.length); + pos += hdr.length; + break; + case ASN1_TAG_VISIBLESTRING: + wpa_hexdump_ascii(MSG_MSGDUMP, "ASN.1: VisibleString", + pos, hdr.length); + pos += hdr.length; + break; + default: + wpa_printf(MSG_DEBUG, "ASN.1: Unknown tag %d", + hdr.tag); + return -1; + } + } + + return 0; +} + + +int main(int argc, char *argv[]) +{ + FILE *f; + u8 buf[3000]; + size_t len; + + wpa_debug_level = 0; + + f = fopen(argv[1], "rb"); + if (f == NULL) + return -1; + len = fread(buf, 1, sizeof(buf), f); + fclose(f); + + if (asn1_parse(buf, len, 0) < 0) + printf("Failed to parse DER ASN.1\n"); + + printf("\n\n"); + + return 0; +} diff --git a/tests/test-x509.c b/tests/test-x509.c new file mode 100644 index 0000000..96181c2 --- /dev/null +++ b/tests/test-x509.c @@ -0,0 +1,44 @@ +/* + * Testing tool for X.509v3 routines + * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * Alternatively, this software may be distributed under the terms of BSD + * license. + * + * See README and COPYING for more details. + */ + +#include "includes.h" + +#include "common.h" +#include "tls/x509v3.h" + +extern int wpa_debug_level; + + +int main(int argc, char *argv[]) +{ + FILE *f; + u8 buf[3000]; + size_t len; + struct x509_certificate *cert; + + wpa_debug_level = 0; + + f = fopen(argv[1], "rb"); + if (f == NULL) + return -1; + len = fread(buf, 1, sizeof(buf), f); + fclose(f); + + cert = x509_certificate_parse(buf, len); + if (cert == NULL) + printf("Failed to parse X.509 certificate\n"); + x509_certificate_free(cert); + + return 0; +} |