Check-in [da2b17a36c]
Overview
Comment:Put all calls to functions that talk to the smartcard inside the big global mutex
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:da2b17a36ce4f88eafbc1941d6dd49972336a915
User & Date: rkeene on 2010-05-14 02:34:57
Other Links: manifest | tags
Context
2010-05-14
03:28
Updated transactional support to recursive transactions check-in: 43170e1dd9 user: rkeene tags: trunk
02:34
Put all calls to functions that talk to the smartcard inside the big global mutex check-in: da2b17a36c user: rkeene tags: trunk
02:16
Added transaction support

Updated reading certs to be transactional

Added function to connect to card check-in: fab9cf1772 user: rkeene tags: trunk

Changes

Modified cackey.c from [4f1f703fd4] to [38178200a5].

  2458   2458   	return(CKR_OK);
  2459   2459   
  2460   2460   	tokenPresent = tokenPresent; /* Supress unused variable warning */
  2461   2461   }
  2462   2462   
  2463   2463   CK_DEFINE_FUNCTION(CK_RV, C_GetSlotInfo)(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) {
  2464   2464   	static CK_UTF8CHAR slotDescription[] = "CACKey Slot";
         2465  +	int mutex_retval;
  2465   2466   	int bytes_to_copy;
  2466   2467   
  2467   2468   	CACKEY_DEBUG_PRINTF("Called.");
  2468   2469   
  2469   2470   	if (pInfo == NULL) {
  2470   2471   		CACKEY_DEBUG_PRINTF("Error. pInfo is NULL.");
  2471   2472   
  2472   2473   		return(CKR_ARGUMENTS_BAD);
  2473   2474   	}
         2475  +
         2476  +	if (!cackey_initialized) {
         2477  +		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
         2478  +
         2479  +		return(CKR_CRYPTOKI_NOT_INITIALIZED);
         2480  +	}
  2474   2481   
  2475   2482   	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
  2476   2483   		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
  2477   2484   
  2478   2485   		return(CKR_SLOT_ID_INVALID);
  2479   2486   	}
         2487  +
         2488  +	mutex_retval = cackey_mutex_lock(cackey_biglock);
         2489  +	if (mutex_retval != 0) {
         2490  +		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
         2491  +
         2492  +		return(CKR_GENERAL_ERROR);
         2493  +	}
  2480   2494   
  2481   2495   	if (cackey_slots[slotID].active == 0) {
  2482   2496   		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
         2497  +
         2498  +		cackey_mutex_unlock(cackey_biglock);
  2483   2499   
  2484   2500   		return(CKR_SLOT_ID_INVALID);
  2485   2501   	}
  2486   2502   
  2487         -	if (!cackey_initialized) {
  2488         -		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
         2503  +	mutex_retval = cackey_mutex_unlock(cackey_biglock);
         2504  +	if (mutex_retval != 0) {
         2505  +		CACKEY_DEBUG_PRINTF("Error.  Unlocking failed.");
  2489   2506   
  2490         -		return(CKR_CRYPTOKI_NOT_INITIALIZED);
         2507  +		return(CKR_GENERAL_ERROR);
  2491   2508   	}
  2492   2509   
  2493   2510   	memset(pInfo->slotDescription, ' ', sizeof(pInfo->slotDescription));
  2494   2511   	memcpy(pInfo->slotDescription, slotDescription, sizeof(slotDescription) - 1);
  2495   2512   
  2496   2513   	memset(pInfo->manufacturerID, ' ', sizeof(pInfo->manufacturerID));
  2497   2514   
................................................................................
  2518   2535   	return(CKR_OK);
  2519   2536   }
  2520   2537   
  2521   2538   CK_DEFINE_FUNCTION(CK_RV, C_GetTokenInfo)(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) {
  2522   2539   	static CK_UTF8CHAR manufacturerID[] = "U.S. Government";
  2523   2540   	static CK_UTF8CHAR defaultLabel[] = "Unknown Token";
  2524   2541   	static CK_UTF8CHAR model[] = "CAC Token";
         2542  +	int mutex_retval;
  2525   2543   
  2526   2544   	CACKEY_DEBUG_PRINTF("Called.");
  2527   2545   
  2528   2546   	if (pInfo == NULL) {
  2529   2547   		CACKEY_DEBUG_PRINTF("Error. pInfo is NULL.");
  2530   2548   
  2531   2549   		return(CKR_ARGUMENTS_BAD);
  2532   2550   	}
         2551  +
         2552  +	if (!cackey_initialized) {
         2553  +		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
         2554  +
         2555  +		return(CKR_CRYPTOKI_NOT_INITIALIZED);
         2556  +	}
  2533   2557   
  2534   2558   	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
  2535   2559   		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
  2536   2560   
  2537   2561   		return(CKR_SLOT_ID_INVALID);
  2538   2562   	}
         2563  +
         2564  +	mutex_retval = cackey_mutex_lock(cackey_biglock);
         2565  +	if (mutex_retval != 0) {
         2566  +		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
         2567  +
         2568  +		return(CKR_GENERAL_ERROR);
         2569  +	}
  2539   2570   
  2540   2571   	if (cackey_slots[slotID].active == 0) {
  2541   2572   		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
  2542   2573   
         2574  +		cackey_mutex_unlock(cackey_biglock);
         2575  +
  2543   2576   		return(CKR_SLOT_ID_INVALID);
  2544   2577   	}
  2545   2578   
  2546         -	if (!cackey_initialized) {
  2547         -		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
  2548         -
  2549         -		return(CKR_CRYPTOKI_NOT_INITIALIZED);
  2550         -	}
  2551         -
  2552   2579   	if (cackey_token_present(&cackey_slots[slotID]) != CACKEY_PCSC_S_TOKENPRESENT) {
  2553   2580   		CACKEY_DEBUG_PRINTF("No token is present in slotID = %lu", slotID);
  2554   2581   
         2582  +		cackey_mutex_unlock(cackey_biglock);
         2583  +
  2555   2584   		return(CKR_TOKEN_NOT_PRESENT);
  2556   2585   	}
         2586  +
         2587  +	mutex_retval = cackey_mutex_unlock(cackey_biglock);
         2588  +	if (mutex_retval != 0) {
         2589  +		CACKEY_DEBUG_PRINTF("Error.  Unlocking failed.");
         2590  +
         2591  +		return(CKR_GENERAL_ERROR);
         2592  +	}
  2557   2593   
  2558   2594   	memset(pInfo->label, ' ', sizeof(pInfo->label));
  2559   2595   	if (1) {
  2560   2596   		memcpy(pInfo->label, defaultLabel, sizeof(defaultLabel) - 1);
  2561   2597   	} else {
  2562   2598   	}
  2563   2599   
