1 /* t-cms-parser.c - basic test for the CMS parser.
2 * Copyright (C) 2001 g10 Code GmbH
4 * This file is part of KSBA.
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.
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.
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/>.
27 #include "../src/ksba.h"
37 dummy_hash_fnc (void *arg, const void *buffer, size_t length)
45 dummy_writer_cb (void *cb_value, const void *buffer, size_t count)
56 one_file (const char *fname)
63 ksba_content_type_t ct;
66 ksba_stop_reason_t stopreason;
74 printf ("*** checking `%s' ***\n", fname);
75 fp = fopen (fname, "rb");
78 fprintf (stderr, "%s:%d: can't open `%s': %s\n",
79 __FILE__, __LINE__, fname, strerror (errno));
83 err = ksba_reader_new (&r);
86 err = ksba_reader_set_file (r, fp);
88 /* Also create a writer so that cms.c won't return an error when
89 writing processed content. */
90 err = ksba_writer_new (&w);
93 err = ksba_writer_set_cb (w, dummy_writer_cb, NULL);
96 switch (ksba_cms_identify (r))
98 case KSBA_CT_DATA: s = "data"; break;
99 case KSBA_CT_SIGNED_DATA: s = "signed data"; break;
100 case KSBA_CT_ENVELOPED_DATA: s = "enveloped data"; break;
101 case KSBA_CT_AUTHENVELOPED_DATA: s = "auth enveloped data"; break;
102 case KSBA_CT_DIGESTED_DATA: s = "digested data"; break;
103 case KSBA_CT_ENCRYPTED_DATA: s = "encrypted data"; break;
104 case KSBA_CT_AUTH_DATA: s = "auth data"; break;
105 case KSBA_CT_SPC_IND_DATA_CTX:s = "spc indirect data context"; break;
106 case KSBA_CT_OPENPGP_KEYBLOCK:s = "openpgp keyblock"; break;
107 default: s = "unknown"; break;
110 printf ("identified as: %s\n", s);
112 err = ksba_cms_new (&cms);
116 err = ksba_cms_set_reader_writer (cms, r, w);
119 err = ksba_cms_parse (cms, &stopreason);
120 fail_if_err2 (fname, err);
122 printf ("stop reason: %d\n", stopreason);
124 s = ksba_cms_get_content_oid (cms, 0);
126 printf ("ContentType: %s\n", s?s:"none");
128 err = ksba_cms_parse (cms, &stopreason);
129 fail_if_err2 (fname, err);
131 printf ("stop reason: %d\n", stopreason);
133 s = ksba_cms_get_content_oid (cms, 1);
136 printf ("EncapsulatedContentType: %s\n", s?s:"none");
137 printf ("DigestAlgorithms:");
139 for (i=0; (algoid = ksba_cms_get_digest_algo_list (cms, i)); i++)
141 printf (" %s", algoid);
145 if (stopreason == KSBA_SR_NEED_HASH)
147 printf("Detached signature\n");
149 ksba_cms_set_hash_function (cms, dummy_hash_fnc, NULL);
153 err = ksba_cms_parse (cms, &stopreason);
154 fail_if_err2 (fname, err);
156 printf ("stop reason: %d\n", stopreason);
158 while (stopreason != KSBA_SR_READY);
161 ct = ksba_cms_get_content_type (cms, 0);
162 if (ct == KSBA_CT_ENVELOPED_DATA || ct == KSBA_CT_AUTHENVELOPED_DATA)
166 err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
170 if (gpg_err_code (err) == GPG_ERR_UNSUPPORTED_CMS_OBJ)
172 printf ("recipient %d"
173 " - kekri or pwri detected\n", idx);
178 fail_if_err2 (fname, err);
181 printf ("recipient %d - issuer: ", idx);
188 printf ("recipient %d - serial: ", idx);
195 dn = ksba_cms_get_enc_val (cms, idx);
198 printf ("recipient %d - enc_val: ", idx);
207 for (idx=0; idx < 1; idx++)
209 err = ksba_cms_get_issuer_serial (cms, idx, &dn, &p);
210 if (gpg_err_code (err) == GPG_ERR_NO_DATA && !idx)
213 printf ("this is a certs-only message\n");
217 fail_if_err2 (fname, err);
220 printf ("signer %d - issuer: ", idx);
228 printf ("signer %d - serial: ", idx);
234 err = ksba_cms_get_message_digest (cms, idx, &dn, &n);
235 fail_if_err2 (fname, err);
238 printf ("signer %d - messageDigest: ", idx);
244 err = ksba_cms_get_sigattr_oids (cms, idx,
245 "1.2.840.113549.1.9.3",&dn);
246 if (err && err != -1)
247 fail_if_err2 (fname, err);
252 for (tmp=dn; *tmp; tmp++)
256 printf ("signer %d - content-type: %s\n", idx, dn);
260 algoid = ksba_cms_get_digest_algo (cms, idx);
262 printf ("signer %d - digest algo: %s\n", idx, algoid?algoid:"?");
264 dn = ksba_cms_get_sig_val (cms, idx);
269 printf ("signer %d - signature: ", idx);
277 printf ("signer %d - signature not found\n", idx);
283 ksba_cms_release (cms);
284 ksba_writer_release (w);
285 ksba_reader_release (r);
293 main (int argc, char **argv)
299 if (argc && !strcmp (*argv, "--verbose"))
307 for (; argc; argc--, argv++)
312 static char *testfiles[] =
314 "samples/detached-sig.cms",
315 "samples/ecdh-sample1.p7m",
316 "samples/ecdsa-sample1.p7s",
317 "samples/rsa-sample1.p7m",
318 "samples/rsa-sample1.p7s",
327 for (idx=0; testfiles[idx]; idx++)
329 fname = prepend_srcdir (testfiles[idx]);
336 printf ("*** all checks done\n");