1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM *
4*9781SMoriah.Waterland@Sun.COM * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM *
8*9781SMoriah.Waterland@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM *
13*9781SMoriah.Waterland@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM *
19*9781SMoriah.Waterland@Sun.COM * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM */
21*9781SMoriah.Waterland@Sun.COM
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24*9781SMoriah.Waterland@Sun.COM * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM */
26*9781SMoriah.Waterland@Sun.COM
27*9781SMoriah.Waterland@Sun.COM
28*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
29*9781SMoriah.Waterland@Sun.COM #include <stdarg.h>
30*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
31*9781SMoriah.Waterland@Sun.COM #include <string.h>
32*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
33*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
34*9781SMoriah.Waterland@Sun.COM #include <signal.h>
35*9781SMoriah.Waterland@Sun.COM #include <locale.h>
36*9781SMoriah.Waterland@Sun.COM #include <sys/param.h>
37*9781SMoriah.Waterland@Sun.COM #include <openssl/bio.h>
38*9781SMoriah.Waterland@Sun.COM #include <openssl/x509v3.h>
39*9781SMoriah.Waterland@Sun.COM #include <openssl/ui.h>
40*9781SMoriah.Waterland@Sun.COM
41*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
42*9781SMoriah.Waterland@Sun.COM #include <libinst.h>
43*9781SMoriah.Waterland@Sun.COM #include <pkgerr.h>
44*9781SMoriah.Waterland@Sun.COM #include <keystore.h>
45*9781SMoriah.Waterland@Sun.COM #include "pkgadm.h"
46*9781SMoriah.Waterland@Sun.COM #include "pkgadm_msgs.h"
47*9781SMoriah.Waterland@Sun.COM
48*9781SMoriah.Waterland@Sun.COM typedef enum {
49*9781SMoriah.Waterland@Sun.COM VerifyFailed,
50*9781SMoriah.Waterland@Sun.COM Accept,
51*9781SMoriah.Waterland@Sun.COM Reject
52*9781SMoriah.Waterland@Sun.COM } VerifyStatus;
53*9781SMoriah.Waterland@Sun.COM
54*9781SMoriah.Waterland@Sun.COM static VerifyStatus verify_trust(X509 *);
55*9781SMoriah.Waterland@Sun.COM static boolean_t is_ca_cert(X509 *);
56*9781SMoriah.Waterland@Sun.COM
57*9781SMoriah.Waterland@Sun.COM /*
58*9781SMoriah.Waterland@Sun.COM * Name: addcert
59*9781SMoriah.Waterland@Sun.COM * Desc: Imports a user certificate into the keystore, along with a
60*9781SMoriah.Waterland@Sun.COM * private key.
61*9781SMoriah.Waterland@Sun.COM * Returns: 0 on success, non-zero otherwise.
62*9781SMoriah.Waterland@Sun.COM */
63*9781SMoriah.Waterland@Sun.COM int
addcert(int argc,char ** argv)64*9781SMoriah.Waterland@Sun.COM addcert(int argc, char **argv)
65*9781SMoriah.Waterland@Sun.COM {
66*9781SMoriah.Waterland@Sun.COM int i;
67*9781SMoriah.Waterland@Sun.COM char keystore_file[MAXPATHLEN] = "";
68*9781SMoriah.Waterland@Sun.COM char *keystore_base = NULL;
69*9781SMoriah.Waterland@Sun.COM char *homedir;
70*9781SMoriah.Waterland@Sun.COM char *passarg = NULL;
71*9781SMoriah.Waterland@Sun.COM char *import_passarg = NULL;
72*9781SMoriah.Waterland@Sun.COM char *altroot = NULL;
73*9781SMoriah.Waterland@Sun.COM char *prog = NULL;
74*9781SMoriah.Waterland@Sun.COM char *alias = NULL;
75*9781SMoriah.Waterland@Sun.COM char *infile = NULL;
76*9781SMoriah.Waterland@Sun.COM char *inkeyfile = NULL;
77*9781SMoriah.Waterland@Sun.COM keystore_encoding_format_t informat = NULL;
78*9781SMoriah.Waterland@Sun.COM char *informat_str = NULL;
79*9781SMoriah.Waterland@Sun.COM int ret = 1;
80*9781SMoriah.Waterland@Sun.COM boolean_t trusted = B_FALSE;
81*9781SMoriah.Waterland@Sun.COM boolean_t implicit_trust = B_FALSE;
82*9781SMoriah.Waterland@Sun.COM
83*9781SMoriah.Waterland@Sun.COM FILE *certfile = NULL;
84*9781SMoriah.Waterland@Sun.COM FILE *keyfile = NULL;
85*9781SMoriah.Waterland@Sun.COM X509 *cert = NULL;
86*9781SMoriah.Waterland@Sun.COM STACK_OF(X509) *trustcerts = NULL;
87*9781SMoriah.Waterland@Sun.COM EVP_PKEY *key = NULL;
88*9781SMoriah.Waterland@Sun.COM PKG_ERR *err = NULL;
89*9781SMoriah.Waterland@Sun.COM keystore_handle_t keystore = NULL;
90*9781SMoriah.Waterland@Sun.COM
91*9781SMoriah.Waterland@Sun.COM while ((i = getopt(argc, argv, ":a:k:e:f:n:P:p:R:ty")) != EOF) {
92*9781SMoriah.Waterland@Sun.COM switch (i) {
93*9781SMoriah.Waterland@Sun.COM case 'a':
94*9781SMoriah.Waterland@Sun.COM prog = optarg;
95*9781SMoriah.Waterland@Sun.COM break;
96*9781SMoriah.Waterland@Sun.COM case 'k':
97*9781SMoriah.Waterland@Sun.COM keystore_base = optarg;
98*9781SMoriah.Waterland@Sun.COM break;
99*9781SMoriah.Waterland@Sun.COM case 'e':
100*9781SMoriah.Waterland@Sun.COM inkeyfile = optarg;
101*9781SMoriah.Waterland@Sun.COM break;
102*9781SMoriah.Waterland@Sun.COM case 'f':
103*9781SMoriah.Waterland@Sun.COM informat_str = optarg;
104*9781SMoriah.Waterland@Sun.COM break;
105*9781SMoriah.Waterland@Sun.COM case 'n':
106*9781SMoriah.Waterland@Sun.COM alias = optarg;
107*9781SMoriah.Waterland@Sun.COM break;
108*9781SMoriah.Waterland@Sun.COM case 'P':
109*9781SMoriah.Waterland@Sun.COM passarg = optarg;
110*9781SMoriah.Waterland@Sun.COM break;
111*9781SMoriah.Waterland@Sun.COM case 'p':
112*9781SMoriah.Waterland@Sun.COM import_passarg = optarg;
113*9781SMoriah.Waterland@Sun.COM break;
114*9781SMoriah.Waterland@Sun.COM case 'R':
115*9781SMoriah.Waterland@Sun.COM altroot = optarg;
116*9781SMoriah.Waterland@Sun.COM break;
117*9781SMoriah.Waterland@Sun.COM case 't':
118*9781SMoriah.Waterland@Sun.COM trusted = B_TRUE;
119*9781SMoriah.Waterland@Sun.COM break;
120*9781SMoriah.Waterland@Sun.COM case 'y':
121*9781SMoriah.Waterland@Sun.COM implicit_trust = B_TRUE;
122*9781SMoriah.Waterland@Sun.COM break;
123*9781SMoriah.Waterland@Sun.COM case ':':
124*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MISSING_OPERAND, optopt);
125*9781SMoriah.Waterland@Sun.COM /* LINTED fallthrough intentional */
126*9781SMoriah.Waterland@Sun.COM case '?':
127*9781SMoriah.Waterland@Sun.COM default:
128*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_USAGE);
129*9781SMoriah.Waterland@Sun.COM goto cleanup;
130*9781SMoriah.Waterland@Sun.COM }
131*9781SMoriah.Waterland@Sun.COM }
132*9781SMoriah.Waterland@Sun.COM
133*9781SMoriah.Waterland@Sun.COM if (!trusted && alias == NULL) {
134*9781SMoriah.Waterland@Sun.COM /* for untrusted (user) certs, we require a name */
135*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_USER_NAME);
136*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_USAGE);
137*9781SMoriah.Waterland@Sun.COM goto cleanup;
138*9781SMoriah.Waterland@Sun.COM } else if (trusted && alias != NULL) {
139*9781SMoriah.Waterland@Sun.COM /* for trusted certs, we cannot have a name */
140*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TRUSTED_NAME);
141*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_USAGE);
142*9781SMoriah.Waterland@Sun.COM goto cleanup;
143*9781SMoriah.Waterland@Sun.COM }
144*9781SMoriah.Waterland@Sun.COM
145*9781SMoriah.Waterland@Sun.COM if (trusted && inkeyfile != NULL) {
146*9781SMoriah.Waterland@Sun.COM /* for trusted certs, we cannot have a private key */
147*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TRUSTED_KEY);
148*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_USAGE);
149*9781SMoriah.Waterland@Sun.COM goto cleanup;
150*9781SMoriah.Waterland@Sun.COM }
151*9781SMoriah.Waterland@Sun.COM
152*9781SMoriah.Waterland@Sun.COM /* last argument should be the path to the certificate */
153*9781SMoriah.Waterland@Sun.COM if ((argc-optind) > 1) {
154*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_USAGE);
155*9781SMoriah.Waterland@Sun.COM goto cleanup;
156*9781SMoriah.Waterland@Sun.COM } else if ((argc-optind) < 1) {
157*9781SMoriah.Waterland@Sun.COM infile = "stdin";
158*9781SMoriah.Waterland@Sun.COM certfile = stdin;
159*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_DEBUG, "Loading stdin certificate");
160*9781SMoriah.Waterland@Sun.COM } else {
161*9781SMoriah.Waterland@Sun.COM infile = argv[optind];
162*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_DEBUG, "Loading <%s> certificate",
163*9781SMoriah.Waterland@Sun.COM argv[optind]);
164*9781SMoriah.Waterland@Sun.COM if ((certfile = fopen(infile, "r")) == NULL) {
165*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_OPEN, infile);
166*9781SMoriah.Waterland@Sun.COM goto cleanup;
167*9781SMoriah.Waterland@Sun.COM }
168*9781SMoriah.Waterland@Sun.COM }
169*9781SMoriah.Waterland@Sun.COM
170*9781SMoriah.Waterland@Sun.COM /*
171*9781SMoriah.Waterland@Sun.COM * if specific key file supplied, open it, otherwise open
172*9781SMoriah.Waterland@Sun.COM * default (stdin)
173*9781SMoriah.Waterland@Sun.COM */
174*9781SMoriah.Waterland@Sun.COM if (inkeyfile != NULL) {
175*9781SMoriah.Waterland@Sun.COM if ((keyfile = fopen(inkeyfile, "r")) == NULL) {
176*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_OPEN, inkeyfile);
177*9781SMoriah.Waterland@Sun.COM goto cleanup;
178*9781SMoriah.Waterland@Sun.COM }
179*9781SMoriah.Waterland@Sun.COM } else {
180*9781SMoriah.Waterland@Sun.COM inkeyfile = "stdin";
181*9781SMoriah.Waterland@Sun.COM keyfile = stdin;
182*9781SMoriah.Waterland@Sun.COM }
183*9781SMoriah.Waterland@Sun.COM
184*9781SMoriah.Waterland@Sun.COM /* set up proper keystore */
185*9781SMoriah.Waterland@Sun.COM if (altroot != NULL) {
186*9781SMoriah.Waterland@Sun.COM if (strlcpy(keystore_file, altroot, MAXPATHLEN) >= MAXPATHLEN) {
187*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG, altroot);
188*9781SMoriah.Waterland@Sun.COM goto cleanup;
189*9781SMoriah.Waterland@Sun.COM }
190*9781SMoriah.Waterland@Sun.COM
191*9781SMoriah.Waterland@Sun.COM if (strlcat(keystore_file, "/", MAXPATHLEN) >= MAXPATHLEN) {
192*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG, altroot);
193*9781SMoriah.Waterland@Sun.COM goto cleanup;
194*9781SMoriah.Waterland@Sun.COM }
195*9781SMoriah.Waterland@Sun.COM }
196*9781SMoriah.Waterland@Sun.COM
197*9781SMoriah.Waterland@Sun.COM if (keystore_base == NULL) {
198*9781SMoriah.Waterland@Sun.COM if (geteuid() == 0 || altroot != NULL) {
199*9781SMoriah.Waterland@Sun.COM /*
200*9781SMoriah.Waterland@Sun.COM * If we have an alternate
201*9781SMoriah.Waterland@Sun.COM * root, then we have no choice but to use
202*9781SMoriah.Waterland@Sun.COM * root's keystore on that alternate root,
203*9781SMoriah.Waterland@Sun.COM * since there is no way to resolve a
204*9781SMoriah.Waterland@Sun.COM * user's home dir given an alternate root
205*9781SMoriah.Waterland@Sun.COM */
206*9781SMoriah.Waterland@Sun.COM if (strlcat(keystore_file, PKGSEC,
207*9781SMoriah.Waterland@Sun.COM MAXPATHLEN) >= MAXPATHLEN) {
208*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
209*9781SMoriah.Waterland@Sun.COM keystore_file);
210*9781SMoriah.Waterland@Sun.COM goto cleanup;
211*9781SMoriah.Waterland@Sun.COM }
212*9781SMoriah.Waterland@Sun.COM } else {
213*9781SMoriah.Waterland@Sun.COM if ((homedir = getenv("HOME")) == NULL) {
214*9781SMoriah.Waterland@Sun.COM /*
215*9781SMoriah.Waterland@Sun.COM * not superuser, but no home dir, so
216*9781SMoriah.Waterland@Sun.COM * use superuser's keystore
217*9781SMoriah.Waterland@Sun.COM */
218*9781SMoriah.Waterland@Sun.COM if (strlcat(keystore_file, PKGSEC,
219*9781SMoriah.Waterland@Sun.COM MAXPATHLEN) >= MAXPATHLEN) {
220*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
221*9781SMoriah.Waterland@Sun.COM keystore_file);
222*9781SMoriah.Waterland@Sun.COM goto cleanup;
223*9781SMoriah.Waterland@Sun.COM }
224*9781SMoriah.Waterland@Sun.COM } else {
225*9781SMoriah.Waterland@Sun.COM if (strlcat(keystore_file, homedir,
226*9781SMoriah.Waterland@Sun.COM MAXPATHLEN) >= MAXPATHLEN) {
227*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
228*9781SMoriah.Waterland@Sun.COM homedir);
229*9781SMoriah.Waterland@Sun.COM goto cleanup;
230*9781SMoriah.Waterland@Sun.COM }
231*9781SMoriah.Waterland@Sun.COM if (strlcat(keystore_file, "/.pkg/security",
232*9781SMoriah.Waterland@Sun.COM MAXPATHLEN) >= MAXPATHLEN) {
233*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
234*9781SMoriah.Waterland@Sun.COM keystore_file);
235*9781SMoriah.Waterland@Sun.COM goto cleanup;
236*9781SMoriah.Waterland@Sun.COM }
237*9781SMoriah.Waterland@Sun.COM }
238*9781SMoriah.Waterland@Sun.COM }
239*9781SMoriah.Waterland@Sun.COM } else {
240*9781SMoriah.Waterland@Sun.COM if (strlcat(keystore_file, keystore_base,
241*9781SMoriah.Waterland@Sun.COM MAXPATHLEN) >= MAXPATHLEN) {
242*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_TOO_LONG,
243*9781SMoriah.Waterland@Sun.COM keystore_base);
244*9781SMoriah.Waterland@Sun.COM goto cleanup;
245*9781SMoriah.Waterland@Sun.COM }
246*9781SMoriah.Waterland@Sun.COM }
247*9781SMoriah.Waterland@Sun.COM
248*9781SMoriah.Waterland@Sun.COM /* figure out input format */
249*9781SMoriah.Waterland@Sun.COM if (informat_str == NULL) {
250*9781SMoriah.Waterland@Sun.COM informat = KEYSTORE_FORMAT_PEM;
251*9781SMoriah.Waterland@Sun.COM } else {
252*9781SMoriah.Waterland@Sun.COM if (ci_streq(informat_str, "pem")) {
253*9781SMoriah.Waterland@Sun.COM informat = KEYSTORE_FORMAT_PEM;
254*9781SMoriah.Waterland@Sun.COM } else if (ci_streq(informat_str, "der")) {
255*9781SMoriah.Waterland@Sun.COM informat = KEYSTORE_FORMAT_DER;
256*9781SMoriah.Waterland@Sun.COM } else {
257*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_BAD_FORMAT, informat_str);
258*9781SMoriah.Waterland@Sun.COM goto cleanup;
259*9781SMoriah.Waterland@Sun.COM }
260*9781SMoriah.Waterland@Sun.COM }
261*9781SMoriah.Waterland@Sun.COM
262*9781SMoriah.Waterland@Sun.COM err = pkgerr_new();
263*9781SMoriah.Waterland@Sun.COM
264*9781SMoriah.Waterland@Sun.COM if (trusted) {
265*9781SMoriah.Waterland@Sun.COM /* load all possible certs */
266*9781SMoriah.Waterland@Sun.COM if (load_all_certs(err, certfile, informat, import_passarg,
267*9781SMoriah.Waterland@Sun.COM &trustcerts) != 0) {
268*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
269*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
270*9781SMoriah.Waterland@Sun.COM goto cleanup;
271*9781SMoriah.Waterland@Sun.COM }
272*9781SMoriah.Waterland@Sun.COM
273*9781SMoriah.Waterland@Sun.COM /* we must have gotten at least one cert, if not, fail */
274*9781SMoriah.Waterland@Sun.COM if (sk_X509_num(trustcerts) < 1) {
275*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_CERTS, infile);
276*9781SMoriah.Waterland@Sun.COM goto cleanup;
277*9781SMoriah.Waterland@Sun.COM }
278*9781SMoriah.Waterland@Sun.COM } else {
279*9781SMoriah.Waterland@Sun.COM /* first, try to load user certificate and key */
280*9781SMoriah.Waterland@Sun.COM if (load_cert_and_key(err, certfile, informat, import_passarg,
281*9781SMoriah.Waterland@Sun.COM &key, &cert) != 0) {
282*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
283*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
284*9781SMoriah.Waterland@Sun.COM goto cleanup;
285*9781SMoriah.Waterland@Sun.COM }
286*9781SMoriah.Waterland@Sun.COM
287*9781SMoriah.Waterland@Sun.COM /* we must have gotten a cert, if not, fail */
288*9781SMoriah.Waterland@Sun.COM if (cert == NULL) {
289*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_CERTS, infile);
290*9781SMoriah.Waterland@Sun.COM goto cleanup;
291*9781SMoriah.Waterland@Sun.COM }
292*9781SMoriah.Waterland@Sun.COM
293*9781SMoriah.Waterland@Sun.COM if (key == NULL) {
294*9781SMoriah.Waterland@Sun.COM /*
295*9781SMoriah.Waterland@Sun.COM * if we are importing a user cert, and did not get
296*9781SMoriah.Waterland@Sun.COM * a key, try to load it from the key file
297*9781SMoriah.Waterland@Sun.COM */
298*9781SMoriah.Waterland@Sun.COM if (keyfile == NULL) {
299*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NEED_KEY, infile);
300*9781SMoriah.Waterland@Sun.COM goto cleanup;
301*9781SMoriah.Waterland@Sun.COM } else {
302*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_DEBUG,
303*9781SMoriah.Waterland@Sun.COM "Loading private key <%s>", inkeyfile);
304*9781SMoriah.Waterland@Sun.COM if (load_cert_and_key(err, keyfile, informat,
305*9781SMoriah.Waterland@Sun.COM import_passarg,
306*9781SMoriah.Waterland@Sun.COM &key, NULL) != 0) {
307*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
308*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR,
309*9781SMoriah.Waterland@Sun.COM MSG_NO_ADDKEY, inkeyfile);
310*9781SMoriah.Waterland@Sun.COM goto cleanup;
311*9781SMoriah.Waterland@Sun.COM }
312*9781SMoriah.Waterland@Sun.COM
313*9781SMoriah.Waterland@Sun.COM if (key == NULL) {
314*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_PRIVKEY,
315*9781SMoriah.Waterland@Sun.COM inkeyfile);
316*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR,
317*9781SMoriah.Waterland@Sun.COM MSG_NO_ADDKEY, inkeyfile);
318*9781SMoriah.Waterland@Sun.COM goto cleanup;
319*9781SMoriah.Waterland@Sun.COM }
320*9781SMoriah.Waterland@Sun.COM }
321*9781SMoriah.Waterland@Sun.COM }
322*9781SMoriah.Waterland@Sun.COM }
323*9781SMoriah.Waterland@Sun.COM
324*9781SMoriah.Waterland@Sun.COM if (trusted) {
325*9781SMoriah.Waterland@Sun.COM /* check validity date of all certificates */
326*9781SMoriah.Waterland@Sun.COM for (i = 0; i < sk_X509_num(trustcerts); i++) {
327*9781SMoriah.Waterland@Sun.COM /* LINTED pointer cast may result in improper algnmnt */
328*9781SMoriah.Waterland@Sun.COM cert = sk_X509_value(trustcerts, i);
329*9781SMoriah.Waterland@Sun.COM if (check_cert(err, cert) != 0) {
330*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
331*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT,
332*9781SMoriah.Waterland@Sun.COM infile);
333*9781SMoriah.Waterland@Sun.COM goto cleanup;
334*9781SMoriah.Waterland@Sun.COM }
335*9781SMoriah.Waterland@Sun.COM }
336*9781SMoriah.Waterland@Sun.COM } else {
337*9781SMoriah.Waterland@Sun.COM /* check validity date of user certificate */
338*9781SMoriah.Waterland@Sun.COM if (check_cert_and_key(err, cert, key) != 0) {
339*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
340*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
341*9781SMoriah.Waterland@Sun.COM goto cleanup;
342*9781SMoriah.Waterland@Sun.COM }
343*9781SMoriah.Waterland@Sun.COM }
344*9781SMoriah.Waterland@Sun.COM
345*9781SMoriah.Waterland@Sun.COM if (trusted && !implicit_trust) {
346*9781SMoriah.Waterland@Sun.COM /*
347*9781SMoriah.Waterland@Sun.COM * if importing more than one cert, must use implicit trust,
348*9781SMoriah.Waterland@Sun.COM * because we can't ask the user to individually trust
349*9781SMoriah.Waterland@Sun.COM * each one, since there may be many
350*9781SMoriah.Waterland@Sun.COM */
351*9781SMoriah.Waterland@Sun.COM if (sk_X509_num(trustcerts) != 1) {
352*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
353*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MULTIPLE_TRUST, infile, "-y");
354*9781SMoriah.Waterland@Sun.COM goto cleanup;
355*9781SMoriah.Waterland@Sun.COM } else {
356*9781SMoriah.Waterland@Sun.COM /* LINTED pointer cast may result in improper algnmnt */
357*9781SMoriah.Waterland@Sun.COM cert = sk_X509_value(trustcerts, 0);
358*9781SMoriah.Waterland@Sun.COM }
359*9781SMoriah.Waterland@Sun.COM
360*9781SMoriah.Waterland@Sun.COM /* ask the user */
361*9781SMoriah.Waterland@Sun.COM switch (verify_trust(cert)) {
362*9781SMoriah.Waterland@Sun.COM case Accept:
363*9781SMoriah.Waterland@Sun.COM /* user accepted */
364*9781SMoriah.Waterland@Sun.COM break;
365*9781SMoriah.Waterland@Sun.COM case Reject:
366*9781SMoriah.Waterland@Sun.COM /* user aborted operation */
367*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_ADDCERT_ABORT);
368*9781SMoriah.Waterland@Sun.COM goto cleanup;
369*9781SMoriah.Waterland@Sun.COM case VerifyFailed:
370*9781SMoriah.Waterland@Sun.COM default:
371*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
372*9781SMoriah.Waterland@Sun.COM goto cleanup;
373*9781SMoriah.Waterland@Sun.COM }
374*9781SMoriah.Waterland@Sun.COM }
375*9781SMoriah.Waterland@Sun.COM
376*9781SMoriah.Waterland@Sun.COM /* now load the key store */
377*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_DEBUG, "Loading keystore <%s>", keystore_file);
378*9781SMoriah.Waterland@Sun.COM
379*9781SMoriah.Waterland@Sun.COM set_passphrase_prompt(MSG_KEYSTORE_PASSPROMPT);
380*9781SMoriah.Waterland@Sun.COM set_passphrase_passarg(passarg);
381*9781SMoriah.Waterland@Sun.COM if (open_keystore(err, keystore_file, prog, pkg_passphrase_cb,
382*9781SMoriah.Waterland@Sun.COM KEYSTORE_ACCESS_READWRITE | KEYSTORE_PATH_HARD, &keystore) != 0) {
383*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
384*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
385*9781SMoriah.Waterland@Sun.COM goto cleanup;
386*9781SMoriah.Waterland@Sun.COM }
387*9781SMoriah.Waterland@Sun.COM
388*9781SMoriah.Waterland@Sun.COM /* now merge the new cert into the keystore */
389*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_DEBUG, "Merging certificate <%s>",
390*9781SMoriah.Waterland@Sun.COM get_subject_display_name(cert));
391*9781SMoriah.Waterland@Sun.COM if (trusted) {
392*9781SMoriah.Waterland@Sun.COM /* merge all trusted certs found */
393*9781SMoriah.Waterland@Sun.COM for (i = 0; i < sk_X509_num(trustcerts); i++) {
394*9781SMoriah.Waterland@Sun.COM /* LINTED pointer cast may result in improper algnmnt */
395*9781SMoriah.Waterland@Sun.COM cert = sk_X509_value(trustcerts, i);
396*9781SMoriah.Waterland@Sun.COM if (merge_ca_cert(err, cert, keystore) != 0) {
397*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
398*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR,
399*9781SMoriah.Waterland@Sun.COM MSG_NO_ADDCERT, infile);
400*9781SMoriah.Waterland@Sun.COM goto cleanup;
401*9781SMoriah.Waterland@Sun.COM
402*9781SMoriah.Waterland@Sun.COM } else {
403*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_TRUSTING,
404*9781SMoriah.Waterland@Sun.COM get_subject_display_name(cert));
405*9781SMoriah.Waterland@Sun.COM }
406*9781SMoriah.Waterland@Sun.COM }
407*9781SMoriah.Waterland@Sun.COM } else {
408*9781SMoriah.Waterland@Sun.COM /* merge user cert */
409*9781SMoriah.Waterland@Sun.COM if (merge_cert_and_key(err, cert, key, alias, keystore) != 0) {
410*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
411*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
412*9781SMoriah.Waterland@Sun.COM goto cleanup;
413*9781SMoriah.Waterland@Sun.COM }
414*9781SMoriah.Waterland@Sun.COM }
415*9781SMoriah.Waterland@Sun.COM
416*9781SMoriah.Waterland@Sun.COM /* now write it back out */
417*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_DEBUG, "Closing keystore");
418*9781SMoriah.Waterland@Sun.COM set_passphrase_prompt(MSG_KEYSTORE_PASSOUTPROMPT);
419*9781SMoriah.Waterland@Sun.COM set_passphrase_passarg(passarg);
420*9781SMoriah.Waterland@Sun.COM if (close_keystore(err, keystore, pkg_passphrase_cb) != 0) {
421*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
422*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
423*9781SMoriah.Waterland@Sun.COM goto cleanup;
424*9781SMoriah.Waterland@Sun.COM }
425*9781SMoriah.Waterland@Sun.COM
426*9781SMoriah.Waterland@Sun.COM if (trusted) {
427*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_TRUSTED, infile);
428*9781SMoriah.Waterland@Sun.COM } else {
429*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_INFO, MSG_ADDED, infile, alias);
430*9781SMoriah.Waterland@Sun.COM }
431*9781SMoriah.Waterland@Sun.COM
432*9781SMoriah.Waterland@Sun.COM ret = 0;
433*9781SMoriah.Waterland@Sun.COM
434*9781SMoriah.Waterland@Sun.COM /* fallthrough intentional */
435*9781SMoriah.Waterland@Sun.COM cleanup:
436*9781SMoriah.Waterland@Sun.COM if (err != NULL)
437*9781SMoriah.Waterland@Sun.COM pkgerr_free(err);
438*9781SMoriah.Waterland@Sun.COM
439*9781SMoriah.Waterland@Sun.COM if (certfile != NULL)
440*9781SMoriah.Waterland@Sun.COM (void) fclose(certfile);
441*9781SMoriah.Waterland@Sun.COM
442*9781SMoriah.Waterland@Sun.COM if (keyfile != NULL)
443*9781SMoriah.Waterland@Sun.COM (void) fclose(keyfile);
444*9781SMoriah.Waterland@Sun.COM
445*9781SMoriah.Waterland@Sun.COM return (ret);
446*9781SMoriah.Waterland@Sun.COM }
447*9781SMoriah.Waterland@Sun.COM
448*9781SMoriah.Waterland@Sun.COM /* Asks user to verify certificate data before proceeding */
verify_trust(X509 * cert)449*9781SMoriah.Waterland@Sun.COM static VerifyStatus verify_trust(X509 *cert)
450*9781SMoriah.Waterland@Sun.COM {
451*9781SMoriah.Waterland@Sun.COM char vfy_trust = 'y';
452*9781SMoriah.Waterland@Sun.COM VerifyStatus ret = Accept;
453*9781SMoriah.Waterland@Sun.COM PKG_ERR *err;
454*9781SMoriah.Waterland@Sun.COM UI *ui = NULL;
455*9781SMoriah.Waterland@Sun.COM
456*9781SMoriah.Waterland@Sun.COM err = pkgerr_new();
457*9781SMoriah.Waterland@Sun.COM /* print cert data */
458*9781SMoriah.Waterland@Sun.COM if (print_cert(err, cert, KEYSTORE_FORMAT_TEXT,
459*9781SMoriah.Waterland@Sun.COM get_subject_display_name(cert), B_TRUE, stdout) != 0) {
460*9781SMoriah.Waterland@Sun.COM log_pkgerr(LOG_MSG_ERR, err);
461*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
462*9781SMoriah.Waterland@Sun.COM goto cleanup;
463*9781SMoriah.Waterland@Sun.COM }
464*9781SMoriah.Waterland@Sun.COM
465*9781SMoriah.Waterland@Sun.COM if ((ui = UI_new()) == NULL) {
466*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MEM);
467*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
468*9781SMoriah.Waterland@Sun.COM goto cleanup;
469*9781SMoriah.Waterland@Sun.COM }
470*9781SMoriah.Waterland@Sun.COM
471*9781SMoriah.Waterland@Sun.COM /*
472*9781SMoriah.Waterland@Sun.COM * The prompt is internationalized, but the valid
473*9781SMoriah.Waterland@Sun.COM * response values are fixed, to avoid any complex
474*9781SMoriah.Waterland@Sun.COM * multibyte processing that results in bugs
475*9781SMoriah.Waterland@Sun.COM */
476*9781SMoriah.Waterland@Sun.COM if (UI_add_input_boolean(ui, MSG_VERIFY_TRUST,
477*9781SMoriah.Waterland@Sun.COM "",
478*9781SMoriah.Waterland@Sun.COM "yY", "nN",
479*9781SMoriah.Waterland@Sun.COM UI_INPUT_FLAG_ECHO, &vfy_trust) <= 0) {
480*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MEM);
481*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
482*9781SMoriah.Waterland@Sun.COM goto cleanup;
483*9781SMoriah.Waterland@Sun.COM }
484*9781SMoriah.Waterland@Sun.COM
485*9781SMoriah.Waterland@Sun.COM if (UI_process(ui) != 0) {
486*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MEM);
487*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
488*9781SMoriah.Waterland@Sun.COM goto cleanup;
489*9781SMoriah.Waterland@Sun.COM }
490*9781SMoriah.Waterland@Sun.COM
491*9781SMoriah.Waterland@Sun.COM if (vfy_trust != 'y') {
492*9781SMoriah.Waterland@Sun.COM ret = Reject;
493*9781SMoriah.Waterland@Sun.COM goto cleanup;
494*9781SMoriah.Waterland@Sun.COM }
495*9781SMoriah.Waterland@Sun.COM
496*9781SMoriah.Waterland@Sun.COM /*
497*9781SMoriah.Waterland@Sun.COM * if the cert does not appear to be a CA cert
498*9781SMoriah.Waterland@Sun.COM * r is not self-signed, verify that as well
499*9781SMoriah.Waterland@Sun.COM */
500*9781SMoriah.Waterland@Sun.COM if (!is_ca_cert(cert)) {
501*9781SMoriah.Waterland@Sun.COM UI_free(ui);
502*9781SMoriah.Waterland@Sun.COM if ((ui = UI_new()) == NULL) {
503*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MEM);
504*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
505*9781SMoriah.Waterland@Sun.COM goto cleanup;
506*9781SMoriah.Waterland@Sun.COM }
507*9781SMoriah.Waterland@Sun.COM
508*9781SMoriah.Waterland@Sun.COM if (UI_add_input_boolean(ui,
509*9781SMoriah.Waterland@Sun.COM MSG_VERIFY_NOT_CA,
510*9781SMoriah.Waterland@Sun.COM "",
511*9781SMoriah.Waterland@Sun.COM "yY", "nN",
512*9781SMoriah.Waterland@Sun.COM UI_INPUT_FLAG_ECHO, &vfy_trust) <= 0) {
513*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
514*9781SMoriah.Waterland@Sun.COM goto cleanup;
515*9781SMoriah.Waterland@Sun.COM }
516*9781SMoriah.Waterland@Sun.COM
517*9781SMoriah.Waterland@Sun.COM if (UI_process(ui) != 0) {
518*9781SMoriah.Waterland@Sun.COM log_msg(LOG_MSG_ERR, MSG_MEM);
519*9781SMoriah.Waterland@Sun.COM ret = VerifyFailed;
520*9781SMoriah.Waterland@Sun.COM goto cleanup;
521*9781SMoriah.Waterland@Sun.COM }
522*9781SMoriah.Waterland@Sun.COM
523*9781SMoriah.Waterland@Sun.COM if (vfy_trust != 'y') {
524*9781SMoriah.Waterland@Sun.COM ret = Reject;
525*9781SMoriah.Waterland@Sun.COM goto cleanup;
526*9781SMoriah.Waterland@Sun.COM }
527*9781SMoriah.Waterland@Sun.COM }
528*9781SMoriah.Waterland@Sun.COM
529*9781SMoriah.Waterland@Sun.COM cleanup:
530*9781SMoriah.Waterland@Sun.COM if (ui != NULL)
531*9781SMoriah.Waterland@Sun.COM UI_free(ui);
532*9781SMoriah.Waterland@Sun.COM
533*9781SMoriah.Waterland@Sun.COM if (err != NULL)
534*9781SMoriah.Waterland@Sun.COM pkgerr_free(err);
535*9781SMoriah.Waterland@Sun.COM
536*9781SMoriah.Waterland@Sun.COM return (ret);
537*9781SMoriah.Waterland@Sun.COM }
538*9781SMoriah.Waterland@Sun.COM /*
539*9781SMoriah.Waterland@Sun.COM * Name: is_ca_cert
540*9781SMoriah.Waterland@Sun.COM * Desc: Determines if a given certificate has the attributes
541*9781SMoriah.Waterland@Sun.COM * of a CA certificate
542*9781SMoriah.Waterland@Sun.COM * Returns: B_TRUE if certificate has attributes of a CA cert
543*9781SMoriah.Waterland@Sun.COM * B_FALSE otherwise
544*9781SMoriah.Waterland@Sun.COM */
545*9781SMoriah.Waterland@Sun.COM static boolean_t
is_ca_cert(X509 * x)546*9781SMoriah.Waterland@Sun.COM is_ca_cert(X509 *x)
547*9781SMoriah.Waterland@Sun.COM {
548*9781SMoriah.Waterland@Sun.COM
549*9781SMoriah.Waterland@Sun.COM /*
550*9781SMoriah.Waterland@Sun.COM * X509_check_purpose causes the extensions that we
551*9781SMoriah.Waterland@Sun.COM * care about to be decoded and stored in the X509
552*9781SMoriah.Waterland@Sun.COM * structure, so we must call it first
553*9781SMoriah.Waterland@Sun.COM * before checking for CA extensions in the X509
554*9781SMoriah.Waterland@Sun.COM * structure
555*9781SMoriah.Waterland@Sun.COM */
556*9781SMoriah.Waterland@Sun.COM (void) X509_check_purpose(x, X509_PURPOSE_ANY, 0);
557*9781SMoriah.Waterland@Sun.COM
558*9781SMoriah.Waterland@Sun.COM /* keyUsage if present should allow cert signing */
559*9781SMoriah.Waterland@Sun.COM if ((x->ex_flags & EXFLAG_KUSAGE) &&
560*9781SMoriah.Waterland@Sun.COM !(x->ex_kusage & KU_KEY_CERT_SIGN)) {
561*9781SMoriah.Waterland@Sun.COM return (B_FALSE);
562*9781SMoriah.Waterland@Sun.COM }
563*9781SMoriah.Waterland@Sun.COM
564*9781SMoriah.Waterland@Sun.COM /* If basicConstraints says not a CA then say so */
565*9781SMoriah.Waterland@Sun.COM if (x->ex_flags & EXFLAG_BCONS) {
566*9781SMoriah.Waterland@Sun.COM if (!(x->ex_flags & EXFLAG_CA)) {
567*9781SMoriah.Waterland@Sun.COM return (B_FALSE);
568*9781SMoriah.Waterland@Sun.COM }
569*9781SMoriah.Waterland@Sun.COM }
570*9781SMoriah.Waterland@Sun.COM
571*9781SMoriah.Waterland@Sun.COM /* no explicit not-a-CA flags set, so assume that it is */
572*9781SMoriah.Waterland@Sun.COM return (B_TRUE);
573*9781SMoriah.Waterland@Sun.COM }
574