Print this page
9642 PKCS#11 softtoken should use explicit_bzero
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Alex Wilson <alex.wilson@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelSoftCommon.c
+++ new/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelSoftCommon.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 + * Copyright (c) 2018, Joyent, Inc.
25 26 */
26 27
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 28 #include <pthread.h>
30 29 #include <errno.h>
31 30 #include <stdio.h>
32 31 #include <strings.h>
33 32 #include <sys/crypto/ioctl.h>
34 33 #include <security/cryptoki.h>
35 34 #include <security/pkcs11t.h>
36 35 #include "softSession.h"
37 36 #include "softObject.h"
38 37 #include "softOps.h"
39 38 #include "softMAC.h"
40 39 #include "kernelSoftCommon.h"
41 40
42 41 /*
43 42 * Do the operation(s) specified by opflag.
44 43 */
45 44 CK_RV
46 45 do_soft_digest(void **s, CK_MECHANISM_PTR pMechanism, CK_BYTE_PTR pData,
47 46 CK_ULONG ulDataLen, CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen,
48 47 int opflag)
49 48 {
50 49 soft_session_t *session_p;
51 50 CK_RV rv = CKR_ARGUMENTS_BAD;
52 51
53 52 session_p = *((soft_session_t **)s);
54 53 if (session_p == NULL) {
55 54 if (!(opflag & OP_INIT)) {
56 55 return (CKR_ARGUMENTS_BAD);
57 56 }
58 57
59 58 session_p = calloc(1, sizeof (soft_session_t));
60 59 /*
61 60 * Initialize the lock for the newly created session.
62 61 * We do only the minimum needed setup for the
63 62 * soft_digest* routines to succeed.
64 63 */
65 64 if (pthread_mutex_init(&session_p->session_mutex, NULL) != 0) {
66 65 free(session_p);
67 66 return (CKR_CANT_LOCK);
68 67 }
69 68
70 69 *s = session_p;
71 70 } else if (opflag & OP_INIT) {
72 71 free_soft_ctx(session_p, OP_DIGEST);
73 72 }
74 73
75 74 if (opflag & OP_INIT) {
76 75 rv = soft_digest_init(session_p, pMechanism);
77 76 if (rv != CKR_OK)
78 77 return (rv);
79 78 }
80 79
81 80 if (opflag & OP_SINGLE) {
82 81 rv = soft_digest(session_p, pData, ulDataLen,
83 82 pDigest, pulDigestLen);
84 83 } else {
85 84 if (opflag & OP_UPDATE) {
86 85 rv = soft_digest_update(session_p, pData, ulDataLen);
87 86 if (rv != CKR_OK)
88 87 return (rv);
89 88 }
90 89
91 90 if (opflag & OP_FINAL) {
92 91 rv = soft_digest_final(session_p,
93 92 pDigest, pulDigestLen);
94 93 }
95 94 }
96 95
97 96 return (rv);
98 97 }
99 98
100 99 /*
101 100 * opflag specifies whether this is a sign or verify.
102 101 */
103 102 CK_RV
104 103 do_soft_hmac_init(void **s, CK_MECHANISM_PTR pMechanism,
105 104 CK_BYTE_PTR kval, CK_ULONG klen, int opflag)
106 105 {
107 106 CK_RV rv;
108 107 soft_object_t keyobj;
109 108 secret_key_obj_t skeyobj;
110 109 soft_object_t *key_p;
111 110 soft_session_t *session_p;
112 111
113 112 session_p = *((soft_session_t **)s);
114 113 if (session_p == NULL) {
115 114 session_p = calloc(1, sizeof (soft_session_t));
116 115 /* See comments in do_soft_digest() above */
117 116 if (pthread_mutex_init(&session_p->session_mutex, NULL) != 0) {
118 117 free(session_p);
119 118 return (CKR_CANT_LOCK);
120 119 }
121 120
122 121 *s = session_p;
123 122 } else if (opflag & OP_INIT) {
124 123 free_soft_ctx(session_p, opflag);
125 124 }
126 125
127 126 /* Do the minimum needed setup for the call to succeed */
128 127 key_p = &keyobj;
129 128 bzero(key_p, sizeof (soft_object_t));
130 129 key_p->class = CKO_SECRET_KEY;
131 130 key_p->key_type = CKK_GENERIC_SECRET;
132 131
133 132 bzero(&skeyobj, sizeof (secret_key_obj_t));
134 133 OBJ_SEC(key_p) = &skeyobj;
135 134 OBJ_SEC_VALUE(key_p) = kval;
136 135 OBJ_SEC_VALUE_LEN(key_p) = klen;
137 136
138 137 rv = soft_hmac_sign_verify_init_common(session_p, pMechanism,
139 138 key_p, opflag & OP_SIGN);
140 139
141 140 return (rv);
142 141 }
143 142
144 143 /*
145 144 * opflag specifies whether this is a sign or verify.
146 145 */
147 146 CK_RV
148 147 do_soft_hmac_update(void **s, CK_BYTE_PTR pData, CK_ULONG ulDataLen, int opflag)
149 148 {
150 149 soft_session_t *session_p;
151 150
152 151 session_p = *((soft_session_t **)s);
153 152 if (session_p == NULL) {
154 153 return (CKR_ARGUMENTS_BAD);
155 154 }
156 155
157 156 return (soft_hmac_sign_verify_update(session_p,
158 157 pData, ulDataLen, opflag & OP_SIGN));
159 158 }
160 159
161 160 /*
162 161 * opflag specifies whether this is a final or single.
163 162 */
164 163 CK_RV
165 164 do_soft_hmac_sign(void **s, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
166 165 CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen, int opflag)
167 166 {
168 167 CK_RV rv;
169 168 soft_session_t *session_p;
170 169 CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
171 170
172 171 session_p = *((soft_session_t **)s);
173 172 if (session_p == NULL || !(opflag & OP_SINGLE || opflag & OP_FINAL)) {
174 173 return (CKR_ARGUMENTS_BAD);
175 174 }
176 175
177 176 rv = soft_hmac_sign_verify_common(session_p, pData, ulDataLen,
178 177 (pSignature != NULL ? hmac : NULL), pulSignatureLen, B_TRUE);
179 178
180 179 if ((rv == CKR_OK) && (pSignature != NULL)) {
181 180 (void) memcpy(pSignature, hmac, *pulSignatureLen);
182 181 }
183 182
184 183 return (rv);
185 184 }
186 185
187 186 /*
188 187 * opflag specifies whether this is a final or single.
189 188 */
190 189 CK_RV
191 190 do_soft_hmac_verify(void **s, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
192 191 CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen, int opflag)
193 192 {
194 193 CK_RV rv;
195 194 CK_ULONG len;
196 195 soft_session_t *session_p;
197 196 soft_hmac_ctx_t *hmac_ctx;
198 197 CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
199 198
200 199 session_p = *((soft_session_t **)s);
201 200 if (session_p == NULL || !(opflag & OP_SINGLE || opflag & OP_FINAL)) {
202 201 return (CKR_ARGUMENTS_BAD);
203 202 }
204 203
205 204 hmac_ctx = (soft_hmac_ctx_t *)session_p->verify.context;
206 205 len = hmac_ctx->hmac_len;
207 206
208 207 rv = soft_hmac_sign_verify_common(session_p, pData,
209 208 ulDataLen, hmac, &len, B_FALSE);
210 209
211 210 if (rv == CKR_OK) {
212 211 if (len != ulSignatureLen) {
213 212 rv = CKR_SIGNATURE_LEN_RANGE;
214 213 }
215 214
216 215 if (memcmp(hmac, pSignature, len) != 0) {
217 216 rv = CKR_SIGNATURE_INVALID;
218 217 }
219 218 }
220 219
221 220 return (rv);
222 221 }
223 222
224 223 /*
225 224 * Helper routine to handle the case when the ctx is abandoned.
226 225 */
↓ open down ↓ |
188 lines elided |
↑ open up ↑ |
227 226 void
228 227 free_soft_ctx(void *s, int opflag)
229 228 {
230 229 soft_session_t *session_p;
231 230
232 231 session_p = (soft_session_t *)s;
233 232 if (session_p == NULL)
234 233 return;
235 234
236 235 if (opflag & OP_SIGN) {
237 - if (session_p->sign.context == NULL)
238 - return;
239 - bzero(session_p->sign.context, sizeof (soft_hmac_ctx_t));
240 - free(session_p->sign.context);
236 + freezero(session_p->sign.context,
237 + sizeof (soft_hmac_ctx_t));
241 238 session_p->sign.context = NULL;
242 239 session_p->sign.flags = 0;
243 240 } else if (opflag & OP_VERIFY) {
244 - if (session_p->verify.context == NULL)
245 - return;
246 - bzero(session_p->verify.context, sizeof (soft_hmac_ctx_t));
247 - free(session_p->verify.context);
241 + freezero(session_p->verify.context,
242 + sizeof (soft_hmac_ctx_t));
248 243 session_p->verify.context = NULL;
249 244 session_p->verify.flags = 0;
250 245 } else {
251 - if (session_p->digest.context == NULL)
252 - return;
253 246 free(session_p->digest.context);
254 247 session_p->digest.context = NULL;
255 248 session_p->digest.flags = 0;
256 249 }
257 250 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX