ByteVector MifarePlusSL3Commands_NEW::readBinaryPlain(
            unsigned char blockno,
            size_t len)
    {
        assert(len / 16 < 255);
        uint8_t nb_block_to_read = static_cast<uint8_t>(len / 16);
/*
        ByteVector mac = {1, 2, 3, 4, 5, 6, 7 , 8};
        ByteVector cmd{0x34, blockno, 0x00, nb_block_to_read};
        cmd.insert(cmd.end(), mac.begin(), mac.end());*/
        assert(auth_);

        ByteVector data(16, 'a');
        data = {0x32, 0x14, 0xA5, 0xF4, 0xDE, 0x18, 0xAE, 0xC8, 0xDA, 0x6F,
                0x50, 0x33, 0x32, 0xB7, 0x10, 0xD7};
       // data = {0x02, 0xBB, 0x2A, 0x18, 0xBA, 0x7C, 0x4B, 0x54, 0xFC, 0x9F,
//                0x8C, 0xAE, 0x37, 0x7F, 0x5C, 0x1A};
        //data = auth_->cipherWriteData(data);

        LOG(DEBUGS) << "Data encryp: " << data;
        ByteVector cmd = {0xA3, blockno, 0x00};
        cmd.insert(cmd.end(), data.begin(), data.end());

        auto mac = auth_->computeWriteMac(0xA3, blockno, data);
        //auto mac = GetMacOnCommand(cmd, auth_->trans_id_, auth_->deriveKMac());
        LOG(DEBUGS) << "Mac = " << mac;
        cmd.insert(cmd.end(), mac.begin(), mac.end());

        auto ret = getReaderCardAdapter()->sendCommand(cmd);
        LOG(DEBUGS) << "Ret from plain read" << ret;
        return ret;
    }
	void BufferReader::SetData(uchar* thePtr, int theCount)
	{
		mData.clear();
		mData.reserve(theCount);
		mData.insert(mData.begin(), thePtr, thePtr + theCount);
		mDataBitSize = mData.size() * 8;
	}
