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