123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- var forge = require('./forge');
- require('./aes');
- require('./tls');
- var tls = module.exports = forge.tls;
- tls.CipherSuites['TLS_RSA_WITH_AES_128_CBC_SHA'] = {
- id: [0x00, 0x2f],
- name: 'TLS_RSA_WITH_AES_128_CBC_SHA',
- initSecurityParameters: function(sp) {
- sp.bulk_cipher_algorithm = tls.BulkCipherAlgorithm.aes;
- sp.cipher_type = tls.CipherType.block;
- sp.enc_key_length = 16;
- sp.block_length = 16;
- sp.fixed_iv_length = 16;
- sp.record_iv_length = 16;
- sp.mac_algorithm = tls.MACAlgorithm.hmac_sha1;
- sp.mac_length = 20;
- sp.mac_key_length = 20;
- },
- initConnectionState: initConnectionState
- };
- tls.CipherSuites['TLS_RSA_WITH_AES_256_CBC_SHA'] = {
- id: [0x00, 0x35],
- name: 'TLS_RSA_WITH_AES_256_CBC_SHA',
- initSecurityParameters: function(sp) {
- sp.bulk_cipher_algorithm = tls.BulkCipherAlgorithm.aes;
- sp.cipher_type = tls.CipherType.block;
- sp.enc_key_length = 32;
- sp.block_length = 16;
- sp.fixed_iv_length = 16;
- sp.record_iv_length = 16;
- sp.mac_algorithm = tls.MACAlgorithm.hmac_sha1;
- sp.mac_length = 20;
- sp.mac_key_length = 20;
- },
- initConnectionState: initConnectionState
- };
- function initConnectionState(state, c, sp) {
- var client = (c.entity === forge.tls.ConnectionEnd.client);
-
- state.read.cipherState = {
- init: false,
- cipher: forge.cipher.createDecipher('AES-CBC', client ?
- sp.keys.server_write_key : sp.keys.client_write_key),
- iv: client ? sp.keys.server_write_IV : sp.keys.client_write_IV
- };
- state.write.cipherState = {
- init: false,
- cipher: forge.cipher.createCipher('AES-CBC', client ?
- sp.keys.client_write_key : sp.keys.server_write_key),
- iv: client ? sp.keys.client_write_IV : sp.keys.server_write_IV
- };
- state.read.cipherFunction = decrypt_aes_cbc_sha1;
- state.write.cipherFunction = encrypt_aes_cbc_sha1;
-
- state.read.macLength = state.write.macLength = sp.mac_length;
- state.read.macFunction = state.write.macFunction = tls.hmac_sha1;
- }
- function encrypt_aes_cbc_sha1(record, s) {
- var rval = false;
-
- var mac = s.macFunction(s.macKey, s.sequenceNumber, record);
- record.fragment.putBytes(mac);
- s.updateSequenceNumber();
-
- var iv;
- if(record.version.minor === tls.Versions.TLS_1_0.minor) {
-
-
- iv = s.cipherState.init ? null : s.cipherState.iv;
- } else {
- iv = forge.random.getBytesSync(16);
- }
- s.cipherState.init = true;
-
- var cipher = s.cipherState.cipher;
- cipher.start({iv: iv});
-
- if(record.version.minor >= tls.Versions.TLS_1_1.minor) {
- cipher.output.putBytes(iv);
- }
-
- cipher.update(record.fragment);
- if(cipher.finish(encrypt_aes_cbc_sha1_padding)) {
-
- record.fragment = cipher.output;
- record.length = record.fragment.length();
- rval = true;
- }
- return rval;
- }
- function encrypt_aes_cbc_sha1_padding(blockSize, input, decrypt) {
-
- if(!decrypt) {
-
-
-
- var padding = blockSize - (input.length() % blockSize);
- input.fillWithByte(padding - 1, padding);
- }
- return true;
- }
- function decrypt_aes_cbc_sha1_padding(blockSize, output, decrypt) {
- var rval = true;
- if(decrypt) {
-
- var len = output.length();
- var paddingLength = output.last();
- for(var i = len - 1 - paddingLength; i < len - 1; ++i) {
- rval = rval && (output.at(i) == paddingLength);
- }
- if(rval) {
-
- output.truncate(paddingLength + 1);
- }
- }
- return rval;
- }
- function decrypt_aes_cbc_sha1(record, s) {
- var rval = false;
- var iv;
- if(record.version.minor === tls.Versions.TLS_1_0.minor) {
-
-
- iv = s.cipherState.init ? null : s.cipherState.iv;
- } else {
-
-
- iv = record.fragment.getBytes(16);
- }
- s.cipherState.init = true;
-
- var cipher = s.cipherState.cipher;
- cipher.start({iv: iv});
-
- cipher.update(record.fragment);
- rval = cipher.finish(decrypt_aes_cbc_sha1_padding);
-
-
-
-
- var macLen = s.macLength;
-
-
- var mac = forge.random.getBytesSync(macLen);
-
- var len = cipher.output.length();
- if(len >= macLen) {
- record.fragment = cipher.output.getBytes(len - macLen);
- mac = cipher.output.getBytes(macLen);
- } else {
-
- record.fragment = cipher.output.getBytes();
- }
- record.fragment = forge.util.createBuffer(record.fragment);
- record.length = record.fragment.length();
-
- var mac2 = s.macFunction(s.macKey, s.sequenceNumber, record);
- s.updateSequenceNumber();
- rval = compareMacs(s.macKey, mac, mac2) && rval;
- return rval;
- }
- function compareMacs(key, mac1, mac2) {
- var hmac = forge.hmac.create();
- hmac.start('SHA1', key);
- hmac.update(mac1);
- mac1 = hmac.digest().getBytes();
- hmac.start(null, null);
- hmac.update(mac2);
- mac2 = hmac.digest().getBytes();
- return mac1 === mac2;
- }
|