Debian Codegraph @

src/proxy/scutils.c

raw
1 /**********************************************************************
2
3 scutils.c
4
5 Description:
6 Routines used internally to work with smart card
7 using PKCS11
8
9 **********************************************************************/
10
11 /**********************************************************************
12 Include header files
13 **********************************************************************/
14 //#include "config.h"
15
16 #ifdef USE_PKCS11
17
18 #include "scutils.h"
19 #include "sslutils.h"
20
21 #ifndef WIN32
22 #define FILE_SEPERATOR "/"
23 #else
24 #define FILE_SEPERATOR "\\"
25 #include <windows.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/stat.h>
31
32 #ifdef USE_PKCS11_DL
33 #include <dlfcn.h>
34 #endif
35 #include <openssl/buffer.h>
36 #include <openssl/crypto.h>
37 #include <openssl/objects.h>
38 #include <openssl/asn1.h>
39 #include <openssl/evp.h>
40 #include <openssl/x509.h>
41 #include <openssl/pem.h>
42 #include <openssl/ssl.h>
43 #include <openssl/rsa.h>
44
45
46 /**********************************************************************
47 Type definitions
48 **********************************************************************/
49
50 /**********************************************************************
51 Module specific prototypes
52 **********************************************************************/
53
54 static int
55 sc_RSA_eay_private_decrypt(int flen,
56 unsigned char * from,
57 unsigned char * to,
58 RSA * rsa,
59 int padding);
60
61 static int
62 sc_RSA_eay_private_encrypt(int flen,
63 unsigned char * from,
64 unsigned char * to,
65 RSA * rsa,
66 int padding);
67
68 /**********************************************************************
69 Define module specific variables
70 **********************************************************************/
71
72 static ERR_STRING_DATA scerr_str_functs[]=
73 {
74 {ERR_PACK(0,SCERR_F_RSA_ENCRYPT,0),"sc_RSA_private_encrypt"},
75 {ERR_PACK(0,SCERR_F_RSA_DECRYPT,0),"sc_RSA_private_decrypt"},
76 {ERR_PACK(0,SCERR_F_SCINIT,0),"sc_init"},
77 {ERR_PACK(0,SCERR_F_GET_RSA_PRIV_KEY_OBJ,0),"sc_get_rsa_priv_key_obj"},
78 {ERR_PACK(0,SCERR_F_GET_PRIV_KEY_OBJ,0),"sc_get_priv_key_obj"},
79 {ERR_PACK(0,SCERR_F_GET_PRIV_KEY_BY_LABEL,0),"sc_get_priv_key_by_label"},
80 {ERR_PACK(0,SCERR_F_GET_CERT_OBJ,0),"sc_get_cert_obj"},
81 {ERR_PACK(0,SCERR_F_FIND_ONE_OBJ,0),"sc_find_one_obj"},
82 {ERR_PACK(0,SCERR_F_FIND_CERT_BY_LABEL,0),"sc_find_cert_by_label"},
83 {ERR_PACK(0,SCERR_F_LOAD_DLL,0),"sc_get_function_list"},
84 {0,NULL},
85 };
86
87 static ERR_STRING_DATA scerr_str_reasons[]=
88 {
89 {SCERR_R_PKCS11_ERROR, "PKCS11 error"},
90 {SCERR_R_SIGNINIT, "C_SignInit"},
91 {SCERR_R_SIGN, "C_Sign"},
92 {SCERR_R_SIGNRECINIT, "C_SignRecoverInit"},
93 {SCERR_R_SIGNREC, "C_SignRecover"},
94 {SCERR_R_INITIALIZE, "C_Initialize"},
95 {SCERR_R_GETSLOTLIST, "C-GetSlotList"},
96 {SCERR_R_OPENSESSION, "C_OpenSession"},
97 {SCERR_R_LOGIN, "C_Login"},
98 {SCERR_R_CREATEOBJ, "C_CreateObject"},
99 {SCERR_R_UNSUPPORTED, "Unsupported feature"},
100 {SCERR_R_GETATTRVAL, "C_GetAttributeValue"},
101 {SCERR_R_FINDOBJINIT, "C_FindObjectInit"},
102 {SCERR_R_FINDOBJ, "C_FindObject"},
103 {SCERR_R_FOUNDMANY, "Found more then one matching key"},
104 {SCERR_R_FIND_FAILED, "Unable to find object on smart card"},
105 {SCERR_R_NO_PKCS11_DLL,"Unable to load the PKCS11 support"},
106 {0,NULL},
107 };
108
109 CK_FUNCTION_LIST_PTR pFunctionList = NULL;
110
111 #ifdef WIN32
112 HMODULE h_m_pkcs11 = NULL;
113 #else
114 void * h_m_pkcs11 = NULL;
115 #endif
116
117 /**********************************************************************
118 Function: sc_get_function_list()
119
120 Description:
121 Get the name of the PKCS11 dll to use from the registry,
122 load it, get the entry for the C_GetFunctionList
123 call it to set the pFunctionList.
124
125 Parameters:
126
127 Returns:
128 the pFunctionList or NULL
129 **********************************************************************/
130 CK_FUNCTION_LIST_PTR
131 sc_get_function_list()
132 {
133 CK_RV status;
134 #if defined(USE_PKCS11_DL) || defined(WIN32)
135 CK_RV (*gfl)(CK_FUNCTION_LIST_PTR_PTR);
136 #endif
137 if (pFunctionList) {
138 return pFunctionList;
139 }
140 #if defined(USE_PKCS11_DL) || defined(WIN32)
141
142 if (!h_m_pkcs11) {
143 char * dllname = NULL;
144 #ifdef WIN32
145 HKEY hkDir = NULL;
146 char val_dllname[512] = {"NONE"};
147 LONG lval;
148 DWORD type;
149
150 if (!h_m_pkcs11) {
151
152 RegOpenKey(HKEY_CURRENT_USER,GSI_REGISTRY_DIR,&hkDir);
153 lval = sizeof(val_dllname) -1;
154 if (hkDir && (RegQueryValueEx(hkDir,
155 "PKCS11.DLL",
156 0,
157 &type,
158 val_dllname,&lval) == ERROR_SUCCESS)) {
159 h_m_pkcs11 = LoadLibrary(val_dllname);
160 }
161
162 if (hkDir) {
163 RegCloseKey(hkDir);
164 }
165 if (!h_m_pkcs11) {
166 SCerr(SCERR_F_SCINIT,SCERR_R_NO_PKCS11_DLL);
167 ERR_add_error_data(2,"Name of DLL=",
168 dllname? dllname:"NONE");
169 return NULL;
170 }
171 }
172 gfl = (CK_RV (*)(CK_FUNCTION_LIST_PTR *))
173 GetProcAddress(h_m_pkcs11,"C_GetFunctionList");
174 #else
175 if (!h_m_pkcs11) {
176 dllname = getenv("PKCS11_LIB");
177 if (!dllname) {
178 dllname = "libDSPKCS.so";
179 }
180 h_m_pkcs11 = dlopen(dllname,RTLD_LAZY);
181 }
182 if (!h_m_pkcs11) {
183 SCerr(SCERR_F_SCINIT,SCERR_R_NO_PKCS11_DLL);
184 ERR_add_error_data(2,"Name of shared library=",
185 dllname);
186 return NULL;
187 }
188
189 gfl = (CK_RV(*)(CK_FUNCTION_LIST_PTR_PTR))
190 dlsym(h_m_pkcs11,"C_GetFunctionList");
191 #endif
192 if (!gfl) {
193 SCerr(SCERR_F_LOAD_DLL,SCERR_R_NO_PKCS11_DLL);
194 ERR_add_error_data(1,"Cant find C_GetFunctionList");
195 return NULL;
196 }
197 }
198 status = (*gfl)(&pFunctionList);
199 #else
200 status = C_GetFunctionList(&pFunctionList);
201 #endif /* PKCS11_DYNLOAD */
202
203 if (status != CKR_OK) {
204 SCerr(SCERR_F_LOAD_DLL,SCERR_R_UNSUPPORTED);
205 ERR_add_error_data(1,sc_ERR_code(status));
206 return NULL;
207 }
208 return pFunctionList;
209 }
210
211 /**********************************************************************
212 Function: ERR_load_scerr_strings()
213
214 Description:
215 Sets up the error tables used by SSL and adds ours
216 using the ERR_LIB_USER
217 Only the first call does anything.
218
219 Parameters:
220 i should be zero the first time any of the ERR_load_.*_string functions is called and
221 non-zero for the rest of the calls.
222 Returns:
223 **********************************************************************/
224
225 int
226 ERR_load_scerr_strings(
227 int i)
228 {
229 static int init=1;
230
231 if (init) {
232 init=0;
233
234 if (i == 0) {
235 SSL_load_error_strings();
236 }
237 ERR_load_strings(ERR_USER_LIB_SCERR_NUMBER,scerr_str_functs);
238 ERR_load_strings(ERR_USER_LIB_SCERR_NUMBER,scerr_str_reasons);
239 i++;
240 }
241 return i;
242 }
243
244 /********************************************************************/
245 /*******************************************************************/
246 /* Temporary function to reuten the error number. Should return char */
247
248 char *
249 sc_ERR_code(
250 CK_RV status)
251 {
252 static char buf[256];
253
254 sprintf(buf,"PKCS#11 return=0x%8.8lx",status);
255 return buf;
256 }
257
258 /********************************************************************/
259
260 int
261 sc_init(
262 CK_SESSION_HANDLE_PTR PsessionHandle,
263 char * card,
264 CK_SLOT_ID_PTR ppslot,
265 char * ppin,
266 CK_USER_TYPE userType,
267 int initialized)
268 {
269 int rc;
270 CK_SLOT_ID rslot;
271 CK_SLOT_ID_PTR pslot;
272
273 if (ppslot) {
274 pslot = ppslot;
275 }
276 else {
277 pslot = &rslot;
278 }
279
280 if (!initialized) {
281 rc = sc_init_one(pslot);
282 if (rc) {
283 return rc;
284 }
285 }
286
287 /*
288 rc = sc_init_info(pslot, &tokenInfo);
289 if (rc) {
290 return rc;
291 }
292 */
293
294 rc = sc_init_open_login(PsessionHandle, pslot, ppin, userType);
295 if (rc) {
296 return rc;
297 }
298 return 0;
299 }
300
301 /***********************************************************
302 Function: sc_init_one
303
304 Description:
305 get the function list pointer first.
306 initialize and find the slot with the card
307
308
309 ***********************************************************/
310 int
311 sc_init_one(
312 CK_SLOT_ID_PTR pslot)
313 {
314 CK_RV status;
315 CK_SLOT_ID list[20];
316 CK_SLOT_ID slot;
317 CK_SLOT_ID_PTR slotList = &list[0];
318 CK_ULONG count = 0;
319 CK_C_Initialize pC_Initialize;
320 CK_C_INITIALIZE_ARGS initArgs;
321 CK_C_INITIALIZE_ARGS_PTR args = NULL;
322 const char * nss_library_params = NULL;
323
324 if (!sc_get_function_list()) {
325 return SCERR_R_INITIALIZE;
326 }
327
328 nss_library_params = getenv("PKCS11_INIT_ARGS");
329 if (nss_library_params) {
330 /* hack to initialize the NSS soft token */
331 memset(&initArgs, 0, sizeof(initArgs));
332 initArgs.flags = CKF_OS_LOCKING_OK;
333 initArgs.pReserved = (void *) nss_library_params;
334 args = &initArgs;
335 }
336
337 pC_Initialize = pFunctionList->C_Initialize;
338 status = (*pC_Initialize)(args);
339
340 if (status != CKR_OK) {
341 SCerr(SCERR_F_SCINIT,SCERR_R_INITIALIZE);
342 ERR_add_error_data(1,sc_ERR_code(status));
343 return SCERR_R_INITIALIZE;
344 }
345 /*
346 status = (*(pFunctionList->C_GetSlotList))(FALSE, NULL, &count);
347 if (status != CKR_OK) {
348 SCerr(SCERR_F_SCINIT,SCERR_R_GETSLOTLIST);
349 ERR_add_error_data(1,sc_ERR_code(status));
350 return SCERR_R_GETSLOTLIST;
351 }
352 fprintf(stderr,"Slotlist count = %d\n",count);
353 */
354 count = 20;
355
356 status = (*(pFunctionList->C_GetSlotList))(FALSE, slotList, &count);
357 if (status != CKR_OK) {
358 SCerr(SCERR_F_SCINIT,SCERR_R_GETSLOTLIST);
359 ERR_add_error_data(1,sc_ERR_code(status));
360 return SCERR_R_GETSLOTLIST;
361 }
362
363 if (count == 0) {
364 SCerr(SCERR_F_SCINIT,SCERR_R_OPENSESSION);
365 ERR_add_error_data(1,"\n No SmartCard readers found");
366 return SCERR_R_OPENSESSION;
367 }
368
369 /*
370 * need to look at all the slots.
371 * Maybe provide the card label then look for it
372 */
373
374 // slot = list[0];
375 slot = list[1];
376 if (pslot) {
377 *pslot = slot;
378 }
379 return 0;
380 }
381
382
383 /***************************************************************
384 Function: sc_init_info
385
386 Description:
387 Read the card info and print debuging
388
389 **************************************************************/
390
391 int
392 sc_init_info(
393 CK_SLOT_ID_PTR pslot,
394 CK_TOKEN_INFO_PTR ptokenInfo)
395 {
396 CK_RV status;
397
398 status = (*(pFunctionList->C_GetTokenInfo))(*pslot, ptokenInfo);
399 if (status != CKR_OK) {
400 SCerr(SCERR_F_SCINIT,SCERR_R_LOGIN);
401 ERR_add_error_data(2, "While reading Smart Card Info",
402 sc_ERR_code(status));
403 return SCERR_R_LOGIN;
404 }
405
406 return 0;
407 }
408
409 /*****************************************************************
410 Function: sc_init_open_login
411
412 Description:
413 Open a session to the card, and login
414
415 *****************************************************************/
416
417 int
418 sc_init_open_login(
419 CK_SESSION_HANDLE_PTR PsessionHandle,
420 CK_SLOT_ID_PTR pslot,
421 char * ppin,
422 CK_USER_TYPE userType)
423 {
424 CK_RV status;
425 char * pin;
426 char rpin[256];
427 /* could also add CKF_EXCLUSIVE_SESSION */
428 int flags =
429 CKF_RW_SESSION | CKF_SERIAL_SESSION ;
430
431 status = (*(pFunctionList->C_OpenSession))(*pslot,
432 flags, 0, NULL, PsessionHandle);
433 if (status != CKR_OK) {
434 SCerr(SCERR_F_SCINIT,SCERR_R_OPENSESSION);
435 ERR_add_error_data(1,sc_ERR_code(status));
436 return SCERR_R_OPENSESSION;
437 }
438
439 if (ppin) /* did user provide the pin? */ {
440 pin = ppin;
441 }
442 else {
443 pin = rpin;
444 memset(rpin,0,sizeof(rpin));
445 #ifdef WIN32
446 read_passphrase_win32_prompt(
447 (userType == CKU_USER) ?
448 "Smart Card User PIN:" : "Smart Card SO PIN:",0);
449 read_passphrase_win32(rpin,sizeof(rpin),0);
450 #else
451 EVP_read_pw_string(rpin,sizeof(rpin),
452 (userType == CKU_USER) ?
453 "Smart Card User PIN:" : "Smart Card SO PIN:",0);
454 #endif
455 /*DEE should test this too */
456 }
457
458 status = (*(pFunctionList->C_Login))(*PsessionHandle, userType,
459 (CK_CHAR_PTR)pin, strlen(pin));
460 memset(rpin,0,sizeof(rpin));
461 if (status != CKR_OK) {
462 SCerr(SCERR_F_SCINIT,SCERR_R_LOGIN);
463 ERR_add_error_data(1,sc_ERR_code(status));
464 return SCERR_R_LOGIN;
465 }
466
467 return 0;
468 }
469
470
471 /*********************************************************************/
472 int
473 sc_final(
474 CK_SESSION_HANDLE sessionHandle)
475 {
476 CK_RV status;
477 status = (*(pFunctionList->C_Logout))(sessionHandle);
478 status = (*(pFunctionList->C_CloseSession))(sessionHandle);
479 return 0;
480 }
481
482
483
484
485 /*******************************************************************/
486 /* find and get data off the card */
487 /*******************************************************************/
488
489 int
490 sc_get_rsa_priv_key_obj(
491 CK_SESSION_HANDLE hSession,
492 CK_OBJECT_HANDLE hPrivKey,
493 RSA ** nrkey)
494 {
495 CK_RV sc_status;
496 CK_BYTE_PTR pModulus = NULL;
497 CK_BYTE_PTR pExponent = NULL;
498 CK_ATTRIBUTE template[] = {
499 {CKA_MODULUS, NULL_PTR, 0},
500 {CKA_PUBLIC_EXPONENT, NULL_PTR, 0}
501 };
502 RSA * rsa = NULL;
503 RSA_METHOD * ometh = NULL;
504 RSA_METHOD * nmeth = NULL;
505
506 rsa = RSA_new();
507 /*
508 * set to use our method for this key. i
509 * This will use the smart card for this key
510 * But to do this requires us to copy the RSA method, and
511 * replace two routines. This is done this way to avoid
512 * chanfges to the SSLeay, and since these routines are not
513 * exported in the Win32 DLL.
514 */
515
516 nmeth = (RSA_METHOD *)malloc(sizeof(RSA_METHOD));
517 if (!nmeth) {
518 return 1; /* DEE need to fix */
519 }
520 ometh = rsa->meth;
521 nmeth->name = ometh->name;
522 nmeth->rsa_pub_enc = ometh->rsa_pub_enc;
523 nmeth->rsa_pub_dec = ometh->rsa_pub_dec;
524 nmeth->rsa_priv_enc = sc_RSA_eay_private_encrypt;
525 nmeth->rsa_priv_dec = sc_RSA_eay_private_decrypt;
526 nmeth->rsa_mod_exp = ometh->rsa_mod_exp;
527 nmeth->bn_mod_exp = ometh->bn_mod_exp;
528 nmeth->init = ometh->init;
529 nmeth->finish = ometh->finish;
530 nmeth->flags = ometh->flags;
531 nmeth->app_data = ometh->app_data;
532
533 rsa->meth = nmeth;
534
535 RSA_set_ex_data(rsa,SC_RSA_EX_DATA_INDEX_SESSION,(char *) hSession);
536 RSA_set_ex_data(rsa,SC_RSA_EX_DATA_INDEX_OBJECT, (char *) hPrivKey);
537
538 sc_status = (*(pFunctionList->C_GetAttributeValue))
539 (hSession, hPrivKey, template, 2);
540
541 /*
542 * HACK for the LITRONIC cards, as the RSA PKCS11 says
543 * Section 9.7.1, the card must return the Modulus
544 */
545 if (sc_status == CKR_ATTRIBUTE_TYPE_INVALID) {
546 *nrkey = rsa;
547 return 0;
548 }
549
550 if (sc_status == CKR_OK) {
551 pModulus = (CK_BYTE_PTR) malloc(template[0].ulValueLen);
552 template[0].pValue = pModulus;
553 pExponent = (CK_BYTE_PTR) malloc(template[1].ulValueLen);
554 template[1].pValue = pExponent;
555
556 sc_status = (*(pFunctionList->C_GetAttributeValue))(hSession,
557 hPrivKey,
558 template,
559 1);
560 }
561
562 if (sc_status != CKR_OK) {
563 SCerr(SCERR_F_GET_RSA_PRIV_KEY_OBJ,SCERR_R_GETATTRVAL);
564 ERR_add_error_data(1,sc_ERR_code(sc_status));
565 free(pModulus);
566 free(pExponent);
567 return 1;
568 }
569
570 rsa->n = BN_bin2bn(pModulus,template[0].ulValueLen,NULL);
571 rsa->e = BN_bin2bn(pExponent,template[1].ulValueLen,NULL);
572
573 free(pModulus);
574 free(pExponent);
575
576 *nrkey = rsa;
577 return 0;
578 }
579 /*******************************************************************/
580 int
581 sc_get_priv_key_obj(
582 CK_SESSION_HANDLE hSession,
583 CK_OBJECT_HANDLE hPrivKey,
584 EVP_PKEY ** npkey)
585 {
586 int rc;
587 CK_RV sc_status;
588 CK_KEY_TYPE keyType = 0;
589 CK_ATTRIBUTE template[] = {
590 {CKA_KEY_TYPE, &keyType, sizeof(keyType)}
591 };
592 RSA * newrkey = NULL;
593 EVP_PKEY * upkey=NULL;
594
595 upkey = EVP_PKEY_new();
596
597 /* We should look at the attribute of the key found to
598 * deside if it is RSA or DSA, then call correct routine.
599 * For now only support RSA.
600 */
601
602 sc_status = (*(pFunctionList->C_GetAttributeValue))(hSession,
603 hPrivKey, template, 1);
604 if (sc_status != CKR_OK) {
605 SCerr(SCERR_F_GET_PRIV_KEY_OBJ,SCERR_R_GETATTRVAL);
606 ERR_add_error_data(1,sc_ERR_code(sc_status));
607 return 1;
608 }
609 switch (keyType) {
610 case (CKK_RSA):
611 rc = sc_get_rsa_priv_key_obj(hSession,
612 hPrivKey, &newrkey);
613 if (rc) {
614 return rc;
615 }
616 EVP_PKEY_assign(upkey, EVP_PKEY_RSA, (char *)newrkey);
617 break;
618
619 default:
620 SCerr(SCERR_F_GET_PRIV_KEY_OBJ,SCERR_R_UNSUPPORTED);
621 return 1;
622 }
623
624 *npkey = upkey;
625 return 0;
626
627 }
628 /*******************************************************************/
629 int
630 sc_get_priv_key_obj_by_label(
631 CK_SESSION_HANDLE hSession,
632 char * mylabel,
633 EVP_PKEY ** npkey)
634 {
635 int rc;
636 CK_OBJECT_HANDLE hKey;
637
638 rc = sc_find_priv_key_obj_by_label(hSession,mylabel,&hKey);
639 if (rc) {
640 return rc;
641 }
642 return sc_get_priv_key_obj(hSession, hKey, npkey);
643 }
644
645
646 /*******************************************************************/
647 int
648 sc_find_priv_key_obj_by_label(
649 CK_SESSION_HANDLE hSession,
650 char * mylabel,
651 CK_OBJECT_HANDLE_PTR phPrivKey)
652 {
653 CK_RV status;
654 CK_KEY_TYPE keyType = CKK_RSA;
655 CK_OBJECT_CLASS keyClass = CKO_PRIVATE_KEY;
656 CK_BBOOL true = TRUE;
657 CK_BBOOL false = FALSE;
658 CK_ATTRIBUTE template[20];
659 int ai;
660 int li = -1;
661 int rc;
662
663 ai = 0;
664 template[ai].type = CKA_CLASS;
665 template[ai].pValue = &keyClass;
666 template[ai].ulValueLen = sizeof(keyClass);
667 ai++;
668
669 template[ai].type = CKA_TOKEN;
670 template[ai].pValue = &true;
671 template[ai].ulValueLen = sizeof(true);
672 ai++;
673
674 if (strlen(mylabel)) {
675 template[ai].type = CKA_LABEL;
676 template[ai].pValue = mylabel;
677 template[ai].ulValueLen = strlen(mylabel) +
678 HACK_PKCS11_LOCAL_STRING_NULL;
679 li = ai;
680 ai++;
681 }
682
683 rc = sc_find_one_obj(hSession, template, ai, phPrivKey);
684 /*
685 * we may or may not have a null as part of the name,
686 * so we will try again this is a modified HACK
687 * If we added the NULL to the test, we wont this time.
688 * If we did not, we will this time.
689 */
690 if (rc && li >= 0) {
691 template[li].ulValueLen += 1 - 2 * HACK_PKCS11_LOCAL_STRING_NULL;
692 rc = sc_find_one_obj(hSession, template, ai, phPrivKey);
693 }
694
695 if (rc) {
696 SCerr(SCERR_F_GET_PRIV_KEY_BY_LABEL,SCERR_R_FIND_FAILED);
697 return 1;
698 }
699 return 0;
700 }
701
702 /*****************************************************************/
703 int
704 sc_find_one_obj(
705 CK_SESSION_HANDLE hSession,
706 CK_ATTRIBUTE_PTR template,
707 int ai,
708 CK_OBJECT_HANDLE_PTR phObject)
709 {
710 CK_RV status;
711 CK_ULONG ulObjectCount;
712
713 status = (*(pFunctionList->C_FindObjectsInit))(hSession,template,ai);
714 if (status != CKR_OK) {
715 SCerr(SCERR_F_FIND_ONE_OBJ,SCERR_R_FINDOBJINIT);
716 ERR_add_error_data(1,sc_ERR_code(status));
717 return 1;
718 }
719 ulObjectCount = 0;
720 status = (*(pFunctionList->C_FindObjects))(hSession,
721 phObject,
722 1,
723 &ulObjectCount);
724 (*(pFunctionList->C_FindObjectsFinal))(hSession);
725 if (status != CKR_OK) {
726 SCerr(SCERR_F_FIND_ONE_OBJ,SCERR_R_FINDOBJ);
727 ERR_add_error_data(1,sc_ERR_code(status));
728 return 1;
729 }
730
731 if (ulObjectCount != 1) {
732 SCerr(SCERR_F_FIND_ONE_OBJ,SCERR_R_FOUNDMANY);
733 return 1;
734 }
735
736 return 0;
737 }
738
739
740 /*******************************************************************/
741 /* find and get certificates off of card */
742 /*******************************************************************/
743 int
744 sc_get_cert_obj(
745 CK_SESSION_HANDLE hSession,
746 CK_OBJECT_HANDLE hCert,
747 X509 ** ncert)
748 {
749 CK_RV sc_status;
750 CK_BYTE_PTR pCert = NULL;
751 unsigned char * tasn1;
752 CK_ATTRIBUTE template[] = {
753 {CKA_VALUE, NULL_PTR, 0}
754 };
755 X509 * x509 = NULL;
756
757 sc_status = (*(pFunctionList->C_GetAttributeValue))(hSession,
758 hCert,
759 template,
760 1);
761
762 if (sc_status == CKR_OK) {
763 pCert = (CK_BYTE_PTR) malloc(template[0].ulValueLen);
764 template[0].pValue = pCert;
765 }
766
767 sc_status = (*(pFunctionList->C_GetAttributeValue))(hSession,
768 hCert,
769 template,
770 1);
771
772 if (sc_status != CKR_OK) {
773 SCerr(SCERR_F_GET_CERT_OBJ,SCERR_R_GETATTRVAL);
774 ERR_add_error_data(1,sc_ERR_code(sc_status));
775 free(pCert);
776 return 1;
777 }
778
779 tasn1 = pCert;
780 x509 = d2i_X509(NULL,&tasn1,template[0].ulValueLen);
781 if (x509 == NULL) {
782 SCerr(SCERR_F_GET_CERT_OBJ,SCERR_R_BAD_CERT_OBJ);
783 free(pCert);
784 return 1;
785 }
786
787 *ncert = x509;
788 free(pCert);
789 return 0;
790 }
791
792
793 /*******************************************************************/
794 int
795 sc_find_cert_obj_by_label(
796 CK_SESSION_HANDLE hSession,
797 char * mylabel,
798 CK_OBJECT_HANDLE_PTR phCert)
799 {
800 CK_RV status;
801 CK_CERTIFICATE_TYPE certType = CKC_X_509;
802 CK_OBJECT_CLASS certClass = CKO_CERTIFICATE;
803 CK_BBOOL true = TRUE;
804 CK_BBOOL false = FALSE;
805 CK_ULONG ulObjectCount;
806 CK_ATTRIBUTE template[20];
807 int ai;
808 int li = -1;
809 int rc;
810
811 ai = 0;
812 template[ai].type = CKA_CLASS;
813 template[ai].pValue = &certClass;
814 template[ai].ulValueLen = sizeof(certClass);
815 ai++;
816
817 template[ai].type = CKA_CERTIFICATE_TYPE;
818 template[ai].pValue = &certType;
819 template[ai].ulValueLen = sizeof(certType);
820 ai++;
821
822 template[ai].type = CKA_TOKEN;
823 template[ai].pValue = &true;
824 template[ai].ulValueLen = sizeof(true);
825 ai++;
826
827 if (strlen(mylabel)) {
828 template[ai].type = CKA_LABEL;
829 template[ai].pValue = mylabel;
830 template[ai].ulValueLen = strlen(mylabel) + HACK_PKCS11_LOCAL_STRING_NULL;
831 li = ai;
832 ai++;
833 }
834
835 rc = sc_find_one_obj(hSession, template, ai, phCert);
836
837 /*
838 * we may or may not have a null as part of the name,
839 * so we will try again this is a modified HACK
840 * If we added the NULL to the test, we wont this time.
841 * If we did not, we will this time.
842 */
843 if (rc && li >= 0) {
844 template[li].ulValueLen += 1 - 2 * HACK_PKCS11_LOCAL_STRING_NULL;
845 rc = sc_find_one_obj(hSession, template, ai, phCert);
846 }
847
848 if (rc) {
849 SCerr(SCERR_F_FIND_CERT_BY_LABEL,SCERR_R_FIND_FAILED);
850 return 1;
851 }
852 return 0;
853 }
854
855 /*******************************************************************/
856 int
857 sc_get_cert_obj_by_label(
858 CK_SESSION_HANDLE hSession,
859 char * mylabel,
860 X509 ** ncert)
861 {
862 int rc;
863 CK_OBJECT_HANDLE hCert;
864
865 rc = sc_find_cert_obj_by_label(hSession,mylabel,&hCert);
866 if (rc) {
867 return rc;
868 }
869 return sc_get_cert_obj(hSession, hCert, ncert);
870 }
871
872 /****************************************************************/
873
874 static int
875 sc_RSA_eay_private_encrypt(
876 int flen,
877 unsigned char * from,
878 unsigned char * to,
879 RSA * rsa,
880 int padding)
881 {
882 CK_ULONG ulsiglen;
883 CK_MECHANISM_PTR pMech = NULL;
884 CK_MECHANISM m_rsa_pkcs = {CKM_RSA_PKCS, 0,0};
885 CK_MECHANISM m_rsa_raw = {CKM_RSA_X_509, 0,0};
886 CK_RV ck_status;
887 CK_SESSION_HANDLE hSession;
888 CK_OBJECT_HANDLE hObject;
889
890 hSession = (CK_SESSION_HANDLE )RSA_get_ex_data(
891 rsa,
892 SC_RSA_EX_DATA_INDEX_SESSION);
893
894 hObject = (CK_OBJECT_HANDLE) RSA_get_ex_data(
895 rsa,
896 SC_RSA_EX_DATA_INDEX_OBJECT);
897
898 switch (padding) {
899 case RSA_PKCS1_PADDING:
900 pMech = &m_rsa_pkcs;
901 break;
902 case RSA_NO_PADDING:
903 pMech = &m_rsa_raw;
904 break;
905 case RSA_SSLV23_PADDING:
906 default:
907 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
908 }
909
910 if (pMech == NULL) {
911 return 0;
912 }
913
914 ck_status = (*(pFunctionList->C_SignInit))(hSession, pMech, hObject);
915 if (ck_status != CKR_OK) {
916 SCerr(SCERR_F_RSA_ENCRYPT,SCERR_R_SIGNINIT);
917 ERR_add_error_data(1,sc_ERR_code(ck_status));
918 return 0;
919 }
920
921 ck_status = (*(pFunctionList->C_Sign))(hSession,
922 from, flen, to, &ulsiglen);
923 if (ck_status != CKR_OK) {
924 SCerr(SCERR_F_RSA_ENCRYPT,SCERR_R_SIGN);
925 ERR_add_error_data(1,sc_ERR_code(ck_status));
926 return 0;
927 }
928
929 return ulsiglen;
930 }
931
932 /***************************************************************/
933
934 static int
935 sc_RSA_eay_private_decrypt(
936 int flen,
937 unsigned char * from,
938 unsigned char * to,
939 RSA * rsa,
940 int padding)
941 {
942 CK_ULONG ulsiglen;
943 CK_MECHANISM_PTR pMech = NULL;
944 CK_MECHANISM m_rsa_pkcs = {CKM_RSA_PKCS, 0,0};
945 CK_MECHANISM m_rsa_raw = {CKM_RSA_X_509, 0,0};
946 CK_RV ck_status;
947 CK_SESSION_HANDLE hSession;
948 CK_OBJECT_HANDLE hObject;
949
950 hSession = (CK_SESSION_HANDLE )RSA_get_ex_data(
951 rsa,
952 SC_RSA_EX_DATA_INDEX_SESSION);
953
954 hObject = (CK_OBJECT_HANDLE) RSA_get_ex_data(
955 rsa,
956 SC_RSA_EX_DATA_INDEX_OBJECT);
957
958 switch (padding) {
959 case RSA_PKCS1_PADDING:
960 pMech = &m_rsa_pkcs;
961 break;
962 case RSA_NO_PADDING:
963 pMech = &m_rsa_raw;
964 break;
965 case RSA_SSLV23_PADDING:
966 default:
967 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
968 }
969
970 if (pMech == NULL) {
971 return 0;
972 }
973
974 ulsiglen = BN_num_bytes(rsa->n);
975
976 ck_status = (*(pFunctionList->C_SignRecoverInit))(hSession,
977 pMech, hObject);
978 if (ck_status != CKR_OK) {
979 SCerr(SCERR_F_RSA_DECRYPT,SCERR_R_SIGNRECINIT);
980 ERR_add_error_data(1,sc_ERR_code(ck_status));
981 return 0;
982 }
983
984 ck_status = (*(pFunctionList->C_SignRecover))(hSession,
985 from,
986 flen,
987 to,
988 &ulsiglen);
989 if (ck_status != CKR_OK) {
990 SCerr(SCERR_F_RSA_DECRYPT,SCERR_R_SIGNREC);
991 ERR_add_error_data(1,sc_ERR_code(ck_status));
992 return 0;
993 }
994 return ulsiglen;
995 }
996 #endif /*USE_PKCS11*/
997
998