Beispiel #3
0
bool receive_request_test(ByteVector &request, int attempt)
{
	String data;
	switch (attempt) {
		case 0:
			data += "GET / HTTP/1.1\r\n";
			data += "Accept: */*\r\n";
			break;
		case 1:
			break;
		case 2:
			data += "User-Agent: Test\r\n";
			data += "Host";
			break;
		case 3:
			data += ": 127.0.0.1\r\n";
			data += "Content-Length: 4\r\n";
			break;
		case 4:
			data += "\r\n";
			break;
		case 5:
			break;
		case 6:
			data += "12";
			break;
		case 7:
			data += "34";
			break;
		default:
			return false;
	}
	
	request.insert(request.end(), data.begin(), data.end());
	return true;
}
Beispiel #4
0
bool CHttpServer::receive_request(ByteVector &request)
{
	if (verbose) RAWTRACE("Receiving request...");

	ByteVector r;
    char buf[BUF_SIZE];
    int attempts = 0;
    for(;;) {
        if (verbose) RAWTRACE("Read portion of data from socket...");
        int n = recv(m_sock, &buf[0], sizeof(buf), 0);
        //RAWTRACE1("RECV: %d", n);
        if (n == -1) {
            int e = RHO_NET_ERROR_CODE;
            if (verbose) RAWTRACE1("RECV ERROR: %d", e);
#if !defined(WINDOWS_PLATFORM)
            if (e == EINTR)
                continue;
#else
			if (e == WSAEINTR)
				continue;
#endif

#if defined(OS_WP8) || (defined(RHODES_QT_PLATFORM) && defined(OS_WINDOWS_DESKTOP)) || defined(OS_WINCE)
            if (e == EAGAIN || e == WSAEWOULDBLOCK) {
#else
            if (e == EAGAIN) {
#endif
                if (!r.empty())
                    break;
                
                if(++attempts > (HTTP_EAGAIN_TIMEOUT*10))
                {
                    if (verbose) RAWLOG_ERROR("Error when receiving data from socket. Client does not send data for " HTTP_EAGAIN_TIMEOUT_STR " sec. Cancel recieve.");
                    return false;
                }

                fd_set fds;
                FD_ZERO(&fds);
                FD_SET(m_sock, &fds);
                timeval tv = {0};
				tv.tv_usec = 100000;//100 MS
                select(m_sock + 1, &fds, 0, 0, &tv);
                continue;
            }
            
            if (verbose) RAWLOG_ERROR1("Error when receiving data from socket: %d", e);
            return false;
        }
        
        if (n == 0) {
            if(!r.empty()) {
                if (verbose) RAWTRACE("Client closed connection gracefully");
                break;
            } else {
                if (verbose) RAWLOG_ERROR("Connection gracefully closed before we receive any data");
                return false;
            }
        } else {
            if (verbose) RAWTRACE1("Actually read %d bytes", n);
            r.insert(r.end(), &buf[0], &buf[0] + n);
        }
    }
    
    if (!r.empty()) {
        request.insert(request.end(), r.begin(), r.end());
        if ( !rho_conf_getBool("log_skip_post") ) {
            String strRequest(request.begin(),request.end());
            if (verbose) RAWTRACE1("Received request:\n%s", strRequest.c_str());
        }
    }
    return true;
}

bool CHttpServer::send_response_impl(String const &data, bool continuation)
{
#ifdef OS_MACOSX
    if ( m_localResponseWriter != 0 ) {
      m_localResponseWriter->writeResponse( data );
      return true;
    }
#endif

    if (verbose) {
        if (continuation)
            if (verbose) RAWTRACE("Send continuation data...");
        else
            if (verbose) RAWTRACE("Sending response...");
    }
    
    // First of all, make socket blocking
#if defined(WINDOWS_PLATFORM)
    unsigned long optval = 0;
        if(::ioctlsocket(m_sock, FIONBIO, &optval) == SOCKET_ERROR) {
        RAWLOG_ERROR1("Can not set blocking socket mode: %d", RHO_NET_ERROR_CODE);
        return false;
    }
#else
    int flags = fcntl(m_sock, F_GETFL);
    if (flags == -1) {
        if (verbose) RAWLOG_ERROR1("Can not get current socket mode: %d", errno);
        return false;
    }
    if (fcntl(m_sock, F_SETFL, flags & ~O_NONBLOCK) == -1) {
        if (verbose) RAWLOG_ERROR1("Can not set blocking socket mode: %d", errno);
        return false;
    }
#endif
    
    size_t pos = 0;
    for(; pos < data.size();) {
        int n = send(m_sock, data.c_str() + pos, data.size() - pos, 0);
        if (n == -1) {
            int e = RHO_NET_ERROR_CODE;
#if !defined(WINDOWS_PLATFORM)
            if (e == EINTR)
                continue;
#endif
            
            if (verbose) RAWLOG_ERROR1("Can not send response data: %d", e);
            return false;
        }
        
        if (n == 0)
            break;
        
        pos += n;
    }
    
    //String dbg_response = response.size() > 100 ? response.substr(0, 100) : response;
    //RAWTRACE2("Sent response:\n%s%s", dbg_response.c_str(), response.size() > 100 ? "..." : "   ");
    if (continuation) {
        if (verbose) RAWTRACE1("Sent response body: %d bytes", data.size());
    }
    else if ( !rho_conf_getBool("log_skip_post") ) {
        if (verbose) RAWTRACE1("Sent response (only headers displayed):\n%s", data.c_str());
    }

    return true;
}

bool CHttpServer::send_response(String const &response, bool redirect)
{
#ifdef OS_ANDROID
    if (redirect) {
        CAutoPtr<IRhoThreadImpl> ptrThread = rho_get_RhoClassFactory()->createThreadImpl();
        ptrThread->sleep(20);
    }
#endif
    return send_response_impl(response, false);
}

String CHttpServer::create_response(String const &reason)
{
    return create_response(reason, "");
}

String CHttpServer::create_response(String const &reason, HeaderList const &headers)
{
    return create_response(reason, headers, "");
}

String CHttpServer::create_response(String const &reason, String const &body)
{
    return create_response(reason, HeaderList(), body);
}

String CHttpServer::create_response(String const &reason, HeaderList const &hdrs, String const &body)
{
    String response = "HTTP/1.1 ";
    response += reason;
    response += "\r\n";
    
    char buf[50];
    snprintf(buf, sizeof(buf), "%d", m_port);
    
    HeaderList headers;
    headers.push_back(Header("Host", String("127.0.0.1:") + buf));
    headers.push_back(Header("Connection", "close"));
    headers.push_back(HttpHeader("Access-Control-Allow-Origin", "*"));
    std::copy(hdrs.begin(), hdrs.end(), std::back_inserter(headers));
    
    for(HeaderList::const_iterator it = headers.begin(), lim = headers.end();
        it != lim; ++it) {
        response += it->name;
        response += ": ";
        response += it->value;
        response += "\r\n";
    }
    
    response += "\r\n";
    
    response += body;
    
    return response;
}
/**
 * Runs the operation.
 */
void ReadWriteThread::run()
    throw ()
{
    try {
        if (m_card.getType() != MemoryCard::TMemoryCard) {
            m_exception = new ReadWriteException(QObject::tr("There's no memory card in your "
                "reader.\nUse the test function in the configuration\ndialog to set up your "
                "reader properly."), ReadWriteException::CSmartcardError);
            return;
        }

        if (m_write && !m_pin.isNull()) {
            // try to unlock
            qDebug() << CURRENT_FUNCTION << "Trying to unlock the card ...";
            m_card.verify(m_pin);
        }

        if (!m_card.selectFile()) {
            m_exception = new ReadWriteException(QObject::tr("<qt>It was not possible to select the "
                "file on the smartcard</qt>"), ReadWriteException::CSmartcardError);
            return;
        }

        if (m_write) {
            // write the random number
            ByteVector byteVector(1);
            byteVector[0] = m_randomNumber;
            qDebug() << CURRENT_FUNCTION << "Writing random =" << byteVector[0];
            m_card.write(0, byteVector);

            // write the password hash and include a length information
            ByteVector pwHash = PasswordHash::generateHash(m_password);
            pwHash.insert(pwHash.begin(), pwHash.size());
            m_card.write(1, pwHash);

            qDebug() << CURRENT_FUNCTION << "Password hash length = " << (pwHash.size()-1);

            // then write the number of bytes
            int numberOfBytes = m_bytes.size();
            byteVector.resize(3);
            byteVector[0] = (numberOfBytes & 0xFF00) >> 8;
            byteVector[1] = numberOfBytes & 0xFF;
            byteVector[2] = 0; // fillbyte
            m_card.write(PasswordHash::MAX_HASH_LENGTH+2, byteVector);

            // and finally write the data
            m_card.write(PasswordHash::MAX_HASH_LENGTH+5, m_bytes);

        } else {
            // read the random number
            if (m_card.read(0, 1)[0] != m_randomNumber) {
                m_exception = new ReadWriteException(QObject::tr("You inserted the wrong smartcard!"),
                    ReadWriteException::CSmartcardError);
                return;
            }

            qDebug() << CURRENT_FUNCTION << "Read randomNumber =" << m_randomNumber;

            // read the password hash, check the password and throw a exception if necessary
            unsigned char len = m_card.read(1, 1)[0];
            ByteVector pwHash = m_card.read(2, len);

            qDebug() << CURRENT_FUNCTION << "Password hash length =" << len;

            if (!PasswordHash::isCorrect(m_password, pwHash)) {
                m_exception = new ReadWriteException(QObject::tr("The given password was wrong."),
                    ReadWriteException::CWrongPassword);
                return;
            }

            // read the number
            ByteVector vec = m_card.read(PasswordHash::MAX_HASH_LENGTH + 2, 2);
            int numberOfBytes = (vec[0] << 8) + (vec[1]);

            qDebug() << CURRENT_FUNCTION << "Read numberOfBytes =" << numberOfBytes;

            Q_ASSERT(numberOfBytes >= 0);

            // read the bytes
            m_bytes = m_card.read(PasswordHash::MAX_HASH_LENGTH+5, numberOfBytes);
        }
    } catch (const CardException& e) {