4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
21 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <cryptoutil.h>
35 kc_create(int argc
, char *argv
[])
41 extern char *optarg_av
;
42 char *filename
= NULL
;
43 int ocsp_set_attr
= 0;
44 boolean_t crl_set_attr
= 0;
45 KMF_POLICY_RECORD plc
;
47 (void) memset(&plc
, 0, sizeof (KMF_POLICY_RECORD
));
49 while ((opt
= getopt_av(argc
, argv
,
53 "e:(ignore-unknown-eku)"
54 "a:(ignore-trust-anchor)"
55 "v:(validity-adjusttime)"
60 "r:(ocsp-use-cert-responder)"
61 "T:(ocsp-response-lifetime)"
62 "R:(ocsp-ignore-response-sign)"
63 "n:(ocsp-responder-cert-name)"
64 "A:(ocsp-responder-cert-serial)"
65 "c:(crl-basefilename)"
69 "S:(crl-ignore-crl-sign)"
70 "D:(crl-ignore-crl-date)"
72 "M:(mapper-directory)"
77 "O:(ekuoids)")) != EOF
) {
80 filename
= get_string(optarg_av
, &rv
);
81 if (filename
== NULL
) {
82 (void) fprintf(stderr
,
83 gettext("Error dbfile input.\n"));
87 plc
.name
= get_string(optarg_av
, &rv
);
88 if (plc
.name
== NULL
) {
89 (void) fprintf(stderr
,
90 gettext("Error policy name.\n"));
94 plc
.ignore_date
= get_boolean(optarg_av
);
95 if (plc
.ignore_date
== -1) {
96 (void) fprintf(stderr
,
97 gettext("Error boolean input.\n"));
102 plc
.ignore_unknown_ekus
=
103 get_boolean(optarg_av
);
104 if (plc
.ignore_unknown_ekus
== -1) {
105 (void) fprintf(stderr
,
106 gettext("Error boolean input.\n"));
111 plc
.ignore_trust_anchor
=
112 get_boolean(optarg_av
);
113 if (plc
.ignore_trust_anchor
== -1) {
114 (void) fprintf(stderr
,
115 gettext("Error boolean input.\n"));
120 plc
.validity_adjusttime
=
121 get_string(optarg_av
, &rv
);
122 if (plc
.validity_adjusttime
== NULL
) {
123 (void) fprintf(stderr
,
124 gettext("Error time input.\n"));
127 /* for syntax checking */
129 plc
.validity_adjusttime
,
131 (void) fprintf(stderr
,
132 gettext("Error time "
139 plc
.ta_name
= get_string(optarg_av
, &rv
);
140 if (plc
.ta_name
== NULL
) {
141 (void) fprintf(stderr
,
142 gettext("Error name input.\n"));
143 } else if (strcasecmp(plc
.ta_name
,
146 /* for syntax checking */
147 if (kmf_dn_parser(plc
.ta_name
,
149 (void) fprintf(stderr
,
150 gettext("Error name "
159 plc
.ta_serial
= get_string(optarg_av
, &rv
);
160 if (plc
.ta_serial
== NULL
) {
161 (void) fprintf(stderr
,
162 gettext("Error serial input.\n"));
164 uchar_t
*bytes
= NULL
;
167 ret
= kmf_hexstr_to_bytes(
168 (uchar_t
*)plc
.ta_serial
,
170 if (ret
!= KMF_OK
|| bytes
== NULL
) {
171 (void) fprintf(stderr
,
172 gettext("serial number "
173 "must be specified as a "
184 plc
.VAL_OCSP_RESPONDER_URI
=
185 get_string(optarg_av
, &rv
);
186 if (plc
.VAL_OCSP_RESPONDER_URI
== NULL
) {
187 (void) fprintf(stderr
, gettext(
188 "Error responder input.\n"));
195 get_string(optarg_av
, &rv
);
196 if (plc
.VAL_OCSP_PROXY
== NULL
) {
197 (void) fprintf(stderr
,
198 gettext("Error proxy input.\n"));
204 plc
.VAL_OCSP_URI_FROM_CERT
=
205 get_boolean(optarg_av
);
206 if (plc
.VAL_OCSP_URI_FROM_CERT
== -1) {
207 (void) fprintf(stderr
,
208 gettext("Error boolean input.\n"));
215 plc
.VAL_OCSP_RESP_LIFETIME
=
216 get_string(optarg_av
, &rv
);
217 if (plc
.VAL_OCSP_RESP_LIFETIME
== NULL
) {
218 (void) fprintf(stderr
,
219 gettext("Error time input.\n"));
222 /* for syntax checking */
224 plc
.VAL_OCSP_RESP_LIFETIME
,
226 (void) fprintf(stderr
,
227 gettext("Error time "
236 plc
.VAL_OCSP_IGNORE_RESP_SIGN
=
237 get_boolean(optarg_av
);
238 if (plc
.VAL_OCSP_IGNORE_RESP_SIGN
== -1) {
239 (void) fprintf(stderr
,
240 gettext("Error boolean input.\n"));
247 plc
.VAL_OCSP_RESP_CERT_NAME
=
248 get_string(optarg_av
, &rv
);
249 if (plc
.VAL_OCSP_RESP_CERT_NAME
== NULL
) {
250 (void) fprintf(stderr
,
251 gettext("Error name input.\n"));
253 KMF_X509_NAME respDN
;
254 /* for syntax checking */
256 plc
.VAL_OCSP_RESP_CERT_NAME
,
257 &respDN
) != KMF_OK
) {
258 (void) fprintf(stderr
,
259 gettext("Error name "
263 kmf_free_dn(&respDN
);
269 plc
.VAL_OCSP_RESP_CERT_SERIAL
=
270 get_string(optarg_av
, &rv
);
271 if (plc
.VAL_OCSP_RESP_CERT_SERIAL
== NULL
) {
272 (void) fprintf(stderr
,
273 gettext("Error serial input.\n"));
275 uchar_t
*bytes
= NULL
;
278 ret
= kmf_hexstr_to_bytes((uchar_t
*)
279 plc
.VAL_OCSP_RESP_CERT_SERIAL
,
281 if (ret
!= KMF_OK
|| bytes
== NULL
) {
282 (void) fprintf(stderr
,
283 gettext("serial number "
284 "must be specified as a "
297 plc
.VAL_CRL_BASEFILENAME
=
298 get_string(optarg_av
, &rv
);
299 if (plc
.VAL_CRL_BASEFILENAME
== NULL
) {
300 (void) fprintf(stderr
,
301 gettext("Error boolean input.\n"));
307 plc
.VAL_CRL_DIRECTORY
=
308 get_string(optarg_av
, &rv
);
309 if (plc
.VAL_CRL_DIRECTORY
== NULL
) {
310 (void) fprintf(stderr
,
311 gettext("Error boolean input.\n"));
317 plc
.VAL_CRL_GET_URI
= get_boolean(optarg_av
);
318 if (plc
.VAL_CRL_GET_URI
== -1) {
319 (void) fprintf(stderr
,
320 gettext("Error boolean input.\n"));
327 plc
.VAL_CRL_PROXY
= get_string(optarg_av
, &rv
);
328 if (plc
.VAL_CRL_PROXY
== NULL
) {
329 (void) fprintf(stderr
,
330 gettext("Error proxy input.\n"));
336 plc
.VAL_CRL_IGNORE_SIGN
=
337 get_boolean(optarg_av
);
338 if (plc
.VAL_CRL_IGNORE_SIGN
== -1) {
339 (void) fprintf(stderr
,
340 gettext("Error boolean input.\n"));
347 plc
.VAL_CRL_IGNORE_DATE
=
348 get_boolean(optarg_av
);
349 if (plc
.VAL_CRL_IGNORE_DATE
== -1) {
350 (void) fprintf(stderr
,
351 gettext("Error boolean input.\n"));
358 plc
.ku_bits
= parseKUlist(optarg_av
);
359 if (plc
.ku_bits
== 0) {
360 (void) fprintf(stderr
, gettext(
361 "Error keyusage input.\n"));
366 if (parseEKUNames(optarg_av
, &plc
) != 0) {
367 (void) fprintf(stderr
,
368 gettext("Error EKU input.\n"));
373 if (parseEKUOIDs(optarg_av
, &plc
) != 0) {
374 (void) fprintf(stderr
,
375 gettext("Error EKU OID input.\n"));
380 plc
.mapper
.mapname
= get_string(optarg_av
, &rv
);
381 if (plc
.mapper
.mapname
== NULL
) {
382 (void) fprintf(stderr
,
383 gettext("Error mapper-name "
388 plc
.mapper
.dir
= get_string(optarg_av
, &rv
);
389 if (plc
.mapper
.dir
== NULL
) {
390 (void) fprintf(stderr
,
391 gettext("Error mapper-dir "
396 plc
.mapper
.pathname
= get_string(optarg_av
,
398 if (plc
.mapper
.pathname
== NULL
) {
399 (void) fprintf(stderr
,
400 gettext("Error mapper-pathname "
405 plc
.mapper
.options
= get_string(optarg_av
, &rv
);
406 if (plc
.mapper
.options
== NULL
) {
407 (void) fprintf(stderr
,
408 gettext("Error mapper-options "
413 (void) fprintf(stderr
,
414 gettext("Error input option.\n"));
423 /* No additional args allowed. */
426 (void) fprintf(stderr
,
427 gettext("Error input option\n"));
432 if (filename
== NULL
) {
433 filename
= strdup(KMF_DEFAULT_POLICY_FILE
);
434 if (filename
== NULL
) {
441 * Must have a policy name. The policy name can not be default
442 * if using the default policy file.
444 if (plc
.name
== NULL
) {
445 (void) fprintf(stderr
,
446 gettext("You must specify a policy name\n"));
449 } else if (strcmp(filename
, KMF_DEFAULT_POLICY_FILE
) == 0 &&
450 strcmp(plc
.name
, KMF_DEFAULT_POLICY_NAME
) == 0) {
451 (void) fprintf(stderr
,
452 gettext("Can not create a default policy in the default "
459 * If the policy file exists and the policy is in the policy file
460 * already, we will not create it again.
462 if (access(filename
, R_OK
) == 0) {
463 POLICY_LIST
*plclist
= NULL
, *pnode
;
466 rv
= load_policies(filename
, &plclist
);
471 while (pnode
!= NULL
&& !found
) {
472 if (strcmp(plc
.name
, pnode
->plc
.name
) == 0)
476 free_policy_list(plclist
);
479 (void) fprintf(stderr
,
480 gettext("Could not create policy \"%s\" - exists "
481 "already\n"), plc
.name
);
488 * If any OCSP attribute is set, turn on the OCSP checking flag.
489 * Also set "has_resp_cert" to be true, if the responder cert
492 if (ocsp_set_attr
> 0)
493 plc
.revocation
|= KMF_REVOCATION_METHOD_OCSP
;
495 if (plc
.VAL_OCSP_RESP_CERT
.name
!= NULL
&&
496 plc
.VAL_OCSP_RESP_CERT
.serial
!= NULL
) {
497 plc
.VAL_OCSP
.has_resp_cert
= B_TRUE
;
501 * Setting mapper-name (with optional mapper-dir) and mapper-pathname is
502 * mutually exclusive. Also, you cannot set options only, you need the
503 * name or pathname, and you can set the directory only with the name,
506 if ((plc
.mapper
.mapname
!= NULL
&& plc
.mapper
.pathname
!= NULL
) ||
507 (plc
.mapper
.dir
!= NULL
&& plc
.mapper
.pathname
!= NULL
) ||
508 (plc
.mapper
.dir
!= NULL
&& plc
.mapper
.mapname
== NULL
) ||
509 (plc
.mapper
.options
!= NULL
&& plc
.mapper
.mapname
== NULL
&&
510 plc
.mapper
.pathname
== NULL
)) {
511 (void) fprintf(stderr
,
512 gettext("Error in mapper input options\n"));
518 * If any CRL attribute is set, turn on the CRL checking flag.
520 if (crl_set_attr
> 0)
521 plc
.revocation
|= KMF_REVOCATION_METHOD_CRL
;
524 * Does a sanity check on the new policy.
526 ret
= kmf_verify_policy(&plc
);
528 print_sanity_error(ret
);
529 rv
= KC_ERR_ADD_POLICY
;
536 ret
= kmf_add_policy_to_db(&plc
, filename
, B_FALSE
);
538 (void) fprintf(stderr
,
539 gettext("Error adding policy to database: 0x%04x\n"), ret
);
540 rv
= KC_ERR_ADD_POLICY
;
544 if (filename
!= NULL
)
547 kmf_free_policy_record(&plc
);