⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mu_sci.c

📁 IBM source for pallas/vulcan/vesta
💻 C
📖 第 1 页 / 共 3 页
字号:
            capdu[2] = 0x04;    /* P1 means select a DF */            capdu[3] = 0x00;    /* P2 */            capdu[4] = 0x0E;    /* Lc */            capdu[5] = 0x31;    /* Data field */            capdu[6] = 0x50;            capdu[7] = 0x41;            capdu[8] = 0x59;            capdu[9] = 0x2E;            capdu[10] = 0x53;            capdu[11] = 0x59;            capdu[12] = 0x53;            capdu[13] = 0x2E;            capdu[14] = 0x44;            capdu[15] = 0x44;            capdu[16] = 0x46;            capdu[17] = 0x30;            capdu[18] = 0x31;            capdu[19] = 0x00;        }        /* response APDU will be only the end sequence */        if ((rc = sc_apdu(sci_id, capdu, rapdu, &length)) == SCI_ERROR_OK)        {            if (loop_test == 0)            {                printf("SELECT FILE command successful.\r\n");                printf("Response APDU in hexadecimal:\r\n");                for (i = 0; i < length; i++)                {                    if (rapdu[i] < 0x10)                    {                        printf("0");                    }                    printf("%x ", rapdu[i]);                }                printf("\r\n");            }        }        else        {            printf("\r\n");            print_error("sc_apdu", rc);        }    }    else    {        rc = SCI_ERROR_CARD_NOT_ACTIVATED;    }    return (rc);}/****************************************************************************** Name         update_record**** Purpose:     issue an UPDATE RECORD command to the Smart Card**** Parameters:  sci_id: zero-based number to identify Smart Card controller**** Returns:     Error status****************************************************************************/SCI_ERROR update_record(unsigned long sci_id){    SCI_ERROR rc = SCI_ERROR_OK;    unsigned long i = 0;    unsigned char capdu[260];    unsigned long length = 260;    unsigned char rapdu[258];    unsigned long ioctl_param;    ioctl(fd[sci_id], IOCTL_GET_IS_CARD_ACTIVATED, &ioctl_param);    if (ioctl_param == 1)    {        capdu[0] = 0xc0;        /* CLA byte */        capdu[1] = 0xdc;        /* INS byte- UPDATE RECORD */        capdu[2] = 0x00;        /* P1 */        capdu[3] = 0x00;        /* P2 */        capdu[4] = 0xff;        /* Lc */        //capdu[4] = 25;        /* Lc */        for (i = 0; i < 255; i++)        {            capdu[i + 5] = (unsigned char) i;        }		length = i + 5;        if (loop_test == 0)        {            printf("Enter the text to be written(up to 255 characters):\r\n");            scanf("%s", (char *) (capdu + 5));        }        /* response APDU will be only the end sequence */        if ((rc = sc_apdu(sci_id, capdu, rapdu, &length)) == SCI_ERROR_OK)        {            if (loop_test == 0)            {                printf("Response APDU in hexadecimal:\r\n");                for (i = 0; i < length; i++)                {                    if (rapdu[i] < 0x10)                    {                        printf("0");                    }                    printf("%x ", rapdu[i]);                }                printf("\r\n");                /* check SW1 and SW2 for success */                if ((rapdu[length - 2] == 0x90)                    && (rapdu[length - 1] == 0x00))                {                    printf("UPDATE RECORD command successful.\r\n");                }                else                {                    printf                        ("Must use SELECT FILE prior to UPDATE RECORD.\r\n");                }            }        }        else        {            printf("\r\n");            print_error("sc_apdu", rc);        }    }    else    {        rc = SCI_ERROR_CARD_NOT_ACTIVATED;    }    return (rc);}/****************************************************************************** Name         read_record**** Purpose:     issue a READ RECORD command to the Smart Card**** Parameters:  sci_id:     zero-based number to identify smart card controller**** Returns:     Error status****************************************************************************/SCI_ERROR read_record(unsigned long sci_id){    SCI_ERROR rc = SCI_ERROR_OK;    unsigned long i = 0;    unsigned char capdu[5];    unsigned long length = 5;    unsigned char rapdu[258];    unsigned char string[255];    unsigned long ioctl_param;    ioctl(fd[sci_id], IOCTL_GET_IS_CARD_ACTIVATED, &ioctl_param);    if (ioctl_param == 1)    {        capdu[0] = 0xc0;        /* CLA byte */        capdu[1] = 0xb2;        /* INS byte- READ RECORD */        capdu[2] = 0x00;        /* P1 */        capdu[3] = 0x00;        /* P2 */        capdu[4] = 0xFF;        /* Lc */        /* response APDU will be data and end sequence */        if ((rc = sc_apdu(sci_id, capdu, rapdu, &length)) == SCI_ERROR_OK)        {            if (loop_test == 0)            {                printf("Response APDU in hexadecimal:\r\n");                for (i = 0; i < length; i++)                {                    if (rapdu[i] < 0x10)                    {                        printf("0");                    }                    printf("%x ", rapdu[i]);                }                printf("\r\n");                /* check SW1 and SW2 for success */                if ((rapdu[length - 2] == 0x90)                    && (rapdu[length - 1] == 0x00))                {                    printf("READ RECORD command successful.\r\n");                    if ((length > 2) && (rapdu[0] == 0))                    {                        printf                            ("Use UPDATE RECORD to write text to the Smart Card.\r\n");                    }                    else                    {                        memcpy((void *) string, (const void *) rapdu, 255);                        printf("Text read from the Smart Card:\r\n%s\r\n",                               string);                    }                }                else                {                    printf("Must use SELECT FILE prior to READ RECORD.\r\n");                }            }        }        else        {            printf("\r\n");            print_error("sc_apdu", rc);        }    }    else    {        rc = SCI_ERROR_CARD_NOT_ACTIVATED;    }    return (rc);}/****************************************************************************** Function:    print_error**** Purpose:     Prints the text of an error**** Parameters:  fn_name: string of the function name that generated the error**              error_code: the error code****************************************************************************/void print_error(char *fn_name, int error_code){    if (error_code != 0)    {        printf("Function \"%s\" failed.\r\n", fn_name);    }    switch (error_code)    {        case SCI_ERROR_DRIVER_NOT_INITIALIZED:            printf("The SCI driver has not been initialized");            break;        case SCI_ERROR_FAIL:            printf("Unspecified error");            break;        case SCI_ERROR_KERNEL_FAIL:            printf("Kernel error");            break;        case SCI_ERROR_NO_ATR:            printf("No ATR (answer-to-reset) received");            break;        case SCI_ERROR_TS_CHARACTER_INVALID:            printf("Invalid TS character in ATR (answer-to-reset)");            break;        case SCI_ERROR_LRC_FAIL:            printf("LRC check failure");            break;        case SCI_ERROR_CRC_FAIL:            printf("CRC check failure");            break;        case SCI_ERROR_LENGTH_FAIL:            printf("More data received than expected");            break;        case SCI_ERROR_PARITY_FAIL:            printf("Parity error");            break;        case SCI_ERROR_RX_OVERFLOW_FAIL:            printf("Receive buffer overflow");            break;        case SCI_ERROR_TX_UNDERRUN_FAIL:            printf("Transmit buffer underrun");            break;        case SCI_ERROR_CARD_NOT_PRESENT:            printf("Smart Card not inserted");            break;        case SCI_ERROR_CARD_NOT_ACTIVATED:            printf("Smart Card not activated- a cold reset must be done");            break;        case SCI_ERROR_AWT_TIMEOUT:            printf("Maximum total time to receive ATR was exceeded.");            break;        case SCI_ERROR_WWT_TIMEOUT:            printf("Work Waiting Time (WWT) time-out");            break;        case SCI_ERROR_CWT_TIMEOUT:            printf("Character Waiting Time (CWT) time-out");            break;        case SCI_ERROR_BWT_TIMEOUT:            printf("Block Waiting Time (BWT) time-out");            break;        case SCI_ERROR_PARAMETER_OUT_OF_RANGE:            printf("One or more parameters are out of range");            break;        case SCI_ERROR_TRANSACTION_ABORTED:            printf("Current transaction has been aborted");            break;        case SCI_ERROR_CLOCK_STOP_DISABLED:            printf("Clock stop is disabled and/or unsupported by this card");            break;        case SCI_ERROR_TX_PENDING:            printf                ("A transmission was attempted while the driver was already transmitting");            break;        case SCI_ERROR_ATR_PENDING:            printf                ("A reset was attempted while waiting on an ATR from a previous reset");            break;        default:            printf("Unknown error");            break;    }    printf(":error code %d\r\n", error_code);}/****************************************************************************** Function:    pps**** Purpose:     Initiate an PPS (Protocol Parameter Selection) exchange.**** Parameters:  sci_id: zero-based number to identify smart card controller****************************************************************************/void pps(unsigned long sci_id){    int rc = SCI_ERROR_OK;    SC_PARAMETERS sc_parameters;    char read_data[80];    unsigned char T = 0;    unsigned char F = 0;    unsigned char D = 0;    unsigned long ioctl_param;    ioctl(fd[sci_id], IOCTL_GET_IS_CARD_ACTIVATED, &ioctl_param);    if (ioctl_param == 1)    {        if ((rc = sc_get_parameters(sci_id, &sc_parameters)) == SCI_ERROR_OK)        {            /* Assume use of FI and DI, unless otherwise specified */            T = sc_parameters.T;            F = sc_parameters.FI;            D = sc_parameters.DI;            printf                ("Default values to be used for Protocol Parameter Selection:\r\n");            printf("T: %u\r\n", T);            printf("F: %u\r\n", F);            printf("D: %u\r\n", D);            printf("Enter proposed T:\r\n");            scanf("%s", read_data);            if (isdigit((int) read_data[0]))            {                T = (unsigned char) atol(read_data);            }            printf("Enter proposed F:\r\n");            scanf("%s", read_data);            if (isdigit((int) read_data[0]))            {                F = (unsigned char) atol(read_data);            }            printf("Enter proposed D:\r\n");            scanf("%s", read_data);            if (isdigit((int) read_data[0]))            {                D = (unsigned char) atol(read_data);            }            if ((rc = sc_pps(sci_id, T, F, D)) == SCI_ERROR_OK)            {                printf("PPS exchange successful.\r\n");            }            else            {                printf("PPS exchange unsuccessful.\r\n");                printf("Card does not support this request.\r\n");                printf                    ("Note: PPS must be initiated immediately following a reset.\r\n");                printf("Initiating reset...\r\n");                if ((rc = sc_reset(sci_id)) == SCI_ERROR_OK)                {                    printf("Reset successful.\r\n");                }                else                {                    print_error("sc_reset", rc);                }            }        }    }    else    {        printf("Smart Card not activated- a cold reset must be done\r\n");    }}/****************************************************************************** Function:    get_ATR_parms**** Purpose:     Display all ATR parameters**** Parameters:  sci_id: zero-based number to identify smart card controller****************************************************************************/void get_ATR_parms(unsigned long sci_id){    int rc = SCI_ERROR_OK;    unsigned char i = 0;    unsigned char atr_size = 0;    unsigned char historical_size = 0;    unsigned char historical_offset = 0;    unsigned char ATR[33];    SC_PARAMETERS sc_parameters;    unsigned long ioctl_param;    ioctl(fd[sci_id], IOCTL_GET_IS_CARD_ACTIVATED, &ioctl_param);    if (ioctl_param == 1)    {        /* retrieve the Answer-to-Reset */        if ((rc = sc_get_atr(sci_id, ATR, &atr_size,                             &historical_offset,                             &historical_size)) == SCI_ERROR_OK)        {            /* get the Smart Card's default parameters */            if ((rc =                 sc_get_parameters(sci_id, &sc_parameters)) == SCI_ERROR_OK)            {                printf("Smart Card %d ATR Parameters:\r\n", sci_id);                printf("------------------------------------");                printf("------------------------------------\n\r");                printf("ATR (Answer-to-Reset) in hexadecimal:\r\n");                for (i = 0; i < atr_size; i++)                {                    if (ATR[i] < 0x10)                    {                        printf("0", ATR[i]);                    }                    printf("%x ", ATR[i]);                }                printf("\r\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -