private static void GeneratePadding(CipherSuite cipherSuite, Record record)
{
BulkCipherAlgorithmType cipherType = cipherSuite.BulkCipherAlgorithm.Type;
if (cipherType == BulkCipherAlgorithmType.Block) {
int blockSize = cipherSuite.BulkCipherAlgorithm.BlockSize;
// Add the required padding to the end of fragment if necessary,
// minimum padding 1 bytes, the length byte of padding itself
int paddingLength = blockSize - (record.Fragment.Length % blockSize);
if (record.Version.HasVariablePadding) {
// Add 0-1 additional blocks
int extra = (new System.Random()).Next(2);
if (paddingLength + extra*blockSize < 256) {
paddingLength += extra*blockSize;
}
}
// Add the actual padding bytes here
byte[] padding = new byte[paddingLength];
if (record.Version == ProtocolVersion.SSL3_0) {
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(padding);
padding[padding.Length-1] = (byte) (padding.Length-1);
} else {
for (int i=1; i<=padding.Length; i++) {
padding[padding.Length-i] = (byte) (padding.Length-1);
}
}
byte[] fragment = new byte[record.Fragment.Length + padding.Length];
Buffer.BlockCopy(record.Fragment, 0, fragment, 0, record.Fragment.Length);
Buffer.BlockCopy(padding, 0, fragment, record.Fragment.Length, padding.Length);
record.Fragment = fragment;
}
}