Thread: [Opentnl-cvs] tnl/tnl netConnection.cpp,1.13,1.14 netInterface.cpp,1.10,1.11 symmetricCipher.cpp,1.3
Brought to you by:
mark_frohnmayer,
s_alanet
From: Mark F. <mar...@us...> - 2005-04-28 20:43:14
|
Update of /cvsroot/opentnl/tnl/tnl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5682/tnl Modified Files: netConnection.cpp netInterface.cpp symmetricCipher.cpp tnlNetConnection.h Log Message: Fixed Zap dedicated server crash when MOTD arrives Symmetric cipher can be created from any size byte buffer (not all zeroes if size isn't exactly right) Arranged NetConnection can now use the arranged secret data as the shared secret data for encryption purposes Index: symmetricCipher.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/symmetricCipher.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** symmetricCipher.cpp 28 Apr 2005 17:30:30 -0000 1.3 --- symmetricCipher.cpp 28 Apr 2005 20:42:55 -0000 1.4 *************** *** 41,52 **** } SymmetricCipher::SymmetricCipher(const ByteBuffer *theByteBuffer) { if(theByteBuffer->getBufferSize() != KeySize * 2) { ! U8 buffer[KeySize]; ! memset(buffer, 0, KeySize); rijndael_setup(buffer, KeySize, 0, (symmetric_key *) &mSymmetricKey); ! memcpy(mInitVector, buffer, BlockSize); } else --- 41,72 ---- } + static void copyWrapBuffer(U8 *destBuffer, U32 destSize, const U8 *srcBuffer, U32 srcSize) + { + if(!srcSize) + { + memset(destBuffer, 0, destSize); + return; + } + U32 copyPos = 0; + while(copyPos < destSize) + { + U32 copyBytes = getMin(destSize - copyPos, srcSize); + memcpy(destBuffer + copyPos, srcBuffer, copyBytes); + copyPos += copyBytes; + } + } + SymmetricCipher::SymmetricCipher(const ByteBuffer *theByteBuffer) { + // if the buffer passed isn't exactly the right size, + // wrap copy it into the dest buffer and init vector. if(theByteBuffer->getBufferSize() != KeySize * 2) { ! U8 buffer[KeySize * 2]; ! memset(buffer, 0, KeySize * 2); ! memcpy(buffer, theByteBuffer->getBuffer(), getMin(U32(KeySize * 2), theByteBuffer->getBufferSize())); ! rijndael_setup(buffer, KeySize, 0, (symmetric_key *) &mSymmetricKey); ! memcpy(mInitVector, buffer + KeySize, BlockSize); } else Index: tnlNetConnection.h =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/tnlNetConnection.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** tnlNetConnection.h 21 Feb 2005 07:48:14 -0000 1.10 --- tnlNetConnection.h 28 Apr 2005 20:42:55 -0000 1.11 *************** *** 92,95 **** --- 92,96 ---- bool mIsArranged; ///< True if this is an arranged connection bool mUsingCrypto; ///< Set to true if this connection is using crypto (public key and symmetric) + bool mUseArrangedSecretAsSharedSecret; ///< Set to true if the arranged connection secret data should be used as the symmetric key for symmetric crypto bool mPuzzleRetried; ///< True if a puzzle solution was already rejected by the server once. Nonce mNonce; ///< Unique nonce generated for this connection to send to the server. *************** *** 117,120 **** --- 118,122 ---- mPuzzleRetried = false; mUsingCrypto = false; + mUseArrangedSecretAsSharedSecret = false; mIsArranged = false; #ifdef TNL_DEBUG *************** *** 335,339 **** /// Connects to a remote host that is also connecting to this connection (negotiated by a third party) ! void connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &myNonce, Nonce &remoteNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange = false, bool requestsCertificate = false); /// Sends a disconnect packet to notify the remote host that this side is terminating the connection for the specified reason. --- 337,341 ---- /// Connects to a remote host that is also connecting to this connection (negotiated by a third party) ! void connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &myNonce, Nonce &remoteNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange = false, bool requestsCertificate = false, bool useArrangedSecretAsSharedSecret = false); /// Sends a disconnect packet to notify the remote host that this side is terminating the connection for the specified reason. Index: netConnection.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/netConnection.cpp,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** netConnection.cpp 21 Feb 2005 07:48:14 -0000 1.13 --- netConnection.cpp 28 Apr 2005 20:42:55 -0000 1.14 *************** *** 799,803 **** } ! void NetConnection::connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &nonce, Nonce &serverNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange, bool requestsCertificate) { mConnectionParameters.mRequestKeyExchange = requestsKeyExchange; --- 799,803 ---- } ! void NetConnection::connectArranged(NetInterface *connectionInterface, const Vector<Address> &possibleAddresses, Nonce &nonce, Nonce &serverNonce, ByteBufferPtr sharedSecret, bool isInitiator, bool requestsKeyExchange, bool requestsCertificate, bool useArrangedSecretAsSharedSecret) { mConnectionParameters.mRequestKeyExchange = requestsKeyExchange; *************** *** 810,813 **** --- 810,816 ---- mConnectionParameters.mArrangedSecret = sharedSecret; mConnectionParameters.mArrangedSecret->takeOwnership(); + mConnectionParameters.mUseArrangedSecretAsSharedSecret = useArrangedSecretAsSharedSecret; + if(useArrangedSecretAsSharedSecret) + mConnectionParameters.mSharedSecret = mConnectionParameters.mArrangedSecret; setInterface(connectionInterface); Index: netInterface.cpp =================================================================== RCS file: /cvsroot/opentnl/tnl/tnl/netInterface.cpp,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** netInterface.cpp 28 Apr 2005 17:30:30 -0000 1.10 --- netInterface.cpp 28 Apr 2005 20:42:55 -0000 1.11 *************** *** 1076,1084 **** else theParams.mPrivateKey = mPrivateKey; theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); //logprintf("shared secret (client) %s", theParams.mSharedSecret->encodeBase64()->getBuffer()); Random::read(theParams.mSymmetricKey, SymmetricCipher::KeySize); - theParams.mUsingCrypto = true; } conn->setNetAddress(theAddress); TNLLogMessageV(LogNetInterface, ("Punch from %s matched nonces - connecting...", theAddress.toString())); --- 1076,1091 ---- else theParams.mPrivateKey = mPrivateKey; + theParams.mUsingCrypto = true; theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); + } + else if(theParams.mUseArrangedSecretAsSharedSecret) + theParams.mUsingCrypto = true; + + if(theParams.mUsingCrypto) + { //logprintf("shared secret (client) %s", theParams.mSharedSecret->encodeBase64()->getBuffer()); Random::read(theParams.mSymmetricKey, SymmetricCipher::KeySize); } + conn->setNetAddress(theAddress); TNLLogMessageV(LogNetInterface, ("Punch from %s matched nonces - connecting...", theAddress.toString())); *************** *** 1108,1114 **** if(out.writeFlag(theParams.mUsingCrypto)) { ! out.write(theParams.mPrivateKey->getPublicKey()); ! innerEncryptPos = out.getBytePosition(); ! out.setBytePosition(innerEncryptPos); out.write(SymmetricCipher::KeySize, theParams.mSymmetricKey); } --- 1115,1127 ---- if(out.writeFlag(theParams.mUsingCrypto)) { ! if(!theParams.mUseArrangedSecretAsSharedSecret) ! { ! // if this arranged connection doesn't already have the shared ! // secret (from the arranged secret), send the public key ! // to the remote host. ! out.write(theParams.mPrivateKey->getPublicKey()); ! innerEncryptPos = out.getBytePosition(); ! out.setBytePosition(innerEncryptPos); ! } out.write(SymmetricCipher::KeySize, theParams.mSymmetricKey); } *************** *** 1187,1207 **** if(stream->readFlag()) { - if(mPrivateKey.isNull()) - return; theParams.mUsingCrypto = true; ! theParams.mPublicKey = new AsymmetricKey(stream); ! theParams.mPrivateKey = mPrivateKey; ! ! U32 decryptPos = stream->getBytePosition(); ! stream->setBytePosition(decryptPos); ! theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); ! SymmetricCipher theCipher(theParams.mSharedSecret); ! ! if(!stream->decryptAndCheckHash(NetConnection::MessageSignatureBytes, decryptPos, &theCipher)) ! return; // now read the first part of the connection's session (symmetric) key stream->read(SymmetricCipher::KeySize, theParams.mSymmetricKey); Random::read(theParams.mInitVector, SymmetricCipher::KeySize); } --- 1200,1228 ---- if(stream->readFlag()) { theParams.mUsingCrypto = true; ! if(!theParams.mUseArrangedSecretAsSharedSecret) ! { ! if(mPrivateKey.isNull()) ! return; ! theParams.mPublicKey = new AsymmetricKey(stream); ! theParams.mPrivateKey = mPrivateKey; + U32 decryptPos = stream->getBytePosition(); + stream->setBytePosition(decryptPos); + theParams.mSharedSecret = theParams.mPrivateKey->computeSharedSecretKey(theParams.mPublicKey); + SymmetricCipher theCipher(theParams.mSharedSecret); + + if(!stream->decryptAndCheckHash(NetConnection::MessageSignatureBytes, decryptPos, &theCipher)) + return; + } // now read the first part of the connection's session (symmetric) key stream->read(SymmetricCipher::KeySize, theParams.mSymmetricKey); + } + if(theParams.mUsingCrypto) + { + // if this connection is using crypto, it is the connection + // receiver who computes the InitVector for the symmetric cipher. Random::read(theParams.mInitVector, SymmetricCipher::KeySize); + conn->setSymmetricCipher(new SymmetricCipher(theParams.mSymmetricKey, theParams.mInitVector)); } *************** *** 1216,1221 **** conn->setNetAddress(theAddress); conn->setInitialRecvSequence(connectSequence); - if(theParams.mUsingCrypto) - conn->setSymmetricCipher(new SymmetricCipher(theParams.mSymmetricKey, theParams.mInitVector)); const char *errorString = NULL; --- 1237,1240 ---- |