- md5
- 8752152c9350887657e962459459089b
- sha1
- 3ac5073838ad90bc195cc44eb1e897e3d932d633
- sha256
- dba9e70324fe7bd72281aecbb0778d87100db8b7810c0c409e7a429b6b017fdb
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
<?php
declare(strict_types=1);
namespace SpomkyLabs\Pki\X509\GeneralName;
use LogicException;
use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString;
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType;
use SpomkyLabs\Pki\ASN1\Type\TaggedType;
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
use UnexpectedValueException;
use function mb_strlen;
/**
* Implements *iPAddress* CHOICE type of *GeneralName*.
*
* Concrete classes `IPv4Address` and `IPv6Address` furthermore implement the parsing semantics.
*
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.6
*/
abstract class IPAddress extends GeneralName
{
protected function __construct(
protected string $ip,
protected ?string $mask = null
) {
parent::__construct(self::TAG_IP_ADDRESS);
}
/**
* @return self
*/
public static function fromChosenASN1(UnspecifiedType $el): GeneralName
{
$octets = $el->asOctetString()
->string();
return match (mb_strlen($octets, '8bit')) {
4, 8 => IPv4Address::fromOctets($octets),
16, 32 => IPv6Address::fromOctets($octets),
default => throw new UnexpectedValueException('Invalid octet length for IP address.'),
};
}
public function string(): string
{
return $this->ip . (isset($this->mask) ? '/' . $this->mask : '');
}
/**
* Get IP address as a string.
*/
public function address(): string
{
return $this->ip;
}
/**
* Check whether mask is present.
*/
public function hasMask(): bool
{
return isset($this->mask);
}
/**
* Get subnet mask as a string.
*/
public function mask(): string
{
if (! $this->hasMask()) {
throw new LogicException('mask is not set.');
}
return $this->mask;
}
/**
* Get octet representation of the IP address.
*/
abstract protected function octets(): string;
protected function choiceASN1(): TaggedType
{
return ImplicitlyTaggedType::create($this->tag, OctetString::create($this->octets()));
}
}
The same content is in 125 releases, Joomla 5.1.0-beta1 to 6.2.0-alpha1. See them all, or every version of this file.