RSAKeyImpl::ByteVec RSAKeyImpl::convertToByteVec(const BIGNUM* bn) { int numBytes = BN_num_bytes(bn); ByteVec byteVector(numBytes); ByteVec::value_type* buffer = new ByteVec::value_type[numBytes]; BN_bn2bin(bn, buffer); for (int i = 0; i < numBytes; ++i) byteVector[i] = buffer[i]; delete [] buffer; return byteVector; }
/** * 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) {