示例#1
0
 public function decode()
 {
     $core = $this->core;
     list($vMajor, $vMinor) = $core->getVersion();
     // Client
     $connOut = $core->getOutDuplex();
     // Server
     $connIn = $core->getInDuplex();
     // ECDHE
     if ($core->cipherSuite->isECDHEEnabled()) {
         $extensions = $core->extensions;
         $data = $extensions->call('Curve', 'decodeClientKeyExchange', '');
         $preMaster = $extensions->call('Curve', 'getPremaster', null);
     } else {
         $preMaster = Core::_pack('C', $vMajor) . Core::_pack('C', $vMinor) . Core::getRandom(46);
         $crtDers = $core->getCrtDers();
         $publicKey = X509::getPublicKey($crtDers);
         openssl_public_encrypt($preMaster, $encPreMaster, $publicKey);
         $data = Core::_pack('n', strlen($encPreMaster)) . $encPreMaster;
     }
     // Set Master Secret, IV and MAC
     $this->setKeys($preMaster, $connIn, $connOut);
     $this->msgType = HandshakeType::CLIENT_KEY_EXCHANGE;
     $this->length = strlen($data);
     return $this->getBinHeader() . $data;
 }
示例#2
0
 public function decode()
 {
     $core = $this->core;
     $extensions = $core->extensions;
     $connOut = $core->getOutDuplex();
     $sessionID = $core->getSessionID();
     list($vMajor, $vMinor) = $core->getVersion();
     // Set server random
     $connOut->random = Core::getRandom(32);
     $sessionLength = strlen($sessionID);
     $data = Core::_pack('C', $vMajor) . Core::_pack('C', $vMinor) . $connOut->random . Core::_pack('C', $sessionLength);
     if ($sessionLength > 0) {
         $data .= $sessionID;
     }
     $cipherSuite = $core->cipherSuite;
     list($cipher1, $cipher2) = $cipherSuite->getID();
     $data .= Core::_pack('C', $cipher1) . Core::_pack('C', $cipher2);
     // Compression method length
     $data .= Core::_pack('C', 0x0);
     $extData = $extensions->onDecodeServerHello();
     if (strlen($extData) > 0) {
         $data .= Core::_pack('n', strlen($extData)) . $extData;
     }
     $this->msgType = 2;
     $this->length = strlen($data);
     return $this->getBinHeader() . $data;
 }
示例#3
0
 protected static function getZeroSeq()
 {
     $seq = [];
     for ($i = 0; $i < 8; $i++) {
         $seq[$i] = Core::_pack('C', 0);
     }
     return $seq;
 }
示例#4
0
 public function onDecodeClientHello()
 {
     $sigData = '';
     foreach (self::$supportedAlgorithmList as $algorithm) {
         $sigData .= Core::_pack('C', $algorithm >> 8) . Core::_pack('C', $algorithm & 0xff);
     }
     $sigData = Core::_pack('n', strlen($sigData)) . $sigData;
     $this->extType = TLSExtensions::TYPE_SIGNATURE_ALGORITHM;
     $this->length = strlen($sigData);
     $data = $this->decodeHeader() . $sigData;
     return $data;
 }
示例#5
0
 public function decode()
 {
     $core = $this->core;
     $crtDers = $core->getCrtDers();
     $crtData = '';
     foreach ($crtDers as $crtDer) {
         $crtLength = strlen($crtDer);
         // Cert Length
         $crtData .= Core::_pack('C', 0x0) . Core::_pack('n', $crtLength) . $crtDer;
     }
     $data = Core::_pack('C', 0x0) . Core::_pack('n', strlen($crtData)) . $crtData;
     $this->msgType = HandshakeType::CERTIFICATE;
     $this->length = strlen($data);
     return $this->getBinHeader() . $data;
 }
示例#6
0
文件: Alert.php 项目: rnaga/php-tls
 public function decode()
 {
     return Core::_pack('C', $this->level) . Core::_pack('C', $this->descCode);
 }
示例#7
0
文件: EcDH.php 项目: rnaga/php-tls
 public function getPublicKey()
 {
     $privateKey = $this->getPrivateKey();
     $this->publicKey = $publicKey = $privateKey->getPublicKey();
     $publicPoint = $publicKey->getPoint();
     // Convert to binary - Uncompressed
     $publicKeyBin = Core::_pack('C', 0x4) . gmp_export($publicPoint->getX(), 1, GMP_BIG_ENDIAN) . gmp_export($publicPoint->getY(), 1, GMP_BIG_ENDIAN);
     return $publicKeyBin;
 }
示例#8
0
 public function calculateMAC()
 {
     $conn = $this->conn;
     $core = $conn->getCore();
     $cipherSuite = $core->cipherSuite;
     list($vMajor, $vMinor) = $core->getVersion();
     if (is_null($this->seq)) {
         $this->seq = self::getZeroSeq();
     }
     $secretMAC = $conn->MAC;
     $contentType = Core::_pack('C', $this->contentType);
     $major = Core::_pack('C', $vMajor);
     $minor = Core::_pack('C', $vMinor);
     $length = Core::_pack('n', strlen($this->payload));
     /*
      * https://tools.ietf.org/html/rfc5246#section-6.2.3.1
      *
      * The MAC is generated as:
      *
      * MAC(MAC_write_key, seq_num +
      *                    TLSCompressed.type +
      *                    TLSCompressed.version +
      *                    TLSCompressed.length +
      *                    TLSCompressed.fragment);
      */
     $concat = implode('', $this->seq) . $contentType . $major . $minor . $length . $this->payload;
     //$macStr = $cipherSuite->hashHmac($concat, $secretMAC, false );
     $mac = $cipherSuite->hashHmac($concat, $secretMAC);
     return $mac;
 }