................................................................................
  2650   2686   
  2651   2687   	CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);
  2652   2688   
  2653   2689   	return(CKR_OK);
  2654   2690   }
  2655   2691   
  2656   2692   CK_DEFINE_FUNCTION(CK_RV, C_GetMechanismInfo)(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_MECHANISM_INFO_PTR pInfo) {
         2693  +	int mutex_retval;
         2694  +
  2657   2695   	CACKEY_DEBUG_PRINTF("Called.");
  2658   2696   
  2659         -	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
  2660         -		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
  2661         -
  2662         -		return(CKR_SLOT_ID_INVALID);
  2663         -	}
  2664         -
  2665         -	if (cackey_slots[slotID].active == 0) {
  2666         -		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
  2667         -
  2668         -		return(CKR_SLOT_ID_INVALID);
  2669         -	}
  2670         -
  2671   2697   	if (pInfo == NULL) {
  2672   2698   		CACKEY_DEBUG_PRINTF("Error. pInfo is NULL.");
  2673   2699   
  2674   2700   		return(CKR_ARGUMENTS_BAD);
  2675   2701   	}
  2676   2702   
  2677   2703   	if (!cackey_initialized) {
  2678   2704   		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
  2679   2705   
  2680   2706   		return(CKR_CRYPTOKI_NOT_INITIALIZED);
  2681   2707   	}
         2708  +
         2709  +	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
         2710  +		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
         2711  +
         2712  +		return(CKR_SLOT_ID_INVALID);
         2713  +	}
         2714  +
         2715  +	mutex_retval = cackey_mutex_lock(cackey_biglock);
         2716  +	if (mutex_retval != 0) {
         2717  +		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
         2718  +
         2719  +		return(CKR_GENERAL_ERROR);
         2720  +	}
         2721  +
         2722  +	if (cackey_slots[slotID].active == 0) {
         2723  +		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
         2724  +
         2725  +		cackey_mutex_unlock(cackey_biglock);
         2726  +
         2727  +		return(CKR_SLOT_ID_INVALID);
         2728  +	}
         2729  +
         2730  +	mutex_retval = cackey_mutex_unlock(cackey_biglock);
         2731  +	if (mutex_retval != 0) {
         2732  +		CACKEY_DEBUG_PRINTF("Error.  Unlocking failed.");
         2733  +
         2734  +		return(CKR_GENERAL_ERROR);
         2735  +	}
  2682   2736   
  2683   2737   	/* XXX: This is untested, and further I'm not really sure if this is correct. */
  2684   2738   	switch (type) {
  2685   2739   		case CKM_RSA_PKCS:
  2686   2740   			pInfo->ulMinKeySize = 512;
  2687   2741   			pInfo->ulMaxKeySize = 8192;
  2688   2742   			pInfo->flags = CKF_HW | CKF_ENCRYPT | CKF_DECRYPT | CKF_SIGN | CKF_VERIFY;
................................................................................
  2752   2806   CK_DEFINE_FUNCTION(CK_RV, C_OpenSession)(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_SESSION_HANDLE_PTR phSession) {
  2753   2807   	unsigned long idx;
  2754   2808   	int mutex_retval;
  2755   2809   	int found_session = 0;
  2756   2810   
  2757   2811   	CACKEY_DEBUG_PRINTF("Called.");
  2758   2812   
  2759         -	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
  2760         -		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
  2761         -
  2762         -		return(CKR_SLOT_ID_INVALID);
  2763         -	}
  2764         -
  2765         -	if (cackey_slots[slotID].active == 0) {
  2766         -		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
  2767         -
  2768         -		return(CKR_SLOT_ID_INVALID);
  2769         -	}
  2770         -
  2771   2813   	if ((flags & CKF_SERIAL_SESSION) != CKF_SERIAL_SESSION) {
  2772   2814   		return(CKR_SESSION_PARALLEL_NOT_SUPPORTED);
  2773   2815   	}
  2774   2816   
  2775   2817   	if (!cackey_initialized) {
  2776   2818   		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
  2777   2819   
  2778   2820   		return(CKR_CRYPTOKI_NOT_INITIALIZED);
  2779   2821   	}
         2822  +
         2823  +	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
         2824  +		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
         2825  +
         2826  +		return(CKR_SLOT_ID_INVALID);
         2827  +	}
         2828  +
         2829  +	mutex_retval = cackey_mutex_lock(cackey_biglock);
         2830  +	if (mutex_retval != 0) {
         2831  +		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
         2832  +
         2833  +		return(CKR_GENERAL_ERROR);
         2834  +	}
         2835  +
         2836  +	if (cackey_slots[slotID].active == 0) {
         2837  +		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
         2838  +
         2839  +		cackey_mutex_unlock(cackey_biglock);
         2840  +
         2841  +		return(CKR_SLOT_ID_INVALID);
         2842  +	}
  2780   2843   
  2781   2844   	/* Verify that the card is actually in the slot. */
  2782   2845   	if (cackey_token_present(&cackey_slots[slotID]) != CACKEY_PCSC_S_TOKENPRESENT) {
  2783   2846   		CACKEY_DEBUG_PRINTF("Error.  Card not present.  Returning CKR_DEVICE_REMOVED");
  2784   2847   
         2848  +		cackey_mutex_unlock(cackey_biglock);
         2849  +
  2785   2850   		return(CKR_DEVICE_REMOVED);
  2786   2851   	}
  2787   2852   
  2788         -	mutex_retval = cackey_mutex_lock(cackey_biglock);
  2789         -	if (mutex_retval != 0) {
  2790         -		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
  2791         -
  2792         -		return(CKR_GENERAL_ERROR);
  2793         -	}
  2794         -
  2795   2853   	for (idx = 1; idx < (sizeof(cackey_sessions) / sizeof(cackey_sessions[0])); idx++) {
  2796   2854   		if (!cackey_sessions[idx].active) {
  2797   2855   			found_session = 1;
  2798   2856   
  2799   2857   			*phSession = idx;
  2800   2858   
  2801   2859   			cackey_sessions[idx].active = 1;
................................................................................
  2903   2961   }
  2904   2962   
  2905   2963   CK_DEFINE_FUNCTION(CK_RV, C_CloseAllSessions)(CK_SLOT_ID slotID) {
  2906   2964   	uint32_t idx;
  2907   2965   	int mutex_retval;
  2908   2966   
  2909   2967   	CACKEY_DEBUG_PRINTF("Called.");
         2968  +
         2969  +	if (!cackey_initialized) {
         2970  +		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
         2971  +
         2972  +		return(CKR_CRYPTOKI_NOT_INITIALIZED);
         2973  +	}
  2910   2974   
  2911   2975   	if (slotID < 0 || slotID >= (sizeof(cackey_slots) / sizeof(cackey_slots[0]))) {
  2912   2976   		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), outside of valid range", slotID);
  2913   2977   
  2914   2978   		return(CKR_SLOT_ID_INVALID);
  2915   2979   	}
         2980  +
         2981  +	mutex_retval = cackey_mutex_lock(cackey_biglock);
         2982  +	if (mutex_retval != 0) {
         2983  +		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
         2984  +
         2985  +		return(CKR_GENERAL_ERROR);
         2986  +	}
  2916   2987   
  2917   2988   	if (cackey_slots[slotID].active == 0) {
  2918   2989   		CACKEY_DEBUG_PRINTF("Error. Invalid slot requested (%lu), slot not currently active", slotID);
  2919   2990   
         2991  +		cackey_mutex_unlock(cackey_biglock);
         2992  +
  2920   2993   		return(CKR_SLOT_ID_INVALID);
  2921   2994   	}
  2922   2995   
  2923         -	if (!cackey_initialized) {
  2924         -		CACKEY_DEBUG_PRINTF("Error.  Not initialized.");
  2925         -
  2926         -		return(CKR_CRYPTOKI_NOT_INITIALIZED);
  2927         -	}
  2928         -
  2929         -	mutex_retval = cackey_mutex_lock(cackey_biglock);
  2930         -	if (mutex_retval != 0) {
  2931         -		CACKEY_DEBUG_PRINTF("Error.  Locking failed.");
  2932         -
  2933         -		return(CKR_GENERAL_ERROR);
  2934         -	}
  2935         -
  2936   2996   	for (idx = 0; idx < (sizeof(cackey_sessions) / sizeof(cackey_sessions[0])); idx++) {
  2937   2997   		if (cackey_sessions[idx].active) {
  2938   2998   			if (cackey_sessions[idx].slotID != slotID) {
  2939   2999   				continue;
  2940   3000   			}
  2941   3001   
  2942   3002   			cackey_mutex_unlock(cackey_biglock);