precedence; } /** * @param int $precedence * * @throws \InvalidArgumentException */ public function setPrecedence(int $precedence): void { if (!Validator::isUnsignedInteger($precedence, 8)) { throw new \InvalidArgumentException('IPSECKEY precedence must be an 8-bit integer.'); } $this->precedence = $precedence; } /** * @return int */ public function getGatewayType(): int { return $this->gatewayType; } /** * @return int */ public function getAlgorithm(): int { return $this->algorithm; } /** * @param int $algorithm * * @throws \InvalidArgumentException */ private function setAlgorithm(int $algorithm): void { if (!Validator::isUnsignedInteger($algorithm, 8)) { throw new \InvalidArgumentException('IPSECKEY algorithm type must be an 8-bit integer.'); } $this->algorithm = $algorithm; } /** * @return string|null */ public function getGateway(): ?string { return $this->gateway; } /** * @param string|null $gateway either &null for no gateway, a fully qualified domain name, or an IPv4 or IPv6 address * * @throws \InvalidArgumentException */ public function setGateway(?string $gateway): void { if (null === $gateway || '.' === $gateway) { $gateway = null; $this->gatewayType = 0; } elseif (Validator::ipv4($gateway)) { $this->gatewayType = 1; } elseif (Validator::ipv6($gateway)) { $this->gatewayType = 2; } elseif (Validator::fullyQualifiedDomainName($gateway)) { $this->gatewayType = 3; } else { throw new \InvalidArgumentException('The gateway must be a fully qualified domain name, null, or a valid IPv4 or IPv6 address.'); } $this->gateway = $gateway; } /** * @return string|null base64 encoded public key */ public function getPublicKey(): ?string { return $this->publicKey; } /** * @param int $algorithm either IPSECKEY::ALGORITHM_NONE, IPSECKEY::ALGORITHM_DSA, IPSECKEY::ALGORITHM_RSA, or IPSECKEY::ALGORITHM_ECDSA * @param string|null $publicKey base64 encoded public key * * @throws \InvalidArgumentException */ public function setPublicKey(int $algorithm, ?string $publicKey): void { if (null === $publicKey) { $this->publicKey = null; $this->setAlgorithm(0); return; } if (!Validator::isBase64Encoded($publicKey)) { throw new \InvalidArgumentException('The public key must be a valid base64 encoded string.'); } $this->publicKey = (string) preg_replace('/[^a-zA-Z0-9\/+=]/', '', $publicKey); $this->setAlgorithm($algorithm); } /** * {@inheritdoc} */ public function toText(): string { $vars = [$this->precedence, $this->gatewayType, $this->algorithm, $this->gateway, $this->publicKey]; if (0 === $this->gatewayType) { $vars[3] = '.'; } if (0 === $this->algorithm) { unset($vars[4]); } return implode(Tokens::SPACE, $vars); } /** * {@inheritdoc} */ public function toWire(): string { $wire = pack('CCC', $this->precedence, $this->gatewayType, $this->algorithm); if (1 === $this->gatewayType || 2 === $this->gatewayType) { if (null === $this->gateway) { throw new \RuntimeException('Gateway cannot be null if IPSECKEY::gatewayType > 0.'); } $wire .= inet_pton($this->gateway); } else { $wire .= RdataTrait::encodeName($this->gateway ?? '.'); } if (self::ALGORITHM_NONE !== $this->algorithm && null !== $this->publicKey) { $wire .= base64_decode($this->publicKey); } return $wire; } /** * {@inheritdoc} * * @return IPSECKEY */ public static function fromText(string $text): RdataInterface { $rdata = explode(Tokens::SPACE, $text); $ipseckey = new self(); $ipseckey->setPrecedence((int) array_shift($rdata)); array_shift($rdata); //Gateway type is inferred from setGateway. $algorithm = (int) array_shift($rdata); $ipseckey->setGateway((string) array_shift($rdata)); $publicKey = (0 === $algorithm) ? null : implode('', $rdata); $ipseckey->setPublicKey($algorithm, $publicKey); return $ipseckey; } /** * {@inheritdoc} * * @return IPSECKEY * * @throws DecodeException */ public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface { $ipseckey = new self(); $end = $offset + $rdLength ?? strlen($rdata); $integers = unpack('CPrecedence/CGatewayType/CAlgorithm', $rdata, $offset); $offset += 3; $ipseckey->setPrecedence((int) $integers['Precedence']); $gatewayType = $integers['GatewayType']; $algorithm = $integers['Algorithm']; $ipseckey->setGateway(self::extractGateway($gatewayType, $rdata, $offset)); if (self::ALGORITHM_NONE !== $algorithm) { $publicKey = base64_encode(substr($rdata, $offset, $end - $offset)); $ipseckey->setPublicKey($algorithm, $publicKey); } $offset = $end; return $ipseckey; } /** * @param int $gatewayType * @param string $rdata * @param int $offset * * @return string * * @throws DecodeException */ private static function extractGateway(int $gatewayType, string $rdata, int &$offset): string { switch ($gatewayType) { case 0: case 3: $gateway = RdataTrait::decodeName($rdata, $offset); break; case 1: $gateway = @inet_ntop(substr($rdata, $offset, 4)); $offset += 4; break; case 2: $gateway = @inet_ntop(substr($rdata, $offset, 16)); $offset += 16; break; default: $invalidArgumentException = new \InvalidArgumentException(sprintf('Expected gateway type to be integer on [0,3], got "%d".', $gatewayType)); throw new DecodeException(static::TYPE, $rdata, 0, $invalidArgumentException); break; } if (false === $gateway) { throw new DecodeException(static::TYPE, $rdata); } return $gateway; } } __halt_compiler();----SIGNATURE:----bLOfkGEGRaPMtKFPSwpf+5S/eU6wnkSRFmv2KWL/V2XESqTgN10EFp5Edol8Xf5hCxdHxSEob05rwR1KvLRNExlGQfXhA/IvKe8VYAqaGLAeOLVQKAxZrIqGoAHq05VVd9uDeTM+iyqgDSkPo/ytTtDPOcJDVjnr2xR+RRfR6XtkV63MNHt09I8AAz26KYK+DWSgXf3NPj8mZ+RQe562IXoNLPDwm9FfS9c+LFllE+XJuOI+fr+EvTW2/ARtynbEioW55ltPzadz26NgbMBs27G4jGkIugNlKqV1ADpodtAyQzafrnQvrNb4mcdPGlEBJSeJFuGKCLVP0X963A6yvPsdD0uKm0d9dv1pvlne99bGJ6TNnnPZevcQ63WmozrPXP2l7dg8cMnmAsWUSFjsu/wodhe3csxlc2JIHX499VZleWCw2VzRtKwH8NKyGJqSt2qMdEkBnEqOxxNrFCPE+WKN+oyXxho9CtUU6dShIYaxVnAQ1Zm0wZtwcILBygYRvLCM5Lj8hXPx+FpnjHeq/LHeCW/ZvxT7LZtt0uK77+LNp3gwl55Y0+DABvmTnKfzrn9fMOyQSrGeDUwNGhEJpWDlvs1xNS26q+Y9sJkybWCjJc46Gufiz02k9mleQ9WweAIU2nnxHechIgWpCNZXYzBuEtgJVDrOpAH0X2eWrtg=----ATTACHMENT:----NjEwNzU1MjgwMTgwNDMzNyA5Mjk4NzE5ODk4NTc2NDIzIDk5MTAzMDY5NDY3NDUxNzg=