| 1 |
/* t-crl-parser.c - basic test for the CRl parser. |
| 2 |
* Copyright (C) 2002, 2004, 2005 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* KSBA is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#undef ENABLE_HASH_LOGGING |
| 21 |
|
| 22 |
#ifdef ENABLE_HASH_LOGGING |
| 23 |
#define _GNU_SOURCE 1 |
| 24 |
#endif |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <time.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
#include "../src/ksba.h" |
| 33 |
|
| 34 |
#include "t-common.h" |
| 35 |
#include "oidtranstbl.h" |
| 36 |
|
| 37 |
|
| 38 |
static int quiet; |
| 39 |
static int verbose; |
| 40 |
|
| 41 |
|
| 42 |
static void |
| 43 |
my_hasher (void *arg, const void *buffer, size_t length) |
| 44 |
{ |
| 45 |
FILE *fp = arg; |
| 46 |
|
| 47 |
if (fp) |
| 48 |
{ |
| 49 |
if ( fwrite (buffer, length, 1, fp) != 1 ) |
| 50 |
fail ("error writing to-be-hashed data"); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/* Return the description for OID; if no description is available |
| 56 |
NULL is returned. */ |
| 57 |
static const char * |
| 58 |
get_oid_desc (const char *oid) |
| 59 |
{ |
| 60 |
int i; |
| 61 |
|
| 62 |
if (oid) |
| 63 |
for (i=0; oidtranstbl[i].oid; i++) |
| 64 |
if (!strcmp (oidtranstbl[i].oid, oid)) |
| 65 |
return oidtranstbl[i].desc; |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
static void |
| 71 |
print_names (int indent, ksba_name_t name) |
| 72 |
{ |
| 73 |
int idx; |
| 74 |
const char *s; |
| 75 |
int indent_all; |
| 76 |
|
| 77 |
if ((indent_all = (indent < 0))) |
| 78 |
indent = - indent; |
| 79 |
|
| 80 |
if (!name) |
| 81 |
{ |
| 82 |
if (!quiet) |
| 83 |
fputs ("none\n", stdout); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
for (idx=0; (s = ksba_name_enum (name, idx)); idx++) |
| 88 |
{ |
| 89 |
char *p = ksba_name_get_uri (name, idx); |
| 90 |
if (!quiet) |
| 91 |
printf ("%*s%s\n", idx||indent_all?indent:0, "", p?p:s); |
| 92 |
xfree (p); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static void |
| 99 |
one_file (const char *fname) |
| 100 |
{ |
| 101 |
gpg_error_t err; |
| 102 |
FILE *fp; |
| 103 |
ksba_reader_t r; |
| 104 |
ksba_crl_t crl; |
| 105 |
ksba_stop_reason_t stopreason; |
| 106 |
int count = 0; |
| 107 |
FILE *hashlog = NULL; |
| 108 |
|
| 109 |
#ifdef ENABLE_HASH_LOGGING |
| 110 |
if (1) |
| 111 |
{ |
| 112 |
char *buf; |
| 113 |
|
| 114 |
if (asprintf (&buf, "%s.hash.log", fname) < 0) |
| 115 |
fail ("asprintf failed"); |
| 116 |
hashlog = fopen (buf, "wb"); |
| 117 |
if (!hashlog) |
| 118 |
fail ("can't create log file"); |
| 119 |
free (buf); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
if (!quiet) |
| 124 |
printf ("*** checking `%s' ***\n", fname); |
| 125 |
fp = fopen (fname, "rb"); |
| 126 |
if (!fp) |
| 127 |
{ |
| 128 |
fprintf (stderr, "%s:%d: can't open `%s': %s\n", |
| 129 |
__FILE__, __LINE__, fname, strerror (errno)); |
| 130 |
exit (1); |
| 131 |
} |
| 132 |
|
| 133 |
err = ksba_reader_new (&r); |
| 134 |
if (err) |
| 135 |
fail_if_err (err); |
| 136 |
err = ksba_reader_set_file (r, fp); |
| 137 |
fail_if_err (err); |
| 138 |
|
| 139 |
err = ksba_crl_new (&crl); |
| 140 |
if (err) |
| 141 |
fail_if_err (err); |
| 142 |
|
| 143 |
err = ksba_crl_set_reader (crl, r); |
| 144 |
fail_if_err (err); |
| 145 |
|
| 146 |
if (hashlog) |
| 147 |
ksba_crl_set_hash_function (crl, my_hasher, hashlog); |
| 148 |
|
| 149 |
do |
| 150 |
{ |
| 151 |
err = ksba_crl_parse (crl, &stopreason); |
| 152 |
fail_if_err2 (fname, err); |
| 153 |
switch (stopreason) |
| 154 |
{ |
| 155 |
case KSBA_SR_BEGIN_ITEMS: |
| 156 |
{ |
| 157 |
const char *algoid; |
| 158 |
char *issuer; |
| 159 |
ksba_isotime_t this, next; |
| 160 |
|
| 161 |
algoid = ksba_crl_get_digest_algo (crl); |
| 162 |
if (!quiet) |
| 163 |
printf ("digest algo: %s\n", algoid? algoid : "[none]"); |
| 164 |
if (algoid && !strcmp (algoid, "1.2.840.113549.1.1.10")) |
| 165 |
{ |
| 166 |
ksba_sexp_t pssparam; |
| 167 |
|
| 168 |
if (!quiet) |
| 169 |
printf (" pss para: "); |
| 170 |
pssparam = ksba_crl_get_sig_val (crl); |
| 171 |
if (!quiet) |
| 172 |
{ |
| 173 |
print_sexp (pssparam); |
| 174 |
putchar ('\n'); |
| 175 |
} |
| 176 |
xfree (pssparam); |
| 177 |
} |
| 178 |
|
| 179 |
err = ksba_crl_get_issuer (crl, &issuer); |
| 180 |
fail_if_err2 (fname, err); |
| 181 |
if (!quiet) |
| 182 |
{ |
| 183 |
printf ("issuer: "); |
| 184 |
print_dn (issuer); |
| 185 |
putchar ('\n'); |
| 186 |
} |
| 187 |
xfree (issuer); |
| 188 |
|
| 189 |
err = ksba_crl_get_update_times (crl, this, next); |
| 190 |
if (gpg_err_code (err) != GPG_ERR_INV_TIME) |
| 191 |
fail_if_err2 (fname, err); |
| 192 |
if (!quiet) |
| 193 |
{ |
| 194 |
printf ("thisUpdate: "); |
| 195 |
print_time (this); |
| 196 |
putchar ('\n'); |
| 197 |
printf ("nextUpdate: "); |
| 198 |
print_time (next); |
| 199 |
putchar ('\n'); |
| 200 |
} |
| 201 |
} |
| 202 |
break; |
| 203 |
|
| 204 |
case KSBA_SR_GOT_ITEM: |
| 205 |
{ |
| 206 |
ksba_sexp_t serial; |
| 207 |
ksba_isotime_t rdate; |
| 208 |
ksba_crl_reason_t reason; |
| 209 |
|
| 210 |
err = ksba_crl_get_item (crl, &serial, rdate, &reason); |
| 211 |
fail_if_err2 (fname, err); |
| 212 |
if (!quiet) |
| 213 |
{ |
| 214 |
printf ("CRL entry %d: s=", ++count); |
| 215 |
print_sexp_hex (serial); |
| 216 |
printf (", t="); |
| 217 |
print_time (rdate); |
| 218 |
printf (", r=%x\n", reason); |
| 219 |
} |
| 220 |
xfree (serial); |
| 221 |
} |
| 222 |
break; |
| 223 |
|
| 224 |
case KSBA_SR_END_ITEMS: |
| 225 |
break; |
| 226 |
|
| 227 |
case KSBA_SR_READY: |
| 228 |
break; |
| 229 |
|
| 230 |
default: |
| 231 |
fail ("unknown stop reason"); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
while (stopreason != KSBA_SR_READY); |
| 236 |
|
| 237 |
if ( !ksba_crl_get_digest_algo (crl)) |
| 238 |
fail ("digest algorithm mismatch"); |
| 239 |
|
| 240 |
{ |
| 241 |
ksba_name_t name1; |
| 242 |
ksba_sexp_t serial; |
| 243 |
ksba_sexp_t keyid; |
| 244 |
|
| 245 |
err = ksba_crl_get_auth_key_id (crl, &keyid, &name1, &serial); |
| 246 |
if (!err || gpg_err_code (err) == GPG_ERR_NO_DATA) |
| 247 |
{ |
| 248 |
if (!quiet) |
| 249 |
fputs ("AuthorityKeyIdentifier: ", stdout); |
| 250 |
if (gpg_err_code (err) == GPG_ERR_NO_DATA) |
| 251 |
{ |
| 252 |
if (!quiet) |
| 253 |
fputs ("none\n", stdout); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
if (name1) |
| 258 |
{ |
| 259 |
print_names (24, name1); |
| 260 |
ksba_name_release (name1); |
| 261 |
if (!quiet) |
| 262 |
{ |
| 263 |
fputs (" serial: ", stdout); |
| 264 |
print_sexp_hex (serial); |
| 265 |
} |
| 266 |
ksba_free (serial); |
| 267 |
} |
| 268 |
if (!quiet) |
| 269 |
putchar ('\n'); |
| 270 |
if (keyid) |
| 271 |
{ |
| 272 |
if (!quiet) |
| 273 |
{ |
| 274 |
fputs (" keyIdentifier: ", stdout); |
| 275 |
print_sexp (keyid); |
| 276 |
putchar ('\n'); |
| 277 |
} |
| 278 |
ksba_free (keyid); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
else |
| 283 |
fail_if_err (err); |
| 284 |
} |
| 285 |
|
| 286 |
{ |
| 287 |
ksba_sexp_t serial; |
| 288 |
|
| 289 |
err = ksba_crl_get_crl_number (crl, &serial); |
| 290 |
if (!err || gpg_err_code (err) == GPG_ERR_NO_DATA) |
| 291 |
{ |
| 292 |
if (!quiet) |
| 293 |
fputs ("crlNumber: ", stdout); |
| 294 |
if (gpg_err_code (err) == GPG_ERR_NO_DATA) |
| 295 |
{ |
| 296 |
if (!quiet) |
| 297 |
fputs ("none", stdout); |
| 298 |
} |
| 299 |
else |
| 300 |
{ |
| 301 |
if (!quiet) |
| 302 |
print_sexp (serial); |
| 303 |
ksba_free (serial); |
| 304 |
} |
| 305 |
if (!quiet) |
| 306 |
putchar ('\n'); |
| 307 |
} |
| 308 |
else |
| 309 |
fail_if_err (err); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
{ |
| 314 |
int idx, crit; |
| 315 |
const char *oid; |
| 316 |
size_t derlen; |
| 317 |
|
| 318 |
for (idx=0; !(err=ksba_crl_get_extension (crl, idx, |
| 319 |
&oid, &crit, |
| 320 |
NULL, &derlen)); idx++) |
| 321 |
{ |
| 322 |
const char *s = get_oid_desc (oid); |
| 323 |
if (!quiet) |
| 324 |
printf ("%sExtn: %s%s%s%s (%lu octets)\n", |
| 325 |
crit? "Crit":"", |
| 326 |
s?" (":"", s?s:"", s?")":"", |
| 327 |
oid, (unsigned long)derlen); |
| 328 |
} |
| 329 |
if (err && gpg_err_code (err) != GPG_ERR_EOF |
| 330 |
&& gpg_err_code (err) != GPG_ERR_NO_DATA ) |
| 331 |
fail_if_err (err); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
{ |
| 336 |
ksba_sexp_t sigval; |
| 337 |
|
| 338 |
sigval = ksba_crl_get_sig_val (crl); |
| 339 |
if (!sigval) |
| 340 |
fail ("signature value missing"); |
| 341 |
if (!quiet) |
| 342 |
{ |
| 343 |
print_sexp (sigval); |
| 344 |
putchar ('\n'); |
| 345 |
} |
| 346 |
xfree (sigval); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
ksba_crl_release (crl); |
| 351 |
ksba_reader_release (r); |
| 352 |
fclose (fp); |
| 353 |
if (hashlog) |
| 354 |
fclose (hashlog); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
int |
| 361 |
main (int argc, char **argv) |
| 362 |
{ |
| 363 |
const char *srcdir = getenv ("srcdir"); |
| 364 |
|
| 365 |
if (!srcdir) |
| 366 |
srcdir = "."; |
| 367 |
|
| 368 |
if (argc) |
| 369 |
{ |
| 370 |
argc--; argv++; |
| 371 |
} |
| 372 |
if (argc && !strcmp (*argv, "--verbose")) |
| 373 |
{ |
| 374 |
verbose = 1; |
| 375 |
argc--; argv++; |
| 376 |
} |
| 377 |
|
| 378 |
if (argc) |
| 379 |
{ |
| 380 |
for (; argc; argc--, argv++) |
| 381 |
one_file (*argv); |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
const char *files[] = { |
| 386 |
"crl_testpki_testpca.der", |
| 387 |
NULL |
| 388 |
}; |
| 389 |
int idx; |
| 390 |
|
| 391 |
if (!verbose) |
| 392 |
quiet = 1; |
| 393 |
|
| 394 |
for (idx=0; files[idx]; idx++) |
| 395 |
{ |
| 396 |
char *fname; |
| 397 |
|
| 398 |
fname = xmalloc (strlen (srcdir) + 10 + strlen (files[idx]) + 1); |
| 399 |
strcpy (fname, srcdir); |
| 400 |
strcat (fname, "/samples/"); |
| 401 |
strcat (fname, files[idx]); |
| 402 |
one_file (fname); |
| 403 |
xfree (fname); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return 0; |
| 408 |
} |