C# Class Universal.Torrent.Client.Encryption.EncryptedSocket

The class that handles.Message Stream Encryption for a connection
Inheritance: IEncryptor.IEncryptor
Mostra file Open project: haroldma/Universal.Torrent

Protected Properties

Property Type Description
AsyncResult AsyncResult
CryptoProvide byte[]
CryptoSelect byte[]
InitialPayload byte[]
PadC byte[]
PadD byte[]
RemoteInitialPayload byte[]
S byte[]
Skey InfoHash
VerificationConstant byte[]

Public Methods

Method Description
AddPayload ( byte buffer ) : void
AddPayload ( byte buffer, int offset, int count ) : void
BeginHandshake ( IConnection socket, AsyncCallback callback, object state ) : IAsyncResult

Begins the message stream encryption handshaking process

BeginHandshake ( IConnection socket, byte initialBuffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult

Begins the message stream encryption handshaking process, beginning with some data already received from the socket.

Decrypt ( byte data, int offset, int length ) : void

Decrypts some data (should only be called after onEncryptorReady)

Encrypt ( byte data, int offset, int length ) : void

Encrypts some data (should only be called after onEncryptorReady)

EncryptedSocket ( EncryptionTypes allowedEncryption ) : System
EndHandshake ( IAsyncResult result ) : void

Protected Methods

Method Description
Combine ( ) : byte[]

Concatenates several byte buffers

CreateCryptors ( string encryptionSalt, string decryptionSalt ) : void

Instantiate the cryptors with the keys: Hash(encryptionSalt, S, SKEY) for the encryptor and Hash(encryptionSalt, S, SKEY) for the decryptor. (encryptionSalt should be "keyA" if you're A, "keyB" if you're B, and reverse for decryptionSalt)

DeLen ( byte data ) : int

Converts a 2-byte big endian integer into an int (reverses operation of Len())

DoDecrypt ( byte data ) : byte[]

Decrypts some data with the RC4 encryptor used in handshaking

DoDecrypt ( byte data, int offset, int length ) : void

Decrypts some data with the RC4 decryptor used in handshaking

DoEncrypt ( byte data ) : byte[]
DoEncrypt ( byte data, int offset, int length ) : void

Encrypts some data with the RC4 encryptor used in handshaking

DoneReceiveY ( ) : void
DoneSynchronize ( ) : void
FillSynchronizeBytes ( bool succeeded, int count, object state ) : void
GeneratePad ( ) : byte[]

Returns a 0 to 512 byte 0-filled pad.

Hash ( ) : byte[]

Hash some data with SHA1

Len ( byte data ) : byte[]

Returns a 2-byte buffer with the length of data

Ready ( ) : void

Signal that the cryptor is now in a state ready to encrypt and decrypt payload data

ReceiveMessage ( byte buffer, int length, AsyncCallback callback ) : void
ReceiveY ( ) : void

Receive the first 768 bits of the transmission from the remote client, which is Y in the protocol (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")

SelectCrypto ( byte remoteCryptoBytes, bool replace ) : int

Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.

SendMessage ( byte toSend ) : void
SendY ( ) : void

Send Y to the remote client, with a random padding that is 0 to 512 bytes long (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")

SetMinCryptoAllowed ( EncryptionTypes allowedEncryption ) : void
Synchronize ( byte syncData, int syncStopPoint ) : void

Read data from the socket until the byte string in syncData is read, or until syncStopPoint is reached (in that case, there is an EncryptionError). (Either "3 A->B: HASH('req1', S)" or "4 B->A: ENCRYPT(VC)")

Private Methods

Method Description
DoneReceive ( bool succeeded, int count, object state ) : void
DoneSend ( bool succeeded, int count, object state ) : void
GenerateX ( ) : void

Generate a 160 bit random number for X

GenerateY ( ) : void

Calculate 2^X mod P

RandomNumber ( int max ) : int

Method Details

AddPayload() public method

public AddPayload ( byte buffer ) : void
buffer byte
return void

AddPayload() public method

public AddPayload ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void

BeginHandshake() public method

Begins the message stream encryption handshaking process
public BeginHandshake ( IConnection socket, AsyncCallback callback, object state ) : IAsyncResult
socket IConnection The socket to perform handshaking with
callback AsyncCallback The callback.
state object The state.
return IAsyncResult

BeginHandshake() public method

Begins the message stream encryption handshaking process, beginning with some data already received from the socket.
public BeginHandshake ( IConnection socket, byte initialBuffer, int offset, int count, AsyncCallback callback, object state ) : IAsyncResult
socket IConnection The socket to perform handshaking with
initialBuffer byte Buffer containing soome data already received from the socket
offset int Offset to begin reading in initialBuffer
count int Number of bytes to read from initialBuffer
callback AsyncCallback The callback.
state object The state.
return IAsyncResult

Combine() protected method

Concatenates several byte buffers
protected Combine ( ) : byte[]
return byte[]

CreateCryptors() protected method

Instantiate the cryptors with the keys: Hash(encryptionSalt, S, SKEY) for the encryptor and Hash(encryptionSalt, S, SKEY) for the decryptor. (encryptionSalt should be "keyA" if you're A, "keyB" if you're B, and reverse for decryptionSalt)
protected CreateCryptors ( string encryptionSalt, string decryptionSalt ) : void
encryptionSalt string The salt to calculate the encryption key with
decryptionSalt string The salt to calculate the decryption key with
return void

DeLen() protected method

Converts a 2-byte big endian integer into an int (reverses operation of Len())
protected DeLen ( byte data ) : int
data byte 2 byte buffer
return int

Decrypt() public method

Decrypts some data (should only be called after onEncryptorReady)
public Decrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin decryption
length int The length.
return void

DoDecrypt() protected method

Decrypts some data with the RC4 encryptor used in handshaking
protected DoDecrypt ( byte data ) : byte[]
data byte Buffers with the data to decrypt
return byte[]

DoDecrypt() protected method

Decrypts some data with the RC4 decryptor used in handshaking
protected DoDecrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin decryption
length int The length.
return void

DoEncrypt() protected method

protected DoEncrypt ( byte data ) : byte[]
data byte
return byte[]

DoEncrypt() protected method

Encrypts some data with the RC4 encryptor used in handshaking
protected DoEncrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin encryption
length int The length.
return void

DoneReceiveY() protected method

protected DoneReceiveY ( ) : void
return void

DoneSynchronize() protected method

protected DoneSynchronize ( ) : void
return void

Encrypt() public method

Encrypts some data (should only be called after onEncryptorReady)
public Encrypt ( byte data, int offset, int length ) : void
data byte The data.
offset int Offset to begin encryption
length int The length.
return void

EncryptedSocket() public method

public EncryptedSocket ( EncryptionTypes allowedEncryption ) : System
allowedEncryption EncryptionTypes
return System

EndHandshake() public method

public EndHandshake ( IAsyncResult result ) : void
result IAsyncResult
return void

FillSynchronizeBytes() protected method

protected FillSynchronizeBytes ( bool succeeded, int count, object state ) : void
succeeded bool
count int
state object
return void

GeneratePad() protected method

Returns a 0 to 512 byte 0-filled pad.
protected GeneratePad ( ) : byte[]
return byte[]

Hash() protected method

Hash some data with SHA1
protected Hash ( ) : byte[]
return byte[]

Len() protected method

Returns a 2-byte buffer with the length of data
protected Len ( byte data ) : byte[]
data byte
return byte[]

Ready() protected method

Signal that the cryptor is now in a state ready to encrypt and decrypt payload data
protected Ready ( ) : void
return void

ReceiveMessage() protected method

protected ReceiveMessage ( byte buffer, int length, AsyncCallback callback ) : void
buffer byte
length int
callback AsyncCallback
return void

ReceiveY() protected method

Receive the first 768 bits of the transmission from the remote client, which is Y in the protocol (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")
protected ReceiveY ( ) : void
return void

SelectCrypto() protected method

Sets CryptoSelect and initializes the stream encryptor and decryptor based on the selected method.
protected SelectCrypto ( byte remoteCryptoBytes, bool replace ) : int
remoteCryptoBytes byte /// The cryptographic methods supported/wanted by the remote client in CryptoProvide /// format. The highest order one available will be selected ///
replace bool if set to true [replace].
return int

SendMessage() protected method

protected SendMessage ( byte toSend ) : void
toSend byte
return void

SendY() protected method

Send Y to the remote client, with a random padding that is 0 to 512 bytes long (Either "1 A->B: Diffie Hellman Ya, PadA" or "2 B->A: Diffie Hellman Yb, PadB")
protected SendY ( ) : void
return void

SetMinCryptoAllowed() protected method

protected SetMinCryptoAllowed ( EncryptionTypes allowedEncryption ) : void
allowedEncryption EncryptionTypes
return void

Synchronize() protected method

Read data from the socket until the byte string in syncData is read, or until syncStopPoint is reached (in that case, there is an EncryptionError). (Either "3 A->B: HASH('req1', S)" or "4 B->A: ENCRYPT(VC)")
protected Synchronize ( byte syncData, int syncStopPoint ) : void
syncData byte Buffer with the data to synchronize to
syncStopPoint int /// Maximum number of bytes (measured from the total received from the socket since connection) /// to read before giving up ///
return void

Property Details

AsyncResult protected_oe property

protected AsyncResult AsyncResult
return AsyncResult

CryptoProvide protected_oe property

protected byte[] CryptoProvide
return byte[]

CryptoSelect protected_oe property

protected byte[] CryptoSelect
return byte[]

InitialPayload protected_oe property

protected byte[] InitialPayload
return byte[]

PadC protected_oe property

protected byte[] PadC
return byte[]

PadD protected_oe property

protected byte[] PadD
return byte[]

RemoteInitialPayload protected_oe property

protected byte[] RemoteInitialPayload
return byte[]

S protected_oe property

protected byte[] S
return byte[]

Skey protected_oe property

protected InfoHash Skey
return InfoHash

VerificationConstant protected_oe property

protected byte[] VerificationConstant
return byte[]