Changeset 3557 for trunk/src

Show
Ignore:
Timestamp:
08/20/08 17:17:59 (3 months ago)
Author:
aj
Message:

Douglas E. Engert:
The pkcs15-gemsafeV1.c code assumes that the key_ref is always 3. But that is
not always the case. In our case it is 4. The patch tries to determine the
key_ref by looking at what appears to be a table of allocated keys, and picking
the first allocated key.

In case this is not always true, the patch will also allow for the the
opensc.conf card flag = n to specify the key_ref as the low order 4 bits of the
flag.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/libopensc/pkcs15-gemsafeV1.c

    r3502 r3557  
    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; 
     
    145147            sc_error(card->ctx, "%s: Invalid object size: %d\n", fn_name, objlen); 
    146148            return 0; 
     149        } 
     150 
     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; 
    147169        } 
    148170 
     
    194216 
    195217    int    r, i; 
     218        int        key_ref = 0x03;  
    196219    struct sc_path path; 
    197220    struct sc_file *file = NULL; 
     
    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; 
     
    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    }