def calFinish(self, finishedLabel): msg = md5(self.cumulatedmsg).digest() + sha1(self.cumulatedmsg).digest() lfinished = [0]*12 prfForVersion(self.params.negotiated.version, lfinished, self.crypto.session.master_secret, finishedLabel, msg) finished = ''.join(lfinished) prefix = "1400000c".decode('hex') return prefix + finished
def keysFromPreMasterSecret(self): version = self.params.negotiated.version clientRandom = self.crypto.session.randombytes.client serverRandom = self.crypto.session.randombytes.server preMasterSecret = self.crypto.session.premaster_secret macLen = self.crypto.session.key.length.mac keyLen = self.crypto.session.key.length.encryption ivLen = self.crypto.session.key.length.iv export = self.params.negotiated.isexport masterSecretLabel = "master secret" keyExpansionLabel = "key expansion" #print 'version', version.encode('hex') #print 'clientrandom', clientRandom.encode('hex') #print 'serverrandom', serverRandom.encode('hex') #print macLen, keyLen, ivLen seed = clientRandom+serverRandom mastersecret = [0]*48 prfForVersion(version,mastersecret,preMasterSecret,masterSecretLabel,seed) masterSecret = ''.join(mastersecret) seed = serverRandom+clientRandom n = 2*macLen + 2*keyLen + 2*ivLen keyBlock = [0]*n prfForVersion(version,keyBlock,masterSecret,keyExpansionLabel,seed) i=0 clientMAC = keyBlock[i:i+macLen] clientMAC = ''.join(clientMAC) i+= macLen serverMAC = keyBlock[i:i+macLen] serverMAC = ''.join(serverMAC) i+=macLen clientKey = keyBlock[i:i+keyLen] clientKey = ''.join(clientKey) i+=keyLen serverKey = keyBlock[i:i+keyLen] serverKey = ''.join(serverKey) i+=keyLen clientIV = [0]*ivLen serverIV = [0]*ivLen if not export: #non-export clientIV = keyBlock[i:i+ivLen] clientIV = ''.join(clientIV) i+=ivLen serverIV = keyBlock[i:i+ivLen] serverIV = ''.join(serverIV) else: fclientKey = [0]*16 prfForVersion(version, fclientKey, clientKey, "client write key", clientRandom+serverRandom) fserverKey = [0]*16 prfForVersion(version, fserverKey, serverKey, "server write key", clientRandom+serverRandom) clientKey = ''.join(fclientKey) serverKey = ''.join(fserverKey) ivBlock = [0]*2*ivLen prfForVersion(version, ivBlock, "", "IV block", clientRandom+serverRandom) clientIV = ''.join(ivBlock[:ivLen]) serverIV = ''.join(ivBlock[ivLen: 2*ivLen]) self.crypto.session.master_secret = masterSecret self.crypto.session.key.server.mac = serverMAC self.crypto.session.key.server.encryption = serverKey self.crypto.session.key.server.iv = serverIV self.crypto.session.key.client.mac = clientMAC self.crypto.session.key.client.encryption = clientKey self.crypto.session.key.client.iv = clientIV