Debian Codegraph @

lib/hx509/hxtool.c

raw
1 /*
2 * Copyright (c) 2004 - 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "hx_locl.h"
35
36 #include <hxtool-commands.h>
37 #include <sl.h>
38 #include <rtbl.h>
39 #include <parse_time.h>
40
41 static hx509_context context;
42
43 static char *stat_file_string;
44 static int version_flag;
45 static int help_flag;
46
47 struct getargs args[] = {
48 { "statistic-file", 0, arg_string, &stat_file_string, NULL, NULL },
49 { "version", 0, arg_flag, &version_flag, NULL, NULL },
50 { "help", 0, arg_flag, &help_flag, NULL, NULL }
51 };
52 int num_args = sizeof(args) / sizeof(args[0]);
53
54 static void
55 usage(int code)
56 {
57 arg_printusage(args, num_args, NULL, "command");
58 printf("Use \"%s help\" to get more help\n", getprogname());
59 exit(code);
60 }
61
62 /*
63 *
64 */
65
66 static void
67 lock_strings(hx509_lock lock, getarg_strings *pass)
68 {
69 int i;
70 for (i = 0; i < pass->num_strings; i++) {
71 int ret = hx509_lock_command_string(lock, pass->strings[i]);
72 if (ret)
73 errx(1, "hx509_lock_command_string: %s: %d",
74 pass->strings[i], ret);
75 }
76 }
77
78 /*
79 *
80 */
81
82 static void
83 certs_strings(hx509_context contextp, const char *type, hx509_certs certs,
84 hx509_lock lock, const getarg_strings *s)
85 {
86 int i, ret;
87
88 for (i = 0; i < s->num_strings; i++) {
89 ret = hx509_certs_append(contextp, certs, lock, s->strings[i]);
90 if (ret)
91 hx509_err(contextp, 1, ret,
92 "hx509_certs_append: %s %s", type, s->strings[i]);
93 }
94 }
95
96 /*
97 *
98 */
99
100 static void
101 parse_oid(const char *str, const heim_oid *def, heim_oid *oid)
102 {
103 int ret;
104 if (str)
105 ret = der_parse_heim_oid (str, " .", oid);
106 else
107 ret = der_copy_oid(def, oid);
108 if (ret)
109 errx(1, "parse_oid failed for: %s", str ? str : "default oid");
110 }
111
112 /*
113 *
114 */
115
116 static void
117 peer_strings(hx509_context contextp,
118 hx509_peer_info *peer,
119 const getarg_strings *s)
120 {
121 AlgorithmIdentifier *val;
122 int ret, i;
123
124 ret = hx509_peer_info_alloc(contextp, peer);
125 if (ret)
126 hx509_err(contextp, 1, ret, "hx509_peer_info_alloc");
127
128 val = calloc(s->num_strings, sizeof(*val));
129 if (val == NULL)
130 err(1, "malloc");
131
132 for (i = 0; i < s->num_strings; i++)
133 parse_oid(s->strings[i], NULL, &val[i].algorithm);
134
135 ret = hx509_peer_info_set_cms_algs(contextp, *peer, val, s->num_strings);
136 if (ret)
137 hx509_err(contextp, 1, ret, "hx509_peer_info_set_cms_algs");
138
139 for (i = 0; i < s->num_strings; i++)
140 free_AlgorithmIdentifier(&val[i]);
141 free(val);
142 }
143
144 /*
145 *
146 */
147
148 struct pem_data {
149 heim_octet_string *os;
150 int detached_data;
151 };
152
153 static int
154 pem_reader(hx509_context contextp, const char *type,
155 const hx509_pem_header *headers,
156 const void *data , size_t length, void *ctx)
157 {
158 struct pem_data *p = (struct pem_data *)ctx;
159 const char *h;
160
161 p->os->data = malloc(length);
162 if (p->os->data == NULL)
163 return ENOMEM;
164 memcpy(p->os->data, data, length);
165 p->os->length = length;
166
167 h = hx509_pem_find_header(headers, "Content-disposition");
168 if (h && strcasecmp(h, "detached") == 0)
169 p->detached_data = 1;
170
171 return 0;
172 }
173
174 /*
175 *
176 */
177
178 int
179 cms_verify_sd(struct cms_verify_sd_options *opt, int argc, char **argv)
180 {
181 hx509_verify_ctx ctx = NULL;
182 heim_oid type;
183 heim_octet_string c, co, signeddata, *sd = NULL;
184 hx509_certs store = NULL;
185 hx509_certs signers = NULL;
186 hx509_certs anchors = NULL;
187 hx509_lock lock;
188 int ret, flags = 0;
189
190 size_t sz;
191 void *p = NULL;
192
193 if (opt->missing_revoke_flag)
194 hx509_context_set_missing_revoke(context, 1);
195
196 hx509_lock_init(context, &lock);
197 lock_strings(lock, &opt->pass_strings);
198
199 ret = hx509_verify_init_ctx(context, &ctx);
200 if (ret)
201 hx509_err(context, 1, ret, "hx509_verify_init_ctx");
202
203 ret = hx509_certs_init(context, "MEMORY:cms-anchors", 0, NULL, &anchors);
204 if (ret)
205 hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
206 ret = hx509_certs_init(context, "MEMORY:cert-store", 0, NULL, &store);
207 if (ret)
208 hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
209
210 certs_strings(context, "anchors", anchors, lock, &opt->anchors_strings);
211 certs_strings(context, "store", store, lock, &opt->certificate_strings);
212
213 if (opt->pem_flag) {
214 struct pem_data pd;
215 FILE *f;
216
217 pd.os = &co;
218 pd.detached_data = 0;
219
220 f = fopen(argv[0], "r");
221 if (f == NULL)
222 err(1, "Failed to open file %s", argv[0]);
223
224 ret = hx509_pem_read(context, f, pem_reader, &pd);
225 fclose(f);
226 if (ret)
227 errx(1, "PEM reader failed: %d", ret);
228
229 if (pd.detached_data && opt->signed_content_string == NULL) {
230 char *r = strrchr(argv[0], '.');
231 if (r && strcasecmp(r, ".pem") == 0) {
232 char *s = strdup(argv[0]);
233 if (s == NULL)
234 errx(1, "malloc: out of memory");
235 s[r - argv[0]] = '\0';
236 ret = _hx509_map_file_os(s, &signeddata);
237 if (ret)
238 errx(1, "map_file: %s: %d", s, ret);
239 free(s);
240 sd = &signeddata;
241 }
242 }
243
244 } else {
245 ret = rk_undumpdata(argv[0], &p, &sz);
246 if (ret)
247 err(1, "map_file: %s: %d", argv[0], ret);
248
249 co.data = p;
250 co.length = sz;
251 }
252
253 if (opt->signed_content_string) {
254 ret = _hx509_map_file_os(opt->signed_content_string, &signeddata);
255 if (ret)
256 errx(1, "map_file: %s: %d", opt->signed_content_string, ret);
257 sd = &signeddata;
258 }
259
260 if (opt->content_info_flag) {
261 heim_octet_string uwco;
262 heim_oid oid;
263
264 ret = hx509_cms_unwrap_ContentInfo(&co, &oid, &uwco, NULL);
265 if (ret)
266 errx(1, "hx509_cms_unwrap_ContentInfo: %d", ret);
267
268 if (der_heim_oid_cmp(&oid, &asn1_oid_id_pkcs7_signedData) != 0)
269 errx(1, "Content is not SignedData");
270 der_free_oid(&oid);
271
272 if (p == NULL)
273 der_free_octet_string(&co);
274 else {
275 rk_xfree(p);
276 p = NULL;
277 }
278 co = uwco;
279 }
280
281 hx509_verify_attach_anchors(ctx, anchors);
282
283 if (!opt->signer_allowed_flag)
284 flags |= HX509_CMS_VS_ALLOW_ZERO_SIGNER;
285 if (opt->allow_wrong_oid_flag)
286 flags |= HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH;
287
288 ret = hx509_cms_verify_signed(context, ctx, flags, co.data, co.length, sd,
289 store, &type, &c, &signers);
290 if (p != co.data)
291 der_free_octet_string(&co);
292 else
293 rk_xfree(p);
294 if (ret)
295 hx509_err(context, 1, ret, "hx509_cms_verify_signed");
296
297 {
298 char *str;
299 der_print_heim_oid(&type, '.', &str);
300 printf("type: %s\n", str);
301 free(str);
302 der_free_oid(&type);
303 }
304 if (signers == NULL) {
305 printf("unsigned\n");
306 } else {
307 printf("signers:\n");
308 hx509_certs_iter_f(context, signers, hx509_ci_print_names, stdout);
309 }
310
311 hx509_verify_destroy_ctx(ctx);
312
313 hx509_certs_free(&store);
314 hx509_certs_free(&signers);
315 hx509_certs_free(&anchors);
316
317 hx509_lock_free(lock);
318
319 if (argc > 1) {
320 ret = _hx509_write_file(argv[1], c.data, c.length);
321 if (ret)
322 errx(1, "hx509_write_file: %d", ret);
323 }
324
325 der_free_octet_string(&c);
326
327 if (sd)
328 _hx509_unmap_file_os(sd);
329
330 return 0;
331 }
332
333 static int
334 print_signer(hx509_context contextp, void *ctx, hx509_cert cert)
335 {
336 hx509_pem_header **header = ctx;
337 char *signer_name = NULL;
338 hx509_name name;
339 int ret;
340
341 ret = hx509_cert_get_subject(cert, &name);
342 if (ret)
343 errx(1, "hx509_cert_get_subject");
344
345 ret = hx509_name_to_string(name, &signer_name);
346 hx509_name_free(&name);
347 if (ret)
348 errx(1, "hx509_name_to_string");
349
350 hx509_pem_add_header(header, "Signer", signer_name);
351
352 free(signer_name);
353 return 0;
354 }
355
356 int
357 cms_create_sd(struct cms_create_sd_options *opt, int argc, char **argv)
358 {
359 heim_oid contentType;
360 hx509_peer_info peer = NULL;
361 heim_octet_string o;
362 hx509_query *q;
363 hx509_lock lock;
364 hx509_certs store, pool, anchors, signer = NULL;
365 size_t sz;
366 void *p;
367 int ret, flags = 0;
368 char *infile, *outfile = NULL;
369
370 memset(&contentType, 0, sizeof(contentType));
371
372 infile = argv[0];
373
374 if (argc < 2) {
375 ret = asprintf(&outfile, "%s.%s", infile,
376 opt->pem_flag ? "pem" : "cms-signeddata");
377 if (ret == -1 || outfile == NULL)
378 errx(1, "out of memory");
379 } else
380 outfile = argv[1];
381
382 hx509_lock_init(context, &lock);
383 lock_strings(lock, &opt->pass_strings);
384
385 ret = hx509_certs_init(context, "MEMORY:cert-store", 0, NULL, &store);
386 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
387 ret = hx509_certs_init(context, "MEMORY:cert-pool", 0, NULL, &pool);
388 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
389
390 certs_strings(context, "store", store, lock, &opt->certificate_strings);
391 certs_strings(context, "pool", pool, lock, &opt->pool_strings);
392
393 if (opt->anchors_strings.num_strings) {
394 ret = hx509_certs_init(context, "MEMORY:cert-anchors",
395 0, NULL, &anchors);
396 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
397 certs_strings(context, "anchors", anchors, lock, &opt->anchors_strings);
398 } else
399 anchors = NULL;
400
401 if (opt->detached_signature_flag)
402 flags |= HX509_CMS_SIGNATURE_DETACHED;
403 if (opt->id_by_name_flag)
404 flags |= HX509_CMS_SIGNATURE_ID_NAME;
405 if (!opt->signer_flag) {
406 flags |= HX509_CMS_SIGNATURE_NO_SIGNER;
407
408 }
409
410 if (opt->signer_flag) {
411 ret = hx509_query_alloc(context, &q);
412 if (ret)
413 errx(1, "hx509_query_alloc: %d", ret);
414
415 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
416 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_DIGITALSIGNATURE);
417
418 if (opt->signer_string)
419 hx509_query_match_friendly_name(q, opt->signer_string);
420
421 ret = hx509_certs_filter(context, store, q, &signer);
422 hx509_query_free(context, q);
423 if (ret)
424 hx509_err(context, 1, ret, "hx509_certs_find");
425 }
426 if (!opt->embedded_certs_flag)
427 flags |= HX509_CMS_SIGNATURE_NO_CERTS;
428 if (opt->embed_leaf_only_flag)
429 flags |= HX509_CMS_SIGNATURE_LEAF_ONLY;
430
431 ret = rk_undumpdata(infile, &p, &sz);
432 if (ret)
433 err(1, "map_file: %s: %d", infile, ret);
434
435 if (opt->peer_alg_strings.num_strings)
436 peer_strings(context, &peer, &opt->peer_alg_strings);
437
438 parse_oid(opt->content_type_string, &asn1_oid_id_pkcs7_data, &contentType);
439
440 ret = hx509_cms_create_signed(context,
441 flags,
442 &contentType,
443 p,
444 sz,
445 NULL,
446 signer,
447 peer,
448 anchors,
449 pool,
450 &o);
451 if (ret)
452 hx509_err(context, 1, ret, "hx509_cms_create_signed: %d", ret);
453
454 hx509_certs_free(&anchors);
455 hx509_certs_free(&pool);
456 hx509_certs_free(&store);
457 rk_xfree(p);
458 hx509_lock_free(lock);
459 hx509_peer_info_free(peer);
460 der_free_oid(&contentType);
461
462 if (opt->content_info_flag) {
463 heim_octet_string wo;
464
465 ret = hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_signedData, &o, &wo);
466 if (ret)
467 errx(1, "hx509_cms_wrap_ContentInfo: %d", ret);
468
469 der_free_octet_string(&o);
470 o = wo;
471 }
472
473 if (opt->pem_flag) {
474 hx509_pem_header *header = NULL;
475 FILE *f;
476
477 hx509_pem_add_header(&header, "Content-disposition",
478 opt->detached_signature_flag ?
479 "detached" : "inline");
480 if (signer) {
481 ret = hx509_certs_iter_f(context, signer, print_signer, header);
482 if (ret)
483 hx509_err(context, 1, ret, "print signer");
484 }
485
486 f = fopen(outfile, "w");
487 if (f == NULL)
488 err(1, "open %s", outfile);
489
490 ret = hx509_pem_write(context, "CMS SIGNEDDATA", header, f,
491 o.data, o.length);
492 fclose(f);
493 hx509_pem_free_header(header);
494 if (ret)
495 errx(1, "hx509_pem_write: %d", ret);
496
497 } else {
498 ret = _hx509_write_file(outfile, o.data, o.length);
499 if (ret)
500 errx(1, "hx509_write_file: %d", ret);
501 }
502
503 hx509_certs_free(&signer);
504 free(o.data);
505
506 return 0;
507 }
508
509 int
510 cms_unenvelope(struct cms_unenvelope_options *opt, int argc, char **argv)
511 {
512 heim_oid contentType = { 0, NULL };
513 heim_octet_string o, co;
514 hx509_certs certs;
515 size_t sz;
516 void *p;
517 int ret;
518 hx509_lock lock;
519 int flags = 0;
520
521 hx509_lock_init(context, &lock);
522 lock_strings(lock, &opt->pass_strings);
523
524 ret = rk_undumpdata(argv[0], &p, &sz);
525 if (ret)
526 err(1, "map_file: %s: %d", argv[0], ret);
527
528 co.data = p;
529 co.length = sz;
530
531 if (opt->content_info_flag) {
532 heim_octet_string uwco;
533 heim_oid oid;
534
535 ret = hx509_cms_unwrap_ContentInfo(&co, &oid, &uwco, NULL);
536 if (ret)
537 errx(1, "hx509_cms_unwrap_ContentInfo: %d", ret);
538
539 if (der_heim_oid_cmp(&oid, &asn1_oid_id_pkcs7_envelopedData) != 0)
540 errx(1, "Content is not SignedData");
541 der_free_oid(&oid);
542
543 co = uwco;
544 }
545
546 ret = hx509_certs_init(context, "MEMORY:cert-store", 0, NULL, &certs);
547 if (ret)
548 errx(1, "hx509_certs_init: MEMORY: %d", ret);
549
550 certs_strings(context, "store", certs, lock, &opt->certificate_strings);
551
552 if (opt->allow_weak_crypto_flag)
553 flags |= HX509_CMS_UE_ALLOW_WEAK;
554
555 ret = hx509_cms_unenvelope(context, certs, flags, co.data, co.length,
556 NULL, 0, &contentType, &o);
557 if (co.data != p)
558 der_free_octet_string(&co);
559 if (ret)
560 hx509_err(context, 1, ret, "hx509_cms_unenvelope");
561
562 rk_xfree(p);
563 hx509_lock_free(lock);
564 hx509_certs_free(&certs);
565 der_free_oid(&contentType);
566
567 ret = _hx509_write_file(argv[1], o.data, o.length);
568 if (ret)
569 errx(1, "hx509_write_file: %d", ret);
570
571 der_free_octet_string(&o);
572
573 return 0;
574 }
575
576 int
577 cms_create_enveloped(struct cms_envelope_options *opt, int argc, char **argv)
578 {
579 heim_oid contentType;
580 heim_octet_string o;
581 const heim_oid *enctype = NULL;
582 hx509_query *q;
583 hx509_certs certs;
584 hx509_cert cert;
585 int ret;
586 size_t sz;
587 void *p;
588 hx509_lock lock;
589 int flags = 0;
590
591 memset(&contentType, 0, sizeof(contentType));
592
593 hx509_lock_init(context, &lock);
594 lock_strings(lock, &opt->pass_strings);
595
596 ret = rk_undumpdata(argv[0], &p, &sz);
597 if (ret)
598 err(1, "map_file: %s: %d", argv[0], ret);
599
600 ret = hx509_certs_init(context, "MEMORY:cert-store", 0, NULL, &certs);
601 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
602
603 certs_strings(context, "store", certs, lock, &opt->certificate_strings);
604
605 if (opt->allow_weak_crypto_flag)
606 flags |= HX509_CMS_EV_ALLOW_WEAK;
607
608 if (opt->encryption_type_string) {
609 enctype = hx509_crypto_enctype_by_name(opt->encryption_type_string);
610 if (enctype == NULL)
611 errx(1, "encryption type: %s no found",
612 opt->encryption_type_string);
613 }
614
615 ret = hx509_query_alloc(context, &q);
616 if (ret)
617 errx(1, "hx509_query_alloc: %d", ret);
618
619 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_ENCIPHERMENT);
620
621 ret = hx509_certs_find(context, certs, q, &cert);
622 hx509_query_free(context, q);
623 if (ret)
624 errx(1, "hx509_certs_find: %d", ret);
625
626 parse_oid(opt->content_type_string, &asn1_oid_id_pkcs7_data, &contentType);
627
628 ret = hx509_cms_envelope_1(context, flags, cert, p, sz, enctype,
629 &contentType, &o);
630 if (ret)
631 errx(1, "hx509_cms_envelope_1: %d", ret);
632
633 hx509_cert_free(cert);
634 hx509_certs_free(&certs);
635 rk_xfree(p);
636 der_free_oid(&contentType);
637
638 if (opt->content_info_flag) {
639 heim_octet_string wo;
640
641 ret = hx509_cms_wrap_ContentInfo(&asn1_oid_id_pkcs7_envelopedData, &o, &wo);
642 if (ret)
643 errx(1, "hx509_cms_wrap_ContentInfo: %d", ret);
644
645 der_free_octet_string(&o);
646 o = wo;
647 }
648
649 hx509_lock_free(lock);
650
651 ret = _hx509_write_file(argv[1], o.data, o.length);
652 if (ret)
653 errx(1, "hx509_write_file: %d", ret);
654
655 der_free_octet_string(&o);
656
657 return 0;
658 }
659
660 static void
661 print_certificate(hx509_context hxcontext, hx509_cert cert, int verbose)
662 {
663 const char *fn;
664 int ret;
665
666 fn = hx509_cert_get_friendly_name(cert);
667 if (fn)
668 printf(" friendly name: %s\n", fn);
669 printf(" private key: %s\n",
670 _hx509_cert_private_key(cert) ? "yes" : "no");
671
672 ret = hx509_print_cert(hxcontext, cert, NULL);
673 if (ret)
674 errx(1, "failed to print cert");
675
676 if (verbose) {
677 hx509_validate_ctx vctx;
678
679 hx509_validate_ctx_init(hxcontext, &vctx);
680 hx509_validate_ctx_set_print(vctx, hx509_print_stdout, stdout);
681 hx509_validate_ctx_add_flags(vctx, HX509_VALIDATE_F_VALIDATE);
682 hx509_validate_ctx_add_flags(vctx, HX509_VALIDATE_F_VERBOSE);
683
684 hx509_validate_cert(hxcontext, vctx, cert);
685
686 hx509_validate_ctx_free(vctx);
687 }
688 }
689
690
691 struct print_s {
692 int counter;
693 int verbose;
694 };
695
696 static int
697 print_f(hx509_context hxcontext, void *ctx, hx509_cert cert)
698 {
699 struct print_s *s = ctx;
700
701 printf("cert: %d\n", s->counter++);
702 print_certificate(context, cert, s->verbose);
703
704 return 0;
705 }
706
707 int
708 pcert_print(struct print_options *opt, int argc, char **argv)
709 {
710 hx509_certs certs;
711 hx509_lock lock;
712 struct print_s s;
713
714 s.counter = 0;
715 s.verbose = opt->content_flag;
716
717 hx509_lock_init(context, &lock);
718 lock_strings(lock, &opt->pass_strings);
719
720 while(argc--) {
721 int ret;
722 ret = hx509_certs_init(context, argv[0], 0, lock, &certs);
723 if (ret) {
724 if (opt->never_fail_flag) {
725 printf("ignoreing failure: %d\n", ret);
726 continue;
727 }
728 hx509_err(context, 1, ret, "hx509_certs_init");
729 }
730 if (opt->info_flag)
731 hx509_certs_info(context, certs, NULL, NULL);
732 hx509_certs_iter_f(context, certs, print_f, &s);
733 hx509_certs_free(&certs);
734 argv++;
735 }
736
737 hx509_lock_free(lock);
738
739 return 0;
740 }
741
742
743 static int
744 validate_f(hx509_context hxcontext, void *ctx, hx509_cert c)
745 {
746 hx509_validate_cert(hxcontext, ctx, c);
747 return 0;
748 }
749
750 int
751 pcert_validate(struct validate_options *opt, int argc, char **argv)
752 {
753 hx509_validate_ctx ctx;
754 hx509_certs certs;
755 hx509_lock lock;
756
757 hx509_lock_init(context, &lock);
758 lock_strings(lock, &opt->pass_strings);
759
760 hx509_validate_ctx_init(context, &ctx);
761 hx509_validate_ctx_set_print(ctx, hx509_print_stdout, stdout);
762 hx509_validate_ctx_add_flags(ctx, HX509_VALIDATE_F_VALIDATE);
763
764 while(argc--) {
765 int ret;
766 ret = hx509_certs_init(context, argv[0], 0, lock, &certs);
767 if (ret)
768 errx(1, "hx509_certs_init: %d", ret);
769 hx509_certs_iter_f(context, certs, validate_f, ctx);
770 hx509_certs_free(&certs);
771 argv++;
772 }
773 hx509_validate_ctx_free(ctx);
774
775 hx509_lock_free(lock);
776
777 return 0;
778 }
779
780 int
781 certificate_copy(struct certificate_copy_options *opt, int argc, char **argv)
782 {
783 hx509_certs certs;
784 hx509_lock inlock, outlock = NULL;
785 int ret;
786
787 hx509_lock_init(context, &inlock);
788 lock_strings(inlock, &opt->in_pass_strings);
789
790 if (opt->out_pass_string) {
791 hx509_lock_init(context, &outlock);
792 ret = hx509_lock_command_string(outlock, opt->out_pass_string);
793 if (ret)
794 errx(1, "hx509_lock_command_string: %s: %d",
795 opt->out_pass_string, ret);
796 }
797
798 ret = hx509_certs_init(context, argv[argc - 1],
799 HX509_CERTS_CREATE, inlock, &certs);
800 if (ret)
801 hx509_err(context, 1, ret, "hx509_certs_init");
802
803 while(argc-- > 1) {
804 int retx;
805 retx = hx509_certs_append(context, certs, inlock, argv[0]);
806 if (retx)
807 hx509_err(context, 1, retx, "hx509_certs_append");
808 argv++;
809 }
810
811 ret = hx509_certs_store(context, certs, 0, outlock);
812 if (ret)
813 hx509_err(context, 1, ret, "hx509_certs_store");
814
815 hx509_certs_free(&certs);
816 hx509_lock_free(inlock);
817 hx509_lock_free(outlock);
818
819 return 0;
820 }
821
822 struct verify {
823 hx509_verify_ctx ctx;
824 hx509_certs chain;
825 const char *hostname;
826 int errors;
827 int count;
828 };
829
830 static int
831 verify_f(hx509_context hxcontext, void *ctx, hx509_cert c)
832 {
833 struct verify *v = ctx;
834 int ret;
835
836 ret = hx509_verify_path(hxcontext, v->ctx, c, v->chain);
837 if (ret) {
838 char *s = hx509_get_error_string(hxcontext, ret);
839 printf("verify_path: %s: %d\n", s, ret);
840 hx509_free_error_string(s);
841 v->errors++;
842 } else {
843 v->count++;
844 printf("path ok\n");
845 }
846
847 if (v->hostname) {
848 ret = hx509_verify_hostname(hxcontext, c, 0, HX509_HN_HOSTNAME,
849 v->hostname, NULL, 0);
850 if (ret) {
851 printf("verify_hostname: %d\n", ret);
852 v->errors++;
853 }
854 }
855
856 return 0;
857 }
858
859 int
860 pcert_verify(struct verify_options *opt, int argc, char **argv)
861 {
862 hx509_certs anchors, chain, certs;
863 hx509_revoke_ctx revoke_ctx;
864 hx509_verify_ctx ctx;
865 struct verify v;
866 int ret;
867
868 memset(&v, 0, sizeof(v));
869
870 if (opt->missing_revoke_flag)
871 hx509_context_set_missing_revoke(context, 1);
872
873 ret = hx509_verify_init_ctx(context, &ctx);
874 if (ret)
875 hx509_err(context, 1, ret, "hx509_verify_init_ctx");
876 ret = hx509_certs_init(context, "MEMORY:anchors", 0, NULL, &anchors);
877 if (ret)
878 hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
879 ret = hx509_certs_init(context, "MEMORY:chain", 0, NULL, &chain);
880 if (ret)
881 hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
882 ret = hx509_certs_init(context, "MEMORY:certs", 0, NULL, &certs);
883 if (ret)
884 hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
885
886 if (opt->allow_proxy_certificate_flag)
887 hx509_verify_set_proxy_certificate(ctx, 1);
888
889 if (opt->time_string) {
890 const char *p;
891 struct tm tm;
892 time_t t;
893
894 memset(&tm, 0, sizeof(tm));
895
896 p = strptime (opt->time_string, "%Y-%m-%d", &tm);
897 if (p == NULL)
898 errx(1, "Failed to parse time %s, need to be on format %%Y-%%m-%%d",
899 opt->time_string);
900
901 t = tm2time (tm, 0);
902
903 hx509_verify_set_time(ctx, t);
904 }
905
906 if (opt->hostname_string)
907 v.hostname = opt->hostname_string;
908 if (opt->max_depth_integer)
909 hx509_verify_set_max_depth(ctx, opt->max_depth_integer);
910
911 ret = hx509_revoke_init(context, &revoke_ctx);
912 if (ret)
913 errx(1, "hx509_revoke_init: %d", ret);
914
915 while(argc--) {
916 char *s = *argv++;
917
918 if (strncmp(s, "chain:", 6) == 0) {
919 s += 6;
920
921 ret = hx509_certs_append(context, chain, NULL, s);
922 if (ret)
923 hx509_err(context, 1, ret, "hx509_certs_append: chain: %s: %d", s, ret);
924
925 } else if (strncmp(s, "anchor:", 7) == 0) {
926 s += 7;
927
928 ret = hx509_certs_append(context, anchors, NULL, s);
929 if (ret)
930 hx509_err(context, 1, ret, "hx509_certs_append: anchor: %s: %d", s, ret);
931
932 } else if (strncmp(s, "cert:", 5) == 0) {
933 s += 5;
934
935 ret = hx509_certs_append(context, certs, NULL, s);
936 if (ret)
937 hx509_err(context, 1, ret, "hx509_certs_append: certs: %s: %d",
938 s, ret);
939
940 } else if (strncmp(s, "crl:", 4) == 0) {
941 s += 4;
942
943 ret = hx509_revoke_add_crl(context, revoke_ctx, s);
944 if (ret)
945 errx(1, "hx509_revoke_add_crl: %s: %d", s, ret);
946
947 } else if (strncmp(s, "ocsp:", 4) == 0) {
948 s += 5;
949
950 ret = hx509_revoke_add_ocsp(context, revoke_ctx, s);
951 if (ret)
952 errx(1, "hx509_revoke_add_ocsp: %s: %d", s, ret);
953
954 } else {
955 errx(1, "unknown option to verify: `%s'\n", s);
956 }
957 }
958
959 hx509_verify_attach_anchors(ctx, anchors);
960 hx509_verify_attach_revoke(ctx, revoke_ctx);
961
962 v.ctx = ctx;
963 v.chain = chain;
964
965 hx509_certs_iter_f(context, certs, verify_f, &v);
966
967 hx509_verify_destroy_ctx(ctx);
968
969 hx509_certs_free(&certs);
970 hx509_certs_free(&chain);
971 hx509_certs_free(&anchors);
972
973 hx509_revoke_free(&revoke_ctx);
974
975
976 if (v.count == 0) {
977 printf("no certs verify at all\n");
978 return 1;
979 }
980
981 if (v.errors) {
982 printf("failed verifing %d checks\n", v.errors);
983 return 1;
984 }
985
986 return 0;
987 }
988
989 int
990 query(struct query_options *opt, int argc, char **argv)
991 {
992 hx509_lock lock;
993 hx509_query *q;
994 hx509_certs certs;
995 hx509_cert c;
996 int ret;
997
998 ret = hx509_query_alloc(context, &q);
999 if (ret)
1000 errx(1, "hx509_query_alloc: %d", ret);
1001
1002 hx509_lock_init(context, &lock);
1003 lock_strings(lock, &opt->pass_strings);
1004
1005 ret = hx509_certs_init(context, "MEMORY:cert-store", 0, NULL, &certs);
1006 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
1007
1008 while (argc > 0) {
1009
1010 ret = hx509_certs_append(context, certs, lock, argv[0]);
1011 if (ret)
1012 errx(1, "hx509_certs_append: %s: %d", argv[0], ret);
1013
1014 argc--;
1015 argv++;
1016 }
1017
1018 if (opt->friendlyname_string)
1019 hx509_query_match_friendly_name(q, opt->friendlyname_string);
1020
1021 if (opt->eku_string) {
1022 heim_oid oid;
1023
1024 parse_oid(opt->eku_string, NULL, &oid);
1025
1026 ret = hx509_query_match_eku(q, &oid);
1027 if (ret)
1028 errx(1, "hx509_query_match_eku: %d", ret);
1029 der_free_oid(&oid);
1030 }
1031
1032 if (opt->private_key_flag)
1033 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1034
1035 if (opt->keyEncipherment_flag)
1036 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_ENCIPHERMENT);
1037
1038 if (opt->digitalSignature_flag)
1039 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_DIGITALSIGNATURE);
1040
1041 if (opt->expr_string)
1042 hx509_query_match_expr(context, q, opt->expr_string);
1043
1044 ret = hx509_certs_find(context, certs, q, &c);
1045 hx509_query_free(context, q);
1046 if (ret)
1047 printf("no match found (%d)\n", ret);
1048 else {
1049 printf("match found\n");
1050 if (opt->print_flag)
1051 print_certificate(context, c, 0);
1052 }
1053
1054 hx509_cert_free(c);
1055 hx509_certs_free(&certs);
1056
1057 hx509_lock_free(lock);
1058
1059 return ret;
1060 }
1061
1062 int
1063 ocsp_fetch(struct ocsp_fetch_options *opt, int argc, char **argv)
1064 {
1065 hx509_certs reqcerts, pool;
1066 heim_octet_string req, nonce_data, *nonce = &nonce_data;
1067 hx509_lock lock;
1068 int i, ret;
1069 char *file;
1070 const char *url = "/";
1071
1072 memset(&nonce, 0, sizeof(nonce));
1073
1074 hx509_lock_init(context, &lock);
1075 lock_strings(lock, &opt->pass_strings);
1076
1077 /* no nonce */
1078 if (!opt->nonce_flag)
1079 nonce = NULL;
1080
1081 if (opt->url_path_string)
1082 url = opt->url_path_string;
1083
1084 ret = hx509_certs_init(context, "MEMORY:ocsp-pool", 0, NULL, &pool);
1085 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
1086
1087 certs_strings(context, "ocsp-pool", pool, lock, &opt->pool_strings);
1088
1089 file = argv[0];
1090
1091 ret = hx509_certs_init(context, "MEMORY:ocsp-req", 0, NULL, &reqcerts);
1092 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
1093
1094 for (i = 1; i < argc; i++) {
1095 ret = hx509_certs_append(context, reqcerts, lock, argv[i]);
1096 if (ret)
1097 errx(1, "hx509_certs_append: req: %s: %d", argv[i], ret);
1098 }
1099
1100 ret = hx509_ocsp_request(context, reqcerts, pool, NULL, NULL, &req, nonce);
1101 if (ret)
1102 errx(1, "hx509_ocsp_request: req: %d", ret);
1103
1104 {
1105 FILE *f;
1106
1107 f = fopen(file, "w");
1108 if (f == NULL)
1109 abort();
1110
1111 fprintf(f,
1112 "POST %s HTTP/1.0\r\n"
1113 "Content-Type: application/ocsp-request\r\n"
1114 "Content-Length: %ld\r\n"
1115 "\r\n",
1116 url,
1117 (unsigned long)req.length);
1118 fwrite(req.data, req.length, 1, f);
1119 fclose(f);
1120 }
1121
1122 if (nonce)
1123 der_free_octet_string(nonce);
1124
1125 hx509_certs_free(&reqcerts);
1126 hx509_certs_free(&pool);
1127
1128 return 0;
1129 }
1130
1131 int
1132 ocsp_print(struct ocsp_print_options *opt, int argc, char **argv)
1133 {
1134 hx509_revoke_ocsp_print(context, argv[0], stdout);
1135 return 0;
1136 }
1137
1138 int
1139 revoke_print(struct revoke_print_options *opt, int argc, char **argv)
1140 {
1141 hx509_revoke_ctx revoke_ctx;
1142 int ret;
1143
1144 ret = hx509_revoke_init(context, &revoke_ctx);
1145 if (ret)
1146 errx(1, "hx509_revoke_init: %d", ret);
1147
1148 while(argc--) {
1149 char *s = *argv++;
1150
1151 if (strncmp(s, "crl:", 4) == 0) {
1152 s += 4;
1153
1154 ret = hx509_revoke_add_crl(context, revoke_ctx, s);
1155 if (ret)
1156 errx(1, "hx509_revoke_add_crl: %s: %d", s, ret);
1157
1158 } else if (strncmp(s, "ocsp:", 4) == 0) {
1159 s += 5;
1160
1161 ret = hx509_revoke_add_ocsp(context, revoke_ctx, s);
1162 if (ret)
1163 errx(1, "hx509_revoke_add_ocsp: %s: %d", s, ret);
1164
1165 } else {
1166 errx(1, "unknown option to verify: `%s'\n", s);
1167 }
1168 }
1169
1170 ret = hx509_revoke_print(context, revoke_ctx, stdout);
1171 if (ret)
1172 warnx("hx509_revoke_print: %d", ret);
1173
1174 return ret;
1175 }
1176
1177 /*
1178 *
1179 */
1180
1181 static int
1182 verify_o(hx509_context hxcontext, void *ctx, hx509_cert c)
1183 {
1184 heim_octet_string *os = ctx;
1185 time_t expiration;
1186 int ret;
1187
1188 ret = hx509_ocsp_verify(context, 0, c, 0,
1189 os->data, os->length, &expiration);
1190 if (ret) {
1191 char *s = hx509_get_error_string(hxcontext, ret);
1192 printf("ocsp_verify: %s: %d\n", s, ret);
1193 hx509_free_error_string(s);
1194 } else
1195 printf("expire: %d\n", (int)expiration);
1196
1197 return ret;
1198 }
1199
1200
1201 int
1202 ocsp_verify(struct ocsp_verify_options *opt, int argc, char **argv)
1203 {
1204 hx509_lock lock;
1205 hx509_certs certs;
1206 int ret, i;
1207 heim_octet_string os;
1208
1209 hx509_lock_init(context, &lock);
1210
1211 if (opt->ocsp_file_string == NULL)
1212 errx(1, "no ocsp file given");
1213
1214 ret = _hx509_map_file_os(opt->ocsp_file_string, &os);
1215 if (ret)
1216 err(1, "map_file: %s: %d", argv[0], ret);
1217
1218 ret = hx509_certs_init(context, "MEMORY:test-certs", 0, NULL, &certs);
1219 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
1220
1221 for (i = 0; i < argc; i++) {
1222 ret = hx509_certs_append(context, certs, lock, argv[i]);
1223 if (ret)
1224 hx509_err(context, 1, ret, "hx509_certs_append: %s", argv[i]);
1225 }
1226
1227 ret = hx509_certs_iter_f(context, certs, verify_o, &os);
1228
1229 hx509_certs_free(&certs);
1230 _hx509_unmap_file_os(&os);
1231 hx509_lock_free(lock);
1232
1233 return ret;
1234 }
1235
1236 static int
1237 read_private_key(const char *fn, hx509_private_key *key)
1238 {
1239 hx509_private_key *keys;
1240 hx509_certs certs;
1241 int ret;
1242
1243 *key = NULL;
1244
1245 ret = hx509_certs_init(context, fn, 0, NULL, &certs);
1246 if (ret)
1247 hx509_err(context, 1, ret, "hx509_certs_init: %s", fn);
1248
1249 ret = _hx509_certs_keys_get(context, certs, &keys);
1250 hx509_certs_free(&certs);
1251 if (ret)
1252 hx509_err(context, 1, ret, "hx509_certs_keys_get");
1253 if (keys[0] == NULL)
1254 errx(1, "no keys in key store: %s", fn);
1255
1256 *key = _hx509_private_key_ref(keys[0]);
1257 _hx509_certs_keys_free(context, keys);
1258
1259 return 0;
1260 }
1261
1262 static void
1263 get_key(const char *fn, const char *type, int optbits,
1264 hx509_private_key *signer)
1265 {
1266 int ret;
1267
1268 if (type) {
1269 BIGNUM *e;
1270 RSA *rsa;
1271 unsigned char *p0, *p;
1272 size_t len;
1273 int bits = 1024;
1274
1275 if (fn == NULL)
1276 errx(1, "no key argument, don't know here to store key");
1277
1278 if (strcasecmp(type, "rsa") != 0)
1279 errx(1, "can only handle rsa keys for now");
1280
1281 e = BN_new();
1282 BN_set_word(e, 0x10001);
1283
1284 if (optbits)
1285 bits = optbits;
1286
1287 rsa = RSA_new();
1288 if(rsa == NULL)
1289 errx(1, "RSA_new failed");
1290
1291 ret = RSA_generate_key_ex(rsa, bits, e, NULL);
1292 if(ret != 1)
1293 errx(1, "RSA_new failed");
1294
1295 BN_free(e);
1296
1297 len = i2d_RSAPrivateKey(rsa, NULL);
1298
1299 p0 = p = malloc(len);
1300 if (p == NULL)
1301 errx(1, "out of memory");
1302
1303 i2d_RSAPrivateKey(rsa, &p);
1304
1305 rk_dumpdata(fn, p0, len);
1306 memset(p0, 0, len);
1307 free(p0);
1308
1309 RSA_free(rsa);
1310
1311 } else if (fn == NULL)
1312 err(1, "no private key");
1313
1314 ret = read_private_key(fn, signer);
1315 if (ret)
1316 err(1, "read_private_key");
1317 }
1318
1319 int
1320 request_create(struct request_create_options *opt, int argc, char **argv)
1321 {
1322 heim_octet_string request;
1323 hx509_request req;
1324 int ret, i;
1325 hx509_private_key signer;
1326 SubjectPublicKeyInfo key;
1327 const char *outfile = argv[0];
1328
1329 memset(&key, 0, sizeof(key));
1330 memset(&signer, 0, sizeof(signer));
1331
1332 get_key(opt->key_string,
1333 opt->generate_key_string,
1334 opt->key_bits_integer,
1335 &signer);
1336
1337 hx509_request_init(context, &req);
1338
1339 if (opt->subject_string) {
1340 hx509_name name = NULL;
1341
1342 ret = hx509_parse_name(context, opt->subject_string, &name);
1343 if (ret)
1344 errx(1, "hx509_parse_name: %d\n", ret);
1345 hx509_request_set_name(context, req, name);
1346
1347 if (opt->verbose_flag) {
1348 char *s;
1349 hx509_name_to_string(name, &s);
1350 printf("%s\n", s);
1351 }
1352 hx509_name_free(&name);
1353 }
1354
1355 for (i = 0; i < opt->email_strings.num_strings; i++) {
1356 ret = _hx509_request_add_email(context, req,
1357 opt->email_strings.strings[i]);
1358 if (ret)
1359 hx509_err(context, 1, ret, "hx509_request_add_email");
1360 }
1361
1362 for (i = 0; i < opt->dnsname_strings.num_strings; i++) {
1363 ret = _hx509_request_add_dns_name(context, req,
1364 opt->dnsname_strings.strings[i]);
1365 if (ret)
1366 hx509_err(context, 1, ret, "hx509_request_add_dns_name");
1367 }
1368
1369
1370 ret = hx509_private_key2SPKI(context, signer, &key);
1371 if (ret)
1372 errx(1, "hx509_private_key2SPKI: %d\n", ret);
1373
1374 ret = hx509_request_set_SubjectPublicKeyInfo(context,
1375 req,
1376 &key);
1377 free_SubjectPublicKeyInfo(&key);
1378 if (ret)
1379 hx509_err(context, 1, ret, "hx509_request_set_SubjectPublicKeyInfo");
1380
1381 ret = _hx509_request_to_pkcs10(context,
1382 req,
1383 signer,
1384 &request);
1385 if (ret)
1386 hx509_err(context, 1, ret, "_hx509_request_to_pkcs10");
1387
1388 hx509_private_key_free(&signer);
1389 hx509_request_free(&req);
1390
1391 if (ret == 0)
1392 rk_dumpdata(outfile, request.data, request.length);
1393 der_free_octet_string(&request);
1394
1395 return 0;
1396 }
1397
1398 int
1399 request_print(struct request_print_options *opt, int argc, char **argv)
1400 {
1401 int ret, i;
1402
1403 printf("request print\n");
1404
1405 for (i = 0; i < argc; i++) {
1406 hx509_request req;
1407
1408 ret = _hx509_request_parse(context, argv[i], &req);
1409 if (ret)
1410 hx509_err(context, 1, ret, "parse_request: %s", argv[i]);
1411
1412 ret = _hx509_request_print(context, req, stdout);
1413 hx509_request_free(&req);
1414 if (ret)
1415 hx509_err(context, 1, ret, "Failed to print file %s", argv[i]);
1416 }
1417
1418 return 0;
1419 }
1420
1421 int
1422 info(void *opt, int argc, char **argv)
1423 {
1424
1425 ENGINE_add_conf_module();
1426
1427 {
1428 const RSA_METHOD *m = RSA_get_default_method();
1429 if (m != NULL)
1430 printf("rsa: %s\n", m->name);
1431 }
1432 {
1433 const DH_METHOD *m = DH_get_default_method();
1434 if (m != NULL)
1435 printf("dh: %s\n", m->name);
1436 }
1437 #ifdef HAVE_HCRYPTO_W_OPENSSL
1438 {
1439 printf("ecdsa: ECDSA_METHOD-not-export\n");
1440 }
1441 #else
1442 {
1443 printf("ecdsa: hcrypto null\n");
1444 }
1445 #endif
1446 {
1447 int ret = RAND_status();
1448 printf("rand: %s\n", ret == 1 ? "ok" : "not available");
1449 }
1450
1451 return 0;
1452 }
1453
1454 int
1455 random_data(void *opt, int argc, char **argv)
1456 {
1457 void *ptr;
1458 int len, ret;
1459
1460 len = parse_bytes(argv[0], "byte");
1461 if (len <= 0) {
1462 fprintf(stderr, "bad argument to random-data\n");
1463 return 1;
1464 }
1465
1466 ptr = malloc(len);
1467 if (ptr == NULL) {
1468 fprintf(stderr, "out of memory\n");
1469 return 1;
1470 }
1471
1472 ret = RAND_bytes(ptr, len);
1473 if (ret != 1) {
1474 free(ptr);
1475 fprintf(stderr, "did not get cryptographic strong random\n");
1476 return 1;
1477 }
1478
1479 fwrite(ptr, len, 1, stdout);
1480 fflush(stdout);
1481
1482 free(ptr);
1483
1484 return 0;
1485 }
1486
1487 int
1488 crypto_available(struct crypto_available_options *opt, int argc, char **argv)
1489 {
1490 AlgorithmIdentifier *val;
1491 unsigned int len, i;
1492 int ret, type = HX509_SELECT_ALL;
1493
1494 if (opt->type_string) {
1495 if (strcmp(opt->type_string, "all") == 0)
1496 type = HX509_SELECT_ALL;
1497 else if (strcmp(opt->type_string, "digest") == 0)
1498 type = HX509_SELECT_DIGEST;
1499 else if (strcmp(opt->type_string, "public-sig") == 0)
1500 type = HX509_SELECT_PUBLIC_SIG;
1501 else if (strcmp(opt->type_string, "secret") == 0)
1502 type = HX509_SELECT_SECRET_ENC;
1503 else
1504 errx(1, "unknown type: %s", opt->type_string);
1505 }
1506
1507 ret = hx509_crypto_available(context, type, NULL, &val, &len);
1508 if (ret)
1509 errx(1, "hx509_crypto_available");
1510
1511 for (i = 0; i < len; i++) {
1512 char *s;
1513 der_print_heim_oid (&val[i].algorithm, '.', &s);
1514 printf("%s\n", s);
1515 free(s);
1516 }
1517
1518 hx509_crypto_free_algs(val, len);
1519
1520 return 0;
1521 }
1522
1523 int
1524 crypto_select(struct crypto_select_options *opt, int argc, char **argv)
1525 {
1526 hx509_peer_info peer = NULL;
1527 AlgorithmIdentifier selected;
1528 int ret, type = HX509_SELECT_DIGEST;
1529 char *s;
1530
1531 if (opt->type_string) {
1532 if (strcmp(opt->type_string, "digest") == 0)
1533 type = HX509_SELECT_DIGEST;
1534 else if (strcmp(opt->type_string, "public-sig") == 0)
1535 type = HX509_SELECT_PUBLIC_SIG;
1536 else if (strcmp(opt->type_string, "secret") == 0)
1537 type = HX509_SELECT_SECRET_ENC;
1538 else
1539 errx(1, "unknown type: %s", opt->type_string);
1540 }
1541
1542 if (opt->peer_cmstype_strings.num_strings)
1543 peer_strings(context, &peer, &opt->peer_cmstype_strings);
1544
1545 ret = hx509_crypto_select(context, type, NULL, peer, &selected);
1546 if (ret)
1547 errx(1, "hx509_crypto_available");
1548
1549 der_print_heim_oid (&selected.algorithm, '.', &s);
1550 printf("%s\n", s);
1551 free(s);
1552 free_AlgorithmIdentifier(&selected);
1553
1554 hx509_peer_info_free(peer);
1555
1556 return 0;
1557 }
1558
1559 int
1560 hxtool_hex(struct hex_options *opt, int argc, char **argv)
1561 {
1562
1563 if (opt->decode_flag) {
1564 char buf[1024], buf2[1024], *p;
1565 ssize_t len;
1566
1567 while(fgets(buf, sizeof(buf), stdin) != NULL) {
1568 buf[strcspn(buf, "\r\n")] = '\0';
1569 p = buf;
1570 while(isspace(*(unsigned char *)p))
1571 p++;
1572 len = hex_decode(p, buf2, strlen(p));
1573 if (len < 0)
1574 errx(1, "hex_decode failed");
1575 if (fwrite(buf2, 1, len, stdout) != (size_t)len)
1576 errx(1, "fwrite failed");
1577 }
1578 } else {
1579 char buf[28], *p;
1580 ssize_t len;
1581
1582 while((len = fread(buf, 1, sizeof(buf), stdin)) != 0) {
1583 len = hex_encode(buf, len, &p);
1584 if (len < 0)
1585 continue;
1586 fprintf(stdout, "%s\n", p);
1587 free(p);
1588 }
1589 }
1590 return 0;
1591 }
1592
1593 struct cert_type_opt {
1594 int pkinit;
1595 };
1596
1597
1598 static int
1599 https_server(hx509_context contextp, hx509_ca_tbs tbs, struct cert_type_opt *opt)
1600 {
1601 return hx509_ca_tbs_add_eku(contextp, tbs, &asn1_oid_id_pkix_kp_serverAuth);
1602 }
1603
1604 static int
1605 https_client(hx509_context contextp, hx509_ca_tbs tbs, struct cert_type_opt *opt)
1606 {
1607 return hx509_ca_tbs_add_eku(contextp, tbs, &asn1_oid_id_pkix_kp_clientAuth);
1608 }
1609
1610 static int
1611 peap_server(hx509_context contextp, hx509_ca_tbs tbs, struct cert_type_opt *opt)
1612 {
1613 return hx509_ca_tbs_add_eku(contextp, tbs, &asn1_oid_id_pkix_kp_serverAuth);
1614 }
1615
1616 static int
1617 pkinit_kdc(hx509_context contextp, hx509_ca_tbs tbs, struct cert_type_opt *opt)
1618 {
1619 opt->pkinit++;
1620 return hx509_ca_tbs_add_eku(contextp, tbs, &asn1_oid_id_pkkdcekuoid);
1621 }
1622
1623 static int
1624 pkinit_client(hx509_context contextp, hx509_ca_tbs tbs, struct cert_type_opt *opt)
1625 {
1626 int ret;
1627
1628 opt->pkinit++;
1629
1630 ret = hx509_ca_tbs_add_eku(contextp, tbs, &asn1_oid_id_pkekuoid);
1631 if (ret)
1632 return ret;
1633
1634 ret = hx509_ca_tbs_add_eku(context, tbs, &asn1_oid_id_ms_client_authentication);
1635 if (ret)
1636 return ret;
1637
1638 return hx509_ca_tbs_add_eku(context, tbs, &asn1_oid_id_pkinit_ms_eku);
1639 }
1640
1641 static int
1642 email_client(hx509_context contextp, hx509_ca_tbs tbs, struct cert_type_opt *opt)
1643 {
1644 return hx509_ca_tbs_add_eku(contextp, tbs, &asn1_oid_id_pkix_kp_emailProtection);
1645 }
1646
1647 struct {
1648 const char *type;
1649 const char *desc;
1650 int (*eval)(hx509_context, hx509_ca_tbs, struct cert_type_opt *);
1651 } certtypes[] = {
1652 {
1653 "https-server",
1654 "Used for HTTPS server and many other TLS server certificate types",
1655 https_server
1656 },
1657 {
1658 "https-client",
1659 "Used for HTTPS client certificates",
1660 https_client
1661 },
1662 {
1663 "email-client",
1664 "Certificate will be use for email",
1665 email_client
1666 },
1667 {
1668 "pkinit-client",
1669 "Certificate used for Kerberos PK-INIT client certificates",
1670 pkinit_client
1671 },
1672 {
1673 "pkinit-kdc",
1674 "Certificates used for Kerberos PK-INIT KDC certificates",
1675 pkinit_kdc
1676 },
1677 {
1678 "peap-server",
1679 "Certificate used for Radius PEAP (Protected EAP)",
1680 peap_server
1681 }
1682 };
1683
1684 static void
1685 print_eval_types(FILE *out)
1686 {
1687 rtbl_t table;
1688 unsigned i;
1689
1690 table = rtbl_create();
1691 rtbl_add_column_by_id (table, 0, "Name", 0);
1692 rtbl_add_column_by_id (table, 1, "Description", 0);
1693
1694 for (i = 0; i < sizeof(certtypes)/sizeof(certtypes[0]); i++) {
1695 rtbl_add_column_entry_by_id(table, 0, certtypes[i].type);
1696 rtbl_add_column_entry_by_id(table, 1, certtypes[i].desc);
1697 }
1698
1699 rtbl_format (table, out);
1700 rtbl_destroy (table);
1701 }
1702
1703 static int
1704 eval_types(hx509_context contextp,
1705 hx509_ca_tbs tbs,
1706 const struct certificate_sign_options *opt)
1707 {
1708 struct cert_type_opt ctopt;
1709 int i;
1710 size_t j;
1711 int ret;
1712
1713 memset(&ctopt, 0, sizeof(ctopt));
1714
1715 for (i = 0; i < opt->type_strings.num_strings; i++) {
1716 const char *type = opt->type_strings.strings[i];
1717
1718 for (j = 0; j < sizeof(certtypes)/sizeof(certtypes[0]); j++) {
1719 if (strcasecmp(type, certtypes[j].type) == 0) {
1720 ret = (*certtypes[j].eval)(contextp, tbs, &ctopt);
1721 if (ret)
1722 hx509_err(contextp, 1, ret,
1723 "Failed to evaluate cert type %s", type);
1724 break;
1725 }
1726 }
1727 if (j >= sizeof(certtypes)/sizeof(certtypes[0])) {
1728 fprintf(stderr, "Unknown certificate type %s\n\n", type);
1729 fprintf(stderr, "Available types:\n");
1730 print_eval_types(stderr);
1731 exit(1);
1732 }
1733 }
1734
1735 for (i = 0; i < opt->pk_init_principal_strings.num_strings; i++) {
1736 const char *pk_init_princ = opt->pk_init_principal_strings.strings[i];
1737
1738 if (!ctopt.pkinit)
1739 errx(1, "pk-init principal given but no pk-init oid");
1740
1741 ret = hx509_ca_tbs_add_san_pkinit(contextp, tbs, pk_init_princ);
1742 if (ret)
1743 hx509_err(contextp, 1, ret, "hx509_ca_tbs_add_san_pkinit");
1744 }
1745
1746 if (opt->ms_upn_string) {
1747 if (!ctopt.pkinit)
1748 errx(1, "MS upn given but no pk-init oid");
1749
1750 ret = hx509_ca_tbs_add_san_ms_upn(contextp, tbs, opt->ms_upn_string);
1751 if (ret)
1752 hx509_err(contextp, 1, ret, "hx509_ca_tbs_add_san_ms_upn");
1753 }
1754
1755
1756 for (i = 0; i < opt->hostname_strings.num_strings; i++) {
1757 const char *hostname = opt->hostname_strings.strings[i];
1758
1759 ret = hx509_ca_tbs_add_san_hostname(contextp, tbs, hostname);
1760 if (ret)
1761 hx509_err(contextp, 1, ret, "hx509_ca_tbs_add_san_hostname");
1762 }
1763
1764 for (i = 0; i < opt->email_strings.num_strings; i++) {
1765 const char *email = opt->email_strings.strings[i];
1766
1767 ret = hx509_ca_tbs_add_san_rfc822name(contextp, tbs, email);
1768 if (ret)
1769 hx509_err(contextp, 1, ret, "hx509_ca_tbs_add_san_hostname");
1770
1771 ret = hx509_ca_tbs_add_eku(contextp, tbs,
1772 &asn1_oid_id_pkix_kp_emailProtection);
1773 if (ret)
1774 hx509_err(contextp, 1, ret, "hx509_ca_tbs_add_eku");
1775 }
1776
1777 if (opt->jid_string) {
1778 ret = hx509_ca_tbs_add_san_jid(contextp, tbs, opt->jid_string);
1779 if (ret)
1780 hx509_err(contextp, 1, ret, "hx509_ca_tbs_add_san_jid");
1781 }
1782
1783 return 0;
1784 }
1785
1786 int
1787 hxtool_ca(struct certificate_sign_options *opt, int argc, char **argv)
1788 {
1789 int ret;
1790 hx509_ca_tbs tbs;
1791 hx509_cert signer = NULL, cert = NULL;
1792 hx509_private_key private_key = NULL;
1793 hx509_private_key cert_key = NULL;
1794 hx509_name subject = NULL;
1795 SubjectPublicKeyInfo spki;
1796 int delta = 0;
1797
1798 memset(&spki, 0, sizeof(spki));
1799
1800 if (opt->ca_certificate_string == NULL && !opt->self_signed_flag)
1801 errx(1, "--ca-certificate argument missing (not using --self-signed)");
1802 if (opt->ca_private_key_string == NULL && opt->generate_key_string == NULL && opt->self_signed_flag)
1803 errx(1, "--ca-private-key argument missing (using --self-signed)");
1804 if (opt->certificate_string == NULL)
1805 errx(1, "--certificate argument missing");
1806
1807 if (opt->template_certificate_string) {
1808 if (opt->template_fields_string == NULL)
1809 errx(1, "--template-certificate not no --template-fields");
1810 }
1811
1812 if (opt->lifetime_string) {
1813 delta = parse_time(opt->lifetime_string, "day");
1814 if (delta < 0)
1815 errx(1, "Invalid lifetime: %s", opt->lifetime_string);
1816 }
1817
1818 if (opt->ca_certificate_string) {
1819 hx509_certs cacerts = NULL;
1820 hx509_query *q;
1821
1822 ret = hx509_certs_init(context, opt->ca_certificate_string, 0,
1823 NULL, &cacerts);
1824 if (ret)
1825 hx509_err(context, 1, ret,
1826 "hx509_certs_init: %s", opt->ca_certificate_string);
1827
1828 ret = hx509_query_alloc(context, &q);
1829 if (ret)
1830 errx(1, "hx509_query_alloc: %d", ret);
1831
1832 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
1833 if (!opt->issue_proxy_flag)
1834 hx509_query_match_option(q, HX509_QUERY_OPTION_KU_KEYCERTSIGN);
1835
1836 ret = hx509_certs_find(context, cacerts, q, &signer);
1837 hx509_query_free(context, q);
1838 hx509_certs_free(&cacerts);
1839 if (ret)
1840 hx509_err(context, 1, ret, "no CA certificate found");
1841 } else if (opt->self_signed_flag) {
1842 if (opt->generate_key_string == NULL
1843 && opt->ca_private_key_string == NULL)
1844 errx(1, "no signing private key");
1845
1846 if (opt->req_string)
1847 errx(1, "can't be self-signing and have a request at the same time");
1848 } else
1849 errx(1, "missing ca key");
1850
1851 if (opt->ca_private_key_string) {
1852
1853 ret = read_private_key(opt->ca_private_key_string, &private_key);
1854 if (ret)
1855 err(1, "read_private_key");
1856
1857 ret = hx509_private_key2SPKI(context, private_key, &spki);
1858 if (ret)
1859 errx(1, "hx509_private_key2SPKI: %d\n", ret);
1860
1861 if (opt->self_signed_flag)
1862 cert_key = private_key;
1863 }
1864
1865 if (opt->req_string) {
1866 hx509_request req;
1867
1868 ret = _hx509_request_parse(context, opt->req_string, &req);
1869 if (ret)
1870 hx509_err(context, 1, ret, "parse_request: %s", opt->req_string);
1871 ret = hx509_request_get_name(context, req, &subject);
1872 if (ret)
1873 hx509_err(context, 1, ret, "get name");
1874 ret = hx509_request_get_SubjectPublicKeyInfo(context, req, &spki);
1875 if (ret)
1876 hx509_err(context, 1, ret, "get spki");
1877 hx509_request_free(&req);
1878 }
1879
1880 if (opt->generate_key_string) {
1881 struct hx509_generate_private_context *keyctx;
1882
1883 ret = _hx509_generate_private_key_init(context,
1884 &asn1_oid_id_pkcs1_rsaEncryption,
1885 &keyctx);
1886 if (ret)
1887 hx509_err(context, 1, ret, "generate private key");
1888
1889 if (opt->issue_ca_flag)
1890 _hx509_generate_private_key_is_ca(context, keyctx);
1891
1892 if (opt->key_bits_integer)
1893 _hx509_generate_private_key_bits(context, keyctx,
1894 opt->key_bits_integer);
1895
1896 ret = _hx509_generate_private_key(context, keyctx,
1897 &cert_key);
1898 _hx509_generate_private_key_free(&keyctx);
1899 if (ret)
1900 hx509_err(context, 1, ret, "generate private key");
1901
1902 ret = hx509_private_key2SPKI(context, cert_key, &spki);
1903 if (ret)
1904 errx(1, "hx509_private_key2SPKI: %d\n", ret);
1905
1906 if (opt->self_signed_flag)
1907 private_key = cert_key;
1908 }
1909
1910 if (opt->certificate_private_key_string) {
1911 ret = read_private_key(opt->certificate_private_key_string, &cert_key);
1912 if (ret)
1913 err(1, "read_private_key for certificate");
1914 }
1915
1916 if (opt->subject_string) {
1917 if (subject)
1918 hx509_name_free(&subject);
1919 ret = hx509_parse_name(context, opt->subject_string, &subject);
1920 if (ret)
1921 hx509_err(context, 1, ret, "hx509_parse_name");
1922 }
1923
1924 /*
1925 *
1926 */
1927
1928 ret = hx509_ca_tbs_init(context, &tbs);
1929 if (ret)
1930 hx509_err(context, 1, ret, "hx509_ca_tbs_init");
1931
1932 if (opt->signature_algorithm_string) {
1933 const AlgorithmIdentifier *sigalg;
1934 if (strcasecmp(opt->signature_algorithm_string, "rsa-with-sha1") == 0)
1935 sigalg = hx509_signature_rsa_with_sha1();
1936 else if (strcasecmp(opt->signature_algorithm_string, "rsa-with-sha256") == 0)
1937 sigalg = hx509_signature_rsa_with_sha256();
1938 else
1939 errx(1, "unsupported sigature algorithm");
1940 hx509_ca_tbs_set_signature_algorithm(context, tbs, sigalg);
1941 }
1942
1943 if (opt->template_certificate_string) {
1944 hx509_cert template;
1945 hx509_certs tcerts;
1946 int flags;
1947
1948 ret = hx509_certs_init(context, opt->template_certificate_string, 0,
1949 NULL, &tcerts);
1950 if (ret)
1951 hx509_err(context, 1, ret,
1952 "hx509_certs_init: %s", opt->template_certificate_string);
1953
1954 ret = hx509_get_one_cert(context, tcerts, &template);
1955
1956 hx509_certs_free(&tcerts);
1957 if (ret)
1958 hx509_err(context, 1, ret, "no template certificate found");
1959
1960 flags = parse_units(opt->template_fields_string,
1961 hx509_ca_tbs_template_units(), "");
1962
1963 ret = hx509_ca_tbs_set_template(context, tbs, flags, template);
1964 if (ret)
1965 hx509_err(context, 1, ret, "hx509_ca_tbs_set_template");
1966
1967 hx509_cert_free(template);
1968 }
1969
1970 if (opt->serial_number_string) {
1971 heim_integer serialNumber;
1972
1973 ret = der_parse_hex_heim_integer(opt->serial_number_string,
1974 &serialNumber);
1975 if (ret)
1976 err(1, "der_parse_hex_heim_integer");
1977 ret = hx509_ca_tbs_set_serialnumber(context, tbs, &serialNumber);
1978 if (ret)
1979 hx509_err(context, 1, ret, "hx509_ca_tbs_init");
1980 der_free_heim_integer(&serialNumber);
1981 }
1982
1983 if (spki.subjectPublicKey.length) {
1984 ret = hx509_ca_tbs_set_spki(context, tbs, &spki);
1985 if (ret)
1986 hx509_err(context, 1, ret, "hx509_ca_tbs_set_spki");
1987 }
1988
1989 if (subject) {
1990 ret = hx509_ca_tbs_set_subject(context, tbs, subject);
1991 if (ret)
1992 hx509_err(context, 1, ret, "hx509_ca_tbs_set_subject");
1993 }
1994
1995 if (opt->crl_uri_string) {
1996 ret = hx509_ca_tbs_add_crl_dp_uri(context, tbs,
1997 opt->crl_uri_string, NULL);
1998 if (ret)
1999 hx509_err(context, 1, ret, "hx509_ca_tbs_add_crl_dp_uri");
2000 }
2001
2002 eval_types(context, tbs, opt);
2003
2004 if (opt->issue_ca_flag) {
2005 ret = hx509_ca_tbs_set_ca(context, tbs, opt->path_length_integer);
2006 if (ret)
2007 hx509_err(context, 1, ret, "hx509_ca_tbs_set_ca");
2008 }
2009 if (opt->issue_proxy_flag) {
2010 ret = hx509_ca_tbs_set_proxy(context, tbs, opt->path_length_integer);
2011 if (ret)
2012 hx509_err(context, 1, ret, "hx509_ca_tbs_set_proxy");
2013 }
2014 if (opt->domain_controller_flag) {
2015 hx509_ca_tbs_set_domaincontroller(context, tbs);
2016 if (ret)
2017 hx509_err(context, 1, ret, "hx509_ca_tbs_set_domaincontroller");
2018 }
2019
2020 if (delta) {
2021 ret = hx509_ca_tbs_set_notAfter_lifetime(context, tbs, delta);
2022 if (ret)
2023 hx509_err(context, 1, ret, "hx509_ca_tbs_set_notAfter_lifetime");
2024 }
2025
2026 if (opt->self_signed_flag) {
2027 ret = hx509_ca_sign_self(context, tbs, private_key, &cert);
2028 if (ret)
2029 hx509_err(context, 1, ret, "hx509_ca_sign_self");
2030 } else {
2031 ret = hx509_ca_sign(context, tbs, signer, &cert);
2032 if (ret)
2033 hx509_err(context, 1, ret, "hx509_ca_sign");
2034 }
2035
2036 if (cert_key) {
2037 ret = _hx509_cert_assign_key(cert, cert_key);
2038 if (ret)
2039 hx509_err(context, 1, ret, "_hx509_cert_assign_key");
2040 }
2041
2042 {
2043 hx509_certs certs;
2044
2045 ret = hx509_certs_init(context, opt->certificate_string,
2046 HX509_CERTS_CREATE, NULL, &certs);
2047 if (ret)
2048 hx509_err(context, 1, ret, "hx509_certs_init");
2049
2050 ret = hx509_certs_add(context, certs, cert);
2051 if (ret)
2052 hx509_err(context, 1, ret, "hx509_certs_add");
2053
2054 ret = hx509_certs_store(context, certs, 0, NULL);
2055 if (ret)
2056 hx509_err(context, 1, ret, "hx509_certs_store");
2057
2058 hx509_certs_free(&certs);
2059 }
2060
2061 if (subject)
2062 hx509_name_free(&subject);
2063 if (signer)
2064 hx509_cert_free(signer);
2065 hx509_cert_free(cert);
2066 free_SubjectPublicKeyInfo(&spki);
2067
2068 if (private_key != cert_key)
2069 hx509_private_key_free(&private_key);
2070 hx509_private_key_free(&cert_key);
2071
2072 hx509_ca_tbs_free(&tbs);
2073
2074 return 0;
2075 }
2076
2077 static int
2078 test_one_cert(hx509_context hxcontext, void *ctx, hx509_cert cert)
2079 {
2080 heim_octet_string sd, c;
2081 hx509_verify_ctx vctx = ctx;
2082 hx509_certs signer = NULL;
2083 heim_oid type;
2084 int ret;
2085
2086 if (_hx509_cert_private_key(cert) == NULL)
2087 return 0;
2088
2089 ret = hx509_cms_create_signed_1(context, 0, NULL, NULL, 0,
2090 NULL, cert, NULL, NULL, NULL, &sd);
2091 if (ret)
2092 errx(1, "hx509_cms_create_signed_1");
2093
2094 ret = hx509_cms_verify_signed(context, vctx, 0, sd.data, sd.length,
2095 NULL, NULL, &type, &c, &signer);
2096 free(sd.data);
2097 if (ret)
2098 hx509_err(context, 1, ret, "hx509_cms_verify_signed");
2099
2100 printf("create-signature verify-sigature done\n");
2101
2102 free(c.data);
2103
2104 return 0;
2105 }
2106
2107 int
2108 test_crypto(struct test_crypto_options *opt, int argc, char ** argv)
2109 {
2110 hx509_verify_ctx vctx;
2111 hx509_certs certs;
2112 hx509_lock lock;
2113 int i, ret;
2114
2115 hx509_lock_init(context, &lock);
2116 lock_strings(lock, &opt->pass_strings);
2117
2118 ret = hx509_certs_init(context, "MEMORY:test-crypto", 0, NULL, &certs);
2119 if (ret) hx509_err(context, 1, ret, "hx509_certs_init: MEMORY");
2120
2121 for (i = 0; i < argc; i++) {
2122 ret = hx509_certs_append(context, certs, lock, argv[i]);
2123 if (ret)
2124 hx509_err(context, 1, ret, "hx509_certs_append");
2125 }
2126
2127 ret = hx509_verify_init_ctx(context, &vctx);
2128 if (ret)
2129 hx509_err(context, 1, ret, "hx509_verify_init_ctx");
2130
2131 hx509_verify_attach_anchors(vctx, certs);
2132
2133 ret = hx509_certs_iter_f(context, certs, test_one_cert, vctx);
2134 if (ret)
2135 hx509_err(context, 1, ret, "hx509_cert_iter");
2136
2137 hx509_certs_free(&certs);
2138
2139 return 0;
2140 }
2141
2142 int
2143 statistic_print(struct statistic_print_options*opt, int argc, char **argv)
2144 {
2145 int type = 0;
2146
2147 if (stat_file_string == NULL)
2148 errx(1, "no stat file");
2149
2150 if (opt->type_integer)
2151 type = opt->type_integer;
2152
2153 hx509_query_unparse_stats(context, type, stdout);
2154 return 0;
2155 }
2156
2157 /*
2158 *
2159 */
2160
2161 int
2162 crl_sign(struct crl_sign_options *opt, int argc, char **argv)
2163 {
2164 hx509_crl crl;
2165 heim_octet_string os;
2166 hx509_cert signer = NULL;
2167 hx509_lock lock;
2168 int ret;
2169
2170 hx509_lock_init(context, &lock);
2171 lock_strings(lock, &opt->pass_strings);
2172
2173 ret = hx509_crl_alloc(context, &crl);
2174 if (ret)
2175 errx(1, "crl alloc");
2176
2177 if (opt->signer_string == NULL)
2178 errx(1, "signer missing");
2179
2180 {
2181 hx509_certs certs = NULL;
2182 hx509_query *q;
2183
2184 ret = hx509_certs_init(context, opt->signer_string, 0,
2185 NULL, &certs);
2186 if (ret)
2187 hx509_err(context, 1, ret,
2188 "hx509_certs_init: %s", opt->signer_string);
2189
2190 ret = hx509_query_alloc(context, &q);
2191 if (ret)
2192 hx509_err(context, 1, ret, "hx509_query_alloc: %d", ret);
2193
2194 hx509_query_match_option(q, HX509_QUERY_OPTION_PRIVATE_KEY);
2195
2196 ret = hx509_certs_find(context, certs, q, &signer);
2197 hx509_query_free(context, q);
2198 hx509_certs_free(&certs);
2199 if (ret)
2200 hx509_err(context, 1, ret, "no signer certificate found");
2201 }
2202
2203 if (opt->lifetime_string) {
2204 int delta;
2205
2206 delta = parse_time(opt->lifetime_string, "day");
2207 if (delta < 0)
2208 errx(1, "Invalid lifetime: %s", opt->lifetime_string);
2209
2210 hx509_crl_lifetime(context, crl, delta);
2211 }
2212
2213 {
2214 hx509_certs revoked = NULL;
2215 int i;
2216
2217 ret = hx509_certs_init(context, "MEMORY:revoked-certs", 0,
2218 NULL, &revoked);
2219 if (ret)
2220 hx509_err(context, 1, ret,
2221 "hx509_certs_init: MEMORY cert");
2222
2223 for (i = 0; i < argc; i++) {
2224 ret = hx509_certs_append(context, revoked, lock, argv[i]);
2225 if (ret)
2226 hx509_err(context, 1, ret, "hx509_certs_append: %s", argv[i]);
2227 }
2228
2229 hx509_crl_add_revoked_certs(context, crl, revoked);
2230 hx509_certs_free(&revoked);
2231 }
2232
2233 hx509_crl_sign(context, signer, crl, &os);
2234
2235 if (opt->crl_file_string)
2236 rk_dumpdata(opt->crl_file_string, os.data, os.length);
2237
2238 free(os.data);
2239
2240 hx509_crl_free(context, &crl);
2241 hx509_cert_free(signer);
2242 hx509_lock_free(lock);
2243
2244 return 0;
2245 }
2246
2247 /*
2248 *
2249 */
2250
2251 int
2252 help(void *opt, int argc, char **argv)
2253 {
2254 sl_slc_help(commands, argc, argv);
2255 return 0;
2256 }
2257
2258 int
2259 main(int argc, char **argv)
2260 {
2261 int ret, optidx = 0;
2262
2263 setprogname (argv[0]);
2264
2265 if(getarg(args, num_args, argc, argv, &optidx))
2266 usage(1);
2267 if(help_flag)
2268 usage(0);
2269 if(version_flag) {
2270 print_version(NULL);
2271 exit(0);
2272 }
2273 argv += optidx;
2274 argc -= optidx;
2275
2276 if (argc == 0)
2277 usage(1);
2278
2279 ret = hx509_context_init(&context);
2280 if (ret)
2281 errx(1, "hx509_context_init failed with %d", ret);
2282
2283 if (stat_file_string)
2284 hx509_query_statistic_file(context, stat_file_string);
2285
2286 ret = sl_command(commands, argc, argv);
2287 if(ret == -1)
2288 warnx ("unrecognized command: %s", argv[0]);
2289
2290 hx509_context_free(&context);
2291
2292 return ret;
2293 }