Ticket #118: algorithm.diff

File algorithm.diff, 4.2 KB (added by aj, 2 years ago)

patch implementing this in pkcs15-tool. maybe opensc-tool would be better?

  • src/tools/pkcs15-tool.c

     
    6767        OPT_PIN, 
    6868        OPT_NEWPIN, 
    6969        OPT_PUK, 
     70        OPT_LIST_ALG, 
    7071}; 
    7172 
    7273#define NELEMENTS(x)    (sizeof(x)/sizeof((x)[0])) 
     
    100101        { "auth-id",            required_argument, 0,   'a' }, 
    101102        { "wait",               no_argument, 0,         'w' }, 
    102103        { "verbose",            no_argument, 0,         'v' }, 
     104        { "list-algorithms",    no_argument, 0,         OPT_LIST_ALG }, 
    103105        { 0, 0, 0, 0 } 
    104106}; 
    105107 
     
    126128        "The auth ID of the PIN to use", 
    127129        "Wait for card insertion", 
    128130        "Verbose operation. Use several times to enable debug output.", 
     131        "Lists Algorithms supported by card", 
    129132}; 
    130133 
    131134sc_context_t *ctx = NULL; 
     
    917920        return 0; 
    918921} 
    919922 
     923static int list_algorithms(void) 
     924{ 
     925        int i; 
     926        const char *aname; 
     927 
     928        if (verbose) 
     929                printf("Card supports %d algorithm(s)\n\n",card->algorithm_count); 
     930 
     931        for (i=0; i < card->algorithm_count; i++) { 
     932                switch (card->algorithms[i].algorithm) { 
     933                case SC_ALGORITHM_RSA: 
     934                        aname = "rsa"; 
     935                        break; 
     936                case SC_ALGORITHM_DSA: 
     937                        aname = "dsa"; 
     938                        aname = "ec"; 
     939                        break; 
     940                case SC_ALGORITHM_DES: 
     941                        aname = "des"; 
     942                        break; 
     943                case SC_ALGORITHM_3DES: 
     944                        aname = "3des"; 
     945                        break; 
     946                case SC_ALGORITHM_MD5: 
     947                        aname = "md5"; 
     948                        break; 
     949                case SC_ALGORITHM_SHA1: 
     950                        aname = "sha1"; 
     951                        break; 
     952                case SC_ALGORITHM_PBKDF2: 
     953                        aname = "pbkdf2"; 
     954                        break; 
     955                case SC_ALGORITHM_PBES2: 
     956                        aname = "pbes2"; 
     957                        break; 
     958                default: 
     959                        aname = "unknown"; 
     960                        break; 
     961                } 
     962 
     963                printf("Algorithm: %s\n", aname); 
     964                printf("Key length: %d\n", card->algorithms[i].key_length); 
     965                printf("Flags:"); 
     966                if (card->algorithms[i].flags & SC_ALGORITHM_ONBOARD_KEY_GEN) 
     967                        printf(" onboard key generation"); 
     968                if (card->algorithms[i].flags & SC_ALGORITHM_NEED_USAGE) 
     969                        printf(" needs usage"); 
     970                if ( card->algorithms[i].algorithm == SC_ALGORITHM_RSA) { 
     971                        int padding = card->algorithms[i].flags 
     972                                        & SC_ALGORITHM_RSA_PADS; 
     973                        int hashes =  card->algorithms[i].flags 
     974                                        & SC_ALGORITHM_RSA_HASHES; 
     975                                         
     976                        printf(" padding ("); 
     977                        if (padding == SC_ALGORITHM_RSA_PAD_NONE)  
     978                                printf(" none"); 
     979                        if (padding & SC_ALGORITHM_RSA_PAD_PKCS1) 
     980                                printf(" pkcs1"); 
     981                        if (padding & SC_ALGORITHM_RSA_PAD_ANSI) 
     982                                printf(" ansi"); 
     983                        if (padding & SC_ALGORITHM_RSA_PAD_ISO9796) 
     984                                printf(" iso9796"); 
     985 
     986                        printf(" ) "); 
     987                        printf("hashes ("); 
     988                        if (hashes & SC_ALGORITHM_RSA_HASH_NONE) 
     989                                printf(" none"); 
     990                        if (hashes & SC_ALGORITHM_RSA_HASH_SHA1) 
     991                                printf(" sha1"); 
     992                        if (hashes & SC_ALGORITHM_RSA_HASH_MD5) 
     993                                printf(" MD5"); 
     994                        if (hashes & SC_ALGORITHM_RSA_HASH_MD5_SHA1) 
     995                                printf(" md5-sha1"); 
     996                        if (hashes & SC_ALGORITHM_RSA_HASH_RIPEMD160) 
     997                                printf(" ripemd160"); 
     998                        printf(" )"); 
     999                } 
     1000                printf("\n"); 
     1001                if (card->algorithms[i].algorithm == SC_ALGORITHM_RSA  
     1002                        && card->algorithms[i].u._rsa.exponent) { 
     1003                        printf("RSA public exponent: %lu\n", (unsigned long) 
     1004                                card->algorithms[i].u._rsa.exponent); 
     1005                } 
     1006 
     1007                if (i < card->algorithm_count) 
     1008                        printf("\n"); 
     1009        } 
     1010        return 0; 
     1011} 
     1012 
    9201013static int dump() 
    9211014{ 
    9221015 
     
    11831276        int do_change_pin = 0; 
    11841277        int do_unblock_pin = 0; 
    11851278        int do_learn_card = 0; 
     1279        int do_list_algorithms = 0; 
    11861280        int action_count = 0; 
    11871281        sc_context_param_t ctx_param; 
    11881282 
     
    12781372                case 'w': 
    12791373                        opt_wait = 1; 
    12801374                        break; 
     1375                case OPT_LIST_ALG: 
     1376                        do_list_algorithms = 1; 
     1377                        action_count++; 
     1378                        break; 
    12811379                } 
    12821380        } 
    12831381        if (action_count == 0) 
     
    12991397        if (err) 
    13001398                goto end; 
    13011399 
     1400        if (do_list_algorithms) { 
     1401                if ((err = list_algorithms())) 
     1402                        goto end; 
     1403                action_count--; 
     1404        } 
     1405        if (action_count <= 0) 
     1406                goto end; 
     1407 
    13021408        if (verbose) 
    13031409                fprintf(stderr, "Trying to find a PKCS#15 compatible card...\n"); 
    13041410        r = sc_pkcs15_bind(card, &p15card); 
  • src/libopensc/opensc.h

     
    164164/* Key encryption algoprithms */ 
    165165#define SC_ALGORITHM_PBES2              256 
    166166 
     167/* Algorithm flags */ 
    167168#define SC_ALGORITHM_ONBOARD_KEY_GEN    0x80000000 
    168169#define SC_ALGORITHM_NEED_USAGE         0x40000000 
    169170#define SC_ALGORITHM_SPECIFIC_FLAGS     0x0000FFFF