var forge=require("./forge");require("./aes"),require("./tls");var tls=module.exports=forge.tls;function initConnectionState(e,t,r){var i=t.entity===forge.tls.ConnectionEnd.client;e.read.cipherState={init:!1,cipher:forge.cipher.createDecipher("AES-CBC",i?r.keys.server_write_key:r.keys.client_write_key),iv:i?r.keys.server_write_IV:r.keys.client_write_IV},e.write.cipherState={init:!1,cipher:forge.cipher.createCipher("AES-CBC",i?r.keys.client_write_key:r.keys.server_write_key),iv:i?r.keys.client_write_IV:r.keys.server_write_IV},e.read.cipherFunction=decrypt_aes_cbc_sha1,e.write.cipherFunction=encrypt_aes_cbc_sha1,e.read.macLength=e.write.macLength=r.mac_length,e.read.macFunction=e.write.macFunction=tls.hmac_sha1}function encrypt_aes_cbc_sha1(e,t){var r,i=!1,n=t.macFunction(t.macKey,t.sequenceNumber,e);e.fragment.putBytes(n),t.updateSequenceNumber(),r=e.version.minor===tls.Versions.TLS_1_0.minor?t.cipherState.init?null:t.cipherState.iv:forge.random.getBytesSync(16),t.cipherState.init=!0;var a=t.cipherState.cipher;return a.start({iv:r}),e.version.minor>=tls.Versions.TLS_1_1.minor&&a.output.putBytes(r),a.update(e.fragment),a.finish(encrypt_aes_cbc_sha1_padding)&&(e.fragment=a.output,e.length=e.fragment.length(),i=!0),i}function encrypt_aes_cbc_sha1_padding(e,t,r){if(!r){var i=e-t.length()%e;t.fillWithByte(i-1,i)}return!0}function decrypt_aes_cbc_sha1_padding(e,t,r){var i=!0;if(r){for(var n=t.length(),a=t.last(),c=n-1-a;c<n-1;++c)i=i&&t.at(c)==a;i&&t.truncate(a+1)}return i}function decrypt_aes_cbc_sha1(e,t){var r,i=!1;r=e.version.minor===tls.Versions.TLS_1_0.minor?t.cipherState.init?null:t.cipherState.iv:e.fragment.getBytes(16),t.cipherState.init=!0;var n=t.cipherState.cipher;n.start({iv:r}),n.update(e.fragment),i=n.finish(decrypt_aes_cbc_sha1_padding);var a=t.macLength,c=forge.random.getBytesSync(a),_=n.output.length();_>=a?(e.fragment=n.output.getBytes(_-a),c=n.output.getBytes(a)):e.fragment=n.output.getBytes(),e.fragment=forge.util.createBuffer(e.fragment),e.length=e.fragment.length();var s=t.macFunction(t.macKey,t.sequenceNumber,e);return t.updateSequenceNumber(),i=compareMacs(t.macKey,c,s)&&i}function compareMacs(e,t,r){var i=forge.hmac.create();return i.start("SHA1",e),i.update(t),t=i.digest().getBytes(),i.start(null,null),i.update(r),t===(r=i.digest().getBytes())}tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA={id:[0,47],name:"TLS_RSA_WITH_AES_128_CBC_SHA",initSecurityParameters:function(e){e.bulk_cipher_algorithm=tls.BulkCipherAlgorithm.aes,e.cipher_type=tls.CipherType.block,e.enc_key_length=16,e.block_length=16,e.fixed_iv_length=16,e.record_iv_length=16,e.mac_algorithm=tls.MACAlgorithm.hmac_sha1,e.mac_length=20,e.mac_key_length=20},initConnectionState:initConnectionState},tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA={id:[0,53],name:"TLS_RSA_WITH_AES_256_CBC_SHA",initSecurityParameters:function(e){e.bulk_cipher_algorithm=tls.BulkCipherAlgorithm.aes,e.cipher_type=tls.CipherType.block,e.enc_key_length=32,e.block_length=16,e.fixed_iv_length=16,e.record_iv_length=16,e.mac_algorithm=tls.MACAlgorithm.hmac_sha1,e.mac_length=20,e.mac_key_length=20},initConnectionState:initConnectionState};