public static function publicEncrypt($data, &$crypted, PKey $key, $padding = OPENSSL_PKCS1_PADDING) { $return = openssl_public_encrypt($data, $crypted, $key->getPublicKey(), $padding); self::handleReturn($return); return $return; }
/** * Tries to establish a association with the op if a store is available. The * method returns null or PSX\OpenId\Provider\Data\Association. Discovery * must be made before calling this method * * @return PSX\OpenId\Provider\Data\Association|null */ private function establishAssociaton($assocType = 'HMAC-SHA256', $sessionType = 'DH-SHA256') { // request association $g = pack('H*', ProviderAbstract::DH_G); $p = pack('H*', ProviderAbstract::DH_P); $pkey = new PKey(array('private_key_type' => OPENSSL_KEYTYPE_DH, 'dh' => array('p' => $p, 'g' => $g))); $details = $pkey->getDetails(); $params = array('openid.ns' => ProviderAbstract::NS, 'openid.mode' => 'associate', 'openid.assoc_type' => $assocType, 'openid.session_type' => $sessionType, 'openid.dh_modulus' => base64_encode(ProviderAbstract::btwoc($details['dh']['p'])), 'openid.dh_gen' => base64_encode(ProviderAbstract::btwoc($details['dh']['g'])), 'openid.dh_consumer_public' => base64_encode(ProviderAbstract::btwoc($details['dh']['pub_key']))); $request = new PostRequest($this->identity->getServer(), array('User-Agent' => __CLASS__ . ' ' . Base::VERSION), $params); $response = $this->http->request($request); if ($response->getStatusCode() == 200) { $data = self::keyValueDecode($response->getBody()); // check values $diff = array_diff(array('ns', 'assoc_handle', 'session_type', 'assoc_type', 'expires_in'), array_keys($data)); if (count($diff) > 0) { throw new Exception('Missing fields ' . implode(', ', $diff)); } if ($data['ns'] != ProviderAbstract::NS) { throw new Exception('Invalid namesspace'); } if (!in_array($data['session_type'], self::$supportedSessionTypes)) { throw new Exception('Invalid session type'); } if (!in_array($data['assoc_type'], self::$supportedAssocTypes)) { throw new Exception('Invalid assoc type'); } // decrypt shared secret if ($data['session_type'] != 'no-encryption') { if (!isset($data['dh_server_public'])) { throw new Exception('DH server public not set'); } if (!isset($data['enc_mac_key'])) { throw new Exception('Encoded mac key not set'); } $dhFunc = str_replace('DH-', '', $data['session_type']); $serverPub = base64_decode($data['dh_server_public']); $dhSec = OpenSsl::dhComputeKey($serverPub, $pkey); $sec = OpenSsl::digest(ProviderAbstract::btwoc($dhSec), $dhFunc, true); $serverSecret = base64_encode($sec ^ base64_decode($data['enc_mac_key'])); } else { if (!isset($data['mac_key'])) { throw new Exception('Mac key not set'); } $dhFunc = null; $serverSecret = $data['mac_key']; } // build association $assoc = new Association(); $assoc->setAssocHandle($data['assoc_handle']); $assoc->setAssocType($data['assoc_type']); $assoc->setSessionType($data['session_type']); $assoc->setSecret($serverSecret); $assoc->setExpire($data['expires_in']); return $assoc; } else { throw new Exception('Could not establish associaton received ' . $response->getStatusCode()); } }
private function generateConsumerRequest() { $g = pack('H*', ProviderAbstract::DH_G); $p = pack('H*', ProviderAbstract::DH_P); $pkey = new PKey(array('private_key_type' => OPENSSL_KEYTYPE_DH, 'dh' => array('p' => $p, 'g' => $g))); $details = $pkey->getDetails(); return array('modulus' => base64_encode(ProviderAbstract::btwoc($details['dh']['p'])), 'gen' => base64_encode(ProviderAbstract::btwoc($details['dh']['g'])), 'consumer_public' => base64_encode(ProviderAbstract::btwoc($details['dh']['pub_key'])), 'pkey' => $pkey); }