protected static function _decodeFromDER(Identifier $identifier, $data, &$offset) { $idx = $offset; if (!$identifier->isPrimitive()) { throw new DecodeException("Null value must be primitive."); } // null type has always zero length Length::expectFromDER($data, $idx, 0); $offset = $idx; return new self(); }
/** * * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() * @return UnspecifiedType */ public function implicit($tag, $class = Identifier::CLASS_UNIVERSAL) { $identifier = $this->_identifier->withClass($class)->withTag($tag); $cls = self::_determineImplClass($identifier); $idx = $this->_offset; $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); return new UnspecifiedType($element); }
/** * * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() * @return UnspecifiedType */ public function implicit($tag, $class = Identifier::CLASS_UNIVERSAL) { $this->_element->expectType($tag); if ($this->_element->typeClass() != $class) { throw new \UnexpectedValueException("Type class " . Identifier::classToName($class) . " expected, got " . Identifier::classToName($this->_element->typeClass()) . "."); } return new UnspecifiedType($this->_element); }
/** * * @see \ASN1\Element::_decodeFromDER() * @return self */ protected static function _decodeFromDER(Identifier $identifier, $data, &$offset) { $idx = $offset; if (!$identifier->isPrimitive()) { throw new DecodeException("DER encoded string must be primitive."); } $length = Length::expectFromDER($data, $idx); $str = $length->length() ? substr($data, $idx, $length->length()) : ""; // substr should never return false, since length is // checked by Length::expectFromDER. assert('is_string($str)', "substr"); $offset = $idx + $length->length(); try { return new static($str); } catch (\InvalidArgumentException $e) { throw new DecodeException($e->getMessage(), null, $e); } }
/** * 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}"; }
/** * Get textual description of the type for debugging purposes. * * @return string */ protected function _typeDescriptorString() { if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { return self::tagToName($this->_typeTag); } return Identifier::classToName($this->typeClass()) . " TAG " . $this->_typeTag; }
/** * * @see \ASN1\Element::isConstructed() * @return bool */ public function isConstructed() { return $this->_identifier->isConstructed(); }
/** * Explode DER structure to DER encoded components that it contains. * * @param string $data * @throws DecodeException * @return string[] */ public static function explodeDER($data) { $offset = 0; $identifier = Identifier::fromDER($data, $offset); if (!$identifier->isConstructed()) { throw new DecodeException("Element is not constructed."); } $length = Length::expectFromDER($data, $offset); $end = $offset + $length->length(); $parts = array(); while ($offset < $end) { // start of the element $idx = $offset; // skip identifier Identifier::fromDER($data, $offset); // decode element length $length = Length::expectFromDER($data, $offset); // extract der encoding of the element $parts[] = substr($data, $idx, $offset - $idx + $length->length()); // update offset over content $offset += $length->length(); } return $parts; }