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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/* test *time_t code, for libreswan
*
* Copyright (C) 2019 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 PRINT(FILE, FMT, ...) \
fprintf(FILE, "%s[%zu]:"FMT"\n", __func__, ti,##__VA_ARGS__)
#define FAIL(FMT, ...) \
PRINT(stderr, FMT,##__VA_ARGS__); \
fails++; \
continue;
static void dn_check(void)
{
static /*const*/ struct test {
const char *out;
size_t len;
uint8_t in[20];
const char *nss;
} tests[] = {
/* reference point */
{
"CN=012345",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6,
'0', '1', '2', '3', '4', '5', },
NULL,
},
/* escape leading '#' and ' ' and trailing ' ' */
{
"CN=\\ # # #",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6,
' ', '#', ' ', '#', ' ', '#', },
.nss = "CN=\\ \\# \\# \\#", /* NSS needs '#' escaped */
},
{
"CN=\\# # #\\ ",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6,
'#', ' ', '#', ' ', '#', ' ', },
.nss = "CN=#1306232023202320", /* NSS totally screws up leading '#' */
},
{
"CN=. \\ ",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6,
'.', ' ', ' ', ' ', ' ', ' ', },
NULL,
},
{
/* don't escape trailing '#' */
"CN=.#####",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6,
'.', '#', '#', '#', '#', '#', },
.nss = "CN=.\\#\\#\\#\\#\\#", /* NSS needs '#' escaped */
},
/* escaping non printable ascii */
{
"CN=\\00y\\00\\00z\\00",
19, /* NULs */
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_UTF8STRING, 6,
0, 'y', 0, 0, 'z', 0, },
NULL,
},
{
/* 31->32 */
"CN=\\1D\\1E\\1F !\\\"",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/ 85, 4, 3,
ASN1_UTF8STRING, 6, 29, 30, 31, 32, 33, 34, },
NULL,
},
{
/* 126->128 */
"CN=|}~\\7F\\80\\81",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/ 85, 4, 3,
ASN1_UTF8STRING, 6, 124, 125, 126, 127, 128, 129, },
NULL,
},
{
"CN=\\+\\,\\;\\<\\>\\\\", 19, /* reserved */
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6, '+', ',', ';', '<', '>', '\\', },
NULL,
},
{
/* not reserved */
"CN==#':$#",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/85, 4, 3,
ASN1_PRINTABLESTRING, 6, '=', '#', '\'', ':', '$', '#', },
.nss = "CN=\\=\\#':$\\#", /* NSS needs '=' escaped */
},
/* unknown OIDs - dump #BER */
{ /* 1-byte */
"2.39=#1306303132333435",
17,
{ ASN1_SEQUENCE, 15,
ASN1_SET, 13,
ASN1_SEQUENCE, 11,
ASN1_OID, 1, /*OID*/ 40*2+39, /* see encoding schema */
ASN1_PRINTABLESTRING, 6, '0', '1', '2', '3', '4', '5', },
NULL,
},
{ /* 3-bytes */
"2.39.1.2=#1306303132333435",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/ 40*2+39, 1, 2,
ASN1_PRINTABLESTRING, 6, '0', '1', '2', '3', '4', '5', },
NULL,
},
{ /*1+2-bytes */
"2.39.16383=#1306303132333435",
19,
{ ASN1_SEQUENCE, 17,
ASN1_SET, 15,
ASN1_SEQUENCE, 13,
ASN1_OID, 3, /*OID*/ 40*2+39, 0x80|(16383>>7), 16383&0x7f,
ASN1_PRINTABLESTRING, 6, '0', '1', '2', '3', '4', '5', },
NULL,
},
};
for (size_t ti = 0; ti < elemsof(tests); ti++) {
/*const*/ struct test *t = &tests[ti];
PRINT(stdout, " -> rfc4514: '%s'%s%s%s", t->out,
t->nss != NULL ? " NSS: '" : "",
t->nss != NULL ? t->nss : "",
t->nss != NULL ? "'" : "");
chunk_t dn = chunk2(t->in, t->len); /* shunk2() */
/* convert it to a string */
#define CHECK_JAM_DN(OUT, NSS_COMPATIBLE) \
{ \
dn_buf dnbuf = { "", }; \
struct jambuf dnjam = ARRAY_AS_JAMBUF(dnbuf.buf); \
jam_raw_dn(&dnjam, ASN1(dn), jam_raw_bytes, NSS_COMPATIBLE); \
if (!streq(dnbuf.buf, OUT)) { \
FAIL(" jam_raw_dn(NSS_COMPATIBLE=%s) returned '%s', expecting '%s'", \
bool_str(NSS_COMPATIBLE), \
dnbuf.buf, OUT); \
} \
}
CHECK_JAM_DN(t->out, false);
if (t->nss != NULL) {
CHECK_JAM_DN(t->nss, true);
}
/* Can NSS can parse its variant? */
{
const char *nss_dn = t->nss != NULL ? t->nss : t->out;
CERTName *nss_name = CERT_AsciiToName(nss_dn);
if (nss_name == NULL) {
/* PORT_Error()? */
FAIL(" CERT_AsciiToName() unexpectedly failed to parse '%s'",
nss_dn);
}
CERT_DestroyName(nss_name);
}
/* see if libreswan can parse it */
#define CHECK_ATODN(IN) \
{ \
chunk_t adn; \
err_t err = atodn(IN, &adn); /* static data */ \
if (err != NULL) { \
FAIL(" atodn('%s') unexpectedly failed: %s", \
IN, err); \
} else { \
dn_buf adnbuf = { "", }; \
struct jambuf adnjam = ARRAY_AS_JAMBUF(adnbuf.buf); \
jam_raw_dn(&adnjam, ASN1(adn), jam_raw_bytes, false); \
if (!streq(adnbuf.buf, t->out)) { \
FAIL(" jam_dn(atodn('%s')) returned '%s', expecting '%s'", \
IN, adnbuf.buf, t->out); \
} \
free_chunk_content(&adn); \
} \
}
CHECK_ATODN(t->out);
if (t->nss != NULL) {
CHECK_ATODN(t->nss);
}
}
}
int main(int argc UNUSED, char *argv[])
{
leak_detective = true;
struct logger *logger = tool_logger(argc, argv);
dn_check();
if (report_leaks(logger)) {
fails++;
}
if (fails > 0) {
fprintf(stderr, "TOTAL FAILURES: %d\n", fails);
return 1;
}
return 0;
}
|