xref: /illumos-gate/usr/src/cmd/mdb/common/modules/crypto/common.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * mdb dcmds for selected structures from
29  * usr/src/uts/common/sys/crypto/common.h
30  */
31 
32 #include <sys/mdb_modapi.h>
33 #include <sys/modctl.h>
34 #include <sys/types.h>
35 #include <sys/crypto/api.h>
36 #include <sys/crypto/common.h>
37 #include <sys/crypto/spi.h>
38 #include <sys/crypto/impl.h>
39 #include "crypto_cmds.h"
40 
41 /*ARGSUSED*/
42 int
crypto_mechanism(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)43 crypto_mechanism(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
44 {
45 	crypto_mechanism_t mch;
46 
47 	if (!(flags & DCMD_ADDRSPEC))
48 		return (DCMD_USAGE);
49 
50 	if (mdb_vread(&mch, sizeof (crypto_mechanism_t), addr) == -1) {
51 		mdb_warn("cannot read %p", addr);
52 		return (DCMD_ERR);
53 	}
54 	/* XXX a future RFE will interpret cm_type */
55 	mdb_printf("cm_type\t%ll#x\n", mch.cm_type);
56 	mdb_printf("cm_param\t%p\n", mch.cm_param);
57 	mdb_printf("cm_param_len\t%u\n", mch.cm_param_len);
58 	return (DCMD_OK);
59 }
60 
61 /*ARGSUSED*/
62 static void
iovec_prt(iovec_t * addr)63 iovec_prt(iovec_t *addr)
64 {
65 	mdb_printf("iov_base\t%p\n", addr->iov_base);
66 	mdb_printf("iov_len\t\t%d\n", addr->iov_len);
67 }
68 
69 /*ARGSUSED*/
70 static void
uio_prt(uio_t * addr)71 uio_prt(uio_t *addr)
72 {
73 	char *segstrings[] = {
74 		"UIO_USERSPACE",
75 		"UIO_SYSSPACE",
76 		"UIO_USERISPACE"
77 	};
78 	iovec_t iov;
79 	uio_t uio;
80 	int i;
81 
82 	mdb_printf("uio\t%p\n", addr);
83 	if (mdb_vread(&uio, sizeof (uio_t), (uintptr_t)addr)
84 		== -1) {
85 		mdb_warn("uio_prt: could not read uio");
86 	}
87 	mdb_inc_indent(4);
88 	for (i = 0; i < uio.uio_iovcnt; i++) {
89 		if (mdb_vread(&iov, sizeof (iovec_t),
90 		    (uintptr_t)(uio.uio_iov +i))
91 			== -1) {
92 			mdb_printf("uio_iov\t?????");
93 			mdb_warn("uio_prt: could not read uio_iov[%s]", i);
94 		} else
95 		    iovec_prt(&iov);
96 	}
97 	mdb_dec_indent(4);
98 	mdb_printf("uio_iovcnt\t%d\n", uio.uio_iovcnt);
99 	mdb_printf("uio_offset\t%lld\n", uio.uio_offset);
100 	mdb_printf("uio_segflg\t%s", segstrings[uio.uio_segflg]);
101 	mdb_printf("uio_fmode\t0%o", (int)uio.uio_fmode);
102 	mdb_printf("uio_limit\t%lld", uio.uio_limit);
103 	mdb_printf("uio_resid\t%ld", uio.uio_resid);
104 }
105 
106 static char *cdstrings[] = {
107 	"INVALID FORMAT",
108 	"CRYPTO_DATA_RAW",
109 	"CRYPTO_DATA_UIO",
110 	"CRYPTO_DATA_MBLK"
111 };
112 
113 /*
114  * Routine to print either of two structrually identical sub-structures --
115  * with different naming conventions.  Might be changed if we decide
116  * to merge the two.  They are the cdu union from crypto_data_t and
117  * the one from crypto_dual_data_t.
118  */
119 
120 typedef union crypto_data_union {
121 	iovec_t	cdu_raw;		/* Raw format */
122 	uio_t	*cdu_uio;		/* uio scatter-gather format */
123 	mblk_t	*cdu_mp;		/* The mblk chain */
124 } crypto_data_union_t;
125 
126 /*ARGSUSED*/
127 static void
prt_cdu(crypto_data_union_t * cdu,int format,const char * prefix)128 prt_cdu(crypto_data_union_t *cdu, int format, const char *prefix)
129 {
130 
131 	switch (format) {
132 		case CRYPTO_DATA_RAW:
133 		    mdb_printf("%s_raw:\n", prefix);
134 		    mdb_inc_indent(4);
135 		    iovec_prt(&cdu->cdu_raw);
136 		    mdb_dec_indent(4);
137 		    break;
138 
139 		case CRYPTO_DATA_UIO:
140 		    mdb_printf("%s_uio:\n", prefix);
141 		    mdb_inc_indent(4);
142 		    uio_prt(cdu->cdu_uio);
143 		    mdb_dec_indent(4);
144 		    break;
145 
146 		case CRYPTO_DATA_MBLK:
147 		    mdb_printf("%s_mp:\t\t%p\n", prefix, cdu->cdu_mp);
148 		    break;
149 
150 		default:
151 		    mdb_printf("cm_format\t??????\n");
152 		    break;
153 	}
154 }
155 
156 /*ARGSUSED*/
157 int
crypto_data(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)158 crypto_data(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
159 {
160 	crypto_data_t data;
161 
162 	if (!(flags & DCMD_ADDRSPEC))
163 		return (DCMD_USAGE);
164 
165 	if (mdb_vread(&data, sizeof (crypto_data_t), addr) == -1) {
166 		mdb_warn("cannot read %p", addr);
167 		return (DCMD_ERR);
168 	}
169 	if ((data.cd_format >= CRYPTO_DATA_RAW) &&
170 	    (data.cd_format <= CRYPTO_DATA_MBLK))
171 		mdb_printf("cm_format\t%s\n", cdstrings[data.cd_format]);
172 	else
173 		mdb_printf("bad cm_format\t%d\n", data.cd_format);
174 	mdb_printf("cm_offset\t%ld\n", data.cd_offset);
175 	mdb_printf("cm_length\t%ld\n", data.cd_length);
176 	mdb_printf("cm_miscdata\t%p\n", data.cd_miscdata);
177 	mdb_inc_indent(4);
178 	prt_cdu((crypto_data_union_t *)&data.cdu, data.cd_format, "cdu");
179 	mdb_dec_indent(4);
180 	return (DCMD_OK);
181 }
182 
183 /*ARGSUSED*/
184 int
crypto_dual_data(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)185 crypto_dual_data(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
186 {
187 	crypto_dual_data_t ddata;
188 
189 	if (!(flags & DCMD_ADDRSPEC))
190 		return (DCMD_USAGE);
191 
192 	if (mdb_vread(&ddata, sizeof (crypto_dual_data_t), addr) == -1) {
193 		mdb_warn("cannot read %p", addr);
194 		return (DCMD_ERR);
195 	}
196 	if ((ddata.dd_format > CRYPTO_DATA_RAW) &&
197 	    (ddata.dd_format <= CRYPTO_DATA_MBLK))
198 		mdb_printf("dd_format\t%s\n", cdstrings[ddata.dd_format]);
199 	else
200 		mdb_printf("bad dd_format\t%d\n", ddata.dd_format);
201 	mdb_printf("dd_offset1\t%ld\n", ddata.dd_offset1);
202 	mdb_printf("dd_len1\t%ld\n", ddata.dd_len1);
203 	mdb_printf("dd_offset2\t%ld\n", ddata.dd_offset2);
204 	mdb_printf("dd_len2\t%ld\n", ddata.dd_len2);
205 	mdb_printf("dd_miscdata\t%p\n", ddata.dd_miscdata);
206 	mdb_printf("cdu:\n");
207 	mdb_inc_indent(4);
208 	prt_cdu((crypto_data_union_t *)&ddata.dd_data.cdu, ddata.dd_format,
209 	    "ddu");
210 	mdb_dec_indent(4);
211 	return (DCMD_OK);
212 }
213 
214 
215 /*ARGSUSED*/
216 int
crypto_key(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)217 crypto_key(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
218 {
219 	crypto_key_t key;
220 
221 	if (!(flags & DCMD_ADDRSPEC))
222 		return (DCMD_USAGE);
223 
224 	if (mdb_vread(&key, sizeof (crypto_key_t), addr) == -1) {
225 		mdb_warn("cannot read %p", addr);
226 		return (DCMD_ERR);
227 	}
228 	switch (key.ck_format) {
229 		case CRYPTO_KEY_RAW:
230 		    mdb_printf("ck_format:\tCRYPTO_KEY_RAW\n");
231 		    mdb_printf(
232 			"cku_data.cku_key_value.cku_data.cku_v_length:\t%d\n",
233 			    key.cku_data.cku_key_value.cku_v_length);
234 		    mdb_printf("cku_data.cku_key_value.cku_v_data:\t%p\n",
235 			key.cku_data.cku_key_value.cku_v_data);
236 		    break;
237 		case CRYPTO_KEY_REFERENCE:
238 		    mdb_printf("ck_format:\tCRYPTO_KEY_REFERENCE\n");
239 		    mdb_printf("cku_data.cku_key_id:\t%u\n",
240 			key.cku_data.cku_key_id);
241 		    break;
242 		case CRYPTO_KEY_ATTR_LIST:
243 			mdb_printf("ck_format:\tCRYPTO_KEY_ATTR_LIST\n");
244 			mdb_printf("cku_data.cku_key_attrs.cku_a_count:\t%u\n",
245 				key.cku_data.cku_key_attrs.cku_a_count);
246 			mdb_printf("cku_data.cku_key_attrs.cku_o_oattr:\t%p\n",
247 				key.cku_data.cku_key_attrs.cku_a_oattr);
248 			break;
249 		default:
250 			mdb_printf("ck_format:\t\t?????\n");
251 			break;
252 	}
253 	return (DCMD_OK);
254 }
255