示例#9
0
 /**
  * Additional Authentication Data
  */
 public function getAAD($length)
 {
     $conn = $this->conn;
     $core = $conn->getCore();
     $cipherSuite = $core->cipherSuite;
     list($vMajor, $vMinor) = $core->getVersion();
     if (is_null($this->seq)) {
         $this->seq = self::getZeroSeq();
     }
     $contentType = Core::_pack('C', $this->contentType);
     $major = Core::_pack('C', $vMajor);
     $minor = Core::_pack('C', $vMinor);
     $length = Core::_pack('n', $length);
     /*
      * https://tools.ietf.org/html/rfc5246#section-6.2.3.3
      *
      *  additional_data = seq_num + TLSCompressed.type +
      *               TLSCompressed.version + TLSCompressed.length;
      *
      */
     $concat = implode('', $this->seq) . $contentType . $major . $minor . $length;
     return $concat;
 }
示例#10
0
 public function decode()
 {
     $core = $this->core;
     $connOut = $core->getOutDuplex();
     list($vMajor, $vMinor) = $core->getVersion();
     // Set client random
     $connOut->random = Core::getRandom(32);
     // Set TLS Version
     $data = Core::_pack('C', $vMajor) . Core::_pack('C', $vMinor);
     // Client Random
     $data .= $connOut->random;
     // Session ID - no session
     $data .= Core::_pack('C', 0x0);
     // Cipher Suite
     $cipherSuiteList = CipherSuites::decodeCipherList();
     $data .= Core::_pack('n', strlen($cipherSuiteList)) . $cipherSuiteList;
     // Compression method
     $data .= Core::_pack('C', 0x1) . Core::_pack('C', $core->getCompressionMethod());
     // Extension Length
     //$data .= Core::_pack('n', 0x00);
     $extensionData = $core->extensions->onDecodeClientHello();
     $data .= Core::_pack('n', strlen($extensionData)) . $extensionData;
     $this->msgType = HandshakeType::CLIENT_HELLO;
     $this->length = strlen($data);
     return $this->getBinHeader() . $data;
 }
示例#11
0
 public static function decodeCipherList()
 {
     $data = '';
     foreach (self::$enabledCipherSuites as $val) {
         $data .= Core::_pack('C', $val >> 8) . Core::_pack('C', $val & 0xff);
     }
     return $data;
 }
示例#12
0
 protected function decodeHeader()
 {
     // MsgType
     $header = Core::_pack('C', 0) . Core::_pack('C', $this->extType) . Core::_pack('n', $this->length);
     return $header;
 }
示例#13
0
文件: Curve.php 项目: rnaga/php-tls
 public function decodeServerKeyExchange()
 {
     $core = $this->core;
     $extensions = $core->extensions;
     $protoVersion = $core->getProtocolVersion();
     /*
      * ECCurveType
      *
      * We only support named curves, which is 0x03 
      *
      * enum { explicit_prime (1), explicit_char2 (2),
      *        named_curve (3), reserved(248..255) } ECCurveType;
      */
     $data = Core::_pack('C', 0x3);
     // Named curve type
     $data .= Core::_pack('n', $this->namedCurveType);
     // ECDH Public Key
     $this->ecdh = new EcDH($this->namedCurveType);
     $dataPublicKey = $this->ecdh->getPublicKey();
     $data .= Core::_pack('C', strlen($dataPublicKey)) . $dataPublicKey;
     /*
      * Signature
      * 
      * https://tools.ietf.org/html/rfc4492 Page 19
      * signed_params:   A hash of the params, with the signature appropriate
      * to that hash applied.  The private key corresponding to the
      * certified public key in the server's Certificate message is used
      * for signing.
      *
      * ServerKeyExchange.signed_params.sha_hash
      *    SHA(ClientHello.random + ServerHello.random +
      *                                      ServerKeyExchange.params);
      */
     $connIn = $core->getInDuplex();
     $connOut = $core->getOutDuplex();
     $dataSign = $connIn->random . $connOut->random . $data;
     $signature = $extensions->call('SignatureAlgorithm', 'getSignature', null, $dataSign);
     if ($protoVersion >= 32) {
         // Signature Hash Alogorithm
         // [null, null] never happens
         list($hash, $sig) = $extensions->call('SignatureAlgorithm', 'getAlgorithm', [null, null]);
         $data .= Core::_pack('C', $hash) . Core::_pack('C', $sig);
     }
     // Append signature
     $data .= Core::_pack('n', strlen($signature)) . $signature;
     $hs = HandShakeFactory::getInstance($core, HandshakeType::SERVER_KEY_EXCHANGE);
     $hs->setMsgType(HandshakeType::SERVER_KEY_EXCHANGE);
     $hs->set('length', strlen($data));
     return $hs->getBinHeader() . $data;
 }
示例#14
0
文件: Record.php 项目: rnaga/php-tls
 public function decode()
 {
     $core = $this->getCore();
     list($vMajor, $vMinor) = $core->getVersion();
     // type
     $data = Core::_pack('C', $this->contentType) . Core::_pack('C', $vMajor) . Core::_pack('C', $vMinor) . Core::_pack('n', $this->length) . $this->payload;
     // Handshake
     if ($this->contentType == ContentType::HANDSHAKE && !$this->conn->isCipherChanged) {
         $core->countHandshakeMessages($this->payload);
     }
     $this->reset();
     return $data;
 }
示例#15
0
 public function getBinHeader()
 {
     // MsgType
     $header = Core::_pack('C', $this->msgType) . Core::_pack('C', 0x0) . Core::_pack('n', $this->length);
     return $header;
 }
示例#16
0
 public function decode()
 {
     return Core::_pack('C', 0x1);
 }