Previous Text Example,
This article continues to describe the second handshake process as follows:
1. Receive packet processing, such:
1. Read the request data as follows:
(1) uint8 marker = packet. read8 (); where Marker = 0x0b indicates the session startup process.
(2) uint16 time = packet. read16 ();
(3) uint8 id = packet. read8 ();
(4) uint16 size = packet. read16 ();
Note: The id value read here is id = 0x38, indicating initiator initial keying chunk, or iikeying data block.
(5) (uint32 &) Farid = request. read32 (); // read Farid
(6) If (request. read7bitlongvalue ()! = Cookie_size) // compare whether the cookie matches
(7) STD: Map <const uint8 *, Cookie *, comparecookies>: iterator itcookie = _ cookies. find (request. current (); // search for a cookie, which is created in the first handshake phase
(8) read the remaining signature and public key as follows:
Request. Next (cookie_size );
Size_t farsignaturelen = (size_t) request. read7bitlongvalue (); evp_digest (request. Current (), farsignaturelen, (uint8 *)
Peer. ID, null, evp_sha256 (), null );
STD: vector <uint8> publickey (request. read7bitvalue ()-2 );
Request. Next (2); // unknown
Request. readraw (& publickey [0], publickey. Size ());
Size_t farcertificatelen = request. read7bitvalue (); // certificate Length
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8667459
2. Response packet processing is as follows:
1. Based on the above encryption, decryption data, Farid, and some client-related data, create a session as follows:
Session & session = _ gateway. createsession (Farid, peer, pdecryptkey, pencryptkey, cookie );
(Uint32 &) Cookie. ID = session. ID; // serial number
2. Write the signature and public key as follows:
_ Writer. write32 (ID );
_ Writer. write7bitlongvalue (_ nonce. Size ());
_ Writer. writeraw (& _ nonce [0], _ nonce. Size ());
_ Writer. write8 (0x58); // unknown
3. Write typeid and size as follows:
Response. write8 (0x78 );
Response. write16 (response. Length ()-response. Position ()-2 );
Note 1: computing encryption and decryption are as follows:
Uint8 decryptkey [aes_key_size];
Uint8 * pdecryptkey = & decryptkey [0];
Uint8 encryptkey [aes_key_size];
Uint8 * pencryptkey = & encryptkey [0];
Rtmfp: computediffiehellmansecret (_ PDH, initiatorkey, initkeysize, sharedsecret );
Rtmfp: computeasymetrickeys (sharedsecret, initiatornonce, initnoncesize, & _ nonce [0], _ nonce. Size (), decryptkey, encryptkey );
The underlying functions call OpenSSL function interfaces. For more information about the functions and usage of these function interfaces, see the update of this series of texts.
NOTE 2: here we are talking about normal handshakes without considering Exception Handling. To learn how to handle exceptions, please pay attention to the update of this series of texts, which will be discussed later.
Now, the second handshake is over.
Not complete to be continued ~~
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8667459