From b49dd2f6b0b2eb145eeca35c14912600eff2029f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Wed, 27 Jul 2005 11:45:55 +0000 Subject: [PATCH] switch to slc git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15769 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/hx509/Makefile.am | 14 +++- lib/hx509/hxtool-commands.in | 121 ++++++++++++++++++++++++++++++ lib/hx509/hxtool.c | 139 ++++++++++++----------------------- 3 files changed, 180 insertions(+), 94 deletions(-) create mode 100644 lib/hx509/hxtool-commands.in diff --git a/lib/hx509/Makefile.am b/lib/hx509/Makefile.am index 4b640d7a0..e916d5842 100644 --- a/lib/hx509/Makefile.am +++ b/lib/hx509/Makefile.am @@ -39,9 +39,17 @@ $(srcdir)/hx509-private.h: include_HEADERS = hx509.h hx509-protos.h hx509_err.h +SLC = $(top_builddir)/lib/sl/slc + noinst_PROGRAMS = hxtool -hxtool_SOURCES = hxtool.c +hxtool-commands.c hxtool-commands.h: hxtool-commands.in $(SLC) + $(SLC) $(srcdir)/hxtool-commands.in + +hxtool_SOURCES = \ + hxtool.c \ + hxtool-commands.c \ + hxtool-commands.h hxtool_CPPFLAGS = $(INCLUDE_des) hxtool_LDADD = libhx509.la $(LIB_roken) ../sl/libsl.la $(LIB_des) @@ -49,7 +57,9 @@ hxtool_LDFLAGS = -pthread EXTRA_DIST = hx509_err.et -CLEANFILES = hx509_err.c hx509_err.h $(TESTS) +CLEANFILES = hx509_err.c hx509_err.h $(TESTS) \ + hxtool-commands.c hxtool-commands.h + # # regression tests diff --git a/lib/hx509/hxtool-commands.in b/lib/hx509/hxtool-commands.in new file mode 100644 index 000000000..2f3b50325 --- /dev/null +++ b/lib/hx509/hxtool-commands.in @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2005 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* $Id$ */ + +command = { + name = "cms-create-sd" + option = { + long = "certificate" + short = "c" + type = "string" + argument = "certificate-store" + help = "certificate store to pull certificates from" + } + option = { + long = "signer" + short = "s" + type = "string" + argument = "certificate-name" + help = "certificate to sign with" + } + min_args="3" + argument="in-file out-file cert ..." + help = "Wrap a file within a SignedData object." +} +command = { + name = "cms-verify-sd" + option = { + long = "certificate" + short = "c" + type = "string" + argument = "certificate-store" + help = "certificate store to pull certificates from" + } + min_args="2" + argument="in-file out-file cert ..." + help = "Verify a file within a SignedData object" +} +command = { + name = "cms-unenvelope" + min_args="2" + argument="in-file out-file" + help = "Unenvelope a file containing a EnvelopedData object" +} +command = { + name = "cms-envelope" + function = "cms_create_enveloped" + min_args="3" + argument="certificate in-file out-file" + help = "Envelope a file containing a EnvelopedData object" +} +command = { + name = "verify" + function = "pcert_verify" + option = { + long = "verbose" + short = "v" + type = "flag" + help = "verbose logging" + } + argument = "cert:foo chain:cert1 chain:cert2 anchor:anchor1 anchor:anchor2" + help = "Verify certificate chain" +} +command = { + name = "print" + function = "pcert_print" + min_args="1" + argument="certificate ..." + help = "Print certificates" +} +command = { + name = "validate" + function = "pcert_validate" + min_args="1" + argument="certificate ..." + help = "Validate content of certificates" +} +command = { + name = "pkcs11" + function = "pcert_pkcs11" + min_args="1" + argument="certificate ..." + help = "Validate content of certificates" +} +command = { + name = "help" + name = "?" + argument = "[command]" + min_args = "0" + max_args = "1" + help = "Help! I need somebody." +} diff --git a/lib/hx509/hxtool.c b/lib/hx509/hxtool.c index 3f9cd8ce2..16257429f 100644 --- a/lib/hx509/hxtool.c +++ b/lib/hx509/hxtool.c @@ -34,6 +34,7 @@ #include "hx_locl.h" RCSID("$Id$"); +#include #include static int version_flag; @@ -52,8 +53,8 @@ usage(int code) exit(code); } -static int -cms_verify_sd(int argc, char **argv) +int +cms_verify_sd(struct cms_verify_sd_options *opt, int argc, char **argv) { int ret; hx509_verify_ctx ctx = NULL; @@ -65,12 +66,6 @@ cms_verify_sd(int argc, char **argv) size_t sz; void *p; - argc--; - argv++; - - if (argc < 2) - errx(1, "argc < 2"); - printf("cms verify signed data\n"); ret = _hx509_map_file(argv[0], &p, &sz); @@ -114,8 +109,8 @@ cms_verify_sd(int argc, char **argv) return 0; } -static int -cms_create_sd(int argc, char **argv) +int +cms_create_sd(struct cms_create_sd_options *opt, int argc, char **argv) { const heim_oid *contentType; heim_octet_string o; @@ -129,9 +124,6 @@ cms_create_sd(int argc, char **argv) contentType = oid_id_pkcs7_data(); - argc--; - argv++; - if (argc < 3) errx(1, "argc < 3"); @@ -175,8 +167,8 @@ cms_create_sd(int argc, char **argv) return 0; } -static int -cms_unenvelope(int argc, char **argv) +int +cms_unenvelope(void *opt, int argc, char **argv) { heim_oid contentType = { 0, NULL }; heim_octet_string o; @@ -186,9 +178,6 @@ cms_unenvelope(int argc, char **argv) int ret; hx509_lock lock; - argc--; - argv++; - if (argc != 3) errx(1, "argc != 3"); @@ -223,8 +212,8 @@ cms_unenvelope(int argc, char **argv) return 0; } -static int -cms_create_enveloped(int argc, char **argv) +int +cms_create_enveloped(void *opt, int argc, char **argv) { heim_octet_string o; heim_oid contentType = { 0, NULL }; @@ -235,9 +224,6 @@ cms_create_enveloped(int argc, char **argv) size_t sz; void *p; - argc--; - argv++; - if (argc != 3) errx(1, "argc ! = 3"); @@ -285,9 +271,6 @@ validate_print(int argc, char **argv, int flags) hx509_certs certs; hx509_lock lock; - argc--; - argv++; - if (argc < 1) errx(1, "argc"); @@ -314,14 +297,14 @@ validate_print(int argc, char **argv, int flags) return 0; } -static int -pcert_print(int argc, char **argv) +int +pcert_print(void *opt, int argc, char **argv) { return validate_print(argc, argv, HX509_VALIDATE_F_VERBOSE); } -static int -pcert_validate(int argc, char **argv) +int +pcert_validate(void *opt, int argc, char **argv) { return validate_print(argc, argv, HX509_VALIDATE_F_VALIDATE); } @@ -346,17 +329,14 @@ verify_f(void *ctx, hx509_cert c) return ret; } -static int -pcert_verify(int argc, char **argv) +int +pcert_verify(struct verify_options *opt, int argc, char **argv) { hx509_certs anchors, chain, certs; hx509_verify_ctx ctx; struct verify v; int ret; - argc--; - argv++; - ret = hx509_verify_init_ctx(&ctx); ret = hx509_certs_init("MEMORY:anchors", 0, NULL, &anchors); ret = hx509_certs_init("MEMORY:chain", 0, NULL, &chain); @@ -411,14 +391,11 @@ pcert_verify(int argc, char **argv) return ret; } -static int -pcert_pkcs11(int argc, char **argv) +int +pcert_pkcs11(void *opt, int argc, char **argv) { int ret; - argc--; - argv++; - if (argc < 1) errx(1, "argc"); @@ -431,59 +408,37 @@ pcert_pkcs11(int argc, char **argv) return 0; } -static int help(int, char **); - -static SL_cmd cmds[] = { - { "cms-create-sd", - cms_create_sd, - "in-file out-file cert ...", - "create signed data" - }, - { "cms-verify-sd", - cms_verify_sd, - "file cert ...", - "verify signed data" - }, - { "cms-unenvelope", - cms_unenvelope, - "cert key in-file out-file", - "unenvelope data" - }, - { "cms-create-envelope", - cms_create_enveloped, - "cert in-file out-file", - "envelope data" - }, - - { "print", - pcert_print, - "cert ...", - "print certificates" - }, - { "validate", - pcert_validate, - "cert ...", - "validate certificates" - }, - { "verify", - pcert_verify, - "cert:foo chain:cert1 chain:cert2 anchor:anchor1 anchor:anchor2", - "verify certificates" - }, - { "pkcs11", - pcert_pkcs11, - "...", - "deal with pkcs11 devices" - }, - { "help", help, "help" }, - { "?" }, - { NULL } -}; - int -help(int argc, char **argv) +help(void *opt, int argc, char **argv) { - sl_help(cmds, argc, argv); + if(argc == 0) { + sl_help(commands, 1, argv - 1 /* XXX */); + } else { + SL_cmd *c = sl_match (commands, argv[0], 0); + if(c == NULL) { + fprintf (stderr, "No such command: %s. " + "Try \"help\" for a list of commands\n", + argv[0]); + } else { + if(c->func) { + char *fake[] = { NULL, "--help", NULL }; + fake[0] = argv[0]; + (*c->func)(2, fake); + fprintf(stderr, "\n"); + } + if(c->help && *c->help) + fprintf (stderr, "%s\n", c->help); + if((++c)->name && c->func == NULL) { + int f = 0; + fprintf (stderr, "Synonyms:"); + while (c->name && c->func == NULL) { + fprintf (stderr, "%s%s", f ? ", " : " ", (c++)->name); + f = 1; + } + fprintf (stderr, "\n"); + } + } + } return 0; } @@ -508,7 +463,7 @@ main(int argc, char **argv) if (argc == 0) usage(1); - ret = sl_command(cmds, argc, argv); + ret = sl_command(commands, argc, argv); if(ret == -1) warnx ("unrecognized command: %s", argv[0]);