📄 sccom.c
字号:
/*-------------------------------------------------------+-----*//* | GMD *//* SYSTEM STAMOD - Version 2.0 +-----*//* *//*-------------------------------------------------------------*//* *//* PACKAGE SCCOM VERSION 2.0 *//* DATE November 1991 *//* BY Levona Eckstein*//* FILENAME *//* sccom.c *//* *//* DESCRIPTION *//* SC - Interface - Module *//* *//* EXPORT DESCRIPTION *//* *//* sc_create create SC-Request-Apdu *//* *//* sc_check check SC-Response-Apdu *//* *//* sc_enc encrypt SC-APDU *//* *//* sc_dec decrypt SC-APDU *//* *//* sc_crmac create SC-APDU with MAC *//* *//* sc_checkmac check SC-APDU with MAC *//* *//* e_KeyId create key identifier *//* *//* e_FileId create file identifier *//* *//* e_KeyAttrList create the key header *//* *//* sccom.h *//* *//* sc_errno error-number *//* *//* sc_errmsg address of error message *//* *//* *//* IMPORT DESCRIPTION *//* sct_error SCT-Error-Table *//* *//* aux_fxdump dump buffer in File *//* *//* sta_aux_sc_apdu dump SC-APDU in file *//* *//* sta_aux_sc_resp dump SC-RESP in file *//* *//* sta_aux_elemlen eleminate length in resp. buffer *//* *//* USES DESCRIPTION *//* sca.h *//* *//* sctint.h *//* *//* sctloc.h *//* *//* sccom.h *//* *//* sctrc.h *//* *//* scloc.h *//* *//* *//* INTERNAL DESCRIPTION *//* *//* e_FileTypeCat create the parameter File Type / *//* File Category *//* *//* e_OperationMode create the Operation Mode Param. *//* *//* e_TwoByte create *//* (HIGH/LOW-Byte) in sc_apdu *//* *//* Ioput transmit one byte in sc_apdu *//* *//* Ioputbuff transmit string in sc_apdu *//* *//* SCalloc allocate buffer *//* *//* SCchecklen check length of parameter *//* *//* Wichtig !!!!!: *//* Es muessen noch die Funktionen sc_e_secure und *//* sc_d_secure (fuer secure messaging) bereitgestellt werden*//*-------------------------------------------------------------*//*-------------------------------------------------------------*//* include-Files *//*-------------------------------------------------------------*/#include <stdio.h>#include <memory.h>#include "sca.h"#include "sctint.h"#include "sctloc.h"#include "sccom.h"#include "sctrc.h"#include "scloc.h"/*-------------------------------------------------------------*//* extern declarations *//*-------------------------------------------------------------*/extern SCTerror sct_error[TABLEN]; /* message table */extern void aux_fxdump();extern int sta_aux_sc_apdu();extern void sta_aux_sc_resp();extern void sta_aux_elemlen();extern void sta_aux_bytestr_free();extern void aux_free2_OctetString();extern void aux_free2_BitString();extern int des_encrypt();extern int des_decrypt();/*-------------------------------------------------------------*//* forward global declarations *//*-------------------------------------------------------------*/int sc_create();int sc_check();int sc_enc();int sc_dec();int sc_crmac();int sc_checkmac();char e_KeyId();char e_FileId();void e_KeyAttrList();/*-------------------------------------------------------------*//* forward local declarations *//*-------------------------------------------------------------*/static char e_FileTypeCat();static char e_OperationMode();static void e_TwoByte();static void Ioput();static void Ioputbuff();static char *SCalloc();static int SCchecklen();/*-------------------------------------------------------------*//* global variable definitions *//*-------------------------------------------------------------*/unsigned int sc_errno; /* error variable */char *sc_errmsg; /* address of error message *//*-------------------------------------------------------------*//* local Variable definitions *//*-------------------------------------------------------------*/#ifdef STREAMstatic BOOL first = FALSE; /* FLAG, if Trace-File open */#endif#ifdef STREAMFILE *sc_trfp; /* Filepointer of trace file */#endif/*--------------------------------------------------------*//* | GMD *//* +-----*//* PROC sc_create VERSION 2.0 *//* DATE November 1991 *//* BY L.Eckstein,GMD *//* *//* DESCRIPTION *//* Create SC-Command *//* This procedure allocates the buffer for the *//* SC-Command and generates the SC-APDU. *//* If sec_mode = TRUE, then the SSC will be set in the *//* APDU. *//* *//* *//* *//* IN DESCRIPTION *//* sc_cmd SC-Command-Structure *//* *//* sec_mode Flag, if secure messaging *//* *//* ssc Send Sequence Counter *//* only used in case of *//* sec_mode = TRUE *//* OUT *//* sc_apdu Pointer of SC-Command-APDU *//* Construction: *//* ______________________ *//* | CLA,INS,P1,P2,L,DATA | *//* ______________________ *//* or: *//* __________________________ *//* | CLA,INS,P1,P2,L,SSC,DATA |*//* __________________________ *//* The memory for sc_apdu->bytes*//* will be allocated by this *//* programm and must be set *//* free by the calling program *//* only in case of no error *//* *//* *//* RETURN DESCRIPTION *//* 0 o.k *//* -1 Error *//* EPARINC *//* ETOOLONG *//* EMEMAVAIL *//* *//* CALLED FUNCTIONS *//* e_KeyId *//* e_FileId *//* e_FileTypeCat *//* e_OperationMode *//* e_TwoByte *//* Ioput *//* Ioputbuff *//* SCalloc *//* SCchecklen *//* sta_aux_sc_apdu *//*--------------------------------------------------------*/intsc_create(sc_cmd, sec_mode, ssc, sc_apdu) struct s_command *sc_cmd; /* SC-Command */ Boolean sec_mode; int ssc; Bytestring *sc_apdu;/* Structure of SC-APDU */{ /*----------------------------------------------------------*/ /* Definitions */ /*----------------------------------------------------------*/ char *scapdu = NULL; unsigned int lscapdu = HEADLEN; char *ptr = NULL; char oldpin[9]; char newpin[9]; char file_type_cat; char finfolen; char kid_1; char kid_2; char fid; unsigned int len = 0; unsigned int class; unsigned int i; char op_mode; int ssc_len; /*----------------------------------------------------------*/ /* Statements */ /*----------------------------------------------------------*/ sc_apdu->nbytes = 0; sc_apdu->bytes = NULL;#ifdef STREAM if (!first) { sc_trfp = fopen("SCCOM.TRC", "wt"); first = TRUE; };#endif sc_errno = 0; if (sec_mode == TRUE) { ssc_len = 1; if (ssc != 0) ssc = ssc % 256; } else { ssc_len = 0; } lscapdu += ssc_len; switch (sc_cmd->sc_header.inscode) { /*--------------------------*/ /* create SC_EXRND */ /*--------------------------*/ case SC_EXRND: if (SCEXRND.di != ICC_TO_IFD) { if (SCEXRND.lrnd != RNDLEN) { sc_errno = ELENERR; sc_errmsg = sct_error[sc_errno].msg; return (-1); } lscapdu += SCEXRND.lrnd; }; len = RNDLEN; if ((SCEXRND.di < ICC_TO_IFD) || (SCEXRND.di > BOTH)) { sc_errno = EPARINC; sc_errmsg = sct_error[sc_errno].msg; return (-1); }; if ((scapdu = SCalloc(lscapdu)) == NULL) return (-1); ptr = scapdu + 2; Ioput(&ptr, (int) SCEXRND.di); Ioput(&ptr, SC_NOTUSED); Ioput(&ptr, len); if (sec_mode == TRUE) Ioput(&ptr, ssc); if (SCEXRND.di != ICC_TO_IFD) Ioputbuff(&ptr, SCEXRND.rnd, SCEXRND.lrnd); break; /*--------------------------*/ /* create SC_GET_CD */ /*--------------------------*/ case SC_GET_CD: if (SCGETCD.cd_len > 8) { sc_errno = EPARINC; sc_errmsg = sct_error[sc_errno].msg; return (-1); }; if ((scapdu = SCalloc(lscapdu)) == NULL) return (-1); ptr = scapdu + 2; Ioput(&ptr, SCGETCD.cd_len); Ioput(&ptr, SC_NOTUSED); Ioput(&ptr, SC_NOTUSED); if (sec_mode == TRUE) Ioput(&ptr, ssc); break; /*--------------------------*/ /* create SC_SETKEY */ /*--------------------------*/ case SC_SETKEY: if ((kid_1 = e_KeyId(SCSETKEY.auth_kid)) == -1) return (-1); if ((kid_2 = e_KeyId(SCSETKEY.conc_kid)) == -1) return (-1); if ((scapdu = SCalloc(lscapdu)) == NULL) return (-1); ptr = scapdu + 2; Ioput(&ptr, kid_1); Ioput(&ptr, kid_2); Ioput(&ptr, SC_NOTUSED); if (sec_mode == TRUE) Ioput(&ptr, ssc); break; /*--------------------------*/ /* create SC_SELECT */ /*--------------------------*/ case SC_SELECT: if ((SCSELECT.id > SF) || (SCSELECT.id < MF) || (SCSELECT.fi < NONE_INFO) || (SCSELECT.fi > SHORT_INFO) || (SCSELECT.fn == NULL) || (strlen(SCSELECT.fn) == 0)) { sc_errno = EPARINC; sc_errmsg = sct_error[sc_errno].msg; return (-1); }; if (SCchecklen(strlen(SCSELECT.fn), MAX_FILENAME) == -1) return (-1); lscapdu += SCPLEN + strlen(SCSELECT.fn); if ((scapdu = SCalloc(lscapdu)) == NULL) return (-1); ptr = scapdu + 2; Ioput(&ptr, (int) SCSELECT.id); Ioput(&ptr, (int) SCSELECT.fi); Ioput(&ptr, SCPLEN + strlen(SCSELECT.fn)); if (sec_mode == TRUE) Ioput(&ptr, ssc); Ioput(&ptr, SCSELECT.scp); Ioputbuff(&ptr, SCSELECT.fn, strlen(SCSELECT.fn)); break; /*--------------------------*/ /* create SC_REGISTER */ /*--------------------------*/ case SC_REGISTER: if ((SCREG.acv > 255) || (SCREG.fn == NULL) || (strlen(SCREG.fn) == 0) || (SCREG.units <= 0)) { sc_errno = EPARINC; sc_errmsg = sct_error[sc_errno].msg; return (-1); };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -