Changeset aae5b93 in OpenSC


Ignore:
Timestamp:
12/21/11 09:26:23 (5 months ago)
Author:
Xiaoshuo
Children:
f0bde7d
Parents:
4251a23
git-author:
Xiaoshuo <xiaoshuo@…> (12/21/11 09:26:23)
git-committer:
Xiaoshuo <xiaoshuo@…> (12/21/11 09:26:23)
Message:

Implement SM by adding sm_wrap_apdu/sm_unwrap_apdu hooks.

Location:
src/libopensc
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/libopensc/apdu.c

    ra28bacf raae5b93  
    516516} 
    517517 
    518 int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu) 
     518static int _sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu) 
    519519{ 
    520520        int r = SC_SUCCESS; 
     
    606606                sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "sc_unlock failed"); 
    607607 
     608        return r; 
     609} 
     610 
     611/* 
     612 * Wrap/Unwrap APDU when requested before sending to reader driver 
     613 * Needed (un)wrapping apdu is noticed by a non-null 
     614 * card->ops->(un)wrap_apdu() function pointer 
     615 * (eg: for Secure Messaging) 
     616 *  @param  card  sc_card_t object for the smartcard 
     617 *  @param  apdu  APDU to be sent 
     618 *  @return SC_SUCCESS on success and an error value otherwise 
     619 */ 
     620int sc_transmit_apdu(sc_card_t *card, sc_apdu_t *apdu) { 
     621        int r; 
     622        sc_context_t *ctx = card->ctx; 
     623 
     624        if (ctx->use_sm != 0) { 
     625                //construct new SM apdu from original apdu 
     626                u8 data[SC_MAX_EXT_APDU_BUFFER_SIZE] = {0}; 
     627                u8 resp[SC_MAX_EXT_APDU_BUFFER_SIZE] = {0}; 
     628                sc_apdu_t wrapped_apdu; 
     629                wrapped_apdu.data = &data[0]; 
     630                wrapped_apdu.datalen = SC_MAX_EXT_APDU_BUFFER_SIZE; 
     631                wrapped_apdu.resp = &resp[0]; 
     632                wrapped_apdu.resplen = SC_MAX_EXT_APDU_BUFFER_SIZE; 
     633 
     634                /* wrap apdu */ 
     635                r = card->ops->sm_wrap_apdu(card, apdu, &wrapped_apdu); 
     636                SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, r, "sm_wrap_apdu error"); 
     637 
     638                /* determine the APDU type if necessary, i.e. to use 
     639                 * short or extended APDUs  */ 
     640                sc_detect_apdu_cse(card, apdu); 
     641 
     642                /* check wrapped apdu */ 
     643                r = sc_check_apdu(card, &wrapped_apdu); 
     644                SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, r, "wrapped APDU is invalid"); 
     645 
     646                r = _sc_transmit_apdu(card, &wrapped_apdu); 
     647                SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, r, "unable to transmit wrapped APDU"); 
     648 
     649                /* unwrap apdu */ 
     650                r = card->ops->sm_unwrap_apdu(card, &wrapped_apdu, apdu); 
     651                SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, r, "sm_unwrap_apdu error"); 
     652 
     653        } else { 
     654                /* no wrap needed, just call reader driver */ 
     655                r = _sc_transmit_apdu(card, apdu); 
     656        } 
    608657        return r; 
    609658} 
  • src/libopensc/card-entersafe.c

    r3efe35d raae5b93  
    131131 
    132132        card->caps = SC_CARD_CAP_RNG;  
     133        card->ctx->use_sm = 0; 
    133134 
    134135        /* we need read_binary&friends with max 224 bytes per read */ 
  • src/libopensc/card-epass2003.c

    r4251a23 raae5b93  
    857857        card->cla = 0x00; 
    858858        card->drv_data = NULL; 
    859  
    860         g_sm = SM_PLAIN; 
     859        card->ctx->use_sm = 1; 
     860 
     861        g_sm = SM_SCP01; 
     862        /* g_sm = SM_PLAIN; */ 
    861863 
    862864        /* decide FIPS/Non-FIPS mode */ 
     
    22172219        epass2003_ops.match_card = epass2003_match_card; 
    22182220        epass2003_ops.init = epass2003_init; 
     2221        epass2003_ops.sm_wrap_apdu = epass2003_sm_wrap_apdu; 
     2222        epass2003_ops.sm_unwrap_apdu = epass2003_sm_unwrap_apdu; 
    22192223        epass2003_ops.write_binary = NULL; 
    22202224        epass2003_ops.write_record = NULL; 
  • src/libopensc/iso7816.c

    r93baf13 raae5b93  
    10121012        NULL,                   /* init   */ 
    10131013        NULL,                   /* finish */ 
     1014        NULL,                   /* sm_wrap_apdu */ 
     1015        NULL,                   /* sm_unwrap_apdu */ 
    10141016        iso7816_read_binary, 
    10151017        iso7816_write_binary, 
  • src/libopensc/opensc.h

    ra28bacf raae5b93  
    464464         * the card cannot be handled with this driver. */ 
    465465        int (*init)(struct sc_card *card); 
     466        /* Called before invoke card_driver->ops->transmit. 
     467         * Performing APDU wrap. If set to NULL, there is no wrapping. 
     468         * Usefull on Secure Messaging APDU encode 
     469         * Returns SC_SUCCESS or error code */ 
     470    int (*sm_wrap_apdu)(struct sc_card *card, 
     471                         struct sc_apdu *plain, struct sc_apdu *sm); 
     472        /* Same as wrap_apdu, but perform unwrap */ 
     473    int (*sm_unwrap_apdu)(struct sc_card *card, 
     474                         struct sc_apdu *sm, struct sc_apdu *plain); 
    466475        /* Called when the card object is being freed.  finish() has to 
    467476         * deallocate all possible private data. */ 
     
    619628        void *mutex; 
    620629 
     630        int use_sm; 
    621631        unsigned int magic; 
    622632} sc_context_t; 
Note: See TracChangeset for help on using the changeset viewer.