| 1 |
/* t-cms-parser.c - basic test for the CMS parser. |
| 2 |
* Copyright (C) 2001 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 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <time.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include "../src/ksba.h" |
| 28 |
|
| 29 |
#include "oidtranstbl.h" |
| 30 |
#include "t-common.h" |
| 31 |
|
| 32 |
|
| 33 |
static int quiet; |
| 34 |
static int verbose; |
| 35 |
|
| 36 |
|
| 37 |
void |
| 38 |
dummy_hash_fnc (void *arg, const void *buffer, size_t length) |
| 39 |
{ |
| 40 |
(void)arg; |
| 41 |
(void)buffer; |
| 42 |
(void)length; |
| 43 |
} |
| 44 |
|
| 45 |
static int |
| 46 |
dummy_writer_cb (void *cb_value, const void *buffer, size_t count) |
| 47 |
{ |
| 48 |
(void)cb_value; |
| 49 |
(void)buffer; |
| 50 |
(void)count; |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
/* Return the description for OID; if no description is available |
| 55 |
NULL is returned. */ |
| 56 |
static const char * |
| 57 |
get_oid_desc (const char *oid) |
| 58 |
{ |
| 59 |
int i; |
| 60 |
|
| 61 |
if (oid) |
| 62 |
for (i=0; oidtranstbl[i].oid; i++) |
| 63 |
if (!strcmp (oidtranstbl[i].oid, oid)) |
| 64 |
return oidtranstbl[i].desc; |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
static void |
| 70 |
print_oid_and_desc (const char *oid, int with_lf) |
| 71 |
{ |
| 72 |
const char *s = get_oid_desc (oid); |
| 73 |
printf ("%s%s%s%s", |
| 74 |
oid, s?" (":"", s?s:"", s?")":""); |
| 75 |
if (with_lf) |
| 76 |
putchar ('\n'); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static gpg_error_t |
| 82 |
dump_one_attribute_set (ksba_cms_t cms, int signer, int unprotected) |
| 83 |
{ |
| 84 |
gpg_error_t err; |
| 85 |
int idx; |
| 86 |
char *oid = NULL; |
| 87 |
unsigned char *der = NULL; |
| 88 |
size_t derlen; |
| 89 |
int plen; |
| 90 |
|
| 91 |
for (idx=0; ; idx++) |
| 92 |
{ |
| 93 |
ksba_free (oid); |
| 94 |
ksba_free (der); |
| 95 |
err = ksba_cms_get_attribute (cms, signer, idx, unprotected, |
| 96 |
&oid, &der, &derlen); |
| 97 |
if (err) |
| 98 |
break; |
| 99 |
plen = printf ("signer %d - %sattr %d: ", |
| 100 |
signer, unprotected?"u":"s", idx); |
| 101 |
print_oid_and_desc (oid, 1); |
| 102 |
if (der) |
| 103 |
{ |
| 104 |
printf ("%*s", plen, ""); |
| 105 |
if (derlen > 96 && verbose < 2) |
| 106 |
{ |
| 107 |
print_hex (der, 96, plen); |
| 108 |
printf ("\n%*s[... --all prints more]",plen,""); |
| 109 |
} |
| 110 |
else |
| 111 |
print_hex (der, derlen, plen); |
| 112 |
putchar ('\n'); |
| 113 |
} |
| 114 |
} |
| 115 |
ksba_free (oid); |
| 116 |
ksba_free (der); |
| 117 |
|
| 118 |
if (gpg_err_code (err) == GPG_ERR_EOF) |
| 119 |
err = 0; |
| 120 |
return err; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
static void |
| 125 |
one_file (const char *fname) |
| 126 |
{ |
| 127 |
gpg_error_t err; |
| 128 |
FILE *fp; |
| 129 |
ksba_reader_t r; |
| 130 |
ksba_writer_t w; |
| 131 |
ksba_cms_t cms; |
| 132 |
ksba_content_type_t ct; |
| 133 |
int i; |
| 134 |
const char *algoid; |
| 135 |
ksba_stop_reason_t stopreason; |
| 136 |
const char *s; |
| 137 |
size_t n; |
| 138 |
ksba_sexp_t p; |
| 139 |
char *dn; |
| 140 |
int idx; |
| 141 |
|
| 142 |
if (!quiet) |
| 143 |
printf ("*** checking `%s' ***\n", fname); |
| 144 |
fp = fopen (fname, "rb"); |
| 145 |
if (!fp) |
| 146 |
{ |
| 147 |
fprintf (stderr, "%s:%d: can't open `%s': %s\n", |
| 148 |
__FILE__, __LINE__, fname, strerror (errno)); |
| 149 |
exit (1); |
| 150 |
} |
| 151 |
|
| 152 |
err = ksba_reader_new (&r); |
| 153 |
if (err) |
| 154 |
fail_if_err (err); |
| 155 |
err = ksba_reader_set_file (r, fp); |
| 156 |
fail_if_err (err); |
| 157 |
/* Also create a writer so that cms.c won't return an error when |
| 158 |
writing processed content. */ |
| 159 |
err = ksba_writer_new (&w); |
| 160 |
if (err) |
| 161 |
fail_if_err (err); |
| 162 |
err = ksba_writer_set_cb (w, dummy_writer_cb, NULL); |
| 163 |
fail_if_err (err); |
| 164 |
|
| 165 |
switch (ksba_cms_identify (r)) |
| 166 |
{ |
| 167 |
case KSBA_CT_DATA: s = "data"; break; |
| 168 |
case KSBA_CT_SIGNED_DATA: s = "signed data"; break; |
| 169 |
case KSBA_CT_ENVELOPED_DATA: s = "enveloped data"; break; |
| 170 |
case KSBA_CT_AUTHENVELOPED_DATA: s = "auth enveloped data"; break; |
| 171 |
case KSBA_CT_DIGESTED_DATA: s = "digested data"; break; |
| 172 |
case KSBA_CT_ENCRYPTED_DATA: s = "encrypted data"; break; |
| 173 |
case KSBA_CT_AUTH_DATA: s = "auth data"; break; |
| 174 |
case KSBA_CT_SPC_IND_DATA_CTX:s = "spc indirect data context"; break; |
| 175 |
case KSBA_CT_OPENPGP_KEYBLOCK:s = "openpgp keyblock"; break; |
| 176 |
default: s = "unknown"; break; |
| 177 |
} |
| 178 |
if (!quiet) |
| 179 |
printf ("identified as: %s\n", s); |
| 180 |
|
| 181 |
err = ksba_cms_new (&cms); |
| 182 |
if (err) |
| 183 |
fail_if_err (err); |
| 184 |
|
| 185 |
err = ksba_cms_set_reader_writer (cms, r, w); |
| 186 |
fail_if_err (err); |
| 187 |
|
| 188 |
err = ksba_cms_parse (cms, &stopreason); |
| 189 |
fail_if_err2 (fname, err); |
| 190 |
if (!quiet) |
| 191 |
printf ("stop reason: %d\n", stopreason); |
| 192 |
|
| 193 |
s = ksba_cms_get_content_oid (cms, 0); |
| 194 |
if (!quiet) |
| 195 |
printf ("ContentType: %s\n", s?s:"none"); |
| 196 |
|
| 197 |
err = ksba_cms_parse (cms, &stopreason); |
| 198 |
fail_if_err2 (fname, err); |
| 199 |
if (!quiet) |
| 200 |
printf ("stop reason: %d\n", stopreason); |
| 201 |
|
| 202 |
s = ksba_cms_get_content_oid (cms, 1); |
| 203 |
if (!quiet) |
| 204 |
{ |
| 205 |
printf ("EncapsulatedContentType: %s\n", s?s:"none"); |
| 206 |
printf ("DigestAlgorithms:"); |
| 207 |
} |
| 208 |
for (i=0; (algoid = ksba_cms_get_digest_algo_list (cms, i)); i++) |
| 209 |
if (!quiet) |
| 210 |
printf (" %s", algoid); |
| 211 |
if (!quiet) |
| 212 |
putchar('\n'); |
| 213 |
|
| 214 |
if (stopreason == KSBA_SR_NEED_HASH) |
| 215 |
if (!quiet) |
| 216 |
printf("Detached signature\n"); |
| 217 |
|
| 218 |
ksba_cms_set_hash_function (cms, dummy_hash_fnc, NULL); |
| 219 |
|
| 220 |
do |
| 221 |
{ |
| 222 |
err = ksba_cms_parse (cms, &stopreason); |
| 223 |
fail_if_err2 (fname, err); |
| 224 |
if (!quiet) |
| 225 |
printf ("stop reason: %d\n", stopreason); |
| 226 |
} |
| 227 |
while (stopreason != KSBA_SR_READY); |
| 228 |
|
| 229 |
|
| 230 |
ct = ksba_cms_get_content_type (cms, 0); |
| 231 |
if (ct == KSBA_CT_ENVELOPED_DATA || ct == KSBA_CT_AUTHENVELOPED_DATA) |
| 232 |
{ |
| 233 |
for (idx=0; ; idx++) |
| 234 |
{ |
| 235 |
err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p); |
| 236 |
if (err == -1) |
| 237 |
break; /* ready */ |
| 238 |
|
| 239 |
if (gpg_err_code (err) == GPG_ERR_UNSUPPORTED_CMS_OBJ) |
| 240 |
{ |
| 241 |
printf ("recipient %d" |
| 242 |
" - kekri or pwri detected\n", idx); |
| 243 |
err = 0; |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
fail_if_err2 (fname, err); |
| 248 |
if (!quiet) |
| 249 |
{ |
| 250 |
printf ("recipient %d - issuer: ", idx); |
| 251 |
print_dn (dn); |
| 252 |
} |
| 253 |
ksba_free (dn); |
| 254 |
if (!quiet) |
| 255 |
{ |
| 256 |
putchar ('\n'); |
| 257 |
printf ("recipient %d - serial: ", idx); |
| 258 |
print_sexp_hex (p); |
| 259 |
putchar ('\n'); |
| 260 |
} |
| 261 |
ksba_free (p); |
| 262 |
} |
| 263 |
|
| 264 |
dn = ksba_cms_get_enc_val (cms, idx); |
| 265 |
if (!quiet) |
| 266 |
{ |
| 267 |
printf ("recipient %d - enc_val: ", idx); |
| 268 |
print_sexp (dn); |
| 269 |
putchar ('\n'); |
| 270 |
} |
| 271 |
ksba_free (dn); |
| 272 |
} |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
for (idx=0; idx < 1; idx++) |
| 277 |
{ |
| 278 |
err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p); |
| 279 |
if (gpg_err_code (err) == GPG_ERR_NO_DATA && !idx) |
| 280 |
{ |
| 281 |
if (!quiet) |
| 282 |
printf ("this is a certs-only message\n"); |
| 283 |
break; |
| 284 |
} |
| 285 |
|
| 286 |
fail_if_err2 (fname, err); |
| 287 |
if (!quiet) |
| 288 |
{ |
| 289 |
printf ("signer %d - issuer: ", idx); |
| 290 |
print_dn (dn); |
| 291 |
putchar ('\n'); |
| 292 |
} |
| 293 |
ksba_free (dn); |
| 294 |
|
| 295 |
if (!quiet) |
| 296 |
{ |
| 297 |
printf ("signer %d - serial: ", idx); |
| 298 |
print_sexp_hex (p); |
| 299 |
putchar ('\n'); |
| 300 |
} |
| 301 |
ksba_free (p); |
| 302 |
|
| 303 |
err = ksba_cms_get_message_digest (cms, idx, &dn, &n); |
| 304 |
fail_if_err2 (fname, err); |
| 305 |
if (!quiet) |
| 306 |
{ |
| 307 |
printf ("signer %d - messageDigest: ", idx); |
| 308 |
print_hex (dn, n, 0); |
| 309 |
putchar ('\n'); |
| 310 |
} |
| 311 |
ksba_free (dn); |
| 312 |
|
| 313 |
err = ksba_cms_get_sigattr_oids (cms, idx, |
| 314 |
"1.2.840.113549.1.9.3",&dn); |
| 315 |
if (err && err != -1) |
| 316 |
fail_if_err2 (fname, err); |
| 317 |
if (err != -1) |
| 318 |
{ |
| 319 |
char *tmp; |
| 320 |
|
| 321 |
for (tmp=dn; *tmp; tmp++) |
| 322 |
if (*tmp == '\n') |
| 323 |
*tmp = ' '; |
| 324 |
if (!quiet) |
| 325 |
printf ("signer %d - content-type: %s\n", idx, dn); |
| 326 |
ksba_free (dn); |
| 327 |
} |
| 328 |
|
| 329 |
algoid = ksba_cms_get_digest_algo (cms, idx); |
| 330 |
if (!quiet) |
| 331 |
printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?"); |
| 332 |
|
| 333 |
dn = ksba_cms_get_sig_val (cms, idx); |
| 334 |
if (dn) |
| 335 |
{ |
| 336 |
if (!quiet) |
| 337 |
{ |
| 338 |
printf ("signer %d - signature: ", idx); |
| 339 |
print_sexp (dn); |
| 340 |
putchar ('\n'); |
| 341 |
} |
| 342 |
} |
| 343 |
else |
| 344 |
{ |
| 345 |
if (!quiet) |
| 346 |
printf ("signer %d - signature not found\n", idx); |
| 347 |
} |
| 348 |
ksba_free (dn); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
if (verbose) |
| 353 |
{ |
| 354 |
int signer; |
| 355 |
|
| 356 |
for (signer=0; ; signer++) |
| 357 |
{ |
| 358 |
err = dump_one_attribute_set (cms, signer, 0); |
| 359 |
if (gpg_err_code (err) == GPG_ERR_NOT_FOUND) |
| 360 |
break; /* No more signer. */ |
| 361 |
fail_if_err2 (fname, err); |
| 362 |
err = dump_one_attribute_set (cms, signer, 1); |
| 363 |
if (gpg_err_code (err) == GPG_ERR_NOT_FOUND) |
| 364 |
break; /* No more signer. */ |
| 365 |
fail_if_err2 (fname, err); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
ksba_cms_release (cms); |
| 370 |
ksba_writer_release (w); |
| 371 |
ksba_reader_release (r); |
| 372 |
fclose (fp); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
int |
| 379 |
main (int argc, char **argv) |
| 380 |
{ |
| 381 |
if (argc) |
| 382 |
{ |
| 383 |
argc--; argv++; |
| 384 |
} |
| 385 |
if (argc && !strcmp (*argv, "--verbose")) |
| 386 |
{ |
| 387 |
verbose = 1; |
| 388 |
argc--; argv++; |
| 389 |
} |
| 390 |
if (argc && !strcmp (*argv, "--all")) |
| 391 |
{ |
| 392 |
verbose = 2; |
| 393 |
argc--; argv++; |
| 394 |
} |
| 395 |
|
| 396 |
if (argc) |
| 397 |
{ |
| 398 |
for (; argc; argc--, argv++) |
| 399 |
one_file (*argv); |
| 400 |
} |
| 401 |
else |
| 402 |
{ |
| 403 |
static char *testfiles[] = |
| 404 |
{ |
| 405 |
"samples/detached-sig.cms", |
| 406 |
"samples/ecdh-sample1.p7m", |
| 407 |
"samples/ecdsa-sample1.p7s", |
| 408 |
"samples/rsa-sample1.p7m", |
| 409 |
"samples/rsa-sample1.p7s", |
| 410 |
NULL |
| 411 |
}; |
| 412 |
char *fname; |
| 413 |
int idx; |
| 414 |
|
| 415 |
if (!verbose) |
| 416 |
quiet = 1; |
| 417 |
|
| 418 |
for (idx=0; testfiles[idx]; idx++) |
| 419 |
{ |
| 420 |
fname = prepend_srcdir (testfiles[idx]); |
| 421 |
one_file (fname); |
| 422 |
free(fname); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
if (!quiet) |
| 427 |
printf ("*** all checks done\n"); |
| 428 |
|
| 429 |
return 0; |
| 430 |
} |