0
|
1 |
/*
|
|
2 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
|
|
3 |
* Use is subject to license terms.
|
|
4 |
*/
|
|
5 |
|
|
6 |
#pragma ident "@(#)hw_pk11_pub.c 1.1 09/08/27 SMI"
|
|
7 |
|
|
8 |
/* crypto/engine/hw_pk11_pub.c */
|
|
9 |
/*
|
|
10 |
* This product includes software developed by the OpenSSL Project for
|
|
11 |
* use in the OpenSSL Toolkit (http://www.openssl.org/).
|
|
12 |
*
|
|
13 |
* This project also referenced hw_pkcs11-0.9.7b.patch written by
|
|
14 |
* Afchine Madjlessi.
|
|
15 |
*/
|
|
16 |
/*
|
|
17 |
* ====================================================================
|
|
18 |
* Copyright (c) 2000-2001 The OpenSSL Project. All rights reserved.
|
|
19 |
*
|
|
20 |
* Redistribution and use in source and binary forms, with or without
|
|
21 |
* modification, are permitted provided that the following conditions
|
|
22 |
* are met:
|
|
23 |
*
|
|
24 |
* 1. Redistributions of source code must retain the above copyright
|
|
25 |
* notice, this list of conditions and the following disclaimer.
|
|
26 |
*
|
|
27 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
28 |
* notice, this list of conditions and the following disclaimer in
|
|
29 |
* the documentation and/or other materials provided with the
|
|
30 |
* distribution.
|
|
31 |
*
|
|
32 |
* 3. All advertising materials mentioning features or use of this
|
|
33 |
* software must display the following acknowledgment:
|
|
34 |
* "This product includes software developed by the OpenSSL Project
|
|
35 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
36 |
*
|
|
37 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
38 |
* endorse or promote products derived from this software without
|
|
39 |
* prior written permission. For written permission, please contact
|
|
40 |
* [email protected].
|
|
41 |
*
|
|
42 |
* 5. Products derived from this software may not be called "OpenSSL"
|
|
43 |
* nor may "OpenSSL" appear in their names without prior written
|
|
44 |
* permission of the OpenSSL Project.
|
|
45 |
*
|
|
46 |
* 6. Redistributions of any form whatsoever must retain the following
|
|
47 |
* acknowledgment:
|
|
48 |
* "This product includes software developed by the OpenSSL Project
|
|
49 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
50 |
*
|
|
51 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
52 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
53 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
54 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
55 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
56 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
57 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
58 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
59 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
60 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
61 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
62 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
63 |
* ====================================================================
|
|
64 |
*
|
|
65 |
* This product includes cryptographic software written by Eric Young
|
|
66 |
* ([email protected]). This product includes software written by Tim
|
|
67 |
* Hudson ([email protected]).
|
|
68 |
*
|
|
69 |
*/
|
|
70 |
|
|
71 |
#include <stdio.h>
|
|
72 |
#include <stdlib.h>
|
|
73 |
#include <string.h>
|
|
74 |
#include <sys/types.h>
|
|
75 |
#include <unistd.h>
|
|
76 |
|
|
77 |
#include <openssl/e_os2.h>
|
|
78 |
#include <openssl/crypto.h>
|
|
79 |
#include <openssl/engine.h>
|
|
80 |
#include <openssl/dso.h>
|
|
81 |
#include <openssl/err.h>
|
|
82 |
#include <openssl/bn.h>
|
|
83 |
#include <openssl/pem.h>
|
|
84 |
#ifndef OPENSSL_NO_RSA
|
|
85 |
#include <openssl/rsa.h>
|
|
86 |
#endif /* OPENSSL_NO_RSA */
|
|
87 |
#ifndef OPENSSL_NO_DSA
|
|
88 |
#include <openssl/dsa.h>
|
|
89 |
#endif /* OPENSSL_NO_DSA */
|
|
90 |
#ifndef OPENSSL_NO_DH
|
|
91 |
#include <openssl/dh.h>
|
|
92 |
#endif /* OPENSSL_NO_DH */
|
|
93 |
#include <openssl/rand.h>
|
|
94 |
#include <openssl/objects.h>
|
|
95 |
#include <openssl/x509.h>
|
|
96 |
#include <cryptlib.h>
|
|
97 |
#include <pthread.h>
|
|
98 |
|
|
99 |
#ifndef OPENSSL_NO_HW
|
|
100 |
#ifndef OPENSSL_NO_HW_PK11
|
|
101 |
|
|
102 |
#include "cryptoki.h"
|
|
103 |
#include "pkcs11.h"
|
|
104 |
#include "hw_pk11_err.h"
|
|
105 |
|
|
106 |
#ifndef OPENSSL_NO_RSA
|
|
107 |
/* RSA stuff */
|
|
108 |
static int pk11_RSA_public_encrypt(int flen, const unsigned char *from,
|
|
109 |
unsigned char *to, RSA *rsa, int padding);
|
|
110 |
static int pk11_RSA_private_encrypt(int flen, const unsigned char *from,
|
|
111 |
unsigned char *to, RSA *rsa, int padding);
|
|
112 |
static int pk11_RSA_public_decrypt(int flen, const unsigned char *from,
|
|
113 |
unsigned char *to, RSA *rsa, int padding);
|
|
114 |
static int pk11_RSA_private_decrypt(int flen, const unsigned char *from,
|
|
115 |
unsigned char *to, RSA *rsa, int padding);
|
|
116 |
static int pk11_RSA_init(RSA *rsa);
|
|
117 |
static int pk11_RSA_finish(RSA *rsa);
|
|
118 |
static int pk11_RSA_sign(int type, const unsigned char *m, unsigned int m_len,
|
|
119 |
unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
|
|
120 |
static int pk11_RSA_verify(int dtype, const unsigned char *m,
|
|
121 |
unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,
|
|
122 |
const RSA *rsa);
|
|
123 |
EVP_PKEY *pk11_load_privkey(ENGINE*, const char *pubkey_file,
|
|
124 |
UI_METHOD *ui_method, void *callback_data);
|
|
125 |
EVP_PKEY *pk11_load_pubkey(ENGINE*, const char *pubkey_file,
|
|
126 |
UI_METHOD *ui_method, void *callback_data);
|
|
127 |
|
|
128 |
static int pk11_RSA_public_encrypt_low(int flen, const unsigned char *from,
|
|
129 |
unsigned char *to, RSA *rsa);
|
|
130 |
static int pk11_RSA_private_encrypt_low(int flen, const unsigned char *from,
|
|
131 |
unsigned char *to, RSA *rsa);
|
|
132 |
static int pk11_RSA_public_decrypt_low(int flen, const unsigned char *from,
|
|
133 |
unsigned char *to, RSA *rsa);
|
|
134 |
static int pk11_RSA_private_decrypt_low(int flen, const unsigned char *from,
|
|
135 |
unsigned char *to, RSA *rsa);
|
|
136 |
|
|
137 |
static CK_OBJECT_HANDLE pk11_get_public_rsa_key(RSA* rsa, RSA** key_ptr,
|
|
138 |
BIGNUM **rsa_n_num, BIGNUM **rsa_e_num, CK_SESSION_HANDLE session);
|
|
139 |
static CK_OBJECT_HANDLE pk11_get_private_rsa_key(RSA* rsa, RSA** key_ptr,
|
|
140 |
BIGNUM **rsa_d_num, CK_SESSION_HANDLE session);
|
|
141 |
|
|
142 |
static int check_new_rsa_key_pub(PK11_SESSION *sp, const RSA *rsa);
|
|
143 |
static int check_new_rsa_key_priv(PK11_SESSION *sp, const RSA *rsa);
|
|
144 |
#endif
|
|
145 |
|
|
146 |
/* DSA stuff */
|
|
147 |
#ifndef OPENSSL_NO_DSA
|
|
148 |
static int pk11_DSA_init(DSA *dsa);
|
|
149 |
static int pk11_DSA_finish(DSA *dsa);
|
|
150 |
static DSA_SIG *pk11_dsa_do_sign(const unsigned char *dgst, int dlen,
|
|
151 |
DSA *dsa);
|
|
152 |
static int pk11_dsa_do_verify(const unsigned char *dgst, int dgst_len,
|
|
153 |
DSA_SIG *sig, DSA *dsa);
|
|
154 |
|
|
155 |
static CK_OBJECT_HANDLE pk11_get_public_dsa_key(DSA* dsa, DSA **key_ptr,
|
|
156 |
BIGNUM **dsa_pub_num, CK_SESSION_HANDLE session);
|
|
157 |
static CK_OBJECT_HANDLE pk11_get_private_dsa_key(DSA* dsa, DSA **key_ptr,
|
|
158 |
BIGNUM **dsa_priv_num, CK_SESSION_HANDLE session);
|
|
159 |
|
|
160 |
static int check_new_dsa_key_pub(PK11_SESSION *sp, DSA *dsa);
|
|
161 |
static int check_new_dsa_key_priv(PK11_SESSION *sp, DSA *dsa);
|
|
162 |
#endif
|
|
163 |
|
|
164 |
/* DH stuff */
|
|
165 |
#ifndef OPENSSL_NO_DH
|
|
166 |
static int pk11_DH_init(DH *dh);
|
|
167 |
static int pk11_DH_finish(DH *dh);
|
|
168 |
static int pk11_DH_generate_key(DH *dh);
|
|
169 |
static int pk11_DH_compute_key(unsigned char *key,
|
|
170 |
const BIGNUM *pub_key, DH *dh);
|
|
171 |
|
|
172 |
static CK_OBJECT_HANDLE pk11_get_dh_key(DH* dh, DH **key_ptr,
|
|
173 |
BIGNUM **priv_key, CK_SESSION_HANDLE session);
|
|
174 |
|
|
175 |
static int check_new_dh_key(PK11_SESSION *sp, DH *dh);
|
|
176 |
#endif
|
|
177 |
|
|
178 |
static int init_template_value(BIGNUM *bn, CK_VOID_PTR *pValue,
|
|
179 |
CK_ULONG *ulValueLen);
|
|
180 |
|
|
181 |
/* Read mode string to be used for fopen() */
|
|
182 |
#if SOLARIS_OPENSSL
|
|
183 |
static char *read_mode_flags = "rF";
|
|
184 |
#else
|
|
185 |
static char *read_mode_flags = "r";
|
|
186 |
#endif
|
|
187 |
|
|
188 |
/*
|
|
189 |
* increment/create reference for an asymmetric key handle via active list
|
|
190 |
* manipulation. If active list operation fails, unlock (if locked), set error
|
|
191 |
* variable and jump to the specified label.
|
|
192 |
*/
|
|
193 |
#define KEY_HANDLE_REFHOLD(key_handle, alg_type, unlock, var, label) \
|
|
194 |
{ \
|
|
195 |
if (pk11_active_add(key_handle, alg_type) < 0) \
|
|
196 |
{ \
|
|
197 |
var = TRUE; \
|
|
198 |
if (unlock) \
|
|
199 |
UNLOCK_OBJSTORE(alg_type); \
|
|
200 |
goto label; \
|
|
201 |
} \
|
|
202 |
}
|
|
203 |
|
|
204 |
/*
|
|
205 |
* Find active list entry according to object handle and return pointer to the
|
|
206 |
* entry otherwise return NULL.
|
|
207 |
*
|
|
208 |
* This function presumes it is called with lock protecting the active list
|
|
209 |
* held.
|
|
210 |
*/
|
|
211 |
static PK11_active *pk11_active_find(CK_OBJECT_HANDLE h, PK11_OPTYPE type)
|
|
212 |
{
|
|
213 |
PK11_active *entry;
|
|
214 |
|
|
215 |
for (entry = active_list[type]; entry != NULL; entry = entry->next)
|
|
216 |
if (entry->h == h)
|
|
217 |
return (entry);
|
|
218 |
|
|
219 |
return (NULL);
|
|
220 |
}
|
|
221 |
|
|
222 |
/*
|
|
223 |
* Search for an entry in the active list using PKCS#11 object handle as a
|
|
224 |
* search key and return refcnt of the found/created entry or -1 in case of
|
|
225 |
* failure.
|
|
226 |
*
|
|
227 |
* This function presumes it is called with lock protecting the active list
|
|
228 |
* held.
|
|
229 |
*/
|
|
230 |
int
|
|
231 |
pk11_active_add(CK_OBJECT_HANDLE h, PK11_OPTYPE type)
|
|
232 |
{
|
|
233 |
PK11_active *entry = NULL;
|
|
234 |
|
|
235 |
if (h == CK_INVALID_HANDLE)
|
|
236 |
{
|
|
237 |
PK11err(PK11_F_ACTIVE_ADD, PK11_R_INVALID_HANDLE);
|
|
238 |
return (-1);
|
|
239 |
}
|
|
240 |
|
|
241 |
/* search for entry in the active list */
|
|
242 |
if ((entry = pk11_active_find(h, type)) != NULL)
|
|
243 |
entry->refcnt++;
|
|
244 |
else
|
|
245 |
{
|
|
246 |
/* not found, create new entry and add it to the list */
|
|
247 |
entry = OPENSSL_malloc(sizeof (PK11_active));
|
|
248 |
if (entry == NULL)
|
|
249 |
{
|
|
250 |
PK11err(PK11_F_ACTIVE_ADD, PK11_R_MALLOC_FAILURE);
|
|
251 |
return (-1);
|
|
252 |
}
|
|
253 |
entry->h = h;
|
|
254 |
entry->refcnt = 1;
|
|
255 |
entry->prev = NULL;
|
|
256 |
entry->next = NULL;
|
|
257 |
/* connect the newly created entry to the list */
|
|
258 |
if (active_list[type] == NULL)
|
|
259 |
active_list[type] = entry;
|
|
260 |
else /* make the entry first in the list */
|
|
261 |
{
|
|
262 |
entry->next = active_list[type];
|
|
263 |
active_list[type]->prev = entry;
|
|
264 |
active_list[type] = entry;
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
return (entry->refcnt);
|
|
269 |
}
|
|
270 |
|
|
271 |
/*
|
|
272 |
* Remove active list entry from the list and free it.
|
|
273 |
*
|
|
274 |
* This function presumes it is called with lock protecting the active list
|
|
275 |
* held.
|
|
276 |
*/
|
|
277 |
void
|
|
278 |
pk11_active_remove(PK11_active *entry, PK11_OPTYPE type)
|
|
279 |
{
|
|
280 |
PK11_active *prev_entry;
|
|
281 |
|
|
282 |
/* remove the entry from the list and free it */
|
|
283 |
if ((prev_entry = entry->prev) != NULL)
|
|
284 |
{
|
|
285 |
prev_entry->next = entry->next;
|
|
286 |
if (entry->next != NULL)
|
|
287 |
entry->next->prev = prev_entry;
|
|
288 |
}
|
|
289 |
else
|
|
290 |
{
|
|
291 |
active_list[type] = entry->next;
|
|
292 |
/* we were the first but not the only one */
|
|
293 |
if (entry->next != NULL)
|
|
294 |
entry->next->prev = NULL;
|
|
295 |
}
|
|
296 |
|
|
297 |
/* sanitization */
|
|
298 |
entry->h = CK_INVALID_HANDLE;
|
|
299 |
entry->prev = NULL;
|
|
300 |
entry->next = NULL;
|
|
301 |
OPENSSL_free(entry);
|
|
302 |
}
|
|
303 |
|
|
304 |
/* Free all entries from the active list. */
|
|
305 |
void
|
|
306 |
pk11_free_active_list(PK11_OPTYPE type)
|
|
307 |
{
|
|
308 |
PK11_active *entry;
|
|
309 |
|
|
310 |
/* only for asymmetric types since only they have C_Find* locks. */
|
|
311 |
switch (type)
|
|
312 |
{
|
|
313 |
case OP_RSA:
|
|
314 |
case OP_DSA:
|
|
315 |
case OP_DH:
|
|
316 |
break;
|
|
317 |
default:
|
|
318 |
return;
|
|
319 |
}
|
|
320 |
|
|
321 |
/* see find_lock array definition for more info on object locking */
|
|
322 |
LOCK_OBJSTORE(type);
|
|
323 |
while ((entry = active_list[type]) != NULL)
|
|
324 |
pk11_active_remove(entry, type);
|
|
325 |
UNLOCK_OBJSTORE(type);
|
|
326 |
}
|
|
327 |
|
|
328 |
/*
|
|
329 |
* Search for active list entry associated with given PKCS#11 object handle,
|
|
330 |
* decrement its refcnt and if it drops to 0, disconnect the entry and free it.
|
|
331 |
*
|
|
332 |
* Return 1 if the PKCS#11 object associated with the entry has no references,
|
|
333 |
* return 0 if there is at least one reference, -1 on error.
|
|
334 |
*
|
|
335 |
* This function presumes it is called with lock protecting the active list
|
|
336 |
* held.
|
|
337 |
*/
|
|
338 |
int
|
|
339 |
pk11_active_delete(CK_OBJECT_HANDLE h, PK11_OPTYPE type)
|
|
340 |
{
|
|
341 |
PK11_active *entry = NULL;
|
|
342 |
|
|
343 |
if ((entry = pk11_active_find(h, type)) == NULL)
|
|
344 |
{
|
|
345 |
PK11err(PK11_F_ACTIVE_DELETE, PK11_R_INVALID_HANDLE);
|
|
346 |
return (-1);
|
|
347 |
}
|
|
348 |
|
|
349 |
OPENSSL_assert(entry->refcnt > 0);
|
|
350 |
entry->refcnt--;
|
|
351 |
if (entry->refcnt == 0)
|
|
352 |
{
|
|
353 |
pk11_active_remove(entry, type);
|
|
354 |
return (1);
|
|
355 |
}
|
|
356 |
|
|
357 |
return (0);
|
|
358 |
}
|
|
359 |
|
|
360 |
#ifndef OPENSSL_NO_RSA
|
|
361 |
/* Our internal RSA_METHOD that we provide pointers to */
|
|
362 |
static RSA_METHOD pk11_rsa =
|
|
363 |
{
|
|
364 |
"PKCS#11 RSA method",
|
|
365 |
pk11_RSA_public_encrypt, /* rsa_pub_encrypt */
|
|
366 |
pk11_RSA_public_decrypt, /* rsa_pub_decrypt */
|
|
367 |
pk11_RSA_private_encrypt, /* rsa_priv_encrypt */
|
|
368 |
pk11_RSA_private_decrypt, /* rsa_priv_decrypt */
|
|
369 |
NULL, /* rsa_mod_exp */
|
|
370 |
NULL, /* bn_mod_exp */
|
|
371 |
pk11_RSA_init, /* init */
|
|
372 |
pk11_RSA_finish, /* finish */
|
|
373 |
RSA_FLAG_SIGN_VER, /* flags */
|
|
374 |
NULL, /* app_data */
|
|
375 |
pk11_RSA_sign, /* rsa_sign */
|
|
376 |
pk11_RSA_verify /* rsa_verify */
|
|
377 |
};
|
|
378 |
|
|
379 |
RSA_METHOD *
|
|
380 |
PK11_RSA(void)
|
|
381 |
{
|
|
382 |
return (&pk11_rsa);
|
|
383 |
}
|
|
384 |
#endif
|
|
385 |
|
|
386 |
#ifndef OPENSSL_NO_DSA
|
|
387 |
/* Our internal DSA_METHOD that we provide pointers to */
|
|
388 |
static DSA_METHOD pk11_dsa =
|
|
389 |
{
|
|
390 |
"PKCS#11 DSA method",
|
|
391 |
pk11_dsa_do_sign, /* dsa_do_sign */
|
|
392 |
NULL, /* dsa_sign_setup */
|
|
393 |
pk11_dsa_do_verify, /* dsa_do_verify */
|
|
394 |
NULL, /* dsa_mod_exp */
|
|
395 |
NULL, /* bn_mod_exp */
|
|
396 |
pk11_DSA_init, /* init */
|
|
397 |
pk11_DSA_finish, /* finish */
|
|
398 |
0, /* flags */
|
|
399 |
NULL /* app_data */
|
|
400 |
};
|
|
401 |
|
|
402 |
DSA_METHOD *
|
|
403 |
PK11_DSA(void)
|
|
404 |
{
|
|
405 |
return (&pk11_dsa);
|
|
406 |
}
|
|
407 |
#endif
|
|
408 |
|
|
409 |
#ifndef OPENSSL_NO_DH
|
|
410 |
/*
|
|
411 |
* PKCS #11 V2.20, section 11.2 specifies that the number of bytes needed for
|
|
412 |
* output buffer may somewhat exceed the precise number of bytes needed, but
|
|
413 |
* should not exceed it by a large amount. That may be caused, for example, by
|
|
414 |
* rounding it up to multiple of X in the underlying bignum library. 8 should be
|
|
415 |
* enough.
|
|
416 |
*/
|
|
417 |
#define DH_BUF_RESERVE 8
|
|
418 |
|
|
419 |
/* Our internal DH_METHOD that we provide pointers to */
|
|
420 |
static DH_METHOD pk11_dh =
|
|
421 |
{
|
|
422 |
"PKCS#11 DH method",
|
|
423 |
pk11_DH_generate_key, /* generate_key */
|
|
424 |
pk11_DH_compute_key, /* compute_key */
|
|
425 |
NULL, /* bn_mod_exp */
|
|
426 |
pk11_DH_init, /* init */
|
|
427 |
pk11_DH_finish, /* finish */
|
|
428 |
0, /* flags */
|
|
429 |
NULL, /* app_data */
|
|
430 |
NULL /* generate_params */
|
|
431 |
};
|
|
432 |
|
|
433 |
DH_METHOD *
|
|
434 |
PK11_DH(void)
|
|
435 |
{
|
|
436 |
return (&pk11_dh);
|
|
437 |
}
|
|
438 |
#endif
|
|
439 |
|
|
440 |
/* Size of an SSL signature: MD5+SHA1 */
|
|
441 |
#define SSL_SIG_LENGTH 36
|
|
442 |
|
|
443 |
/* Lengths of DSA data and signature */
|
|
444 |
#define DSA_DATA_LEN 20
|
|
445 |
#define DSA_SIGNATURE_LEN 40
|
|
446 |
|
|
447 |
static CK_BBOOL true = TRUE;
|
|
448 |
static CK_BBOOL false = FALSE;
|
|
449 |
|
|
450 |
#ifndef OPENSSL_NO_RSA
|
|
451 |
/*
|
|
452 |
* Similiar to OpenSSL to take advantage of the paddings. The goal is to
|
|
453 |
* support all paddings in this engine although PK11 library does not
|
|
454 |
* support all the paddings used in OpenSSL.
|
|
455 |
* The input errors should have been checked in the padding functions.
|
|
456 |
*/
|
|
457 |
static int pk11_RSA_public_encrypt(int flen, const unsigned char *from,
|
|
458 |
unsigned char *to, RSA *rsa, int padding)
|
|
459 |
{
|
|
460 |
int i, num = 0, r = -1;
|
|
461 |
unsigned char *buf = NULL;
|
|
462 |
|
|
463 |
num = BN_num_bytes(rsa->n);
|
|
464 |
if ((buf = (unsigned char *)OPENSSL_malloc(num)) == NULL)
|
|
465 |
{
|
|
466 |
RSAerr(PK11_F_RSA_PUB_ENC, PK11_R_MALLOC_FAILURE);
|
|
467 |
goto err;
|
|
468 |
}
|
|
469 |
|
|
470 |
switch (padding)
|
|
471 |
{
|
|
472 |
case RSA_PKCS1_PADDING:
|
|
473 |
i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
|
|
474 |
break;
|
|
475 |
#ifndef OPENSSL_NO_SHA
|
|
476 |
case RSA_PKCS1_OAEP_PADDING:
|
|
477 |
i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
|
|
478 |
break;
|
|
479 |
#endif
|
|
480 |
case RSA_SSLV23_PADDING:
|
|
481 |
i = RSA_padding_add_SSLv23(buf, num, from, flen);
|
|
482 |
break;
|
|
483 |
case RSA_NO_PADDING:
|
|
484 |
i = RSA_padding_add_none(buf, num, from, flen);
|
|
485 |
break;
|
|
486 |
default:
|
|
487 |
RSAerr(PK11_F_RSA_PUB_ENC, PK11_R_UNKNOWN_PADDING_TYPE);
|
|
488 |
goto err;
|
|
489 |
}
|
|
490 |
if (i <= 0) goto err;
|
|
491 |
|
|
492 |
/* PK11 functions are called here */
|
|
493 |
r = pk11_RSA_public_encrypt_low(num, buf, to, rsa);
|
|
494 |
err:
|
|
495 |
if (buf != NULL)
|
|
496 |
{
|
|
497 |
OPENSSL_cleanse(buf, num);
|
|
498 |
OPENSSL_free(buf);
|
|
499 |
}
|
|
500 |
return (r);
|
|
501 |
}
|
|
502 |
|
|
503 |
|
|
504 |
/*
|
|
505 |
* Similar to Openssl to take advantage of the paddings. The input errors
|
|
506 |
* should be catched in the padding functions
|
|
507 |
*/
|
|
508 |
static int pk11_RSA_private_encrypt(int flen, const unsigned char *from,
|
|
509 |
unsigned char *to, RSA *rsa, int padding)
|
|
510 |
{
|
|
511 |
int i, num = 0, r = -1;
|
|
512 |
unsigned char *buf = NULL;
|
|
513 |
|
|
514 |
num = BN_num_bytes(rsa->n);
|
|
515 |
if ((buf = (unsigned char *)OPENSSL_malloc(num)) == NULL)
|
|
516 |
{
|
|
517 |
RSAerr(PK11_F_RSA_PRIV_ENC, PK11_R_MALLOC_FAILURE);
|
|
518 |
goto err;
|
|
519 |
}
|
|
520 |
|
|
521 |
switch (padding)
|
|
522 |
{
|
|
523 |
case RSA_PKCS1_PADDING:
|
|
524 |
i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
|
|
525 |
break;
|
|
526 |
case RSA_NO_PADDING:
|
|
527 |
i = RSA_padding_add_none(buf, num, from, flen);
|
|
528 |
break;
|
|
529 |
case RSA_SSLV23_PADDING:
|
|
530 |
default:
|
|
531 |
RSAerr(PK11_F_RSA_PRIV_ENC, PK11_R_UNKNOWN_PADDING_TYPE);
|
|
532 |
goto err;
|
|
533 |
}
|
|
534 |
if (i <= 0) goto err;
|
|
535 |
|
|
536 |
/* PK11 functions are called here */
|
|
537 |
r = pk11_RSA_private_encrypt_low(num, buf, to, rsa);
|
|
538 |
err:
|
|
539 |
if (buf != NULL)
|
|
540 |
{
|
|
541 |
OPENSSL_cleanse(buf, num);
|
|
542 |
OPENSSL_free(buf);
|
|
543 |
}
|
|
544 |
return (r);
|
|
545 |
}
|
|
546 |
|
|
547 |
/* Similar to OpenSSL code. Input errors are also checked here */
|
|
548 |
static int pk11_RSA_private_decrypt(int flen, const unsigned char *from,
|
|
549 |
unsigned char *to, RSA *rsa, int padding)
|
|
550 |
{
|
|
551 |
BIGNUM f;
|
|
552 |
int j, num = 0, r = -1;
|
|
553 |
unsigned char *p;
|
|
554 |
unsigned char *buf = NULL;
|
|
555 |
|
|
556 |
BN_init(&f);
|
|
557 |
|
|
558 |
num = BN_num_bytes(rsa->n);
|
|
559 |
|
|
560 |
if ((buf = (unsigned char *)OPENSSL_malloc(num)) == NULL)
|
|
561 |
{
|
|
562 |
RSAerr(PK11_F_RSA_PRIV_DEC, PK11_R_MALLOC_FAILURE);
|
|
563 |
goto err;
|
|
564 |
}
|
|
565 |
|
|
566 |
/*
|
|
567 |
* This check was for equality but PGP does evil things
|
|
568 |
* and chops off the top '0' bytes
|
|
569 |
*/
|
|
570 |
if (flen > num)
|
|
571 |
{
|
|
572 |
RSAerr(PK11_F_RSA_PRIV_DEC,
|
|
573 |
PK11_R_DATA_GREATER_THAN_MOD_LEN);
|
|
574 |
goto err;
|
|
575 |
}
|
|
576 |
|
|
577 |
/* make data into a big number */
|
|
578 |
if (BN_bin2bn(from, (int)flen, &f) == NULL)
|
|
579 |
goto err;
|
|
580 |
|
|
581 |
if (BN_ucmp(&f, rsa->n) >= 0)
|
|
582 |
{
|
|
583 |
RSAerr(PK11_F_RSA_PRIV_DEC,
|
|
584 |
PK11_R_DATA_TOO_LARGE_FOR_MODULUS);
|
|
585 |
goto err;
|
|
586 |
}
|
|
587 |
|
|
588 |
/* PK11 functions are called here */
|
|
589 |
r = pk11_RSA_private_decrypt_low(flen, from, buf, rsa);
|
|
590 |
|
|
591 |
/*
|
|
592 |
* PK11 CKM_RSA_X_509 mechanism pads 0's at the beginning.
|
|
593 |
* Needs to skip these 0's paddings here.
|
|
594 |
*/
|
|
595 |
for (j = 0; j < r; j++)
|
|
596 |
if (buf[j] != 0)
|
|
597 |
break;
|
|
598 |
|
|
599 |
p = buf + j;
|
|
600 |
j = r - j; /* j is only used with no-padding mode */
|
|
601 |
|
|
602 |
switch (padding)
|
|
603 |
{
|
|
604 |
case RSA_PKCS1_PADDING:
|
|
605 |
r = RSA_padding_check_PKCS1_type_2(to, num, p, j, num);
|
|
606 |
break;
|
|
607 |
#ifndef OPENSSL_NO_SHA
|
|
608 |
case RSA_PKCS1_OAEP_PADDING:
|
|
609 |
r = RSA_padding_check_PKCS1_OAEP(to, num, p, j, num, NULL, 0);
|
|
610 |
break;
|
|
611 |
#endif
|
|
612 |
case RSA_SSLV23_PADDING:
|
|
613 |
r = RSA_padding_check_SSLv23(to, num, p, j, num);
|
|
614 |
break;
|
|
615 |
case RSA_NO_PADDING:
|
|
616 |
r = RSA_padding_check_none(to, num, p, j, num);
|
|
617 |
break;
|
|
618 |
default:
|
|
619 |
RSAerr(PK11_F_RSA_PRIV_DEC, PK11_R_UNKNOWN_PADDING_TYPE);
|
|
620 |
goto err;
|
|
621 |
}
|
|
622 |
if (r < 0)
|
|
623 |
RSAerr(PK11_F_RSA_PRIV_DEC, PK11_R_PADDING_CHECK_FAILED);
|
|
624 |
|
|
625 |
err:
|
|
626 |
BN_clear_free(&f);
|
|
627 |
if (buf != NULL)
|
|
628 |
{
|
|
629 |
OPENSSL_cleanse(buf, num);
|
|
630 |
OPENSSL_free(buf);
|
|
631 |
}
|
|
632 |
return (r);
|
|
633 |
}
|
|
634 |
|
|
635 |
/* Similar to OpenSSL code. Input errors are also checked here */
|
|
636 |
static int pk11_RSA_public_decrypt(int flen, const unsigned char *from,
|
|
637 |
unsigned char *to, RSA *rsa, int padding)
|
|
638 |
{
|
|
639 |
BIGNUM f;
|
|
640 |
int i, num = 0, r = -1;
|
|
641 |
unsigned char *p;
|
|
642 |
unsigned char *buf = NULL;
|
|
643 |
|
|
644 |
BN_init(&f);
|
|
645 |
num = BN_num_bytes(rsa->n);
|
|
646 |
buf = (unsigned char *)OPENSSL_malloc(num);
|
|
647 |
if (buf == NULL)
|
|
648 |
{
|
|
649 |
RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_MALLOC_FAILURE);
|
|
650 |
goto err;
|
|
651 |
}
|
|
652 |
|
|
653 |
/*
|
|
654 |
* This check was for equality but PGP does evil things
|
|
655 |
* and chops off the top '0' bytes
|
|
656 |
*/
|
|
657 |
if (flen > num)
|
|
658 |
{
|
|
659 |
RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_DATA_GREATER_THAN_MOD_LEN);
|
|
660 |
goto err;
|
|
661 |
}
|
|
662 |
|
|
663 |
if (BN_bin2bn(from, flen, &f) == NULL)
|
|
664 |
goto err;
|
|
665 |
|
|
666 |
if (BN_ucmp(&f, rsa->n) >= 0)
|
|
667 |
{
|
|
668 |
RSAerr(PK11_F_RSA_PUB_DEC,
|
|
669 |
PK11_R_DATA_TOO_LARGE_FOR_MODULUS);
|
|
670 |
goto err;
|
|
671 |
}
|
|
672 |
|
|
673 |
/* PK11 functions are called here */
|
|
674 |
r = pk11_RSA_public_decrypt_low(flen, from, buf, rsa);
|
|
675 |
|
|
676 |
/*
|
|
677 |
* PK11 CKM_RSA_X_509 mechanism pads 0's at the beginning.
|
|
678 |
* Needs to skip these 0's here
|
|
679 |
*/
|
|
680 |
for (i = 0; i < r; i++)
|
|
681 |
if (buf[i] != 0)
|
|
682 |
break;
|
|
683 |
|
|
684 |
p = buf + i;
|
|
685 |
i = r - i; /* i is only used with no-padding mode */
|
|
686 |
|
|
687 |
switch (padding)
|
|
688 |
{
|
|
689 |
case RSA_PKCS1_PADDING:
|
|
690 |
r = RSA_padding_check_PKCS1_type_1(to, num, p, i, num);
|
|
691 |
break;
|
|
692 |
case RSA_NO_PADDING:
|
|
693 |
r = RSA_padding_check_none(to, num, p, i, num);
|
|
694 |
break;
|
|
695 |
default:
|
|
696 |
RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_UNKNOWN_PADDING_TYPE);
|
|
697 |
goto err;
|
|
698 |
}
|
|
699 |
if (r < 0)
|
|
700 |
RSAerr(PK11_F_RSA_PUB_DEC, PK11_R_PADDING_CHECK_FAILED);
|
|
701 |
|
|
702 |
err:
|
|
703 |
BN_clear_free(&f);
|
|
704 |
if (buf != NULL)
|
|
705 |
{
|
|
706 |
OPENSSL_cleanse(buf, num);
|
|
707 |
OPENSSL_free(buf);
|
|
708 |
}
|
|
709 |
return (r);
|
|
710 |
}
|
|
711 |
|
|
712 |
/*
|
|
713 |
* This function implements RSA public encryption using C_EncryptInit and
|
|
714 |
* C_Encrypt pk11 interfaces. Note that the CKM_RSA_X_509 is used here.
|
|
715 |
* The calling function allocated sufficient memory in "to" to store results.
|
|
716 |
*/
|
|
717 |
static int pk11_RSA_public_encrypt_low(int flen,
|
|
718 |
const unsigned char *from, unsigned char *to, RSA *rsa)
|
|
719 |
{
|
|
720 |
CK_ULONG bytes_encrypted = flen;
|
|
721 |
int retval = -1;
|
|
722 |
CK_RV rv;
|
|
723 |
CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0};
|
|
724 |
CK_MECHANISM *p_mech = &mech_rsa;
|
|
725 |
CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE;
|
|
726 |
PK11_SESSION *sp;
|
|
727 |
|
|
728 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
729 |
return (-1);
|
|
730 |
|
|
731 |
(void) check_new_rsa_key_pub(sp, rsa);
|
|
732 |
|
|
733 |
h_pub_key = sp->opdata_rsa_pub_key;
|
|
734 |
if (h_pub_key == CK_INVALID_HANDLE)
|
|
735 |
h_pub_key = sp->opdata_rsa_pub_key =
|
|
736 |
pk11_get_public_rsa_key(rsa, &sp->opdata_rsa_pub,
|
|
737 |
&sp->opdata_rsa_n_num, &sp->opdata_rsa_e_num,
|
|
738 |
sp->session);
|
|
739 |
|
|
740 |
if (h_pub_key != CK_INVALID_HANDLE)
|
|
741 |
{
|
|
742 |
rv = pFuncList->C_EncryptInit(sp->session, p_mech,
|
|
743 |
h_pub_key);
|
|
744 |
|
|
745 |
if (rv != CKR_OK)
|
|
746 |
{
|
|
747 |
PK11err_add_data(PK11_F_RSA_PUB_ENC_LOW,
|
|
748 |
PK11_R_ENCRYPTINIT, rv);
|
|
749 |
pk11_return_session(sp, OP_RSA);
|
|
750 |
return (-1);
|
|
751 |
}
|
|
752 |
|
|
753 |
rv = pFuncList->C_Encrypt(sp->session,
|
|
754 |
(unsigned char *)from, flen, to, &bytes_encrypted);
|
|
755 |
|
|
756 |
if (rv != CKR_OK)
|
|
757 |
{
|
|
758 |
PK11err_add_data(PK11_F_RSA_PUB_ENC_LOW,
|
|
759 |
PK11_R_ENCRYPT, rv);
|
|
760 |
pk11_return_session(sp, OP_RSA);
|
|
761 |
return (-1);
|
|
762 |
}
|
|
763 |
retval = bytes_encrypted;
|
|
764 |
}
|
|
765 |
|
|
766 |
pk11_return_session(sp, OP_RSA);
|
|
767 |
return (retval);
|
|
768 |
}
|
|
769 |
|
|
770 |
|
|
771 |
/*
|
|
772 |
* This function implements RSA private encryption using C_SignInit and
|
|
773 |
* C_Sign pk11 APIs. Note that CKM_RSA_X_509 is used here.
|
|
774 |
* The calling function allocated sufficient memory in "to" to store results.
|
|
775 |
*/
|
|
776 |
static int pk11_RSA_private_encrypt_low(int flen,
|
|
777 |
const unsigned char *from, unsigned char *to, RSA *rsa)
|
|
778 |
{
|
|
779 |
CK_ULONG ul_sig_len = flen;
|
|
780 |
int retval = -1;
|
|
781 |
CK_RV rv;
|
|
782 |
CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0};
|
|
783 |
CK_MECHANISM *p_mech = &mech_rsa;
|
|
784 |
CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE;
|
|
785 |
PK11_SESSION *sp;
|
|
786 |
|
|
787 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
788 |
return (-1);
|
|
789 |
|
|
790 |
(void) check_new_rsa_key_priv(sp, rsa);
|
|
791 |
|
|
792 |
h_priv_key = sp->opdata_rsa_priv_key;
|
|
793 |
if (h_priv_key == CK_INVALID_HANDLE)
|
|
794 |
h_priv_key = sp->opdata_rsa_priv_key =
|
|
795 |
pk11_get_private_rsa_key(rsa, &sp->opdata_rsa_priv,
|
|
796 |
&sp->opdata_rsa_d_num, sp->session);
|
|
797 |
|
|
798 |
if (h_priv_key != CK_INVALID_HANDLE)
|
|
799 |
{
|
|
800 |
rv = pFuncList->C_SignInit(sp->session, p_mech,
|
|
801 |
h_priv_key);
|
|
802 |
|
|
803 |
if (rv != CKR_OK)
|
|
804 |
{
|
|
805 |
PK11err_add_data(PK11_F_RSA_PRIV_ENC_LOW,
|
|
806 |
PK11_R_SIGNINIT, rv);
|
|
807 |
pk11_return_session(sp, OP_RSA);
|
|
808 |
return (-1);
|
|
809 |
}
|
|
810 |
|
|
811 |
rv = pFuncList->C_Sign(sp->session,
|
|
812 |
(unsigned char *)from, flen, to, &ul_sig_len);
|
|
813 |
|
|
814 |
if (rv != CKR_OK)
|
|
815 |
{
|
|
816 |
PK11err_add_data(PK11_F_RSA_PRIV_ENC_LOW, PK11_R_SIGN,
|
|
817 |
rv);
|
|
818 |
pk11_return_session(sp, OP_RSA);
|
|
819 |
return (-1);
|
|
820 |
}
|
|
821 |
|
|
822 |
retval = ul_sig_len;
|
|
823 |
}
|
|
824 |
|
|
825 |
pk11_return_session(sp, OP_RSA);
|
|
826 |
return (retval);
|
|
827 |
}
|
|
828 |
|
|
829 |
|
|
830 |
/*
|
|
831 |
* This function implements RSA private decryption using C_DecryptInit and
|
|
832 |
* C_Decrypt pk11 APIs. Note that CKM_RSA_X_509 mechanism is used here.
|
|
833 |
* The calling function allocated sufficient memory in "to" to store results.
|
|
834 |
*/
|
|
835 |
static int pk11_RSA_private_decrypt_low(int flen,
|
|
836 |
const unsigned char *from, unsigned char *to, RSA *rsa)
|
|
837 |
{
|
|
838 |
CK_ULONG bytes_decrypted = flen;
|
|
839 |
int retval = -1;
|
|
840 |
CK_RV rv;
|
|
841 |
CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0};
|
|
842 |
CK_MECHANISM *p_mech = &mech_rsa;
|
|
843 |
CK_OBJECT_HANDLE h_priv_key;
|
|
844 |
PK11_SESSION *sp;
|
|
845 |
|
|
846 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
847 |
return (-1);
|
|
848 |
|
|
849 |
(void) check_new_rsa_key_priv(sp, rsa);
|
|
850 |
|
|
851 |
h_priv_key = sp->opdata_rsa_priv_key;
|
|
852 |
if (h_priv_key == CK_INVALID_HANDLE)
|
|
853 |
h_priv_key = sp->opdata_rsa_priv_key =
|
|
854 |
pk11_get_private_rsa_key(rsa, &sp->opdata_rsa_priv,
|
|
855 |
&sp->opdata_rsa_d_num, sp->session);
|
|
856 |
|
|
857 |
if (h_priv_key != CK_INVALID_HANDLE)
|
|
858 |
{
|
|
859 |
rv = pFuncList->C_DecryptInit(sp->session, p_mech,
|
|
860 |
h_priv_key);
|
|
861 |
|
|
862 |
if (rv != CKR_OK)
|
|
863 |
{
|
|
864 |
PK11err_add_data(PK11_F_RSA_PRIV_DEC_LOW,
|
|
865 |
PK11_R_DECRYPTINIT, rv);
|
|
866 |
pk11_return_session(sp, OP_RSA);
|
|
867 |
return (-1);
|
|
868 |
}
|
|
869 |
|
|
870 |
rv = pFuncList->C_Decrypt(sp->session,
|
|
871 |
(unsigned char *)from, flen, to, &bytes_decrypted);
|
|
872 |
|
|
873 |
if (rv != CKR_OK)
|
|
874 |
{
|
|
875 |
PK11err_add_data(PK11_F_RSA_PRIV_DEC_LOW,
|
|
876 |
PK11_R_DECRYPT, rv);
|
|
877 |
pk11_return_session(sp, OP_RSA);
|
|
878 |
return (-1);
|
|
879 |
}
|
|
880 |
retval = bytes_decrypted;
|
|
881 |
}
|
|
882 |
|
|
883 |
pk11_return_session(sp, OP_RSA);
|
|
884 |
return (retval);
|
|
885 |
}
|
|
886 |
|
|
887 |
|
|
888 |
/*
|
|
889 |
* This function implements RSA public decryption using C_VerifyRecoverInit
|
|
890 |
* and C_VerifyRecover pk11 APIs. Note that CKM_RSA_X_509 is used here.
|
|
891 |
* The calling function allocated sufficient memory in "to" to store results.
|
|
892 |
*/
|
|
893 |
static int pk11_RSA_public_decrypt_low(int flen,
|
|
894 |
const unsigned char *from, unsigned char *to, RSA *rsa)
|
|
895 |
{
|
|
896 |
CK_ULONG bytes_decrypted = flen;
|
|
897 |
int retval = -1;
|
|
898 |
CK_RV rv;
|
|
899 |
CK_MECHANISM mech_rsa = {CKM_RSA_X_509, NULL, 0};
|
|
900 |
CK_MECHANISM *p_mech = &mech_rsa;
|
|
901 |
CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE;
|
|
902 |
PK11_SESSION *sp;
|
|
903 |
|
|
904 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
905 |
return (-1);
|
|
906 |
|
|
907 |
(void) check_new_rsa_key_pub(sp, rsa);
|
|
908 |
|
|
909 |
h_pub_key = sp->opdata_rsa_pub_key;
|
|
910 |
if (h_pub_key == CK_INVALID_HANDLE)
|
|
911 |
h_pub_key = sp->opdata_rsa_pub_key =
|
|
912 |
pk11_get_public_rsa_key(rsa, &sp->opdata_rsa_pub,
|
|
913 |
&sp->opdata_rsa_n_num, &sp->opdata_rsa_e_num,
|
|
914 |
sp->session);
|
|
915 |
|
|
916 |
if (h_pub_key != CK_INVALID_HANDLE)
|
|
917 |
{
|
|
918 |
rv = pFuncList->C_VerifyRecoverInit(sp->session,
|
|
919 |
p_mech, h_pub_key);
|
|
920 |
|
|
921 |
if (rv != CKR_OK)
|
|
922 |
{
|
|
923 |
PK11err_add_data(PK11_F_RSA_PUB_DEC_LOW,
|
|
924 |
PK11_R_VERIFYRECOVERINIT, rv);
|
|
925 |
pk11_return_session(sp, OP_RSA);
|
|
926 |
return (-1);
|
|
927 |
}
|
|
928 |
|
|
929 |
rv = pFuncList->C_VerifyRecover(sp->session,
|
|
930 |
(unsigned char *)from, flen, to, &bytes_decrypted);
|
|
931 |
|
|
932 |
if (rv != CKR_OK)
|
|
933 |
{
|
|
934 |
PK11err_add_data(PK11_F_RSA_PUB_DEC_LOW,
|
|
935 |
PK11_R_VERIFYRECOVER, rv);
|
|
936 |
pk11_return_session(sp, OP_RSA);
|
|
937 |
return (-1);
|
|
938 |
}
|
|
939 |
retval = bytes_decrypted;
|
|
940 |
}
|
|
941 |
|
|
942 |
pk11_return_session(sp, OP_RSA);
|
|
943 |
return (retval);
|
|
944 |
}
|
|
945 |
|
|
946 |
static int pk11_RSA_init(RSA *rsa)
|
|
947 |
{
|
|
948 |
/*
|
|
949 |
* This flag in the RSA_METHOD enables the new rsa_sign,
|
|
950 |
* rsa_verify functions. See rsa.h for details.
|
|
951 |
*/
|
|
952 |
rsa->flags |= RSA_FLAG_SIGN_VER;
|
|
953 |
|
|
954 |
return (1);
|
|
955 |
}
|
|
956 |
|
|
957 |
static int pk11_RSA_finish(RSA *rsa)
|
|
958 |
{
|
|
959 |
/*
|
|
960 |
* Since we are overloading OpenSSL's native RSA_eay_finish() we need
|
|
961 |
* to do the same as in the original function, i.e. to free bignum
|
|
962 |
* structures.
|
|
963 |
*/
|
|
964 |
if (rsa->_method_mod_n != NULL)
|
|
965 |
BN_MONT_CTX_free(rsa->_method_mod_n);
|
|
966 |
if (rsa->_method_mod_p != NULL)
|
|
967 |
BN_MONT_CTX_free(rsa->_method_mod_p);
|
|
968 |
if (rsa->_method_mod_q != NULL)
|
|
969 |
BN_MONT_CTX_free(rsa->_method_mod_q);
|
|
970 |
|
|
971 |
return (1);
|
|
972 |
}
|
|
973 |
|
|
974 |
/*
|
|
975 |
* Standard engine interface function. Majority codes here are from
|
|
976 |
* rsa/rsa_sign.c. We replaced the decrypt function call by C_Sign of PKCS#11.
|
|
977 |
* See more details in rsa/rsa_sign.c
|
|
978 |
*/
|
|
979 |
static int pk11_RSA_sign(int type, const unsigned char *m, unsigned int m_len,
|
|
980 |
unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
|
|
981 |
{
|
|
982 |
X509_SIG sig;
|
|
983 |
ASN1_TYPE parameter;
|
|
984 |
int i, j;
|
|
985 |
unsigned char *p, *s = NULL;
|
|
986 |
X509_ALGOR algor;
|
|
987 |
ASN1_OCTET_STRING digest;
|
|
988 |
CK_RV rv;
|
|
989 |
CK_MECHANISM mech_rsa = {CKM_RSA_PKCS, NULL, 0};
|
|
990 |
CK_MECHANISM *p_mech = &mech_rsa;
|
|
991 |
CK_OBJECT_HANDLE h_priv_key;
|
|
992 |
PK11_SESSION *sp = NULL;
|
|
993 |
int ret = 0;
|
|
994 |
unsigned long ulsiglen;
|
|
995 |
|
|
996 |
/* Encode the digest */
|
|
997 |
/* Special case: SSL signature, just check the length */
|
|
998 |
if (type == NID_md5_sha1)
|
|
999 |
{
|
|
1000 |
if (m_len != SSL_SIG_LENGTH)
|
|
1001 |
{
|
|
1002 |
PK11err(PK11_F_RSA_SIGN,
|
|
1003 |
PK11_R_INVALID_MESSAGE_LENGTH);
|
|
1004 |
goto err;
|
|
1005 |
}
|
|
1006 |
i = SSL_SIG_LENGTH;
|
|
1007 |
s = (unsigned char *)m;
|
|
1008 |
}
|
|
1009 |
else
|
|
1010 |
{
|
|
1011 |
sig.algor = &algor;
|
|
1012 |
sig.algor->algorithm = OBJ_nid2obj(type);
|
|
1013 |
if (sig.algor->algorithm == NULL)
|
|
1014 |
{
|
|
1015 |
PK11err(PK11_F_RSA_SIGN,
|
|
1016 |
PK11_R_UNKNOWN_ALGORITHM_TYPE);
|
|
1017 |
goto err;
|
|
1018 |
}
|
|
1019 |
if (sig.algor->algorithm->length == 0)
|
|
1020 |
{
|
|
1021 |
PK11err(PK11_F_RSA_SIGN,
|
|
1022 |
PK11_R_UNKNOWN_ASN1_OBJECT_ID);
|
|
1023 |
goto err;
|
|
1024 |
}
|
|
1025 |
parameter.type = V_ASN1_NULL;
|
|
1026 |
parameter.value.ptr = NULL;
|
|
1027 |
sig.algor->parameter = ¶meter;
|
|
1028 |
|
|
1029 |
sig.digest = &digest;
|
|
1030 |
sig.digest->data = (unsigned char *)m;
|
|
1031 |
sig.digest->length = m_len;
|
|
1032 |
|
|
1033 |
i = i2d_X509_SIG(&sig, NULL);
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
j = RSA_size(rsa);
|
|
1037 |
if ((i - RSA_PKCS1_PADDING) > j)
|
|
1038 |
{
|
|
1039 |
PK11err(PK11_F_RSA_SIGN, PK11_R_DIGEST_TOO_BIG);
|
|
1040 |
goto err;
|
|
1041 |
}
|
|
1042 |
|
|
1043 |
if (type != NID_md5_sha1)
|
|
1044 |
{
|
|
1045 |
s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1));
|
|
1046 |
if (s == NULL)
|
|
1047 |
{
|
|
1048 |
PK11err(PK11_F_RSA_SIGN, PK11_R_MALLOC_FAILURE);
|
|
1049 |
goto err;
|
|
1050 |
}
|
|
1051 |
p = s;
|
|
1052 |
(void) i2d_X509_SIG(&sig, &p);
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
1056 |
goto err;
|
|
1057 |
|
|
1058 |
(void) check_new_rsa_key_priv(sp, rsa);
|
|
1059 |
|
|
1060 |
h_priv_key = sp->opdata_rsa_priv_key;
|
|
1061 |
if (h_priv_key == CK_INVALID_HANDLE)
|
|
1062 |
h_priv_key = sp->opdata_rsa_priv_key =
|
|
1063 |
pk11_get_private_rsa_key((RSA *)rsa,
|
|
1064 |
&sp->opdata_rsa_priv,
|
|
1065 |
&sp->opdata_rsa_d_num, sp->session);
|
|
1066 |
|
|
1067 |
if (h_priv_key != CK_INVALID_HANDLE)
|
|
1068 |
{
|
|
1069 |
rv = pFuncList->C_SignInit(sp->session, p_mech, h_priv_key);
|
|
1070 |
|
|
1071 |
if (rv != CKR_OK)
|
|
1072 |
{
|
|
1073 |
PK11err_add_data(PK11_F_RSA_SIGN, PK11_R_SIGNINIT, rv);
|
|
1074 |
goto err;
|
|
1075 |
}
|
|
1076 |
|
|
1077 |
ulsiglen = j;
|
|
1078 |
rv = pFuncList->C_Sign(sp->session, s, i, sigret,
|
|
1079 |
(CK_ULONG_PTR) &ulsiglen);
|
|
1080 |
*siglen = ulsiglen;
|
|
1081 |
|
|
1082 |
if (rv != CKR_OK)
|
|
1083 |
{
|
|
1084 |
PK11err_add_data(PK11_F_RSA_SIGN, PK11_R_SIGN, rv);
|
|
1085 |
goto err;
|
|
1086 |
}
|
|
1087 |
ret = 1;
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
err:
|
|
1091 |
if (type != NID_md5_sha1)
|
|
1092 |
{
|
|
1093 |
(void) memset(s, 0, (unsigned int)(j + 1));
|
|
1094 |
OPENSSL_free(s);
|
|
1095 |
}
|
|
1096 |
|
|
1097 |
pk11_return_session(sp, OP_RSA);
|
|
1098 |
return (ret);
|
|
1099 |
}
|
|
1100 |
|
|
1101 |
static int pk11_RSA_verify(int type, const unsigned char *m,
|
|
1102 |
unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,
|
|
1103 |
const RSA *rsa)
|
|
1104 |
{
|
|
1105 |
X509_SIG sig;
|
|
1106 |
ASN1_TYPE parameter;
|
|
1107 |
int i, j;
|
|
1108 |
unsigned char *p, *s = NULL;
|
|
1109 |
X509_ALGOR algor;
|
|
1110 |
ASN1_OCTET_STRING digest;
|
|
1111 |
CK_RV rv;
|
|
1112 |
CK_MECHANISM mech_rsa = {CKM_RSA_PKCS, NULL, 0};
|
|
1113 |
CK_MECHANISM *p_mech = &mech_rsa;
|
|
1114 |
CK_OBJECT_HANDLE h_pub_key;
|
|
1115 |
PK11_SESSION *sp = NULL;
|
|
1116 |
int ret = 0;
|
|
1117 |
|
|
1118 |
/* Encode the digest */
|
|
1119 |
/* Special case: SSL signature, just check the length */
|
|
1120 |
if (type == NID_md5_sha1)
|
|
1121 |
{
|
|
1122 |
if (m_len != SSL_SIG_LENGTH)
|
|
1123 |
{
|
|
1124 |
PK11err(PK11_F_RSA_VERIFY,
|
|
1125 |
PK11_R_INVALID_MESSAGE_LENGTH);
|
|
1126 |
goto err;
|
|
1127 |
}
|
|
1128 |
i = SSL_SIG_LENGTH;
|
|
1129 |
s = (unsigned char *)m;
|
|
1130 |
}
|
|
1131 |
else
|
|
1132 |
{
|
|
1133 |
sig.algor = &algor;
|
|
1134 |
sig.algor->algorithm = OBJ_nid2obj(type);
|
|
1135 |
if (sig.algor->algorithm == NULL)
|
|
1136 |
{
|
|
1137 |
PK11err(PK11_F_RSA_VERIFY,
|
|
1138 |
PK11_R_UNKNOWN_ALGORITHM_TYPE);
|
|
1139 |
goto err;
|
|
1140 |
}
|
|
1141 |
if (sig.algor->algorithm->length == 0)
|
|
1142 |
{
|
|
1143 |
PK11err(PK11_F_RSA_VERIFY,
|
|
1144 |
PK11_R_UNKNOWN_ASN1_OBJECT_ID);
|
|
1145 |
goto err;
|
|
1146 |
}
|
|
1147 |
parameter.type = V_ASN1_NULL;
|
|
1148 |
parameter.value.ptr = NULL;
|
|
1149 |
sig.algor->parameter = ¶meter;
|
|
1150 |
sig.digest = &digest;
|
|
1151 |
sig.digest->data = (unsigned char *)m;
|
|
1152 |
sig.digest->length = m_len;
|
|
1153 |
i = i2d_X509_SIG(&sig, NULL);
|
|
1154 |
}
|
|
1155 |
|
|
1156 |
j = RSA_size(rsa);
|
|
1157 |
if ((i - RSA_PKCS1_PADDING) > j)
|
|
1158 |
{
|
|
1159 |
PK11err(PK11_F_RSA_VERIFY, PK11_R_DIGEST_TOO_BIG);
|
|
1160 |
goto err;
|
|
1161 |
}
|
|
1162 |
|
|
1163 |
if (type != NID_md5_sha1)
|
|
1164 |
{
|
|
1165 |
s = (unsigned char *)OPENSSL_malloc((unsigned int)(j + 1));
|
|
1166 |
if (s == NULL)
|
|
1167 |
{
|
|
1168 |
PK11err(PK11_F_RSA_VERIFY, PK11_R_MALLOC_FAILURE);
|
|
1169 |
goto err;
|
|
1170 |
}
|
|
1171 |
p = s;
|
|
1172 |
(void) i2d_X509_SIG(&sig, &p);
|
|
1173 |
}
|
|
1174 |
|
|
1175 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
1176 |
goto err;
|
|
1177 |
|
|
1178 |
(void) check_new_rsa_key_pub(sp, rsa);
|
|
1179 |
|
|
1180 |
h_pub_key = sp->opdata_rsa_pub_key;
|
|
1181 |
if (h_pub_key == CK_INVALID_HANDLE)
|
|
1182 |
h_pub_key = sp->opdata_rsa_pub_key =
|
|
1183 |
pk11_get_public_rsa_key((RSA *)rsa, &sp->opdata_rsa_pub,
|
|
1184 |
&sp->opdata_rsa_n_num, &sp->opdata_rsa_e_num,
|
|
1185 |
sp->session);
|
|
1186 |
|
|
1187 |
if (h_pub_key != CK_INVALID_HANDLE)
|
|
1188 |
{
|
|
1189 |
rv = pFuncList->C_VerifyInit(sp->session, p_mech,
|
|
1190 |
h_pub_key);
|
|
1191 |
|
|
1192 |
if (rv != CKR_OK)
|
|
1193 |
{
|
|
1194 |
PK11err_add_data(PK11_F_RSA_VERIFY, PK11_R_VERIFYINIT,
|
|
1195 |
rv);
|
|
1196 |
goto err;
|
|
1197 |
}
|
|
1198 |
rv = pFuncList->C_Verify(sp->session, s, i, sigbuf,
|
|
1199 |
(CK_ULONG)siglen);
|
|
1200 |
|
|
1201 |
if (rv != CKR_OK)
|
|
1202 |
{
|
|
1203 |
PK11err_add_data(PK11_F_RSA_VERIFY, PK11_R_VERIFY, rv);
|
|
1204 |
goto err;
|
|
1205 |
}
|
|
1206 |
ret = 1;
|
|
1207 |
}
|
|
1208 |
|
|
1209 |
err:
|
|
1210 |
if (type != NID_md5_sha1)
|
|
1211 |
{
|
|
1212 |
(void) memset(s, 0, (unsigned int)siglen);
|
|
1213 |
OPENSSL_free(s);
|
|
1214 |
}
|
|
1215 |
|
|
1216 |
pk11_return_session(sp, OP_RSA);
|
|
1217 |
return (ret);
|
|
1218 |
}
|
|
1219 |
|
|
1220 |
/* load RSA private key from a file */
|
|
1221 |
/* ARGSUSED */
|
|
1222 |
EVP_PKEY *pk11_load_privkey(ENGINE* e, const char *privkey_file,
|
|
1223 |
UI_METHOD *ui_method, void *callback_data)
|
|
1224 |
{
|
|
1225 |
EVP_PKEY *pkey = NULL;
|
|
1226 |
FILE *pubkey;
|
|
1227 |
CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE;
|
|
1228 |
RSA *rsa;
|
|
1229 |
PK11_SESSION *sp;
|
|
1230 |
|
|
1231 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
1232 |
return (NULL);
|
|
1233 |
|
|
1234 |
if ((pubkey = fopen(privkey_file, read_mode_flags)) != NULL)
|
|
1235 |
{
|
|
1236 |
pkey = PEM_read_PrivateKey(pubkey, NULL, NULL, NULL);
|
|
1237 |
(void) fclose(pubkey);
|
|
1238 |
if (pkey != NULL)
|
|
1239 |
{
|
|
1240 |
rsa = EVP_PKEY_get1_RSA(pkey);
|
|
1241 |
if (rsa != NULL)
|
|
1242 |
{
|
|
1243 |
(void) check_new_rsa_key_priv(sp, rsa);
|
|
1244 |
|
|
1245 |
h_priv_key = sp->opdata_rsa_priv_key =
|
|
1246 |
pk11_get_private_rsa_key(rsa,
|
|
1247 |
&sp->opdata_rsa_priv, &sp->opdata_rsa_d_num,
|
|
1248 |
sp->session);
|
|
1249 |
if (h_priv_key == CK_INVALID_HANDLE)
|
|
1250 |
{
|
|
1251 |
EVP_PKEY_free(pkey);
|
|
1252 |
pkey = NULL;
|
|
1253 |
}
|
|
1254 |
}
|
|
1255 |
else
|
|
1256 |
{
|
|
1257 |
EVP_PKEY_free(pkey);
|
|
1258 |
pkey = NULL;
|
|
1259 |
}
|
|
1260 |
}
|
|
1261 |
}
|
|
1262 |
|
|
1263 |
pk11_return_session(sp, OP_RSA);
|
|
1264 |
return (pkey);
|
|
1265 |
}
|
|
1266 |
|
|
1267 |
/* load RSA public key from a file */
|
|
1268 |
/* ARGSUSED */
|
|
1269 |
EVP_PKEY *pk11_load_pubkey(ENGINE* e, const char *pubkey_file,
|
|
1270 |
UI_METHOD *ui_method, void *callback_data)
|
|
1271 |
{
|
|
1272 |
EVP_PKEY *pkey = NULL;
|
|
1273 |
FILE *pubkey;
|
|
1274 |
CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE;
|
|
1275 |
RSA *rsa;
|
|
1276 |
PK11_SESSION *sp;
|
|
1277 |
|
|
1278 |
if ((sp = pk11_get_session(OP_RSA)) == NULL)
|
|
1279 |
return (NULL);
|
|
1280 |
|
|
1281 |
if ((pubkey = fopen(pubkey_file, read_mode_flags)) != NULL)
|
|
1282 |
{
|
|
1283 |
pkey = PEM_read_PUBKEY(pubkey, NULL, NULL, NULL);
|
|
1284 |
(void) fclose(pubkey);
|
|
1285 |
if (pkey != NULL)
|
|
1286 |
{
|
|
1287 |
rsa = EVP_PKEY_get1_RSA(pkey);
|
|
1288 |
if (rsa != NULL)
|
|
1289 |
{
|
|
1290 |
(void) check_new_rsa_key_pub(sp, rsa);
|
|
1291 |
|
|
1292 |
h_pub_key = sp->opdata_rsa_pub_key =
|
|
1293 |
pk11_get_public_rsa_key(rsa,
|
|
1294 |
&sp->opdata_rsa_pub, &sp->opdata_rsa_n_num,
|
|
1295 |
&sp->opdata_rsa_e_num, sp->session);
|
|
1296 |
if (h_pub_key == CK_INVALID_HANDLE)
|
|
1297 |
{
|
|
1298 |
EVP_PKEY_free(pkey);
|
|
1299 |
pkey = NULL;
|
|
1300 |
}
|
|
1301 |
}
|
|
1302 |
else
|
|
1303 |
{
|
|
1304 |
EVP_PKEY_free(pkey);
|
|
1305 |
pkey = NULL;
|
|
1306 |
}
|
|
1307 |
}
|
|
1308 |
}
|
|
1309 |
|
|
1310 |
pk11_return_session(sp, OP_RSA);
|
|
1311 |
return (pkey);
|
|
1312 |
}
|
|
1313 |
|
|
1314 |
/*
|
|
1315 |
* Create a public key object in a session from a given rsa structure.
|
|
1316 |
* The *rsa_n_num and *rsa_e_num pointers are non-NULL for RSA public keys.
|
|
1317 |
*/
|
|
1318 |
static CK_OBJECT_HANDLE pk11_get_public_rsa_key(RSA* rsa,
|
|
1319 |
RSA** key_ptr, BIGNUM **rsa_n_num, BIGNUM **rsa_e_num,
|
|
1320 |
CK_SESSION_HANDLE session)
|
|
1321 |
{
|
|
1322 |
CK_RV rv;
|
|
1323 |
CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
|
|
1324 |
CK_ULONG found;
|
|
1325 |
CK_OBJECT_CLASS o_key = CKO_PUBLIC_KEY;
|
|
1326 |
CK_KEY_TYPE k_type = CKK_RSA;
|
|
1327 |
CK_ULONG ul_key_attr_count = 7;
|
|
1328 |
CK_BBOOL rollback = FALSE;
|
|
1329 |
|
|
1330 |
CK_ATTRIBUTE a_key_template[] =
|
|
1331 |
{
|
|
1332 |
{CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)},
|
|
1333 |
{CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)},
|
|
1334 |
{CKA_TOKEN, &false, sizeof (true)},
|
|
1335 |
{CKA_ENCRYPT, &true, sizeof (true)},
|
|
1336 |
{CKA_VERIFY_RECOVER, &true, sizeof (true)},
|
|
1337 |
{CKA_MODULUS, (void *)NULL, 0},
|
|
1338 |
{CKA_PUBLIC_EXPONENT, (void *)NULL, 0}
|
|
1339 |
};
|
|
1340 |
|
|
1341 |
int i;
|
|
1342 |
|
|
1343 |
a_key_template[0].pValue = &o_key;
|
|
1344 |
a_key_template[1].pValue = &k_type;
|
|
1345 |
|
|
1346 |
a_key_template[5].ulValueLen = BN_num_bytes(rsa->n);
|
|
1347 |
a_key_template[5].pValue = (CK_VOID_PTR)OPENSSL_malloc(
|
|
1348 |
(size_t)a_key_template[5].ulValueLen);
|
|
1349 |
if (a_key_template[5].pValue == NULL)
|
|
1350 |
{
|
|
1351 |
PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1352 |
goto malloc_err;
|
|
1353 |
}
|
|
1354 |
|
|
1355 |
BN_bn2bin(rsa->n, a_key_template[5].pValue);
|
|
1356 |
|
|
1357 |
a_key_template[6].ulValueLen = BN_num_bytes(rsa->e);
|
|
1358 |
a_key_template[6].pValue = (CK_VOID_PTR)OPENSSL_malloc(
|
|
1359 |
(size_t)a_key_template[6].ulValueLen);
|
|
1360 |
if (a_key_template[6].pValue == NULL)
|
|
1361 |
{
|
|
1362 |
PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1363 |
goto malloc_err;
|
|
1364 |
}
|
|
1365 |
|
|
1366 |
BN_bn2bin(rsa->e, a_key_template[6].pValue);
|
|
1367 |
|
|
1368 |
/* see find_lock array definition for more info on object locking */
|
|
1369 |
LOCK_OBJSTORE(OP_RSA);
|
|
1370 |
rv = pFuncList->C_FindObjectsInit(session, a_key_template,
|
|
1371 |
ul_key_attr_count);
|
|
1372 |
|
|
1373 |
if (rv != CKR_OK)
|
|
1374 |
{
|
|
1375 |
PK11err_add_data(PK11_F_GET_PUB_RSA_KEY, PK11_R_FINDOBJECTSINIT,
|
|
1376 |
rv);
|
|
1377 |
goto err;
|
|
1378 |
}
|
|
1379 |
|
|
1380 |
rv = pFuncList->C_FindObjects(session, &h_key, 1, &found);
|
|
1381 |
|
|
1382 |
if (rv != CKR_OK)
|
|
1383 |
{
|
|
1384 |
PK11err_add_data(PK11_F_GET_PUB_RSA_KEY,
|
|
1385 |
PK11_R_FINDOBJECTS, rv);
|
|
1386 |
goto err;
|
|
1387 |
}
|
|
1388 |
|
|
1389 |
rv = pFuncList->C_FindObjectsFinal(session);
|
|
1390 |
|
|
1391 |
if (rv != CKR_OK)
|
|
1392 |
{
|
|
1393 |
PK11err_add_data(PK11_F_GET_PUB_RSA_KEY,
|
|
1394 |
PK11_R_FINDOBJECTSFINAL, rv);
|
|
1395 |
goto err;
|
|
1396 |
}
|
|
1397 |
|
|
1398 |
if (found == 0)
|
|
1399 |
{
|
|
1400 |
rv = pFuncList->C_CreateObject(session,
|
|
1401 |
a_key_template, ul_key_attr_count, &h_key);
|
|
1402 |
if (rv != CKR_OK)
|
|
1403 |
{
|
|
1404 |
PK11err_add_data(PK11_F_GET_PUB_RSA_KEY,
|
|
1405 |
PK11_R_CREATEOBJECT, rv);
|
|
1406 |
goto err;
|
|
1407 |
}
|
|
1408 |
}
|
|
1409 |
|
|
1410 |
if (rsa_n_num != NULL)
|
|
1411 |
if ((*rsa_n_num = BN_dup(rsa->n)) == NULL)
|
|
1412 |
{
|
|
1413 |
PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1414 |
rollback = TRUE;
|
|
1415 |
goto err;
|
|
1416 |
}
|
|
1417 |
if (rsa_e_num != NULL)
|
|
1418 |
if ((*rsa_e_num = BN_dup(rsa->e)) == NULL)
|
|
1419 |
{
|
|
1420 |
PK11err(PK11_F_GET_PUB_RSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1421 |
BN_free(*rsa_n_num);
|
|
1422 |
*rsa_n_num = NULL;
|
|
1423 |
rollback = TRUE;
|
|
1424 |
goto err;
|
|
1425 |
}
|
|
1426 |
|
|
1427 |
/* LINTED: E_CONSTANT_CONDITION */
|
|
1428 |
KEY_HANDLE_REFHOLD(h_key, OP_RSA, FALSE, rollback, err);
|
|
1429 |
if (key_ptr != NULL)
|
|
1430 |
*key_ptr = rsa;
|
|
1431 |
|
|
1432 |
err:
|
|
1433 |
if (rollback)
|
|
1434 |
{
|
|
1435 |
/*
|
|
1436 |
* We do not care about the return value from C_DestroyObject()
|
|
1437 |
* since we are doing rollback.
|
|
1438 |
*/
|
|
1439 |
if (found == 0)
|
|
1440 |
(void) pFuncList->C_DestroyObject(session, h_key);
|
|
1441 |
h_key = CK_INVALID_HANDLE;
|
|
1442 |
}
|
|
1443 |
|
|
1444 |
UNLOCK_OBJSTORE(OP_RSA);
|
|
1445 |
|
|
1446 |
malloc_err:
|
|
1447 |
for (i = 5; i <= 6; i++)
|
|
1448 |
{
|
|
1449 |
if (a_key_template[i].pValue != NULL)
|
|
1450 |
{
|
|
1451 |
OPENSSL_free(a_key_template[i].pValue);
|
|
1452 |
a_key_template[i].pValue = NULL;
|
|
1453 |
}
|
|
1454 |
}
|
|
1455 |
|
|
1456 |
return (h_key);
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
/*
|
|
1460 |
* Create a private key object in the session from a given rsa structure.
|
|
1461 |
* The *rsa_d_num pointer is non-NULL for RSA private keys.
|
|
1462 |
*/
|
|
1463 |
static CK_OBJECT_HANDLE pk11_get_private_rsa_key(RSA* rsa,
|
|
1464 |
RSA** key_ptr, BIGNUM **rsa_d_num, CK_SESSION_HANDLE session)
|
|
1465 |
{
|
|
1466 |
CK_RV rv;
|
|
1467 |
CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
|
|
1468 |
int i;
|
|
1469 |
CK_ULONG found;
|
|
1470 |
CK_OBJECT_CLASS o_key = CKO_PRIVATE_KEY;
|
|
1471 |
CK_KEY_TYPE k_type = CKK_RSA;
|
|
1472 |
CK_ULONG ul_key_attr_count = 14;
|
|
1473 |
CK_BBOOL rollback = FALSE;
|
|
1474 |
|
|
1475 |
/* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys */
|
|
1476 |
CK_ATTRIBUTE a_key_template[] =
|
|
1477 |
{
|
|
1478 |
{CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)},
|
|
1479 |
{CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)},
|
|
1480 |
{CKA_TOKEN, &false, sizeof (true)},
|
|
1481 |
{CKA_SENSITIVE, &false, sizeof (true)},
|
|
1482 |
{CKA_DECRYPT, &true, sizeof (true)},
|
|
1483 |
{CKA_SIGN, &true, sizeof (true)},
|
|
1484 |
{CKA_MODULUS, (void *)NULL, 0},
|
|
1485 |
{CKA_PUBLIC_EXPONENT, (void *)NULL, 0},
|
|
1486 |
{CKA_PRIVATE_EXPONENT, (void *)NULL, 0},
|
|
1487 |
{CKA_PRIME_1, (void *)NULL, 0},
|
|
1488 |
{CKA_PRIME_2, (void *)NULL, 0},
|
|
1489 |
{CKA_EXPONENT_1, (void *)NULL, 0},
|
|
1490 |
{CKA_EXPONENT_2, (void *)NULL, 0},
|
|
1491 |
{CKA_COEFFICIENT, (void *)NULL, 0}
|
|
1492 |
};
|
|
1493 |
|
|
1494 |
a_key_template[0].pValue = &o_key;
|
|
1495 |
a_key_template[1].pValue = &k_type;
|
|
1496 |
|
|
1497 |
/* Put the private key components into the template */
|
|
1498 |
if (init_template_value(rsa->n, &a_key_template[6].pValue,
|
|
1499 |
&a_key_template[6].ulValueLen) == 0 ||
|
|
1500 |
init_template_value(rsa->e, &a_key_template[7].pValue,
|
|
1501 |
&a_key_template[7].ulValueLen) == 0 ||
|
|
1502 |
init_template_value(rsa->d, &a_key_template[8].pValue,
|
|
1503 |
&a_key_template[8].ulValueLen) == 0 ||
|
|
1504 |
init_template_value(rsa->p, &a_key_template[9].pValue,
|
|
1505 |
&a_key_template[9].ulValueLen) == 0 ||
|
|
1506 |
init_template_value(rsa->q, &a_key_template[10].pValue,
|
|
1507 |
&a_key_template[10].ulValueLen) == 0 ||
|
|
1508 |
init_template_value(rsa->dmp1, &a_key_template[11].pValue,
|
|
1509 |
&a_key_template[11].ulValueLen) == 0 ||
|
|
1510 |
init_template_value(rsa->dmq1, &a_key_template[12].pValue,
|
|
1511 |
&a_key_template[12].ulValueLen) == 0 ||
|
|
1512 |
init_template_value(rsa->iqmp, &a_key_template[13].pValue,
|
|
1513 |
&a_key_template[13].ulValueLen) == 0)
|
|
1514 |
{
|
|
1515 |
PK11err(PK11_F_GET_PRIV_RSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1516 |
goto malloc_err;
|
|
1517 |
}
|
|
1518 |
|
|
1519 |
/* see find_lock array definition for more info on object locking */
|
|
1520 |
LOCK_OBJSTORE(OP_RSA);
|
|
1521 |
rv = pFuncList->C_FindObjectsInit(session, a_key_template,
|
|
1522 |
ul_key_attr_count);
|
|
1523 |
|
|
1524 |
if (rv != CKR_OK)
|
|
1525 |
{
|
|
1526 |
PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY,
|
|
1527 |
PK11_R_FINDOBJECTSINIT, rv);
|
|
1528 |
goto err;
|
|
1529 |
}
|
|
1530 |
|
|
1531 |
rv = pFuncList->C_FindObjects(session, &h_key, 1, &found);
|
|
1532 |
|
|
1533 |
if (rv != CKR_OK)
|
|
1534 |
{
|
|
1535 |
PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY,
|
|
1536 |
PK11_R_FINDOBJECTS, rv);
|
|
1537 |
goto err;
|
|
1538 |
}
|
|
1539 |
|
|
1540 |
rv = pFuncList->C_FindObjectsFinal(session);
|
|
1541 |
|
|
1542 |
if (rv != CKR_OK)
|
|
1543 |
{
|
|
1544 |
PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY,
|
|
1545 |
PK11_R_FINDOBJECTSFINAL, rv);
|
|
1546 |
goto err;
|
|
1547 |
}
|
|
1548 |
|
|
1549 |
if (found == 0)
|
|
1550 |
{
|
|
1551 |
rv = pFuncList->C_CreateObject(session,
|
|
1552 |
a_key_template, ul_key_attr_count, &h_key);
|
|
1553 |
if (rv != CKR_OK)
|
|
1554 |
{
|
|
1555 |
PK11err_add_data(PK11_F_GET_PRIV_RSA_KEY,
|
|
1556 |
PK11_R_CREATEOBJECT, rv);
|
|
1557 |
goto err;
|
|
1558 |
}
|
|
1559 |
}
|
|
1560 |
|
|
1561 |
if (rsa_d_num != NULL)
|
|
1562 |
if ((*rsa_d_num = BN_dup(rsa->d)) == NULL)
|
|
1563 |
{
|
|
1564 |
PK11err(PK11_F_GET_PRIV_RSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1565 |
rollback = TRUE;
|
|
1566 |
goto err;
|
|
1567 |
}
|
|
1568 |
|
|
1569 |
/* LINTED: E_CONSTANT_CONDITION */
|
|
1570 |
KEY_HANDLE_REFHOLD(h_key, OP_RSA, FALSE, rollback, err);
|
|
1571 |
if (key_ptr != NULL)
|
|
1572 |
*key_ptr = rsa;
|
|
1573 |
|
|
1574 |
err:
|
|
1575 |
if (rollback)
|
|
1576 |
{
|
|
1577 |
/*
|
|
1578 |
* We do not care about the return value from C_DestroyObject()
|
|
1579 |
* since we are doing rollback.
|
|
1580 |
*/
|
|
1581 |
if (found == 0)
|
|
1582 |
(void) pFuncList->C_DestroyObject(session, h_key);
|
|
1583 |
h_key = CK_INVALID_HANDLE;
|
|
1584 |
}
|
|
1585 |
|
|
1586 |
UNLOCK_OBJSTORE(OP_RSA);
|
|
1587 |
|
|
1588 |
malloc_err:
|
|
1589 |
/*
|
|
1590 |
* 6 to 13 entries in the key template are key components.
|
|
1591 |
* They need to be freed apon exit or error.
|
|
1592 |
*/
|
|
1593 |
for (i = 6; i <= 13; i++)
|
|
1594 |
{
|
|
1595 |
if (a_key_template[i].pValue != NULL)
|
|
1596 |
{
|
|
1597 |
(void) memset(a_key_template[i].pValue, 0,
|
|
1598 |
a_key_template[i].ulValueLen);
|
|
1599 |
OPENSSL_free(a_key_template[i].pValue);
|
|
1600 |
a_key_template[i].pValue = NULL;
|
|
1601 |
}
|
|
1602 |
}
|
|
1603 |
|
|
1604 |
return (h_key);
|
|
1605 |
}
|
|
1606 |
|
|
1607 |
/*
|
|
1608 |
* Check for cache miss and clean the object pointer and handle
|
|
1609 |
* in such case. Return 1 for cache hit, 0 for cache miss.
|
|
1610 |
*/
|
|
1611 |
static int check_new_rsa_key_pub(PK11_SESSION *sp, const RSA *rsa)
|
|
1612 |
{
|
|
1613 |
/*
|
|
1614 |
* Provide protection against RSA structure reuse by making the
|
|
1615 |
* check for cache hit stronger. Only public components of RSA
|
|
1616 |
* key matter here so it is sufficient to compare them with values
|
|
1617 |
* cached in PK11_SESSION structure.
|
|
1618 |
*/
|
|
1619 |
if ((sp->opdata_rsa_pub != rsa) ||
|
|
1620 |
(BN_cmp(sp->opdata_rsa_n_num, rsa->n) != 0) ||
|
|
1621 |
(BN_cmp(sp->opdata_rsa_e_num, rsa->e) != 0))
|
|
1622 |
{
|
|
1623 |
/*
|
|
1624 |
* We do not check the return value because even in case of
|
|
1625 |
* failure the sp structure will have both key pointer
|
|
1626 |
* and object handle cleaned and pk11_destroy_object()
|
|
1627 |
* reports the failure to the OpenSSL error message buffer.
|
|
1628 |
*/
|
|
1629 |
(void) pk11_destroy_rsa_object_pub(sp, TRUE);
|
|
1630 |
return (0);
|
|
1631 |
}
|
|
1632 |
return (1);
|
|
1633 |
}
|
|
1634 |
|
|
1635 |
/*
|
|
1636 |
* Check for cache miss and clean the object pointer and handle
|
|
1637 |
* in such case. Return 1 for cache hit, 0 for cache miss.
|
|
1638 |
*/
|
|
1639 |
static int check_new_rsa_key_priv(PK11_SESSION *sp, const RSA *rsa)
|
|
1640 |
{
|
|
1641 |
/*
|
|
1642 |
* Provide protection against RSA structure reuse by making the
|
|
1643 |
* check for cache hit stronger. Comparing private exponent of RSA
|
|
1644 |
* key with value cached in PK11_SESSION structure should
|
|
1645 |
* be sufficient.
|
|
1646 |
*/
|
|
1647 |
if ((sp->opdata_rsa_priv != rsa) ||
|
|
1648 |
(BN_cmp(sp->opdata_rsa_d_num, rsa->d) != 0))
|
|
1649 |
{
|
|
1650 |
/*
|
|
1651 |
* We do not check the return value because even in case of
|
|
1652 |
* failure the sp structure will have both key pointer
|
|
1653 |
* and object handle cleaned and pk11_destroy_object()
|
|
1654 |
* reports the failure to the OpenSSL error message buffer.
|
|
1655 |
*/
|
|
1656 |
(void) pk11_destroy_rsa_object_priv(sp, TRUE);
|
|
1657 |
return (0);
|
|
1658 |
}
|
|
1659 |
return (1);
|
|
1660 |
}
|
|
1661 |
#endif
|
|
1662 |
|
|
1663 |
#ifndef OPENSSL_NO_DSA
|
|
1664 |
/* The DSA function implementation */
|
|
1665 |
/* ARGSUSED */
|
|
1666 |
static int pk11_DSA_init(DSA *dsa)
|
|
1667 |
{
|
|
1668 |
return (1);
|
|
1669 |
}
|
|
1670 |
|
|
1671 |
/* ARGSUSED */
|
|
1672 |
static int pk11_DSA_finish(DSA *dsa)
|
|
1673 |
{
|
|
1674 |
return (1);
|
|
1675 |
}
|
|
1676 |
|
|
1677 |
|
|
1678 |
static DSA_SIG *
|
|
1679 |
pk11_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
|
|
1680 |
{
|
|
1681 |
BIGNUM *r = NULL, *s = NULL;
|
|
1682 |
int i;
|
|
1683 |
DSA_SIG *dsa_sig = NULL;
|
|
1684 |
|
|
1685 |
CK_RV rv;
|
|
1686 |
CK_MECHANISM Mechanism_dsa = {CKM_DSA, NULL, 0};
|
|
1687 |
CK_MECHANISM *p_mech = &Mechanism_dsa;
|
|
1688 |
CK_OBJECT_HANDLE h_priv_key;
|
|
1689 |
|
|
1690 |
/*
|
|
1691 |
* The signature is the concatenation of r and s,
|
|
1692 |
* each is 20 bytes long
|
|
1693 |
*/
|
|
1694 |
unsigned char sigret[DSA_SIGNATURE_LEN];
|
|
1695 |
unsigned long siglen = DSA_SIGNATURE_LEN;
|
|
1696 |
unsigned int siglen2 = DSA_SIGNATURE_LEN / 2;
|
|
1697 |
|
|
1698 |
PK11_SESSION *sp = NULL;
|
|
1699 |
|
|
1700 |
if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
|
|
1701 |
{
|
|
1702 |
PK11err(PK11_F_DSA_SIGN, PK11_R_MISSING_KEY_COMPONENT);
|
|
1703 |
goto ret;
|
|
1704 |
}
|
|
1705 |
|
|
1706 |
i = BN_num_bytes(dsa->q); /* should be 20 */
|
|
1707 |
if (dlen > i)
|
|
1708 |
{
|
|
1709 |
PK11err(PK11_F_DSA_SIGN, PK11_R_INVALID_SIGNATURE_LENGTH);
|
|
1710 |
goto ret;
|
|
1711 |
}
|
|
1712 |
|
|
1713 |
if ((sp = pk11_get_session(OP_DSA)) == NULL)
|
|
1714 |
goto ret;
|
|
1715 |
|
|
1716 |
(void) check_new_dsa_key_priv(sp, dsa);
|
|
1717 |
|
|
1718 |
h_priv_key = sp->opdata_dsa_priv_key;
|
|
1719 |
if (h_priv_key == CK_INVALID_HANDLE)
|
|
1720 |
h_priv_key = sp->opdata_dsa_priv_key =
|
|
1721 |
pk11_get_private_dsa_key((DSA *)dsa,
|
|
1722 |
&sp->opdata_dsa_priv,
|
|
1723 |
&sp->opdata_dsa_priv_num, sp->session);
|
|
1724 |
|
|
1725 |
if (h_priv_key != CK_INVALID_HANDLE)
|
|
1726 |
{
|
|
1727 |
rv = pFuncList->C_SignInit(sp->session, p_mech, h_priv_key);
|
|
1728 |
|
|
1729 |
if (rv != CKR_OK)
|
|
1730 |
{
|
|
1731 |
PK11err_add_data(PK11_F_DSA_SIGN, PK11_R_SIGNINIT, rv);
|
|
1732 |
goto ret;
|
|
1733 |
}
|
|
1734 |
|
|
1735 |
(void) memset(sigret, 0, siglen);
|
|
1736 |
rv = pFuncList->C_Sign(sp->session,
|
|
1737 |
(unsigned char *) dgst, dlen, sigret,
|
|
1738 |
(CK_ULONG_PTR) &siglen);
|
|
1739 |
|
|
1740 |
if (rv != CKR_OK)
|
|
1741 |
{
|
|
1742 |
PK11err_add_data(PK11_F_DSA_SIGN, PK11_R_SIGN, rv);
|
|
1743 |
goto ret;
|
|
1744 |
}
|
|
1745 |
}
|
|
1746 |
|
|
1747 |
|
|
1748 |
if ((s = BN_new()) == NULL)
|
|
1749 |
{
|
|
1750 |
PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE);
|
|
1751 |
goto ret;
|
|
1752 |
}
|
|
1753 |
|
|
1754 |
if ((r = BN_new()) == NULL)
|
|
1755 |
{
|
|
1756 |
PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE);
|
|
1757 |
goto ret;
|
|
1758 |
}
|
|
1759 |
|
|
1760 |
if ((dsa_sig = DSA_SIG_new()) == NULL)
|
|
1761 |
{
|
|
1762 |
PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE);
|
|
1763 |
goto ret;
|
|
1764 |
}
|
|
1765 |
|
|
1766 |
if (BN_bin2bn(sigret, siglen2, r) == NULL ||
|
|
1767 |
BN_bin2bn(&sigret[siglen2], siglen2, s) == NULL)
|
|
1768 |
{
|
|
1769 |
PK11err(PK11_F_DSA_SIGN, PK11_R_MALLOC_FAILURE);
|
|
1770 |
goto ret;
|
|
1771 |
}
|
|
1772 |
|
|
1773 |
dsa_sig->r = r;
|
|
1774 |
dsa_sig->s = s;
|
|
1775 |
|
|
1776 |
ret:
|
|
1777 |
if (dsa_sig == NULL)
|
|
1778 |
{
|
|
1779 |
if (r != NULL)
|
|
1780 |
BN_free(r);
|
|
1781 |
if (s != NULL)
|
|
1782 |
BN_free(s);
|
|
1783 |
}
|
|
1784 |
|
|
1785 |
pk11_return_session(sp, OP_DSA);
|
|
1786 |
return (dsa_sig);
|
|
1787 |
}
|
|
1788 |
|
|
1789 |
static int
|
|
1790 |
pk11_dsa_do_verify(const unsigned char *dgst, int dlen, DSA_SIG *sig,
|
|
1791 |
DSA *dsa)
|
|
1792 |
{
|
|
1793 |
int i;
|
|
1794 |
CK_RV rv;
|
|
1795 |
int retval = 0;
|
|
1796 |
CK_MECHANISM Mechanism_dsa = {CKM_DSA, NULL, 0};
|
|
1797 |
CK_MECHANISM *p_mech = &Mechanism_dsa;
|
|
1798 |
CK_OBJECT_HANDLE h_pub_key;
|
|
1799 |
|
|
1800 |
unsigned char sigbuf[DSA_SIGNATURE_LEN];
|
|
1801 |
unsigned long siglen = DSA_SIGNATURE_LEN;
|
|
1802 |
unsigned long siglen2 = DSA_SIGNATURE_LEN/2;
|
|
1803 |
|
|
1804 |
PK11_SESSION *sp = NULL;
|
|
1805 |
|
|
1806 |
if (BN_is_zero(sig->r) || sig->r->neg || BN_ucmp(sig->r, dsa->q) >= 0)
|
|
1807 |
{
|
|
1808 |
PK11err(PK11_F_DSA_VERIFY,
|
|
1809 |
PK11_R_INVALID_DSA_SIGNATURE_R);
|
|
1810 |
goto ret;
|
|
1811 |
}
|
|
1812 |
|
|
1813 |
if (BN_is_zero(sig->s) || sig->s->neg || BN_ucmp(sig->s, dsa->q) >= 0)
|
|
1814 |
{
|
|
1815 |
PK11err(PK11_F_DSA_VERIFY,
|
|
1816 |
PK11_R_INVALID_DSA_SIGNATURE_S);
|
|
1817 |
goto ret;
|
|
1818 |
}
|
|
1819 |
|
|
1820 |
i = BN_num_bytes(dsa->q); /* should be 20 */
|
|
1821 |
|
|
1822 |
if (dlen > i)
|
|
1823 |
{
|
|
1824 |
PK11err(PK11_F_DSA_VERIFY,
|
|
1825 |
PK11_R_INVALID_SIGNATURE_LENGTH);
|
|
1826 |
goto ret;
|
|
1827 |
}
|
|
1828 |
|
|
1829 |
if ((sp = pk11_get_session(OP_DSA)) == NULL)
|
|
1830 |
goto ret;
|
|
1831 |
|
|
1832 |
(void) check_new_dsa_key_pub(sp, dsa);
|
|
1833 |
|
|
1834 |
h_pub_key = sp->opdata_dsa_pub_key;
|
|
1835 |
if (h_pub_key == CK_INVALID_HANDLE)
|
|
1836 |
h_pub_key = sp->opdata_dsa_pub_key =
|
|
1837 |
pk11_get_public_dsa_key((DSA *)dsa, &sp->opdata_dsa_pub,
|
|
1838 |
&sp->opdata_dsa_pub_num, sp->session);
|
|
1839 |
|
|
1840 |
if (h_pub_key != CK_INVALID_HANDLE)
|
|
1841 |
{
|
|
1842 |
rv = pFuncList->C_VerifyInit(sp->session, p_mech,
|
|
1843 |
h_pub_key);
|
|
1844 |
|
|
1845 |
if (rv != CKR_OK)
|
|
1846 |
{
|
|
1847 |
PK11err_add_data(PK11_F_DSA_VERIFY, PK11_R_VERIFYINIT,
|
|
1848 |
rv);
|
|
1849 |
goto ret;
|
|
1850 |
}
|
|
1851 |
|
|
1852 |
/*
|
|
1853 |
* The representation of each of the two big numbers could
|
|
1854 |
* be shorter than DSA_SIGNATURE_LEN/2 bytes so we need
|
|
1855 |
* to act accordingly and shift if necessary.
|
|
1856 |
*/
|
|
1857 |
(void) memset(sigbuf, 0, siglen);
|
|
1858 |
BN_bn2bin(sig->r, sigbuf + siglen2 - BN_num_bytes(sig->r));
|
|
1859 |
BN_bn2bin(sig->s, &sigbuf[siglen2] + siglen2 -
|
|
1860 |
BN_num_bytes(sig->s));
|
|
1861 |
|
|
1862 |
rv = pFuncList->C_Verify(sp->session,
|
|
1863 |
(unsigned char *) dgst, dlen, sigbuf, (CK_ULONG)siglen);
|
|
1864 |
|
|
1865 |
if (rv != CKR_OK)
|
|
1866 |
{
|
|
1867 |
PK11err_add_data(PK11_F_DSA_VERIFY, PK11_R_VERIFY, rv);
|
|
1868 |
goto ret;
|
|
1869 |
}
|
|
1870 |
}
|
|
1871 |
|
|
1872 |
retval = 1;
|
|
1873 |
ret:
|
|
1874 |
|
|
1875 |
pk11_return_session(sp, OP_DSA);
|
|
1876 |
return (retval);
|
|
1877 |
}
|
|
1878 |
|
|
1879 |
|
|
1880 |
/*
|
|
1881 |
* Create a public key object in a session from a given dsa structure.
|
|
1882 |
* The *dsa_pub_num pointer is non-NULL for DSA public keys.
|
|
1883 |
*/
|
|
1884 |
static CK_OBJECT_HANDLE pk11_get_public_dsa_key(DSA* dsa,
|
|
1885 |
DSA **key_ptr, BIGNUM **dsa_pub_num, CK_SESSION_HANDLE session)
|
|
1886 |
{
|
|
1887 |
CK_RV rv;
|
|
1888 |
CK_OBJECT_CLASS o_key = CKO_PUBLIC_KEY;
|
|
1889 |
CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
|
|
1890 |
CK_ULONG found;
|
|
1891 |
CK_KEY_TYPE k_type = CKK_DSA;
|
|
1892 |
CK_ULONG ul_key_attr_count = 8;
|
|
1893 |
CK_BBOOL rollback = FALSE;
|
|
1894 |
int i;
|
|
1895 |
|
|
1896 |
CK_ATTRIBUTE a_key_template[] =
|
|
1897 |
{
|
|
1898 |
{CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)},
|
|
1899 |
{CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)},
|
|
1900 |
{CKA_TOKEN, &false, sizeof (true)},
|
|
1901 |
{CKA_VERIFY, &true, sizeof (true)},
|
|
1902 |
{CKA_PRIME, (void *)NULL, 0}, /* p */
|
|
1903 |
{CKA_SUBPRIME, (void *)NULL, 0}, /* q */
|
|
1904 |
{CKA_BASE, (void *)NULL, 0}, /* g */
|
|
1905 |
{CKA_VALUE, (void *)NULL, 0} /* pub_key - y */
|
|
1906 |
};
|
|
1907 |
|
|
1908 |
a_key_template[0].pValue = &o_key;
|
|
1909 |
a_key_template[1].pValue = &k_type;
|
|
1910 |
|
|
1911 |
if (init_template_value(dsa->p, &a_key_template[4].pValue,
|
|
1912 |
&a_key_template[4].ulValueLen) == 0 ||
|
|
1913 |
init_template_value(dsa->q, &a_key_template[5].pValue,
|
|
1914 |
&a_key_template[5].ulValueLen) == 0 ||
|
|
1915 |
init_template_value(dsa->g, &a_key_template[6].pValue,
|
|
1916 |
&a_key_template[6].ulValueLen) == 0 ||
|
|
1917 |
init_template_value(dsa->pub_key, &a_key_template[7].pValue,
|
|
1918 |
&a_key_template[7].ulValueLen) == 0)
|
|
1919 |
{
|
|
1920 |
PK11err(PK11_F_GET_PUB_DSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1921 |
goto malloc_err;
|
|
1922 |
}
|
|
1923 |
|
|
1924 |
/* see find_lock array definition for more info on object locking */
|
|
1925 |
LOCK_OBJSTORE(OP_DSA);
|
|
1926 |
rv = pFuncList->C_FindObjectsInit(session, a_key_template,
|
|
1927 |
ul_key_attr_count);
|
|
1928 |
|
|
1929 |
if (rv != CKR_OK)
|
|
1930 |
{
|
|
1931 |
PK11err_add_data(PK11_F_GET_PUB_DSA_KEY, PK11_R_FINDOBJECTSINIT,
|
|
1932 |
rv);
|
|
1933 |
goto err;
|
|
1934 |
}
|
|
1935 |
|
|
1936 |
rv = pFuncList->C_FindObjects(session, &h_key, 1, &found);
|
|
1937 |
|
|
1938 |
if (rv != CKR_OK)
|
|
1939 |
{
|
|
1940 |
PK11err_add_data(PK11_F_GET_PUB_DSA_KEY,
|
|
1941 |
PK11_R_FINDOBJECTS, rv);
|
|
1942 |
goto err;
|
|
1943 |
}
|
|
1944 |
|
|
1945 |
rv = pFuncList->C_FindObjectsFinal(session);
|
|
1946 |
|
|
1947 |
if (rv != CKR_OK)
|
|
1948 |
{
|
|
1949 |
PK11err_add_data(PK11_F_GET_PUB_DSA_KEY,
|
|
1950 |
PK11_R_FINDOBJECTSFINAL, rv);
|
|
1951 |
goto err;
|
|
1952 |
}
|
|
1953 |
|
|
1954 |
if (found == 0)
|
|
1955 |
{
|
|
1956 |
rv = pFuncList->C_CreateObject(session,
|
|
1957 |
a_key_template, ul_key_attr_count, &h_key);
|
|
1958 |
if (rv != CKR_OK)
|
|
1959 |
{
|
|
1960 |
PK11err_add_data(PK11_F_GET_PUB_DSA_KEY,
|
|
1961 |
PK11_R_CREATEOBJECT, rv);
|
|
1962 |
goto err;
|
|
1963 |
}
|
|
1964 |
}
|
|
1965 |
|
|
1966 |
if (dsa_pub_num != NULL)
|
|
1967 |
if ((*dsa_pub_num = BN_dup(dsa->pub_key)) == NULL)
|
|
1968 |
{
|
|
1969 |
PK11err(PK11_F_GET_PUB_DSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
1970 |
rollback = TRUE;
|
|
1971 |
goto err;
|
|
1972 |
}
|
|
1973 |
|
|
1974 |
/* LINTED: E_CONSTANT_CONDITION */
|
|
1975 |
KEY_HANDLE_REFHOLD(h_key, OP_DSA, FALSE, rollback, err);
|
|
1976 |
if (key_ptr != NULL)
|
|
1977 |
*key_ptr = dsa;
|
|
1978 |
|
|
1979 |
err:
|
|
1980 |
if (rollback)
|
|
1981 |
{
|
|
1982 |
/*
|
|
1983 |
* We do not care about the return value from C_DestroyObject()
|
|
1984 |
* since we are doing rollback.
|
|
1985 |
*/
|
|
1986 |
if (found == 0)
|
|
1987 |
(void) pFuncList->C_DestroyObject(session, h_key);
|
|
1988 |
h_key = CK_INVALID_HANDLE;
|
|
1989 |
}
|
|
1990 |
|
|
1991 |
UNLOCK_OBJSTORE(OP_DSA);
|
|
1992 |
|
|
1993 |
malloc_err:
|
|
1994 |
for (i = 4; i <= 7; i++)
|
|
1995 |
{
|
|
1996 |
if (a_key_template[i].pValue != NULL)
|
|
1997 |
{
|
|
1998 |
OPENSSL_free(a_key_template[i].pValue);
|
|
1999 |
a_key_template[i].pValue = NULL;
|
|
2000 |
}
|
|
2001 |
}
|
|
2002 |
|
|
2003 |
return (h_key);
|
|
2004 |
}
|
|
2005 |
|
|
2006 |
/*
|
|
2007 |
* Create a private key object in the session from a given dsa structure
|
|
2008 |
* The *dsa_priv_num pointer is non-NULL for DSA private keys.
|
|
2009 |
*/
|
|
2010 |
static CK_OBJECT_HANDLE pk11_get_private_dsa_key(DSA* dsa,
|
|
2011 |
DSA **key_ptr, BIGNUM **dsa_priv_num, CK_SESSION_HANDLE session)
|
|
2012 |
{
|
|
2013 |
CK_RV rv;
|
|
2014 |
CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
|
|
2015 |
CK_OBJECT_CLASS o_key = CKO_PRIVATE_KEY;
|
|
2016 |
int i;
|
|
2017 |
CK_ULONG found;
|
|
2018 |
CK_KEY_TYPE k_type = CKK_DSA;
|
|
2019 |
CK_ULONG ul_key_attr_count = 9;
|
|
2020 |
CK_BBOOL rollback = FALSE;
|
|
2021 |
|
|
2022 |
/* Both CKA_TOKEN and CKA_SENSITIVE have to be FALSE for session keys */
|
|
2023 |
CK_ATTRIBUTE a_key_template[] =
|
|
2024 |
{
|
|
2025 |
{CKA_CLASS, (void *) NULL, sizeof (CK_OBJECT_CLASS)},
|
|
2026 |
{CKA_KEY_TYPE, (void *) NULL, sizeof (CK_KEY_TYPE)},
|
|
2027 |
{CKA_TOKEN, &false, sizeof (true)},
|
|
2028 |
{CKA_SENSITIVE, &false, sizeof (true)},
|
|
2029 |
{CKA_SIGN, &true, sizeof (true)},
|
|
2030 |
{CKA_PRIME, (void *)NULL, 0}, /* p */
|
|
2031 |
{CKA_SUBPRIME, (void *)NULL, 0}, /* q */
|
|
2032 |
{CKA_BASE, (void *)NULL, 0}, /* g */
|
|
2033 |
{CKA_VALUE, (void *)NULL, 0} /* priv_key - x */
|
|
2034 |
};
|
|
2035 |
|
|
2036 |
a_key_template[0].pValue = &o_key;
|
|
2037 |
a_key_template[1].pValue = &k_type;
|
|
2038 |
|
|
2039 |
/* Put the private key components into the template */
|
|
2040 |
if (init_template_value(dsa->p, &a_key_template[5].pValue,
|
|
2041 |
&a_key_template[5].ulValueLen) == 0 ||
|
|
2042 |
init_template_value(dsa->q, &a_key_template[6].pValue,
|
|
2043 |
&a_key_template[6].ulValueLen) == 0 ||
|
|
2044 |
init_template_value(dsa->g, &a_key_template[7].pValue,
|
|
2045 |
&a_key_template[7].ulValueLen) == 0 ||
|
|
2046 |
init_template_value(dsa->priv_key, &a_key_template[8].pValue,
|
|
2047 |
&a_key_template[8].ulValueLen) == 0)
|
|
2048 |
{
|
|
2049 |
PK11err(PK11_F_GET_PRIV_DSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
2050 |
goto malloc_err;
|
|
2051 |
}
|
|
2052 |
|
|
2053 |
/* see find_lock array definition for more info on object locking */
|
|
2054 |
LOCK_OBJSTORE(OP_DSA);
|
|
2055 |
rv = pFuncList->C_FindObjectsInit(session, a_key_template,
|
|
2056 |
ul_key_attr_count);
|
|
2057 |
|
|
2058 |
if (rv != CKR_OK)
|
|
2059 |
{
|
|
2060 |
PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY,
|
|
2061 |
PK11_R_FINDOBJECTSINIT, rv);
|
|
2062 |
goto err;
|
|
2063 |
}
|
|
2064 |
|
|
2065 |
rv = pFuncList->C_FindObjects(session, &h_key, 1, &found);
|
|
2066 |
|
|
2067 |
if (rv != CKR_OK)
|
|
2068 |
{
|
|
2069 |
PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY,
|
|
2070 |
PK11_R_FINDOBJECTS, rv);
|
|
2071 |
goto err;
|
|
2072 |
}
|
|
2073 |
|
|
2074 |
rv = pFuncList->C_FindObjectsFinal(session);
|
|
2075 |
|
|
2076 |
if (rv != CKR_OK)
|
|
2077 |
{
|
|
2078 |
PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY,
|
|
2079 |
PK11_R_FINDOBJECTSFINAL, rv);
|
|
2080 |
goto err;
|
|
2081 |
}
|
|
2082 |
|
|
2083 |
if (found == 0)
|
|
2084 |
{
|
|
2085 |
rv = pFuncList->C_CreateObject(session,
|
|
2086 |
a_key_template, ul_key_attr_count, &h_key);
|
|
2087 |
if (rv != CKR_OK)
|
|
2088 |
{
|
|
2089 |
PK11err_add_data(PK11_F_GET_PRIV_DSA_KEY,
|
|
2090 |
PK11_R_CREATEOBJECT, rv);
|
|
2091 |
goto err;
|
|
2092 |
}
|
|
2093 |
}
|
|
2094 |
|
|
2095 |
if (dsa_priv_num != NULL)
|
|
2096 |
if ((*dsa_priv_num = BN_dup(dsa->priv_key)) == NULL)
|
|
2097 |
{
|
|
2098 |
PK11err(PK11_F_GET_PRIV_DSA_KEY, PK11_R_MALLOC_FAILURE);
|
|
2099 |
rollback = TRUE;
|
|
2100 |
goto err;
|
|
2101 |
}
|
|
2102 |
|
|
2103 |
/* LINTED: E_CONSTANT_CONDITION */
|
|
2104 |
KEY_HANDLE_REFHOLD(h_key, OP_DSA, FALSE, rollback, err);
|
|
2105 |
if (key_ptr != NULL)
|
|
2106 |
*key_ptr = dsa;
|
|
2107 |
|
|
2108 |
err:
|
|
2109 |
if (rollback)
|
|
2110 |
{
|
|
2111 |
/*
|
|
2112 |
* We do not care about the return value from C_DestroyObject()
|
|
2113 |
* since we are doing rollback.
|
|
2114 |
*/
|
|
2115 |
if (found == 0)
|
|
2116 |
(void) pFuncList->C_DestroyObject(session, h_key);
|
|
2117 |
h_key = CK_INVALID_HANDLE;
|
|
2118 |
}
|
|
2119 |
|
|
2120 |
UNLOCK_OBJSTORE(OP_DSA);
|
|
2121 |
|
|
2122 |
malloc_err:
|
|
2123 |
/*
|
|
2124 |
* 5 to 8 entries in the key template are key components.
|
|
2125 |
* They need to be freed apon exit or error.
|
|
2126 |
*/
|
|
2127 |
for (i = 5; i <= 8; i++)
|
|
2128 |
{
|
|
2129 |
if (a_key_template[i].pValue != NULL)
|
|
2130 |
{
|
|
2131 |
(void) memset(a_key_template[i].pValue, 0,
|
|
2132 |
a_key_template[i].ulValueLen);
|
|
2133 |
OPENSSL_free(a_key_template[i].pValue);
|
|
2134 |
a_key_template[i].pValue = NULL;
|
|
2135 |
}
|
|
2136 |
}
|
|
2137 |
|
|
2138 |
return (h_key);
|
|
2139 |
}
|
|
2140 |
|
|
2141 |
/*
|
|
2142 |
* Check for cache miss and clean the object pointer and handle
|
|
2143 |
* in such case. Return 1 for cache hit, 0 for cache miss.
|
|
2144 |
*/
|
|
2145 |
static int check_new_dsa_key_pub(PK11_SESSION *sp, DSA *dsa)
|
|
2146 |
{
|
|
2147 |
/*
|
|
2148 |
* Provide protection against DSA structure reuse by making the
|
|
2149 |
* check for cache hit stronger. Only public key component of DSA
|
|
2150 |
* key matters here so it is sufficient to compare it with value
|
|
2151 |
* cached in PK11_SESSION structure.
|
|
2152 |
*/
|
|
2153 |
if ((sp->opdata_dsa_pub != dsa) ||
|
|
2154 |
(BN_cmp(sp->opdata_dsa_pub_num, dsa->pub_key) != 0))
|
|
2155 |
{
|
|
2156 |
/*
|
|
2157 |
* We do not check the return value because even in case of
|
|
2158 |
* failure the sp structure will have both key pointer
|
|
2159 |
* and object handle cleaned and pk11_destroy_object()
|
|
2160 |
* reports the failure to the OpenSSL error message buffer.
|
|
2161 |
*/
|
|
2162 |
(void) pk11_destroy_dsa_object_pub(sp, TRUE);
|
|
2163 |
return (0);
|
|
2164 |
}
|
|
2165 |
return (1);
|
|
2166 |
}
|
|
2167 |
|
|
2168 |
/*
|
|
2169 |
* Check for cache miss and clean the object pointer and handle
|
|
2170 |
* in such case. Return 1 for cache hit, 0 for cache miss.
|
|
2171 |
*/
|
|
2172 |
static int check_new_dsa_key_priv(PK11_SESSION *sp, DSA *dsa)
|
|
2173 |
{
|
|
2174 |
/*
|
|
2175 |
* Provide protection against DSA structure reuse by making the
|
|
2176 |
* check for cache hit stronger. Only private key component of DSA
|
|
2177 |
* key matters here so it is sufficient to compare it with value
|
|
2178 |
* cached in PK11_SESSION structure.
|
|
2179 |
*/
|
|
2180 |
if ((sp->opdata_dsa_priv != dsa) ||
|
|
2181 |
(BN_cmp(sp->opdata_dsa_priv_num, dsa->priv_key) != 0))
|
|
2182 |
{
|
|
2183 |
/*
|
|
2184 |
* We do not check the return value because even in case of
|
|
2185 |
* failure the sp structure will have both key pointer
|
|
2186 |
* and object handle cleaned and pk11_destroy_object()
|
|
2187 |
* reports the failure to the OpenSSL error message buffer.
|
|
2188 |
*/
|
|
2189 |
(void) pk11_destroy_dsa_object_priv(sp, TRUE);
|
|
2190 |
return (0);
|
|
2191 |
}
|
|
2192 |
return (1);
|
|
2193 |
}
|
|
2194 |
#endif
|
|
2195 |
|
|
2196 |
|
|
2197 |
#ifndef OPENSSL_NO_DH
|
|
2198 |
/* The DH function implementation */
|
|
2199 |
/* ARGSUSED */
|
|
2200 |
static int pk11_DH_init(DH *dh)
|
|
2201 |
{
|
|
2202 |
return (1);
|
|
2203 |
}
|
|
2204 |
|
|
2205 |
/* ARGSUSED */
|
|
2206 |
static int pk11_DH_finish(DH *dh)
|
|
2207 |
{
|
|
2208 |
return (1);
|
|
2209 |
}
|
|
2210 |
|
|
2211 |
/*
|
|
2212 |
* Generate DH key-pair.
|
|
2213 |
*
|
|
2214 |
* Warning: Unlike OpenSSL's DH_generate_key(3) we ignore dh->priv_key
|
|
2215 |
* and override it even if it is set. OpenSSL does not touch dh->priv_key
|
|
2216 |
* if set and just computes dh->pub_key. It looks like PKCS#11 standard
|
|
2217 |
* is not capable of providing this functionality. This could be a problem
|
|
2218 |
* for applications relying on OpenSSL's semantics.
|
|
2219 |
*/
|
|
2220 |
static int pk11_DH_generate_key(DH *dh)
|
|
2221 |
{
|
|
2222 |
CK_ULONG i;
|
|
2223 |
CK_RV rv, rv1;
|
|
2224 |
int reuse_mem_len = 0, ret = 0;
|
|
2225 |
PK11_SESSION *sp = NULL;
|
|
2226 |
CK_BYTE_PTR reuse_mem;
|
|
2227 |
|
|
2228 |
CK_MECHANISM mechanism = {CKM_DH_PKCS_KEY_PAIR_GEN, NULL_PTR, 0};
|
|
2229 |
CK_OBJECT_HANDLE h_pub_key = CK_INVALID_HANDLE;
|
|
2230 |
CK_OBJECT_HANDLE h_priv_key = CK_INVALID_HANDLE;
|
|
2231 |
|
|
2232 |
CK_ULONG ul_pub_key_attr_count = 3;
|
|
2233 |
CK_ATTRIBUTE pub_key_template[] =
|
|
2234 |
{
|
|
2235 |
{CKA_PRIVATE, &false, sizeof (false)},
|
|
2236 |
{CKA_PRIME, (void *)NULL, 0},
|
|
2237 |
{CKA_BASE, (void *)NULL, 0}
|
|
2238 |
};
|
|
2239 |
|
|
2240 |
CK_ULONG ul_priv_key_attr_count = 3;
|
|
2241 |
CK_ATTRIBUTE priv_key_template[] =
|
|
2242 |
{
|
|
2243 |
{CKA_PRIVATE, &false, sizeof (false)},
|
|
2244 |
{CKA_SENSITIVE, &false, sizeof (false)},
|
|
2245 |
{CKA_DERIVE, &true, sizeof (true)}
|
|
2246 |
};
|
|
2247 |
|
|
2248 |
CK_ULONG pub_key_attr_result_count = 1;
|
|
2249 |
CK_ATTRIBUTE pub_key_result[] =
|
|
2250 |
{
|
|
2251 |
{CKA_VALUE, (void *)NULL, 0}
|
|
2252 |
};
|
|
2253 |
|
|
2254 |
CK_ULONG priv_key_attr_result_count = 1;
|
|
2255 |
CK_ATTRIBUTE priv_key_result[] =
|
|
2256 |
{
|
|
2257 |
{CKA_VALUE, (void *)NULL, 0}
|
|
2258 |
};
|
|
2259 |
|
|
2260 |
pub_key_template[1].ulValueLen = BN_num_bytes(dh->p);
|
|
2261 |
if (pub_key_template[1].ulValueLen > 0)
|
|
2262 |
{
|
|
2263 |
/*
|
|
2264 |
* We must not increase ulValueLen by DH_BUF_RESERVE since that
|
|
2265 |
* could cause the same rounding problem. See definition of
|
|
2266 |
* DH_BUF_RESERVE above.
|
|
2267 |
*/
|
|
2268 |
pub_key_template[1].pValue =
|
|
2269 |
OPENSSL_malloc(pub_key_template[1].ulValueLen +
|
|
2270 |
DH_BUF_RESERVE);
|
|
2271 |
if (pub_key_template[1].pValue == NULL)
|
|
2272 |
{
|
|
2273 |
PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE);
|
|
2274 |
goto err;
|
|
2275 |
}
|
|
2276 |
|
|
2277 |
i = BN_bn2bin(dh->p, pub_key_template[1].pValue);
|
|
2278 |
}
|
|
2279 |
else
|
|
2280 |
goto err;
|
|
2281 |
|
|
2282 |
pub_key_template[2].ulValueLen = BN_num_bytes(dh->g);
|
|
2283 |
if (pub_key_template[2].ulValueLen > 0)
|
|
2284 |
{
|
|
2285 |
pub_key_template[2].pValue =
|
|
2286 |
OPENSSL_malloc(pub_key_template[2].ulValueLen +
|
|
2287 |
DH_BUF_RESERVE);
|
|
2288 |
if (pub_key_template[2].pValue == NULL)
|
|
2289 |
{
|
|
2290 |
PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE);
|
|
2291 |
goto err;
|
|
2292 |
}
|
|
2293 |
|
|
2294 |
i = BN_bn2bin(dh->g, pub_key_template[2].pValue);
|
|
2295 |
}
|
|
2296 |
else
|
|
2297 |
goto err;
|
|
2298 |
|
|
2299 |
/*
|
|
2300 |
* Note: we are only using PK11_SESSION structure for getting
|
|
2301 |
* a session handle. The objects created in this function are
|
|
2302 |
* destroyed before return and thus not cached.
|
|
2303 |
*/
|
|
2304 |
if ((sp = pk11_get_session(OP_DH)) == NULL)
|
|
2305 |
goto err;
|
|
2306 |
|
|
2307 |
rv = pFuncList->C_GenerateKeyPair(sp->session,
|
|
2308 |
&mechanism,
|
|
2309 |
pub_key_template,
|
|
2310 |
ul_pub_key_attr_count,
|
|
2311 |
priv_key_template,
|
|
2312 |
ul_priv_key_attr_count,
|
|
2313 |
&h_pub_key,
|
|
2314 |
&h_priv_key);
|
|
2315 |
if (rv != CKR_OK)
|
|
2316 |
{
|
|
2317 |
PK11err_add_data(PK11_F_DH_GEN_KEY, PK11_R_GEN_KEY, rv);
|
|
2318 |
goto err;
|
|
2319 |
}
|
|
2320 |
|
|
2321 |
/*
|
|
2322 |
* Reuse the larger memory allocated. We know the larger memory
|
|
2323 |
* should be sufficient for reuse.
|
|
2324 |
*/
|
|
2325 |
if (pub_key_template[1].ulValueLen > pub_key_template[2].ulValueLen)
|
|
2326 |
{
|
|
2327 |
reuse_mem = pub_key_template[1].pValue;
|
|
2328 |
reuse_mem_len = pub_key_template[1].ulValueLen + DH_BUF_RESERVE;
|
|
2329 |
}
|
|
2330 |
else
|
|
2331 |
{
|
|
2332 |
reuse_mem = pub_key_template[2].pValue;
|
|
2333 |
reuse_mem_len = pub_key_template[2].ulValueLen + DH_BUF_RESERVE;
|
|
2334 |
}
|
|
2335 |
|
|
2336 |
rv = pFuncList->C_GetAttributeValue(sp->session, h_pub_key,
|
|
2337 |
pub_key_result, pub_key_attr_result_count);
|
|
2338 |
rv1 = pFuncList->C_GetAttributeValue(sp->session, h_priv_key,
|
|
2339 |
priv_key_result, priv_key_attr_result_count);
|
|
2340 |
|
|
2341 |
if (rv != CKR_OK || rv1 != CKR_OK)
|
|
2342 |
{
|
|
2343 |
rv = (rv != CKR_OK) ? rv : rv1;
|
|
2344 |
PK11err_add_data(PK11_F_DH_GEN_KEY,
|
|
2345 |
PK11_R_GETATTRIBUTVALUE, rv);
|
|
2346 |
goto err;
|
|
2347 |
}
|
|
2348 |
|
|
2349 |
if (((CK_LONG) pub_key_result[0].ulValueLen) <= 0 ||
|
|
2350 |
((CK_LONG) priv_key_result[0].ulValueLen) <= 0)
|
|
2351 |
{
|
|
2352 |
PK11err(PK11_F_DH_GEN_KEY, PK11_R_GETATTRIBUTVALUE);
|
|
2353 |
goto err;
|
|
2354 |
}
|
|
2355 |
|
|
2356 |
/* Reuse the memory allocated */
|
|
2357 |
pub_key_result[0].pValue = reuse_mem;
|
|
2358 |
pub_key_result[0].ulValueLen = reuse_mem_len;
|
|
2359 |
|
|
2360 |
rv = pFuncList->C_GetAttributeValue(sp->session, h_pub_key,
|
|
2361 |
pub_key_result, pub_key_attr_result_count);
|
|
2362 |
|
|
2363 |
if (rv != CKR_OK)
|
|
2364 |
{
|
|
2365 |
PK11err_add_data(PK11_F_DH_GEN_KEY,
|
|
2366 |
PK11_R_GETATTRIBUTVALUE, rv);
|
|
2367 |
goto err;
|
|
2368 |
}
|
|
2369 |
|
|
2370 |
if (pub_key_result[0].type == CKA_VALUE)
|
|
2371 |
{
|
|
2372 |
if (dh->pub_key == NULL)
|
|
2373 |
if ((dh->pub_key = BN_new()) == NULL)
|
|
2374 |
{
|
|
2375 |
PK11err(PK11_F_DH_GEN_KEY,
|
|
2376 |
PK11_R_MALLOC_FAILURE);
|
|
2377 |
goto err;
|
|
2378 |
}
|
|
2379 |
dh->pub_key = BN_bin2bn(pub_key_result[0].pValue,
|
|
2380 |
pub_key_result[0].ulValueLen, dh->pub_key);
|
|
2381 |
if (dh->pub_key == NULL)
|
|
2382 |
{
|
|
2383 |
PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE);
|
|
2384 |
goto err;
|
|
2385 |
}
|
|
2386 |
}
|
|
2387 |
|
|
2388 |
/* Reuse the memory allocated */
|
|
2389 |
priv_key_result[0].pValue = reuse_mem;
|
|
2390 |
priv_key_result[0].ulValueLen = reuse_mem_len;
|
|
2391 |
|
|
2392 |
rv = pFuncList->C_GetAttributeValue(sp->session, h_priv_key,
|
|
2393 |
priv_key_result, priv_key_attr_result_count);
|
|
2394 |
|
|
2395 |
if (rv != CKR_OK)
|
|
2396 |
{
|
|
2397 |
PK11err_add_data(PK11_F_DH_GEN_KEY,
|
|
2398 |
PK11_R_GETATTRIBUTVALUE, rv);
|
|
2399 |
goto err;
|
|
2400 |
}
|
|
2401 |
|
|
2402 |
if (priv_key_result[0].type == CKA_VALUE)
|
|
2403 |
{
|
|
2404 |
if (dh->priv_key == NULL)
|
|
2405 |
if ((dh->priv_key = BN_new()) == NULL)
|
|
2406 |
{
|
|
2407 |
PK11err(PK11_F_DH_GEN_KEY,
|
|
2408 |
PK11_R_MALLOC_FAILURE);
|
|
2409 |
goto err;
|
|
2410 |
}
|
|
2411 |
dh->priv_key = BN_bin2bn(priv_key_result[0].pValue,
|
|
2412 |
priv_key_result[0].ulValueLen, dh->priv_key);
|
|
2413 |
if (dh->priv_key == NULL)
|
|
2414 |
{
|
|
2415 |
PK11err(PK11_F_DH_GEN_KEY, PK11_R_MALLOC_FAILURE);
|
|
2416 |
goto err;
|
|
2417 |
}
|
|
2418 |
}
|
|
2419 |
|
|
2420 |
ret = 1;
|
|
2421 |
|
|
2422 |
err:
|
|
2423 |
|
|
2424 |
if (h_pub_key != CK_INVALID_HANDLE)
|
|
2425 |
{
|
|
2426 |
rv = pFuncList->C_DestroyObject(sp->session, h_pub_key);
|
|
2427 |
if (rv != CKR_OK)
|
|
2428 |
{
|
|
2429 |
PK11err_add_data(PK11_F_DH_GEN_KEY,
|
|
2430 |
PK11_R_DESTROYOBJECT, rv);
|
|
2431 |
}
|
|
2432 |
}
|
|
2433 |
|
|
2434 |
if (h_priv_key != CK_INVALID_HANDLE)
|
|
2435 |
{
|
|
2436 |
rv = pFuncList->C_DestroyObject(sp->session, h_priv_key);
|
|
2437 |
if (rv != CKR_OK)
|
|
2438 |
{
|
|
2439 |
PK11err_add_data(PK11_F_DH_GEN_KEY,
|
|
2440 |
PK11_R_DESTROYOBJECT, rv);
|
|
2441 |
}
|
|
2442 |
}
|
|
2443 |
|
|
2444 |
for (i = 1; i <= 2; i++)
|
|
2445 |
{
|
|
2446 |
if (pub_key_template[i].pValue != NULL)
|
|
2447 |
{
|
|
2448 |
OPENSSL_free(pub_key_template[i].pValue);
|
|
2449 |
pub_key_template[i].pValue = NULL;
|
|
2450 |
}
|
|
2451 |
}
|
|
2452 |
|
|
2453 |
pk11_return_session(sp, OP_DH);
|
|
2454 |
return (ret);
|
|
2455 |
}
|
|
2456 |
|
|
2457 |
static int pk11_DH_compute_key(unsigned char *key, const BIGNUM *pub_key,
|
|
2458 |
DH *dh)
|
|
2459 |
{
|
|
2460 |
int i;
|
|
2461 |
CK_MECHANISM mechanism = {CKM_DH_PKCS_DERIVE, NULL_PTR, 0};
|
|
2462 |
CK_OBJECT_CLASS key_class = CKO_SECRET_KEY;
|
|
2463 |
CK_KEY_TYPE key_type = CKK_GENERIC_SECRET;
|
|
2464 |
CK_OBJECT_HANDLE h_derived_key = CK_INVALID_HANDLE;
|
|
2465 |
CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
|
|
2466 |
|
|
2467 |
CK_ULONG ul_priv_key_attr_count = 2;
|
|
2468 |
CK_ATTRIBUTE priv_key_template[] =
|
|
2469 |
{
|
|
2470 |
{CKA_CLASS, (void*) NULL, sizeof (key_class)},
|
|
2471 |
{CKA_KEY_TYPE, (void*) NULL, sizeof (key_type)},
|
|
2472 |
};
|
|
2473 |
|
|
2474 |
CK_ULONG priv_key_attr_result_count = 1;
|
|
2475 |
CK_ATTRIBUTE priv_key_result[] =
|
|
2476 |
{
|
|
2477 |
{CKA_VALUE, (void *)NULL, 0}
|
|
2478 |
};
|
|
2479 |
|
|
2480 |
CK_RV rv;
|
|
2481 |
int ret = -1;
|
|
2482 |
PK11_SESSION *sp = NULL;
|
|
2483 |
|
|
2484 |
if (dh->priv_key == NULL)
|
|
2485 |
goto err;
|
|
2486 |
|
|
2487 |
priv_key_template[0].pValue = &key_class;
|
|
2488 |
priv_key_template[1].pValue = &key_type;
|
|
2489 |
|
|
2490 |
if ((sp = pk11_get_session(OP_DH)) == NULL)
|
|
2491 |
goto err;
|
|
2492 |
|
|
2493 |
mechanism.ulParameterLen = BN_num_bytes(pub_key);
|
|
2494 |
mechanism.pParameter = OPENSSL_malloc(mechanism.ulParameterLen);
|
|
2495 |
if (mechanism.pParameter == NULL)
|
|
2496 |
{
|
|
2497 |
PK11err(PK11_F_DH_COMP_KEY, PK11_R_MALLOC_FAILURE);
|
|
2498 |
goto err;
|
|
2499 |
}
|
|
2500 |
BN_bn2bin(pub_key, mechanism.pParameter);
|
|
2501 |
|
|
2502 |
(void) check_new_dh_key(sp, dh);
|
|
2503 |
|
|
2504 |
h_key = sp->opdata_dh_key;
|
|
2505 |
if (h_key == CK_INVALID_HANDLE)
|
|
2506 |
h_key = sp->opdata_dh_key =
|
|
2507 |
pk11_get_dh_key((DH*) dh, &sp->opdata_dh,
|
|
2508 |
&sp->opdata_dh_priv_num, sp->session);
|
|
2509 |
|
|
2510 |
if (h_key == CK_INVALID_HANDLE)
|
|
2511 |
{
|
|
2512 |
PK11err(PK11_F_DH_COMP_KEY, PK11_R_CREATEOBJECT);
|
|
2513 |
goto err;
|
|
2514 |
}
|
|
2515 |
|
|
2516 |
rv = pFuncList->C_DeriveKey(sp->session,
|
|
2517 |
&mechanism,
|
|
2518 |
h_key,
|
|
2519 |
priv_key_template,
|
|
2520 |
ul_priv_key_attr_count,
|
|
2521 |
&h_derived_key);
|
|
2522 |
if (rv != CKR_OK)
|
|
2523 |
{
|
|
2524 |
PK11err_add_data(PK11_F_DH_COMP_KEY, PK11_R_DERIVEKEY, rv);
|
|
2525 |
goto err;
|
|
2526 |
}
|
|
2527 |
|
|
2528 |
rv = pFuncList->C_GetAttributeValue(sp->session, h_derived_key,
|
|
2529 |
priv_key_result, priv_key_attr_result_count);
|
|
2530 |
|
|
2531 |
if (rv != CKR_OK)
|
|
2532 |
{
|
|
2533 |
PK11err_add_data(PK11_F_DH_COMP_KEY, PK11_R_GETATTRIBUTVALUE,
|
|
2534 |
rv);
|
|
2535 |
goto err;
|
|
2536 |
}
|
|
2537 |
|
|
2538 |
if (((CK_LONG) priv_key_result[0].ulValueLen) <= 0)
|
|
2539 |
{
|
|
2540 |
PK11err(PK11_F_DH_COMP_KEY, PK11_R_GETATTRIBUTVALUE);
|
|
2541 |
goto err;
|
|
2542 |
}
|
|
2543 |
priv_key_result[0].pValue =
|
|
2544 |
OPENSSL_malloc(priv_key_result[0].ulValueLen);
|
|
2545 |
if (!priv_key_result[0].pValue)
|
|
2546 |
{
|
|
2547 |
PK11err(PK11_F_DH_COMP_KEY, PK11_R_MALLOC_FAILURE);
|
|
2548 |
goto err;
|
|
2549 |
}
|
|
2550 |
|
|
2551 |
rv = pFuncList->C_GetAttributeValue(sp->session, h_derived_key,
|
|
2552 |
priv_key_result, priv_key_attr_result_count);
|
|
2553 |
|
|
2554 |
if (rv != CKR_OK)
|
|
2555 |
{
|
|
2556 |
PK11err_add_data(PK11_F_DH_COMP_KEY, PK11_R_GETATTRIBUTVALUE,
|
|
2557 |
rv);
|
|
2558 |
goto err;
|
|
2559 |
}
|
|
2560 |
|
|
2561 |
/*
|
|
2562 |
* OpenSSL allocates the output buffer 'key' which is the same
|
|
2563 |
* length of the public key. It is long enough for the derived key
|
|
2564 |
*/
|
|
2565 |
if (priv_key_result[0].type == CKA_VALUE)
|
|
2566 |
{
|
|
2567 |
/*
|
|
2568 |
* CKM_DH_PKCS_DERIVE mechanism is not supposed to strip
|
|
2569 |
* leading zeros from a computed shared secret. However,
|
|
2570 |
* OpenSSL always did it so we must do the same here. The
|
|
2571 |
* vagueness of the spec regarding leading zero bytes was
|
|
2572 |
* finally cleared with TLS 1.1 (RFC 4346) saying that leading
|
|
2573 |
* zeros are stripped before the computed data is used as the
|
|
2574 |
* pre-master secret.
|
|
2575 |
*/
|
|
2576 |
for (i = 0; i < priv_key_result[0].ulValueLen; ++i)
|
|
2577 |
{
|
|
2578 |
if (((char *)priv_key_result[0].pValue)[i] != 0)
|
|
2579 |
break;
|
|
2580 |
}
|
|
2581 |
|
|
2582 |
(void) memcpy(key, ((char *)priv_key_result[0].pValue) + i,
|
|
2583 |
priv_key_result[0].ulValueLen - i);
|
|
2584 |
ret = priv_key_result[0].ulValueLen - i;
|
|
2585 |
}
|
|
2586 |
|
|
2587 |
err:
|
|
2588 |
|
|
2589 |
if (h_derived_key != CK_INVALID_HANDLE)
|
|
2590 |
{
|
|
2591 |
rv = pFuncList->C_DestroyObject(sp->session, h_derived_key);
|
|
2592 |
if (rv != CKR_OK)
|
|
2593 |
{
|
|
2594 |
PK11err_add_data(PK11_F_DH_COMP_KEY,
|
|
2595 |
PK11_R_DESTROYOBJECT, rv);
|
|
2596 |
}
|
|
2597 |
}
|
|
2598 |
if (priv_key_result[0].pValue)
|
|
2599 |
{
|
|
2600 |
OPENSSL_free(priv_key_result[0].pValue);
|
|
2601 |
priv_key_result[0].pValue = NULL;
|
|
2602 |
}
|
|
2603 |
|
|
2604 |
if (mechanism.pParameter)
|
|
2605 |
{
|
|
2606 |
OPENSSL_free(mechanism.pParameter);
|
|
2607 |
mechanism.pParameter = NULL;
|
|
2608 |
}
|
|
2609 |
|
|
2610 |
pk11_return_session(sp, OP_DH);
|
|
2611 |
return (ret);
|
|
2612 |
}
|
|
2613 |
|
|
2614 |
|
|
2615 |
static CK_OBJECT_HANDLE pk11_get_dh_key(DH* dh,
|
|
2616 |
DH **key_ptr, BIGNUM **dh_priv_num, CK_SESSION_HANDLE session)
|
|
2617 |
{
|
|
2618 |
CK_RV rv;
|
|
2619 |
CK_OBJECT_HANDLE h_key = CK_INVALID_HANDLE;
|
|
2620 |
CK_OBJECT_CLASS class = CKO_PRIVATE_KEY;
|
|
2621 |
CK_KEY_TYPE key_type = CKK_DH;
|
|
2622 |
CK_ULONG found;
|
|
2623 |
CK_BBOOL rollback = FALSE;
|
|
2624 |
int i;
|
|
2625 |
|
|
2626 |
CK_ULONG ul_key_attr_count = 7;
|
|
2627 |
CK_ATTRIBUTE key_template[] =
|
|
2628 |
{
|
|
2629 |
{CKA_CLASS, (void*) NULL, sizeof (class)},
|
|
2630 |
{CKA_KEY_TYPE, (void*) NULL, sizeof (key_type)},
|
|
2631 |
{CKA_DERIVE, &true, sizeof (true)},
|
|
2632 |
{CKA_PRIVATE, &false, sizeof (false)},
|
|
2633 |
{CKA_PRIME, (void *) NULL, 0},
|
|
2634 |
{CKA_BASE, (void *) NULL, 0},
|
|
2635 |
{CKA_VALUE, (void *) NULL, 0},
|
|
2636 |
};
|
|
2637 |
|
|
2638 |
key_template[0].pValue = &class;
|
|
2639 |
key_template[1].pValue = &key_type;
|
|
2640 |
|
|
2641 |
key_template[4].ulValueLen = BN_num_bytes(dh->p);
|
|
2642 |
key_template[4].pValue = (CK_VOID_PTR)OPENSSL_malloc(
|
|
2643 |
(size_t)key_template[4].ulValueLen);
|
|
2644 |
if (key_template[4].pValue == NULL)
|
|
2645 |
{
|
|
2646 |
PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE);
|
|
2647 |
goto malloc_err;
|
|
2648 |
}
|
|
2649 |
|
|
2650 |
BN_bn2bin(dh->p, key_template[4].pValue);
|
|
2651 |
|
|
2652 |
key_template[5].ulValueLen = BN_num_bytes(dh->g);
|
|
2653 |
key_template[5].pValue = (CK_VOID_PTR)OPENSSL_malloc(
|
|
2654 |
(size_t)key_template[5].ulValueLen);
|
|
2655 |
if (key_template[5].pValue == NULL)
|
|
2656 |
{
|
|
2657 |
PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE);
|
|
2658 |
goto malloc_err;
|
|
2659 |
}
|
|
2660 |
|
|
2661 |
BN_bn2bin(dh->g, key_template[5].pValue);
|
|
2662 |
|
|
2663 |
key_template[6].ulValueLen = BN_num_bytes(dh->priv_key);
|
|
2664 |
key_template[6].pValue = (CK_VOID_PTR)OPENSSL_malloc(
|
|
2665 |
(size_t)key_template[6].ulValueLen);
|
|
2666 |
if (key_template[6].pValue == NULL)
|
|
2667 |
{
|
|
2668 |
PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE);
|
|
2669 |
goto malloc_err;
|
|
2670 |
}
|
|
2671 |
|
|
2672 |
BN_bn2bin(dh->priv_key, key_template[6].pValue);
|
|
2673 |
|
|
2674 |
/* see find_lock array definition for more info on object locking */
|
|
2675 |
LOCK_OBJSTORE(OP_DH);
|
|
2676 |
rv = pFuncList->C_FindObjectsInit(session, key_template,
|
|
2677 |
ul_key_attr_count);
|
|
2678 |
|
|
2679 |
if (rv != CKR_OK)
|
|
2680 |
{
|
|
2681 |
PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_FINDOBJECTSINIT, rv);
|
|
2682 |
goto err;
|
|
2683 |
}
|
|
2684 |
|
|
2685 |
rv = pFuncList->C_FindObjects(session, &h_key, 1, &found);
|
|
2686 |
|
|
2687 |
if (rv != CKR_OK)
|
|
2688 |
{
|
|
2689 |
PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_FINDOBJECTS, rv);
|
|
2690 |
goto err;
|
|
2691 |
}
|
|
2692 |
|
|
2693 |
rv = pFuncList->C_FindObjectsFinal(session);
|
|
2694 |
|
|
2695 |
if (rv != CKR_OK)
|
|
2696 |
{
|
|
2697 |
PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_FINDOBJECTSFINAL,
|
|
2698 |
rv);
|
|
2699 |
goto err;
|
|
2700 |
}
|
|
2701 |
|
|
2702 |
if (found == 0)
|
|
2703 |
{
|
|
2704 |
rv = pFuncList->C_CreateObject(session,
|
|
2705 |
key_template, ul_key_attr_count, &h_key);
|
|
2706 |
if (rv != CKR_OK)
|
|
2707 |
{
|
|
2708 |
PK11err_add_data(PK11_F_GET_DH_KEY, PK11_R_CREATEOBJECT,
|
|
2709 |
rv);
|
|
2710 |
goto err;
|
|
2711 |
}
|
|
2712 |
}
|
|
2713 |
|
|
2714 |
if (dh_priv_num != NULL)
|
|
2715 |
if ((*dh_priv_num = BN_dup(dh->priv_key)) == NULL)
|
|
2716 |
{
|
|
2717 |
PK11err(PK11_F_GET_DH_KEY, PK11_R_MALLOC_FAILURE);
|
|
2718 |
rollback = TRUE;
|
|
2719 |
goto err;
|
|
2720 |
}
|
|
2721 |
|
|
2722 |
/* LINTED: E_CONSTANT_CONDITION */
|
|
2723 |
KEY_HANDLE_REFHOLD(h_key, OP_DH, FALSE, rollback, err);
|
|
2724 |
if (key_ptr != NULL)
|
|
2725 |
*key_ptr = dh;
|
|
2726 |
|
|
2727 |
err:
|
|
2728 |
if (rollback)
|
|
2729 |
{
|
|
2730 |
/*
|
|
2731 |
* We do not care about the return value from C_DestroyObject()
|
|
2732 |
* since we are doing rollback.
|
|
2733 |
*/
|
|
2734 |
if (found == 0)
|
|
2735 |
(void) pFuncList->C_DestroyObject(session, h_key);
|
|
2736 |
h_key = CK_INVALID_HANDLE;
|
|
2737 |
}
|
|
2738 |
|
|
2739 |
UNLOCK_OBJSTORE(OP_DH);
|
|
2740 |
|
|
2741 |
malloc_err:
|
|
2742 |
for (i = 4; i <= 6; i++)
|
|
2743 |
{
|
|
2744 |
if (key_template[i].pValue != NULL)
|
|
2745 |
{
|
|
2746 |
OPENSSL_free(key_template[i].pValue);
|
|
2747 |
key_template[i].pValue = NULL;
|
|
2748 |
}
|
|
2749 |
}
|
|
2750 |
|
|
2751 |
return (h_key);
|
|
2752 |
}
|
|
2753 |
|
|
2754 |
/*
|
|
2755 |
* Check for cache miss and clean the object pointer and handle
|
|
2756 |
* in such case. Return 1 for cache hit, 0 for cache miss.
|
|
2757 |
*
|
|
2758 |
* Note: we rely on pk11_destroy_dh_key_objects() to set sp->opdata_dh
|
|
2759 |
* to CK_INVALID_HANDLE even when it fails to destroy the object.
|
|
2760 |
*/
|
|
2761 |
static int check_new_dh_key(PK11_SESSION *sp, DH *dh)
|
|
2762 |
{
|
|
2763 |
/*
|
|
2764 |
* Provide protection against DH structure reuse by making the
|
|
2765 |
* check for cache hit stronger. Private key component of DH key
|
|
2766 |
* is unique so it is sufficient to compare it with value cached
|
|
2767 |
* in PK11_SESSION structure.
|
|
2768 |
*/
|
|
2769 |
if ((sp->opdata_dh != dh) ||
|
|
2770 |
(BN_cmp(sp->opdata_dh_priv_num, dh->priv_key) != 0))
|
|
2771 |
{
|
|
2772 |
/*
|
|
2773 |
* We do not check the return value because even in case of
|
|
2774 |
* failure the sp structure will have both key pointer
|
|
2775 |
* and object handle cleaned and pk11_destroy_object()
|
|
2776 |
* reports the failure to the OpenSSL error message buffer.
|
|
2777 |
*/
|
|
2778 |
(void) pk11_destroy_dh_object(sp, TRUE);
|
|
2779 |
return (0);
|
|
2780 |
}
|
|
2781 |
return (1);
|
|
2782 |
}
|
|
2783 |
#endif
|
|
2784 |
|
|
2785 |
/*
|
|
2786 |
* Local function to simplify key template population
|
|
2787 |
* Return 0 -- error, 1 -- no error
|
|
2788 |
*/
|
|
2789 |
static int init_template_value(BIGNUM *bn, CK_VOID_PTR *p_value,
|
|
2790 |
CK_ULONG *ul_value_len)
|
|
2791 |
{
|
|
2792 |
CK_ULONG len = BN_num_bytes(bn);
|
|
2793 |
if (len == 0)
|
|
2794 |
return (1);
|
|
2795 |
|
|
2796 |
*ul_value_len = len;
|
|
2797 |
*p_value = (CK_VOID_PTR)OPENSSL_malloc((size_t)*ul_value_len);
|
|
2798 |
if (*p_value == NULL)
|
|
2799 |
return (0);
|
|
2800 |
|
|
2801 |
BN_bn2bin(bn, *p_value);
|
|
2802 |
|
|
2803 |
return (1);
|
|
2804 |
}
|
|
2805 |
|
|
2806 |
#endif /* OPENSSL_NO_HW_PK11 */
|
|
2807 |
#endif /* OPENSSL_NO_HW */
|