Ticket #179: gemsafeV1.key_ref.patch

File gemsafeV1.key_ref.patch, 2.6 KB (added by dengert, 4 years ago)

patch to pkcs15-gemsafeV1.c

  • pkcs15-gemsafeV1.c

     
    114114        { NULL, NULL, 0, 0, NULL, 0, NULL, 0} 
    115115}; 
    116116 
    117 static int gemsafe_get_cert_len(sc_card_t *card, sc_path_t *path) 
     117static int gemsafe_get_cert_len(sc_card_t *card, sc_path_t *path,  
     118        int *key_ref) 
    118119{ 
    119120        const char *fn_name = "gemsafe_get_cert_len"; 
    120121        int r; 
     122        int ind; 
    121123        u8  ibuf[248]; 
    122124        struct sc_file *file; 
    123125        size_t objlen, certlen; 
     
    146148            return 0; 
    147149        } 
    148150 
     151        /* 
     152         * We need to find the private key associated with the cert 
     153         * It looks like the first thing in the block is a table of 
     154         * which keys are allocated.  
     155         * We will look for the first allocated key, and save the  
     156         * key_ref. The table is small and is in the first 248 bytes. 
     157         * If for some reason this is not true, we can still override 
     158         * the key_ref in the opensc.conf with flag = n. 
     159         */ 
     160        ind = 2; /* skip length */ 
     161        while (ibuf[ind] == 0x01) { 
     162                if (ibuf[ind+1] == 0xFE) { 
     163                        *key_ref = ibuf[ind+4]; 
     164                        sc_debug(card->ctx, "Using key_ref %d found at offset %d\n", 
     165                                        *key_ref, ind); 
     166                        break; 
     167                } 
     168                ind = ind + 8; 
     169        } 
     170 
    149171        /* Using (block+1) in while loop avoids using final cert object data block */ 
    150172        while (!found && ( (block+1) * 248 < objlen) ) { 
    151173            /* Check current buffer */ 
     
    193215    const char *fn_name = "sc_pkcs15emu_gemsafe_init"; 
    194216 
    195217    int    r, i; 
     218        int        key_ref = 0x03;  
    196219    struct sc_path path; 
    197220    struct sc_file *file = NULL; 
    198221    struct sc_card *card = p15card->card; 
     
    252275            struct sc_pkcs15_id  p15Id; 
    253276 
    254277            sc_format_path(gemsafe_cert[i].path, &path); 
    255             if (!gemsafe_get_cert_len(card, &path)) 
     278            if (!gemsafe_get_cert_len(card, &path, &key_ref)) 
    256279                    /* skip errors */ 
    257280                    continue; 
    258281            sc_pkcs15_format_id(gemsafe_cert[i].id, &p15Id); 
     
    284307                    pauthId = &authId; 
    285308            } else 
    286309                    pauthId = NULL; 
     310                        /* 
     311                         * the key ref may be different for different sites  
     312                         * by adding flags=n where the low order 4 bits can be 
     313                         * the key ref we can force it.  
     314                         */ 
     315                        if ( p15card->card->flags & 0x0F) { 
     316                                key_ref = p15card->card->flags & 0x0F; 
     317                                sc_debug(p15card->card->ctx, 
     318                                        "Overriding key_ref  with %d\n", key_ref); 
     319                        }  
    287320            sc_pkcs15emu_add_prkey(p15card, &p15Id, gemsafe_prkeys[i].label, 
    288321                            SC_PKCS15_TYPE_PRKEY_RSA, 
    289322                            gemsafe_prkeys[i].modulus_len, gemsafe_prkeys[i].usage, 
    290                             &path, gemsafe_prkeys[i].ref, pauthId, 
     323                            &path, key_ref, pauthId, 
    291324                            gemsafe_prkeys[i].obj_flags); 
    292325    } 
    293326