/** * * @see \X501\StringPrep\StringPrep::prepare() * @throws \LogicException If string type is not supported * @param string $string String to prepare * @return string UTF-8 encoded string */ public function apply($string) { switch ($this->_type) { // UTF-8 string as is case Element::TYPE_UTF8_STRING: return $string; // PrintableString maps directly to UTF-8 // PrintableString maps directly to UTF-8 case Element::TYPE_PRINTABLE_STRING: return $string; // UCS-2 to UTF-8 // UCS-2 to UTF-8 case Element::TYPE_BMP_STRING: return mb_convert_encoding($string, "UTF-8", "UCS-2BE"); // UCS-4 to UTF-8 // UCS-4 to UTF-8 case Element::TYPE_UNIVERSAL_STRING: return mb_convert_encoding($string, "UTF-8", "UCS-4BE"); // TeletexString mapping is a local matter. // We take a shortcut here and encode it as a hexstring. // TeletexString mapping is a local matter. // We take a shortcut here and encode it as a hexstring. case Element::TYPE_T61_STRING: $el = new T61String($string); return "#" . bin2hex($el->toDER()); } throw new \LogicException("Unsupported string type " . Element::tagToName($this->_type) . "."); }
/** * Get textual description of the wrapped element for debugging purposes. * * @return string */ private function _typeDescriptorString() { $type_cls = $this->_element->typeClass(); $tag = $this->_element->tag(); if ($type_cls == Identifier::CLASS_UNIVERSAL) { return Element::tagToName($tag); } return Identifier::classToName($type_cls) . " TAG {$tag}"; }
/** * Generate ASN.1 element. * * @throws \UnexpectedValueException * @return StringType */ public function toASN1() { switch ($this->_tag) { case Element::TYPE_IA5_STRING: return new IA5String($this->_text); case Element::TYPE_VISIBLE_STRING: return new VisibleString($this->_text); case Element::TYPE_BMP_STRING: return new BMPString($this->_text); case Element::TYPE_UTF8_STRING: return new UTF8String($this->_text); default: throw new \UnexpectedValueException("Type " . Element::tagToName($this->_tag) . " not supported."); } }
/** * Generate ASN.1 structure. * * @throws \LogicException * @return Element */ public function toASN1() { switch ($this->_type) { case Element::TYPE_OCTET_STRING: return new OctetString($this->_value); case Element::TYPE_UTF8_STRING: return new UTF8String($this->_value); case Element::TYPE_OBJECT_IDENTIFIER: return new ObjectIdentifier($this->_value); } throw new \LogicException("Type " . Element::tagToName($this->_type) . " not supported."); }
/** * Generate ASN.1. * * @throws \UnexpectedValueException * @return TimeType */ public function toASN1() { $dt = $this->_dt; switch ($this->_type) { case Element::TYPE_UTC_TIME: return new UTCTime($dt); case Element::TYPE_GENERALIZED_TIME: // GeneralizedTime must not contain fractional seconds // (rfc5280 4.1.2.5.2) if ($dt->format("u") != 0) { // remove fractional seconds (round down) $dt = \DateTimeImmutable::createFromFormat("Y-m-d H:i:s", $dt->format("Y-m-d H:i:s"), $dt->getTimezone()); } return new GeneralizedTime($dt); } throw new \UnexpectedValueException("Time type " . Element::tagToName($this->_type) . " not supported."); }
/** * Get ASN.1 class name for given DirectoryString type tag. * * @param int $tag * @throws \UnexpectedValueException * @return string */ private static function _tagToASN1Class($tag) { if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { throw new \UnexpectedValueException("Type " . Element::tagToName($tag) . " is not valid DirectoryString."); } return self::MAP_TAG_TO_CLASS[$tag]; }