xref: /titanic_41/usr/src/uts/common/inet/kssl/kssl.c (revision 6be356c5780a1ccb886bba08d6eb56b61f021564)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * The system call and DDI interface for the kernel SSL module
31  */
32 
33 #include <sys/types.h>
34 #include <sys/modctl.h>
35 #include <sys/conf.h>
36 #include <sys/stat.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #include <sys/kmem.h>
40 #include <sys/errno.h>
41 #include <sys/ksynch.h>
42 #include <sys/file.h>
43 #include <sys/open.h>
44 #include <sys/cred.h>
45 #include <sys/proc.h>
46 #include <sys/task.h>
47 #include <sys/mkdev.h>
48 #include <sys/model.h>
49 #include <sys/sysmacros.h>
50 #include <sys/policy.h>
51 #include <sys/crypto/common.h>
52 #include <sys/crypto/api.h>
53 #include <c2/audit.h>
54 #include <sys/kstat.h>
55 
56 #include "kssl.h"
57 #include "ksslimpl.h"
58 
59 /*
60  * DDI entry points.
61  */
62 static int kssl_attach(dev_info_t *, ddi_attach_cmd_t);
63 static int kssl_detach(dev_info_t *, ddi_detach_cmd_t);
64 static int kssl_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
65 static int kssl_open(dev_t *, int, int, cred_t *);
66 static int kssl_close(dev_t, int, int, cred_t *);
67 static int kssl_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
68 
69 static int kssl_constructor(void *buf, void *arg, int kmflags);
70 static void kssl_destructor(void *buf, void *arg);
71 
72 /*
73  * Module linkage.
74  */
75 static struct cb_ops cbops = {
76 	kssl_open,		/* cb_open */
77 	kssl_close,		/* cb_close */
78 	nodev,			/* cb_strategy */
79 	nodev,			/* cb_print */
80 	nodev,			/* cb_dump */
81 	nodev,			/* cb_read */
82 	nodev,			/* cb_write */
83 	kssl_ioctl,		/* cb_ioctl */
84 	nodev,			/* cb_devmap */
85 	nodev,			/* cb_mmap */
86 	nodev,			/* cb_segmap */
87 	nochpoll,		/* cb_chpoll */
88 	ddi_prop_op,		/* cb_prop_op */
89 	NULL,			/* cb_streamtab */
90 	D_MP,			/* cb_flag */
91 	CB_REV,			/* cb_rev */
92 	nodev,			/* cb_aread */
93 	nodev,			/* cb_awrite */
94 };
95 
96 static struct dev_ops devops = {
97 	DEVO_REV,		/* devo_rev */
98 	0,			/* devo_refcnt */
99 	kssl_getinfo,		/* devo_getinfo */
100 	nulldev,		/* devo_identify */
101 	nulldev,		/* devo_probe */
102 	kssl_attach,		/* devo_attach */
103 	kssl_detach,		/* devo_detach */
104 	nodev,			/* devo_reset */
105 	&cbops,			/* devo_cb_ops */
106 	NULL,			/* devo_bus_ops */
107 	NULL,			/* devo_power */
108 };
109 
110 static struct modldrv modldrv = {
111 	&mod_driverops,			/* drv_modops */
112 	"Kernel SSL Interface v%I%",	/* drv_linkinfo */
113 	&devops,
114 };
115 
116 static struct modlinkage modlinkage = {
117 	MODREV_1,		/* ml_rev */
118 	&modldrv,		/* ml_linkage */
119 	NULL
120 };
121 
122 static dev_info_t *kssl_dip = NULL;
123 
124 crypto_mechanism_t rsa_x509_mech = {CRYPTO_MECH_INVALID, NULL, 0};
125 crypto_mechanism_t hmac_md5_mech = {CRYPTO_MECH_INVALID, NULL, 0};
126 crypto_mechanism_t hmac_sha1_mech = {CRYPTO_MECH_INVALID, NULL, 0};
127 crypto_call_flag_t kssl_call_flag = CRYPTO_ALWAYS_QUEUE;
128 
129 KSSLCipherDef cipher_defs[] = { /* indexed by SSL3BulkCipher */
130 	/* type bsize keysz crypto_mech_type_t */
131 
132 	{type_stream, 0, 0, CRYPTO_MECH_INVALID},
133 
134 	/* mech_type to be initialized with CKM_RC4's */
135 	{type_stream, 0, 16, CRYPTO_MECH_INVALID},
136 
137 	/* mech_type to be initialized with CKM_DES_CBC's */
138 	{type_block, 8, 8, CRYPTO_MECH_INVALID},
139 
140 	/* mech_type to be initialized with CKM_DES3_CBC's */
141 	{type_block, 8, 24, CRYPTO_MECH_INVALID},
142 };
143 
144 int kssl_enabled = 1;
145 struct kmem_cache *kssl_cache;
146 
147 static void kssl_global_init();
148 static void kssl_init_mechs();
149 static void kssl_event_callback(uint32_t, void *);
150 
151 /*
152  * DDI entry points.
153  */
154 int
155 _init(void)
156 {
157 	return (mod_install(&modlinkage));
158 }
159 
160 int
161 _info(struct modinfo *modinfop)
162 {
163 	return (mod_info(&modlinkage, modinfop));
164 }
165 
166 /* ARGSUSED */
167 static int
168 kssl_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
169 {
170 	switch (cmd) {
171 	case DDI_INFO_DEVT2DEVINFO:
172 		*result = kssl_dip;
173 		return (DDI_SUCCESS);
174 
175 	case DDI_INFO_DEVT2INSTANCE:
176 		*result = (void *)0;
177 		return (DDI_SUCCESS);
178 	}
179 	return (DDI_FAILURE);
180 }
181 
182 static int
183 kssl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
184 {
185 	if (cmd != DDI_ATTACH) {
186 		return (DDI_FAILURE);
187 	}
188 
189 	if (ddi_get_instance(dip) != 0) {
190 		/* we only allow instance 0 to attach */
191 		return (DDI_FAILURE);
192 	}
193 
194 	/* create the minor node */
195 	if (ddi_create_minor_node(dip, "kssl", S_IFCHR, 0, DDI_PSEUDO, 0) !=
196 	    DDI_SUCCESS) {
197 		cmn_err(CE_WARN, "kssl_attach: failed creating minor node");
198 		ddi_remove_minor_node(dip, NULL);
199 		return (DDI_FAILURE);
200 	}
201 
202 	kssl_dip = dip;
203 
204 	kssl_global_init();
205 
206 	return (DDI_SUCCESS);
207 }
208 
209 static int
210 kssl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
211 {
212 	if (cmd != DDI_DETACH)
213 		return (DDI_FAILURE);
214 
215 	if (kssl_entry_tab_nentries != 0)
216 		return (DDI_FAILURE);
217 
218 	mutex_destroy(&kssl_tab_mutex);
219 	kssl_dip = NULL;
220 
221 	if (kssl_cache != NULL) {
222 		kmem_cache_destroy(kssl_cache);
223 	}
224 
225 	ddi_remove_minor_node(dip, NULL);
226 
227 	return (DDI_SUCCESS);
228 }
229 
230 static crypto_notify_handle_t prov_update_handle = NULL;
231 
232 /* ARGSUSED */
233 static int
234 kssl_open(dev_t *devp, int flag, int otyp, cred_t *credp)
235 {
236 	if (otyp != OTYP_CHR)
237 		return (ENXIO);
238 
239 	if (kssl_dip == NULL)
240 		return (ENXIO);
241 
242 	/* first time here? initialize everything */
243 	if (rsa_x509_mech.cm_type == CRYPTO_MECH_INVALID) {
244 		kssl_init_mechs();
245 		prov_update_handle = crypto_notify_events(
246 		    kssl_event_callback, CRYPTO_EVENT_PROVIDERS_CHANGE);
247 	}
248 
249 	/* exclusive opens are not supported */
250 	if (flag & FEXCL)
251 		return (ENOTSUP);
252 
253 	return (0);
254 }
255 
256 /* ARGSUSED */
257 static int
258 kssl_close(dev_t dev, int flag, int otyp, cred_t *credp)
259 {
260 	return (0);
261 }
262 
263 #define	KSSL_MAX_KEYANDCERTS	80000	/* max 64K plus a little margin */
264 
265 /* ARGSUSED */
266 static int
267 kssl_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
268     int *rval)
269 {
270 	int error = EINVAL;
271 
272 #define	ARG	((caddr_t)arg)
273 
274 	if (secpolicy_net_config(c, B_FALSE) != 0) {
275 		return (EPERM);
276 	}
277 
278 	switch (cmd) {
279 	case KSSL_ADD_ENTRY: {
280 		kssl_params_t kssl_params_s, *kssl_params = &kssl_params_s;
281 		uint64_t len;
282 
283 		if (copyin(ARG, kssl_params, sizeof (kssl_params_s)) != 0) {
284 			return (EFAULT);
285 		}
286 
287 		len = kssl_params->kssl_params_size;
288 		if (len < sizeof (kssl_params_t) ||
289 		    len > KSSL_MAX_KEYANDCERTS) {
290 			return (EINVAL);
291 		}
292 
293 		kssl_params = kmem_alloc(len, KM_SLEEP);
294 
295 		/* Get the whole structure and parameters in one move */
296 		if (copyin(ARG, kssl_params, len) != 0) {
297 			kmem_free(kssl_params, len);
298 			return (EFAULT);
299 		}
300 		error = kssl_add_entry(kssl_params);
301 #ifdef C2_AUDIT
302 	if (audit_active)
303 		audit_kssl(KSSL_ADD_ENTRY, kssl_params, error);
304 #endif
305 		kmem_free(kssl_params, len);
306 		break;
307 	}
308 	case KSSL_DELETE_ENTRY: {
309 		struct sockaddr_in server_addr;
310 
311 		if (copyin(ARG, &server_addr, sizeof (server_addr)) != 0) {
312 			return (EFAULT);
313 		}
314 
315 		error = kssl_delete_entry(&server_addr);
316 #ifdef C2_AUDIT
317 	if (audit_active)
318 		audit_kssl(KSSL_DELETE_ENTRY, &server_addr, error);
319 #endif
320 		break;
321 	}
322 	}
323 
324 	return (error);
325 }
326 
327 #define	NUM_MECHS	6
328 mech_to_cipher_t mech_to_cipher_tab[NUM_MECHS] = {
329 	{CRYPTO_MECH_INVALID, SUN_CKM_RSA_X_509,
330 	    {SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_WITH_RC4_128_SHA,
331 	    SSL_RSA_WITH_DES_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA,
332 	    SSL_RSA_WITH_NULL_SHA}},
333 	{CRYPTO_MECH_INVALID, SUN_CKM_MD5_HMAC, {SSL_RSA_WITH_RC4_128_MD5}},
334 	{CRYPTO_MECH_INVALID, SUN_CKM_SHA1_HMAC,
335 	    {SSL_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_DES_CBC_SHA,
336 	    SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_NULL_SHA}},
337 	{CRYPTO_MECH_INVALID, SUN_CKM_RC4,
338 	    {SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_WITH_RC4_128_SHA}},
339 	{CRYPTO_MECH_INVALID, SUN_CKM_DES_CBC, {SSL_RSA_WITH_DES_CBC_SHA}},
340 	{CRYPTO_MECH_INVALID, SUN_CKM_DES3_CBC,
341 	    {SSL_RSA_WITH_3DES_EDE_CBC_SHA}}
342 };
343 
344 static void
345 kssl_init_mechs()
346 {
347 	mech_to_cipher_tab[0].mech = rsa_x509_mech.cm_type =
348 	    crypto_mech2id(SUN_CKM_RSA_X_509);
349 	mech_to_cipher_tab[1].mech = hmac_md5_mech.cm_type =
350 	    crypto_mech2id(SUN_CKM_MD5_HMAC);
351 	mech_to_cipher_tab[2].mech = hmac_sha1_mech.cm_type =
352 	    crypto_mech2id(SUN_CKM_SHA1_HMAC);
353 
354 	mech_to_cipher_tab[3].mech = cipher_defs[cipher_rc4].mech_type =
355 	    crypto_mech2id(SUN_CKM_RC4);
356 	mech_to_cipher_tab[4].mech = cipher_defs[cipher_des].mech_type =
357 	    crypto_mech2id(SUN_CKM_DES_CBC);
358 	mech_to_cipher_tab[5].mech = cipher_defs[cipher_3des].mech_type =
359 	    crypto_mech2id(SUN_CKM_DES3_CBC);
360 }
361 
362 static int
363 is_in_suites(uint16_t s, uint16_t *sarray)
364 {
365 	int i;
366 
367 	for (i = 0; i < CIPHER_SUITE_COUNT; i++) {
368 		if (s == sarray[i])
369 			return (1);
370 	}
371 
372 	return (0);
373 }
374 
375 static int
376 is_in_mechlist(char *name, crypto_mech_name_t *mechs, int count)
377 {
378 	int i;
379 
380 	for (i = 0; i < count; i++) {
381 		if (strncmp(name, mechs[i], CRYPTO_MAX_MECH_NAME) == 0)
382 			return (1);
383 	}
384 
385 	return (0);
386 }
387 
388 /*
389  * Callback function invoked by the crypto framework when a provider's
390  * mechanism is available/unavailable. This callback updates entries in the
391  * kssl_entry_tab[] to make changes to the cipher suites of an entry
392  * which are affected by the mechansim.
393  */
394 static void
395 kssl_event_callback(uint32_t event, void *event_arg)
396 {
397 	int i, j;
398 	int cnt, rcnt;
399 	uint16_t s;
400 	boolean_t changed;
401 	crypto_mech_name_t *mechs;
402 	uint_t mech_count;
403 	mech_to_cipher_t *mc;
404 	kssl_entry_t *old;
405 	kssl_entry_t *new;
406 	uint16_t tmp_suites[CIPHER_SUITE_COUNT];
407 	uint16_t dis_list[CIPHER_SUITE_COUNT];
408 	crypto_notify_event_change_t *prov_change =
409 	    (crypto_notify_event_change_t *)event_arg;
410 
411 	/* ignore events for which we didn't register */
412 	if (event != CRYPTO_EVENT_PROVIDERS_CHANGE) {
413 		return;
414 	}
415 
416 	for (i = 0; i < NUM_MECHS; i++) {
417 		mc = &(mech_to_cipher_tab[i]);
418 		if (mc->mech == CRYPTO_MECH_INVALID)
419 			continue;
420 
421 		/*
422 		 * Check if this crypto framework provider mechanism being
423 		 * added or removed affects us.
424 		 */
425 		if (strncmp(mc->name, prov_change->ec_mech_name,
426 		    CRYPTO_MAX_MECH_NAME) == 0)
427 			break;
428 	}
429 
430 	if (i == NUM_MECHS)
431 		return;
432 
433 	mechs = crypto_get_mech_list(&mech_count, KM_SLEEP);
434 	if (mechs == NULL)
435 		return;
436 
437 	mutex_enter(&kssl_tab_mutex);
438 
439 	for (i = 0; i < kssl_entry_tab_size; i++) {
440 		if ((old = kssl_entry_tab[i]) == NULL)
441 			continue;
442 
443 		cnt = 0;
444 		rcnt = 0;
445 		changed = B_FALSE;
446 		for (j = 0; j < CIPHER_SUITE_COUNT; j++) {
447 			tmp_suites[j] = CIPHER_NOTSET;
448 			dis_list[j] = CIPHER_NOTSET;
449 		}
450 
451 		/*
452 		 * We start with the saved cipher suite list for the new entry.
453 		 * If a mechanism is disabled, resulting in a cipher suite being
454 		 * disabled now, we take it out from the list for the new entry.
455 		 * If a mechanism is enabled, resulting in a cipher suite being
456 		 * enabled now, we don't need to do any thing.
457 		 */
458 		if (!is_in_mechlist(mc->name, mechs, mech_count)) {
459 			for (j = 0; j < CIPHER_SUITE_COUNT; j++) {
460 				s = mc->kssl_suites[j];
461 				if (s == 0)
462 					break;
463 				if (is_in_suites(s, old->kssl_saved_Suites)) {
464 					/* Disable this cipher suite */
465 					if (!is_in_suites(s, dis_list))
466 						dis_list[cnt++] = s;
467 				}
468 			}
469 		}
470 
471 		for (j = 0; j < CIPHER_SUITE_COUNT; j++) {
472 			s = old->kssl_saved_Suites[j];
473 			if (!is_in_suites(s, dis_list))
474 				tmp_suites[rcnt] = s;
475 
476 			if (!changed &&
477 			    (tmp_suites[rcnt] != old->kssl_cipherSuites[rcnt]))
478 				changed = B_TRUE;
479 			rcnt++;
480 		}
481 
482 		if (changed) {
483 			new = kmem_zalloc(sizeof (kssl_entry_t), KM_NOSLEEP);
484 			if (new == NULL)
485 				continue;
486 
487 			*new = *old;		/* Structure copy */
488 			old->ke_no_freeall = B_TRUE;
489 			new->ke_refcnt = 0;
490 			new->kssl_cipherSuites_nentries = rcnt;
491 			for (j = 0; j < CIPHER_SUITE_COUNT; j++)
492 				new->kssl_cipherSuites[j] = tmp_suites[j];
493 
494 			KSSL_ENTRY_REFHOLD(new);
495 			kssl_entry_tab[i] = new;
496 			KSSL_ENTRY_REFRELE(old);
497 		}
498 	}
499 
500 	mutex_exit(&kssl_tab_mutex);
501 	crypto_free_mech_list(mechs, mech_count);
502 }
503 
504 
505 kssl_stats_t *kssl_statp;
506 
507 static void
508 kssl_global_init()
509 {
510 	kstat_t *ksp;
511 
512 	mutex_init(&kssl_tab_mutex, NULL, MUTEX_DRIVER, NULL);
513 
514 	kssl_cache = kmem_cache_create("kssl_cache", sizeof (ssl_t),
515 	    0, kssl_constructor, kssl_destructor, NULL, NULL, NULL, 0);
516 
517 	if ((ksp = kstat_create("kssl", 0, "kssl_stats", "crypto",
518 	    KSTAT_TYPE_NAMED, sizeof (kssl_stats_t) / sizeof (kstat_named_t),
519 	    KSTAT_FLAG_PERSISTENT)) != NULL) {
520 		kssl_statp = ksp->ks_data;
521 
522 		kstat_named_init(&kssl_statp->sid_cache_lookups,
523 		    "kssl_sid_cache_lookups", KSTAT_DATA_UINT64);
524 		kstat_named_init(&kssl_statp->sid_cache_hits,
525 		    "kssl_sid_cache_hits", KSTAT_DATA_UINT64);
526 		kstat_named_init(&kssl_statp->sid_uncached,
527 		    "kssl_sid_uncached", KSTAT_DATA_UINT64);
528 
529 		kstat_named_init(&kssl_statp->full_handshakes,
530 		    "kssl_full_handshakes", KSTAT_DATA_UINT64);
531 		kstat_named_init(&kssl_statp->resumed_sessions,
532 		    "kssl_resumed_sessions", KSTAT_DATA_UINT64);
533 		kstat_named_init(&kssl_statp->fallback_connections,
534 		    "kssl_fallback_connections", KSTAT_DATA_UINT64);
535 		kstat_named_init(&kssl_statp->proxy_fallback_failed,
536 		    "kssl_proxy_fallback_failed", KSTAT_DATA_UINT64);
537 		kstat_named_init(&kssl_statp->appdata_record_ins,
538 		    "kssl_appdata_record_ins", KSTAT_DATA_UINT64);
539 		kstat_named_init(&kssl_statp->appdata_record_outs,
540 		    "kssl_appdata_record_outs", KSTAT_DATA_UINT64);
541 
542 		kstat_named_init(&kssl_statp->alloc_fails, "kssl_alloc_fails",
543 		    KSTAT_DATA_UINT64);
544 		kstat_named_init(&kssl_statp->fatal_alerts,
545 		    "kssl_fatal_alerts", KSTAT_DATA_UINT64);
546 		kstat_named_init(&kssl_statp->warning_alerts,
547 		    "kssl_warning_alerts", KSTAT_DATA_UINT64);
548 		kstat_named_init(&kssl_statp->no_suite_found,
549 		    "kssl_no_suite_found", KSTAT_DATA_UINT64);
550 		kstat_named_init(&kssl_statp->compute_mac_failure,
551 		    "kssl_compute_mac_failure", KSTAT_DATA_UINT64);
552 		kstat_named_init(&kssl_statp->verify_mac_failure,
553 		    "kssl_verify_mac_failure", KSTAT_DATA_UINT64);
554 		kstat_named_init(&kssl_statp->record_decrypt_failure,
555 		    "kssl_record_decrypt_failure", KSTAT_DATA_UINT64);
556 		kstat_named_init(&kssl_statp->bad_pre_master_secret,
557 		    "kssl_bad_pre_master_secret", KSTAT_DATA_UINT64);
558 
559 		kstat_install(ksp);
560 	};
561 }
562 
563 /*ARGSUSED*/
564 static int
565 kssl_constructor(void *buf, void *arg, int kmflags)
566 {
567 	ssl_t *ssl = buf;
568 
569 	mutex_init(&ssl->kssl_lock, NULL, MUTEX_DEFAULT, NULL);
570 
571 	return (0);
572 }
573 
574 /*ARGSUSED*/
575 static void
576 kssl_destructor(void *buf, void *arg)
577 {
578 	ssl_t *ssl = buf;
579 	mutex_destroy(&ssl->kssl_lock);
580 }
581