BOOL COneCommand::GetReceivedBuffer(CByteArray &buffer) { BOOL ret;int i,size;BYTE src,dst; if((size=m_ReceiveBuffer.GetSize())!=buffer.GetSize()) { buffer.RemoveAll(); buffer.Append(m_ReceiveBuffer); return TRUE; } else { ret=FALSE; for(i=0;i<size;i++) { src=m_ReceiveBuffer.GetAt(i); dst=buffer.GetAt(i); if(src!=dst) { buffer.SetAt(i,src); ret=TRUE; } } return ret; } }
static CByteArray ReadInternal(CPCSC *poPCSC, SCARDHANDLE hCard, unsigned long ulOffset, unsigned long ulMaxLen) { unsigned long ulLen = ulMaxLen > 252 ? 252 : ulMaxLen; long lretVal = 0; unsigned char tucReadDat[5] = {0xFF, 0xB2}; tucReadDat[2] = (unsigned char)(ulOffset/256); tucReadDat[3] = (unsigned char)(ulOffset%256); tucReadDat[4] = (unsigned char)(ulLen); CByteArray oCmd(tucReadDat, sizeof(tucReadDat)); CByteArray oData = poPCSC->Transmit(hCard, oCmd, &lretVal); oData.Chop(2); // remove SW12 // If we read enough, or there isn't more to read, return if (ulMaxLen == ulLen || oData.Size() < ulLen) { MWLOG(LEV_INFO, MOD_CAL, L" Read %d bytes from the SIS card", oData.Size()); return oData; } ulLen = oData.Size(); ulOffset += ulLen; ulMaxLen -= ulLen; ulLen = ulMaxLen > 252 ? 252 : ulMaxLen; tucReadDat[2] = (unsigned char)(ulOffset/256); tucReadDat[3] = (unsigned char)(ulOffset%256); tucReadDat[4] = (unsigned char)(ulLen); oCmd = CByteArray(tucReadDat, sizeof(tucReadDat)); CByteArray oData2 = poPCSC->Transmit(hCard, oCmd,&lretVal); oData2.Chop(2); // remove SW12 oData.Append(oData2); MWLOG(LEV_INFO, MOD_CAL, L" Read %d bytes from the SIS card", oData.Size()); return oData; }
CByteArray GetData(const std::string & csData) { CByteArray oData; // If it starts with "file:", we have to read a file if (csData.substr(0, 7) == "file://") { const char *csFileName = csData.c_str() + 7; #ifdef WIN32 FILE *f; fopen_s(&f,csFileName, "rb"); #else FILE *f = fopen(csFileName, "rb"); #endif if (f != NULL) { unsigned char tucBuf[1000]; size_t len = fread(tucBuf, 1, sizeof(tucBuf), f); while (len != 0) { oData.Append(tucBuf, (unsigned long) len); len = fread(tucBuf, 1, sizeof(tucBuf), f); } printf("Read %d bytes from file \"%s\"\n", oData.Size(), csFileName); fclose(f); } else printf("Couldn't open file \"%s\", using empty data\n", csFileName); } else { oData = CByteArray(csData.c_str()); printf("Input: %d bytes\n", oData.Size()); } return oData; }
bool CPkiCard::PinCmd(tPinOperation operation, const tPin & Pin, const std::string & csPin1, const std::string & csPin2, unsigned long & ulRemaining, const tPrivKey *pKey) { // No standard for Logoff, so each card has to implement // it's own command here. if (operation == PIN_OP_LOGOFF ) return LogOff(Pin); bool bRet = false; std::string csReadPin1, csReadPin2; const std::string *pcsPin1 = &csPin1; const std::string *pcsPin2 = &csPin2; bool bAskPIN = csPin1.empty(); bool bUsePinpad = bAskPIN ? m_poPinpad->UsePinpad(operation) : false; bad_pin: //If no Pin(s) provided and it's no Pinpad reader -> ask Pins if (bAskPIN && !bUsePinpad) { showPinDialog(operation, Pin, csReadPin1, csReadPin2, pKey); pcsPin1 = &csReadPin1; pcsPin2 = &csReadPin2; } CByteArray oPinBuf = MakePinBuf(Pin, *pcsPin1, bUsePinpad); if (operation != PIN_OP_VERIFY) oPinBuf.Append(MakePinBuf(Pin, *pcsPin2, bUsePinpad)); CByteArray oAPDU = MakePinCmd(operation, Pin); // add CLA, INS, P1, P2 oAPDU.Append((unsigned char) oPinBuf.Size()); // add P3 oAPDU.Append(oPinBuf); CByteArray oResp; bool bSelected = false; // Don't remove these brackets!! { CAutoLock autolock(this); // Select the path where the Pin is, if necessary if (!Pin.csPath.empty() && !bSelected && Pin.csPath != "3F00") { SelectFile(Pin.csPath); bSelected = true; } // Send the command if (csPin1.empty() && bUsePinpad) oResp = m_poPinpad->PinCmd(operation, Pin, PinUsage2Pinpad(Pin, pKey), oAPDU, ulRemaining); else oResp = SendAPDU(oAPDU); } unsigned long ulSW12 = getSW12(oResp); if (ulSW12 == 0x9000) bRet = true; else if (ulSW12 == 0x6983) ulRemaining = 0; else if (ulSW12 / 16 == 0x63C) ulRemaining = ulSW12 % 16; else throw CMWEXCEPTION(m_poContext->m_oPCSC.SW12ToErr(ulSW12)); #ifndef NO_DIALOGS // Bad PIN: show a dialog to ask the user to try again // PIN blocked: show a dialog to tell the user if (bAskPIN && !bRet) { DlgPinUsage usage = PinUsage2Dlg(Pin, pKey); DlgRet dlgret = DlgBadPin(usage, utilStringWiden(Pin.csLabel).c_str(), ulRemaining); if (0 != ulRemaining && DLG_RETRY == dlgret) goto bad_pin; } #endif // If PIN command OK and no SSO, then state that we have now // verified this PIN, this info is needed in the Sign() method if (bRet && !m_poContext->m_bSSO) { bool bFound = false; for (size_t i = 0; i < m_verifiedPINs.size() && !bFound; i++) bFound = (m_verifiedPINs[i] == Pin.ulID); if (!bFound) m_verifiedPINs.push_back(Pin.ulID); } return bRet; }
CTobCompiler::EToken CTobCompiler::CompilePass1() { EToken r; CString str, buf; CByteArray* bin = 0; for(;;) { if((r = GetToken(str)) <= 0) return r; if(r == SYM_KEY) { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found keyword after '.'"; return ERR_NORMAL; } if(str == "def") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found token after '.def'"; return ERR_NORMAL; } if((r = GetToken(buf)) < 0) return r; if(r == SYM_BIN) { bin = new CByteArray; if((r = GetBinary(*bin)) < 0) { delete bin; return r; } } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; bin = new CByteArray; for(LPCTSTR p = buf; *p; ++p) bin->Add(*(U8*)p); if(r == SYM_STRING) bin->Add(0); } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; bin = new CByteArray; for(LPCTSTR p = buf; *p; ++p) bin->Add(*(U8*)p); if(r == SYM_STRING) bin->Add(0); } else { m_err = "not found '[' or ''' or '\"' after '.def <token>''"; return ERR_NORMAL; } m_defbin.SetAt(str, bin); bin = 0; } else if(str == "change") { if((r = GetToken(str)) < 0) return r; if(r != SYM_STR) { m_err = "not found ''' after '.change'"; return ERR_NORMAL; } if((r = GetString(str, false)) < 0) return r; if( str.IsEmpty() || str.GetLength()>2 || str.GetLength() == 2 && (U8)str[0]<0x80) { m_err = "only one character is allowed after '.change'"; return ERR_NORMAL; } U16 c = (U8)str[0]; if(str.GetLength() == 2) c = (c<<8) + (U8)str[1]; if((r = GetToken(str)) < 0) return r; if(r == SYM_BIN) { CByteArray tmp; if((r = GetBinary(tmp)) < 0) return r; buf.Empty(); int i, n = tmp.GetSize(); for(i = 0; i < n; ++i) { U8 b = tmp.GetAt(i); if(!b) break; buf += (TCHAR)b; } } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; } else { m_err = "not found ''' or '\"' after '.change <char>'"; return ERR_NORMAL; } m_change.SetAt(c, buf); } else if(str == "bit") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.bit'"; return ERR_NORMAL; } int bit = atoi(str); if(bit!=8 && bit!=16 && bit!=24 && bit!=32) { m_err = "only '.bit 8/16/24/32' is available"; return ERR_NORMAL; } m_bit_i = bit; } else if(str == "maxc") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxc'"; return ERR_NORMAL; } m_maxc = atoi(str); } else if(str == "maxw") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxw'"; return ERR_NORMAL; } m_maxw = atoi(str); } else if(str == "maxs") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxs'"; return ERR_NORMAL; } m_maxs = atoi(str); } else { m_err.Format("unknwon keyword '%s' after '.'", (LPCTSTR)str); return ERR_NORMAL; } } else if(r == SYM_BIN) { if((r = GetBinary(m_bin)) < 0) return r; } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; for(LPCTSTR p=buf;* p; ++p) m_bin.Add(*(U8*)p); if(r == SYM_STRING) m_bin.Add(0); } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; for(LPCTSTR p=buf;* p; ++p) m_bin.Add(*(U8*)p); if(r == SYM_STRING) m_bin.Add(0); } else if(r == SYM_TOKEN) { if(!m_defbin.Lookup(str, bin)) { m_err.Format("not defined token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_bin.Append(*bin); bin = 0; } else if(r == SYM_ADDR) { if((r = GetToken(str)) < 0) return r; SExpInfo* ti = new SExpInfo(m_bin.GetSize(), m_line); if(r == SYM_EXP_BEGIN) { for(;;) { if((r = GetToken(buf)) < 0) { delete ti; return r; } if(r == SYM_TOKEN || r == SYM_NUM) ti->m_item.Add(buf); else if(r == SYM_ADD) ti->m_item.Add("+"); else if(r == SYM_SUB) ti->m_item.Add("-"); else if(r == SYM_ADDR) ti->m_item.Add("@"); else if(r == SYM_EXP_END) break; else { delete ti; m_err.Format("found unknown token '%s' in expression", (LPCTSTR)buf); return ERR_NORMAL; } } } else if(r == SYM_TOKEN) ti->m_item.Add(str); else { delete ti; m_err = "not found token or expression after '@'"; return ERR_NORMAL; } m_exp.Add(ti); for(int i = m_bit_i/8; i--;) m_bin.Add(0); } else if(r == SYM_LABEL) { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found label after '#'"; return ERR_NORMAL; } m_label.SetAt(str, m_bin.GetSize()); } else { m_err = "unknown or bad symbol"; return ERR_NORMAL; } } }
CTobCompiler::EToken CTobCompiler::CompilePass1() { EToken r, LastToken; CString str, buf, sizebuf; CByteArray* bin; SValueType* val; SExpression* exp; for(;;) { BOOL bSkipGetToken = FALSE; if ((r = GetToken(str)) <= 0) return r; _SkipGetToken: if (r == SYM_DEF) { /* if (m_bin.GetSize() > 0) { m_err = "const token can not defined after data"; return ERR_NORMAL; } */ if ((r = GetToken(str)) < 0) return r; if (r != SYM_TOKEN) { m_err = "not found token after '@'"; return ERR_NORMAL; } if (str == "_IGNORECASE") { m_Option.bIgnoreCase = TRUE; continue; } if ((r = GetToken(buf)) < 0) return r; if (r == SYM_BIN) { bin = new CByteArray; if ((r = GetBinary(*bin)) < 0) { delete bin; return r; } AddMap(m_binmap, str, bin); } else if (r == SYM_STR) { bin = new CByteArray; if ((r = GetString(*bin)) < 0) { delete bin; return r; } AddMap(m_binmap, str, bin); } else if (r == SYM_INT) { ULONG Length; BOOL bHex = (buf[0] == '0' && buf[1] == 'x'); r = GetToken(sizebuf); LastToken = r; if (r == SYM_TYPE) { r = GetToken(sizebuf); if (r != SYM_TOKEN) { m_err = "not found type after ':'"; return ERR_NORMAL; } Length = GetSizeFromType(sizebuf[0]); if (Length == -1) { m_err.Format("unknown type '%c' after ':'", sizebuf[0]); return ERR_NORMAL; } } else { Length = -1; } val = new SValueType((int)strtoul(buf, NULL, bHex ? 16 : 10), Length); AddMap(m_valmap, str, val); if (Length == -1) bSkipGetToken = TRUE; r = SYM_INT; } else if (r == SYM_FLOAT) { AddMap(m_valmap, str, new SValueType(atof(buf))); } else { m_err.Format("not found '[' or ''' or '\"' or number after '@%s'", (LPCTSTR)str); return ERR_NORMAL; } if (m_Option.bIgnoreCase) str.MakeUpper(); if (str == "_DEFI") { if (r != SYM_INT || val->ival < 1 || val->ival > 4) { m_err = "_DEFI must be 1/2/3/4"; return ERR_NORMAL; } m_Option.DefInt = val->ival; } else if (str == "_DEFF") { if (r != SYM_INT || val->ival != 4 && val->ival != 8) { m_err = "_DEFF must be 4/8"; return ERR_NORMAL; } m_Option.DefFloat = val->ival; } else if (str == "_MOD") { if (r != SYM_INT) { m_err = "_MOD must be number"; return ERR_NORMAL; } while (m_bin.GetSize() % val->ival) m_bin.Add(0); } else if (str == "_INCLUDE") { FILE *fsrc; WCHAR szPath[MAX_PATH]; CByteArray *bin; if (m_Option.bIgnoreCase) str.MakeLower(); if (!m_binmap.Lookup(str, bin)) continue; m_binmap.RemoveKey(str); MultiByteToWideChar( CP_GB2312, 0, (LPSTR)bin->GetData(), -1, szPath, countof(szPath)); delete bin; fsrc = m_fsrc; m_fsrc = _wfopen(szPath, L"rb"); if (m_fsrc == NULL) { m_fsrc = fsrc; m_err.Format("can't open include file '%S'", szPath); return ERR_SEVERE; } else { EToken r; for(;;) { r = CompilePass1(); if (r == ERR_EOF) break; if (r < 0) { if (!ErrorHandlerInternal(GetErrorString(m_err))) break; } if (r == ERR_SEVERE) break; } fclose(m_fsrc); m_fsrc = fsrc; if (r < 0) return r; } } if (bSkipGetToken) { str = sizebuf; r = LastToken; goto _SkipGetToken; } } else if (r == SYM_BIN) { if ((r = GetBinary(m_bin)) < 0) return r; } else if (r == SYM_STR) { if ((r = GetString(m_bin)) < 0) return r; } else if (r == SYM_TOKEN) { if (!m_binmap.Lookup(str, bin)) { m_err.Format("unknown token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_bin.Append(*bin); } else if (r == SYM_LABEL) { if ((r = GetToken(str)) < 0) return r; if (r != SYM_TOKEN) { m_err = "not found token after '#'"; return ERR_NORMAL; } if (m_valmap.Lookup(str, val)) { m_err.Format("already defined label token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_valmap.SetAt(str, new SValueType(m_bin.GetSize())); } else if (r == SYM_EXP_BEGIN) { int i = m_exparr.GetSize(); do { int len = m_Option.DefInt; BOOL hastype = FALSE; exp = new SExpression(m_bin.GetSize(), m_line); for(;;) { if ((r = GetToken(buf)) < 0) { delete exp; return r; } if (r == SYM_TOKEN) { val = NULL; if (!hastype && len > 0 && m_valmap.Lookup(buf, val) && val->isfloat) len = -m_Option.DefFloat; exp->m_item.Add(buf); if (val != NULL && val->m_size != -1) len = val->isfloat ? -val->m_size : val->m_size; } else if (r == SYM_ADD) { exp->m_item.Add("+"); } else if (r == SYM_SUB) { exp->m_item.Add("-"); } else if (r == SYM_DEF) { exp->m_item.Add("@"); } else if (r == SYM_INT) { exp->m_item.Add(buf); } else if (r == SYM_FLOAT) { if (!hastype && len > 0) len = -m_Option.DefFloat; exp->m_item.Add(buf); } else if (r == SYM_TYPE) { int newlen; if ((r = GetToken(buf)) < 0) { delete exp; return r; } if (r != SYM_TOKEN) { delete exp; m_err = "not found type after ':'"; return ERR_NORMAL; } newlen = GetSizeFromType(buf[0]); if (newlen == -1) { delete exp; m_err.Format("unknown type '%c' after ':'", buf[0]); return ERR_NORMAL; } if (hastype && newlen != len) { delete exp; m_err = "found different types in one expression"; return ERR_NORMAL; } hastype = TRUE; len = newlen; } else if (r == SYM_BIN) { if (exp->m_item.GetSize() > 0) { delete exp; m_err = "found different types in one expression"; return ERR_NORMAL; } if ((r = GetBinary(m_bin)) < 0) { delete exp; return r; } } else if (r == SYM_STR) { if (exp->m_item.GetSize() > 0) { delete exp; m_err = "found different types in one expression"; return ERR_NORMAL; } if ((r = GetString(m_bin)) < 0) { delete exp; return r; } } else if (r == SYM_NEXT || r == SYM_EXP_END) { break; } else { delete exp; m_err.Format("unknown or bad symbol1 '%s' in expression", (LPCTSTR)str); return ERR_NORMAL; } } if (exp->m_item.GetSize() <= 0) { delete exp; } else { exp->m_size = len; m_exparr.Add(exp); if (len < 0) len = -len; while(len--) m_bin.Add(0); } } while(r != SYM_EXP_END); for(int j = m_exparr.GetSize(); i < j; ++i) m_exparr[i]->m_self = m_bin.GetSize(); } else { m_err.Format("unknown or bad symbol2 '%s'", (LPCTSTR)str); return ERR_NORMAL; } } }
CTobCompiler::EToken CTobCompiler::GetString(CByteArray& bin) { CByteArray temp; int c, d, e; for(;;) { if ((c = fgetc(m_fsrc)) < 0) break; if (c == '\n') ++m_line; else if (ms_tokentable[c] == 3) { m_err.Format("found invalid char 0x%02X in src file at %p", c, ftell(m_fsrc)-1); return ERR_NORMAL; } else if (c < ' ') continue; else if (c == '\'' || c == '"') { if ((d = fgetc(m_fsrc)) > 0) { if (d == ':') { if ((d = fgetc(m_fsrc)) < 0) break; e = GetSizeFromType(d); if (e == -1) { m_err.Format("unknown type '%c' after ':'", d); return ERR_NORMAL; } d = temp.GetSize(); for(int i = 0; i < e; ++i) bin.Add(((BYTE*)&d)[i]); } else if (fseek(m_fsrc, -1, SEEK_CUR)) { m_err.Format("can not seek src file at %p", ftell(m_fsrc)); return ERR_SEVERE; } } bin.Append(temp); if (c == '"') bin.Add(0); if (m_Option.bIgnoreCase) strlwr((LPSTR)bin.GetData()); return SYM_STR; } else if (c == '\\') { if ((c = fgetc(m_fsrc)) < 0) break; switch(CHAR_UPPER(c)) { case 'N': c = '\n'; break; case 'R': c = '\r'; break; case 'T': c = '\t'; break; case '0': c = '\0'; break; case '1': c = '\x01'; break; case '2': c = '\x02'; break; case '3': c = '\x03'; break; case '4': c = '\x04'; break; case '5': c = '\x05'; break; case '6': c = '\x06'; break; case '7': c = '\x07'; break; case '8': c = '\x08'; break; case '9': c = '\x09'; break; case 'A': c = '\x0a'; break; case 'B': c = '\x0b'; break; case 'C': c = '\x0c'; break; case 'D': c = '\x0d'; break; case 'E': c = '\x0e'; break; case 'F': c = '\x0f'; break; case 'X': { if ((c = fgetc(m_fsrc)) < 0) goto end_; BYTE b0 = ms_hextable[c]; if (b0 == 16) { m_err.Format("bad hex char 0x%02X after '\\x'", b0); return ERR_NORMAL; } if ((c = fgetc(m_fsrc)) < 0) goto end_; BYTE b1 = ms_hextable[c]; if (b1 == 16) { m_err.Format("bad hex char 0x%02X after '\\x'", b1); return ERR_NORMAL; } temp.Add((BYTE)((b0<<4) + b1)); } } } if (c >= 0x81) { if ((d = fgetc(m_fsrc)) < 0) break; if (d < 0x40) { if (fseek(m_fsrc, -1, SEEK_CUR)) { m_err.Format("can not seek src file at %p", ftell(m_fsrc)); return ERR_SEVERE; } m_err.Format("found half wide char at %p", ftell(m_fsrc)-1); return ERR_NORMAL; } temp.Add((BYTE)c); temp.Add((BYTE)d); } else { temp.Add((BYTE)c); } } end_: if (!feof(m_fsrc)) m_err.Format("can not read src file at %p", ftell(m_fsrc)); else m_err = "bad string at the end of src file"; return ERR_SEVERE; }