1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/* Test ASN.1 code, for libreswan
*
* Copyright (C) 2022 Andrew Cagney
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
*
* This library 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 Library General Public
* License for more details.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <cert.h>
#include "lswtool.h" /* for tool_init_log() */
#include "x509.h"
#include "asn1.h"
#include "lswalloc.h" /* for leak_detective; */
int fails = 0;
#define FAIL(FMT, ...) \
{ \
PRINT(stderr, " FAIL: "FMT,##__VA_ARGS__); \
fails++; \
continue; \
}
static void is_asn1_printablestring_check(void)
{
static /*const*/ struct test {
bool ok;
const char *str;
} tests[] = {
{ true, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '()+,-./:=?", },
{ true, "", },
/* 000-037 */
{ false, "\001", },
{ false, "\037", },
/* 040-047 */
{ true, " ", },
{ false, "!", },
{ false, "\"", },
{ false, "#", },
{ false, "$", },
{ false, "%", },
{ false, "&", },
/* 050-057 */
{ true, "(", },
{ true, ")", },
{ false, "*", },
{ true, "+", },
{ true, ",", },
{ true, "-", },
{ true, ".", },
{ true, "/", },
/* 060-067 */
{ true, "01234567", },
/* 070-077 */
{ true, "8", },
{ true, "9", },
{ true, ":", },
{ false, ";", },
{ false, "<", },
{ true, "=", },
{ false, ">", },
{ true, "?", },
/* 100-137 */
{ false, "@", },
{ true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", },
{ false, "[", },
{ false, "\\", },
{ false, "]", },
{ false, "^", },
{ false, "_", },
/* 140-177 */
{ false, "`", },
{ true, "abcdefghijklmnopqrstuvwxyz", },
{ false, "{", },
{ false, "|", },
{ false, "}", },
{ false, "~", },
{ false, "\177", },
{ false, "\200", },
{ false, "\377", },
};
for (size_t ti = 0; ti < elemsof(tests); ti++) {
/*const*/ struct test *t = &tests[ti];
#define PRINT(FILE, FMT, ...) \
{ \
fprintf(FILE, "%s[%zu]:", __func__, ti); \
if (char_isprint(t->str[0])) { \
fprintf(FILE, " \"%s\"", t->str); \
} else { \
fprintf(FILE, "%4o", (unsigned char)t->str[0]); \
} \
fprintf(FILE, "->%s", bool_str(t->ok)); \
fprintf(FILE, FMT"\n", ##__VA_ARGS__); \
}
PRINT(stdout, "");
shunk_t str = shunk1(t->str);
bool ok = is_asn1_printablestring(str);
if (ok != t->ok) {
FAIL("returned %s", bool_str(ok));
}
#undef PRINT
}
}
static void unwrap_asn1_length_check(void)
{
static /*const*/ struct test {
bool ok;
size_t length;
struct {
size_t len;
uint8_t ptr[512];
} hunk;
size_t left;
} tests[] = {
/* 1+0 byte length is 0 */
{ false, .hunk = { 0, { 0, }, }, .length = 0, .left = 0, },
{ true, .hunk = { 1, { 0, }, }, .length = 0, .left = 0, },
{ true, .hunk = { 2, { 0, }, }, .length = 0, .left = 1, },
/* 1+0 byte length is 1 */
{ false, .hunk = { 0, { 1, }, }, .length = 0, .left = 0, },
{ false, .hunk = { 1, { 1, }, }, .length = 0, .left = 0, },
{ true, .hunk = { 2, { 1, }, }, .length = 1, .left = 1, },
/* 1+0 byte length is 127 */
{ false, .hunk = { 127, { 127, }, }, .length = 0, .left = 126, },
{ true, .hunk = { 128, { 127, }, }, .length = 127, .left = 127, },
{ true, .hunk = { 129, { 127, }, }, .length = 127, .left = 128, },
/* 1+1 byte length is 1 */
{ false, .hunk = { 1, { 0x81, 1, }, }, .length = 0, .left = 0, },
{ false, .hunk = { 2, { 0x81, 1, }, }, .length = 0, .left = 0, },
{ true, .hunk = { 3, { 0x81, 1, }, }, .length = 1, .left = 1, },
{ true, .hunk = { 4, { 0x81, 1, }, }, .length = 1, .left = 2, },
/* 1+1 byte length is 255 */
{ false, .hunk = { 256, { 0x81, 255, }, }, .length = 0, .left = 254, },
{ true, .hunk = { 257, { 0x81, 255, }, }, .length = 255, .left = 255, },
{ true, .hunk = { 258, { 0x81, 255, }, }, .length = 255, .left = 256, },
/* 1+2 byte length is 258 */
{ false, .hunk = { 1, { 0x82, 1, 2, }, }, .length = 0, .left = 0, },
{ false, .hunk = { 2, { 0x82, 1, 2, }, }, .length = 0, .left = 1, },
{ false, .hunk = { 3, { 0x82, 1, 2, }, }, .length = 0, .left = 0, },
{ false, .hunk = { 260, { 0x82, 1, 2, }, }, .length = 0, .left = 257, },
{ true, .hunk = { 261, { 0x82, 1, 2, }, }, .length = 258, .left = 258, },
{ true, .hunk = { 262, { 0x82, 1, 2, }, }, .length = 258, .left = 259, },
};
for (size_t ti = 0; ti < elemsof(tests); ti++) {
/*const*/ struct test *t = &tests[ti];
#define PRINT(FILE, FMT, ...) \
{ \
fprintf(FILE, "%s[%zu]:", __func__, ti); \
fprintf(FILE, " ok=%s length=%zd left=%zd hunk(%zu):", \
bool_str(t->ok), t->length, t->left, t->hunk.len); \
for (unsigned u = 0; u < t->hunk.len; u++) { \
fprintf(FILE, " %02x", t->hunk.ptr[u]); \
} \
fprintf(FILE, FMT"\n", ##__VA_ARGS__); \
}
PRINT(stdout, "");
if (t->hunk.len > sizeof(t->hunk.ptr)) {
FAIL("buffer overflow");
}
asn1_t bytes = { .ptr = t->hunk.ptr, .len = t->hunk.len, };
size_t length;
err_t e = unwrap_asn1_length(&bytes, &length);
if (e != NULL) {
if (t->ok) {
FAIL("unexpected error: %s", e);
} else {
/* expected */
continue;
}
} else if (!t->ok) {
FAIL("unexpectedly succeeded");
}
if (t->left != bytes.len) {
FAIL("returned left %zu, expecting %zu", bytes.len, t->left);
}
if (t->length != length) {
FAIL("returned length %zu, expecting %zu", length, t->length);
}
}
}
int main(int argc UNUSED, char *argv[])
{
leak_detective = true;
struct logger *logger = tool_logger(argc, argv);
is_asn1_printablestring_check();
unwrap_asn1_length_check();
if (report_leaks(logger)) {
fails++;
}
if (fails > 0) {
fprintf(stderr, "TOTAL FAILURES: %d\n", fails);
return 1;
}
return 0;
}
|