Documentation

ASN1
in package

AbstractYes

Pure-PHP ASN.1 Parser

Tags
author

Jim Wigginton terrafrost@php.net

access

public

Table of Contents

Constants

ANY_MAP  = [ self::TYPE_BOOLEAN => true, self::TYPE_INTEGER => true, self::TYPE_BIT_STRING => 'bitString', self::TYPE_OCTET_STRING => 'octetString', self::TYPE_NULL => 'null', self::TYPE_OBJECT_IDENTIFIER => 'objectIdentifier', self::TYPE_REAL => true, self::TYPE_ENUMERATED => 'enumerated', self::TYPE_UTF8_STRING => 'utf8String', self::TYPE_NUMERIC_STRING => 'numericString', self::TYPE_PRINTABLE_STRING => 'printableString', self::TYPE_TELETEX_STRING => 'teletexString', self::TYPE_VIDEOTEX_STRING => 'videotexString', self::TYPE_IA5_STRING => 'ia5String', self::TYPE_UTC_TIME => 'utcTime', self::TYPE_GENERALIZED_TIME => 'generalTime', self::TYPE_GRAPHIC_STRING => 'graphicString', self::TYPE_VISIBLE_STRING => 'visibleString', self::TYPE_GENERAL_STRING => 'generalString', self::TYPE_UNIVERSAL_STRING => 'universalString', //self::TYPE_CHARACTER_STRING => 'characterString', self::TYPE_BMP_STRING => 'bmpString', ]
Type mapping table for the ANY type.
CLASS_APPLICATION  = 1
CLASS_CONTEXT_SPECIFIC  = 2
CLASS_PRIVATE  = 3
CLASS_UNIVERSAL  = 0
STRING_TYPE_SIZE  = [self::TYPE_UTF8_STRING => 0, self::TYPE_BMP_STRING => 2, self::TYPE_UNIVERSAL_STRING => 4, self::TYPE_PRINTABLE_STRING => 1, self::TYPE_TELETEX_STRING => 1, self::TYPE_IA5_STRING => 1, self::TYPE_VISIBLE_STRING => 1]
String type to character size mapping table.
TYPE_ANY  = -2
TYPE_BIT_STRING  = 3
TYPE_BMP_STRING  = 30
TYPE_BOOLEAN  = 1
TYPE_CHOICE  = -1
TYPE_ENUMERATED  = 10
TYPE_GENERAL_STRING  = 27
TYPE_GENERALIZED_TIME  = 24
TYPE_GRAPHIC_STRING  = 25
TYPE_IA5_STRING  = 22
TYPE_INTEGER  = 2
TYPE_NULL  = 5
TYPE_NUMERIC_STRING  = 18
TYPE_OBJECT_IDENTIFIER  = 6
TYPE_OCTET_STRING  = 4
TYPE_PRINTABLE_STRING  = 19
TYPE_REAL  = 9
TYPE_SEQUENCE  = 16
TYPE_SET  = 17
TYPE_TELETEX_STRING  = 20
TYPE_UNIVERSAL_STRING  = 28
TYPE_UTC_TIME  = 23
TYPE_UTF8_STRING  = 12
TYPE_VIDEOTEX_STRING  = 21
TYPE_VISIBLE_STRING  = 26

Properties

$encoded  : string
DER Encoded String
$filters  : array<string|int, mixed>
Filters
$format  : string
Default date format
$location  : array<string|int, mixed>
Current Location of most recent ASN.1 encode process
$oids  : array<string|int, mixed>
ASN.1 object identifiers
$reverseOIDs  : array<string|int, mixed>
ASN.1 object identifier reverse mapping

Methods

asn1map()  : array<string|int, mixed>|bool|Element
ASN.1 Map
convert()  : string
String type conversion
decodeBER()  : array<string|int, mixed>
Parse BER-encoding
decodeLength()  : int
DER-decode the length
decodeOID()  : string
BER-decode the OID
encodeDER()  : string
ASN.1 Encode
encodeLength()  : string
DER-encode the length
encodeOID()  : string
DER-encode the OID
extractBER()  : string
Extract raw BER from Base64 encoding
getOID()  : string
Returns the OID corresponding to a name
loadOIDs()  : mixed
Load OIDs
setFilters()  : mixed
Set filters
setTimeFormat()  : mixed
Set the time format
decode_ber()  : array<string|int, mixed>|bool
Parse BER-encoding (Helper function)
decodeTime()  : string
BER-decode the time
encode_der()  : string
ASN.1 Encode (Helper function)

Constants

ANY_MAP

Type mapping table for the ANY type.

public array<string|int, mixed> ANY_MAP = [ self::TYPE_BOOLEAN => true, self::TYPE_INTEGER => true, self::TYPE_BIT_STRING => 'bitString', self::TYPE_OCTET_STRING => 'octetString', self::TYPE_NULL => 'null', self::TYPE_OBJECT_IDENTIFIER => 'objectIdentifier', self::TYPE_REAL => true, self::TYPE_ENUMERATED => 'enumerated', self::TYPE_UTF8_STRING => 'utf8String', self::TYPE_NUMERIC_STRING => 'numericString', self::TYPE_PRINTABLE_STRING => 'printableString', self::TYPE_TELETEX_STRING => 'teletexString', self::TYPE_VIDEOTEX_STRING => 'videotexString', self::TYPE_IA5_STRING => 'ia5String', self::TYPE_UTC_TIME => 'utcTime', self::TYPE_GENERALIZED_TIME => 'generalTime', self::TYPE_GRAPHIC_STRING => 'graphicString', self::TYPE_VISIBLE_STRING => 'visibleString', self::TYPE_GENERAL_STRING => 'generalString', self::TYPE_UNIVERSAL_STRING => 'universalString', //self::TYPE_CHARACTER_STRING => 'characterString', self::TYPE_BMP_STRING => 'bmpString', ]

Structured or unknown types are mapped to a \phpseclib3\File\ASN1\Element. Unambiguous types get the direct mapping (int/real/bool). Others are mapped as a choice, with an extra indexing level.

Tags
access

public

CLASS_APPLICATION

public mixed CLASS_APPLICATION = 1

CLASS_CONTEXT_SPECIFIC

public mixed CLASS_CONTEXT_SPECIFIC = 2

CLASS_PRIVATE

public mixed CLASS_PRIVATE = 3

CLASS_UNIVERSAL

public mixed CLASS_UNIVERSAL = 0

STRING_TYPE_SIZE

String type to character size mapping table.

public array<string|int, mixed> STRING_TYPE_SIZE = [self::TYPE_UTF8_STRING => 0, self::TYPE_BMP_STRING => 2, self::TYPE_UNIVERSAL_STRING => 4, self::TYPE_PRINTABLE_STRING => 1, self::TYPE_TELETEX_STRING => 1, self::TYPE_IA5_STRING => 1, self::TYPE_VISIBLE_STRING => 1]

Non-convertable types are absent from this table. size == 0 indicates variable length encoding.

Tags
access

public

TYPE_ANY

public mixed TYPE_ANY = -2

TYPE_BIT_STRING

public mixed TYPE_BIT_STRING = 3

TYPE_BMP_STRING

public mixed TYPE_BMP_STRING = 30

TYPE_BOOLEAN

public mixed TYPE_BOOLEAN = 1

TYPE_CHOICE

public mixed TYPE_CHOICE = -1

TYPE_ENUMERATED

public mixed TYPE_ENUMERATED = 10

TYPE_GENERAL_STRING

public mixed TYPE_GENERAL_STRING = 27

TYPE_GENERALIZED_TIME

public mixed TYPE_GENERALIZED_TIME = 24

TYPE_GRAPHIC_STRING

public mixed TYPE_GRAPHIC_STRING = 25

TYPE_IA5_STRING

public mixed TYPE_IA5_STRING = 22

TYPE_INTEGER

public mixed TYPE_INTEGER = 2

TYPE_NULL

public mixed TYPE_NULL = 5

TYPE_NUMERIC_STRING

public mixed TYPE_NUMERIC_STRING = 18

TYPE_OBJECT_IDENTIFIER

public mixed TYPE_OBJECT_IDENTIFIER = 6

TYPE_OCTET_STRING

public mixed TYPE_OCTET_STRING = 4

TYPE_PRINTABLE_STRING

public mixed TYPE_PRINTABLE_STRING = 19

TYPE_REAL

public mixed TYPE_REAL = 9

TYPE_SEQUENCE

public mixed TYPE_SEQUENCE = 16

TYPE_SET

public mixed TYPE_SET = 17

TYPE_TELETEX_STRING

public mixed TYPE_TELETEX_STRING = 20

TYPE_UNIVERSAL_STRING

public mixed TYPE_UNIVERSAL_STRING = 28

TYPE_UTC_TIME

public mixed TYPE_UTC_TIME = 23

TYPE_UTF8_STRING

public mixed TYPE_UTF8_STRING = 12

TYPE_VIDEOTEX_STRING

public mixed TYPE_VIDEOTEX_STRING = 21

TYPE_VISIBLE_STRING

public mixed TYPE_VISIBLE_STRING = 26

Properties

$encoded

DER Encoded String

private static string $encoded

In case we need to create ASN1\Element object's..

Tags
access

private

see
self::decodeDER()

$filters

Filters

private static array<string|int, mixed> $filters

If the mapping type is self::TYPE_ANY what do we actually encode it as?

Tags
access

private

see
self::encode_der()

$location

Current Location of most recent ASN.1 encode process

private static array<string|int, mixed> $location

Useful for debug purposes

Tags
access

private

see
self::encode_der()

$reverseOIDs

ASN.1 object identifier reverse mapping

private static array<string|int, mixed> $reverseOIDs = []
Tags
access

private

Methods

asn1map()

ASN.1 Map

public static asn1map(array<string|int, mixed> $decoded, array<string|int, mixed> $mapping[, array<string|int, mixed> $special = [] ]) : array<string|int, mixed>|bool|Element

Provides an ASN.1 semantic mapping ($mapping) from a parsed BER-encoding to a human readable format.

"Special" mappings may be applied on a per tag-name basis via $special.

Parameters
$decoded : array<string|int, mixed>
$mapping : array<string|int, mixed>
$special : array<string|int, mixed> = []
Tags
access

public

Return values
array<string|int, mixed>|bool|Element

convert()

String type conversion

public static convert(string $in[, int $from = self::TYPE_UTF8_STRING ][, int $to = self::TYPE_UTF8_STRING ]) : string

This is a lazy conversion, dealing only with character size. No real conversion table is used.

Parameters
$in : string
$from : int = self::TYPE_UTF8_STRING
$to : int = self::TYPE_UTF8_STRING
Tags
access

public

Return values
string

decodeBER()

Parse BER-encoding

public static decodeBER(string $encoded) : array<string|int, mixed>

Serves a similar purpose to openssl's asn1parse

Parameters
$encoded : string
Tags
access

public

Return values
array<string|int, mixed>

decodeLength()

DER-decode the length

public static decodeLength(string &$string) : int

DER supports lengths up to (2**8)127, however, we'll only support lengths up to (28)**4. See X.690 paragraph 8.1.3 for more information.

Parameters
$string : string
Tags
access

public

Return values
int

decodeOID()

BER-decode the OID

public static decodeOID(string $content) : string

Called by _decode_ber()

Parameters
$content : string
Tags
access

public

Return values
string

encodeDER()

ASN.1 Encode

public static encodeDER(Element|string|array<string|int, mixed> $source, array<string|int, mixed> $mapping[, array<string|int, mixed> $special = [] ]) : string

DER-encodes an ASN.1 semantic mapping ($mapping). Some libraries would probably call this function an ASN.1 compiler.

"Special" mappings can be applied via $special.

Parameters
$source : Element|string|array<string|int, mixed>
$mapping : array<string|int, mixed>
$special : array<string|int, mixed> = []
Tags
access

public

Return values
string

encodeLength()

DER-encode the length

public static encodeLength(int $length) : string

DER supports lengths up to (2**8)127, however, we'll only support lengths up to (28)**4. See X.690 paragraph 8.1.3 for more information.

Parameters
$length : int
Tags
access

public

Return values
string

encodeOID()

DER-encode the OID

public static encodeOID(string $source) : string

Called by _encode_der()

Parameters
$source : string
Tags
access

public

Return values
string

extractBER()

Extract raw BER from Base64 encoding

public static extractBER(string $str) : string
Parameters
$str : string
Tags
access

private

Return values
string

getOID()

Returns the OID corresponding to a name

public static getOID(string $name) : string

What's returned in the associative array returned by loadX509() (or load*()) is either a name or an OID if no OID to name mapping is available. The problem with this is that what may be an unmapped OID in one version of phpseclib may not be unmapped in the next version, so apps that are looking at this OID may not be able to work from version to version.

This method will return the OID if a name is passed to it and if no mapping is avialable it'll assume that what's being passed to it already is an OID and return that instead. A few examples.

getOID('2.16.840.1.101.3.4.2.1') == '2.16.840.1.101.3.4.2.1' getOID('id-sha256') == '2.16.840.1.101.3.4.2.1' getOID('zzz') == 'zzz'

Parameters
$name : string
Tags
access

public

Return values
string

loadOIDs()

Load OIDs

public static loadOIDs(array<string|int, mixed> $oids) : mixed

Load the relevant OIDs for a particular ASN.1 semantic mapping. Previously loaded OIDs are retained.

Parameters
$oids : array<string|int, mixed>
Tags
access

public

setFilters()

Set filters

public static setFilters(array<string|int, mixed> $filters) : mixed

See \phpseclib3\File\X509, etc, for an example. Previously loaded filters are not retained.

Parameters
$filters : array<string|int, mixed>
Tags
access

public

setTimeFormat()

Set the time format

public static setTimeFormat(string $format) : mixed

Sets the time / date format for asn1map().

Parameters
$format : string
Tags
access

public

decode_ber()

Parse BER-encoding (Helper function)

private static decode_ber(string $encoded[, int $start = 0 ][, int $encoded_pos = 0 ]) : array<string|int, mixed>|bool

Sometimes we want to get the BER encoding of a particular tag. $start lets us do that without having to reencode. $encoded is passed by reference for the recursive calls done for self::TYPE_BIT_STRING and self::TYPE_OCTET_STRING. In those cases, the indefinite length is used.

Parameters
$encoded : string
$start : int = 0
$encoded_pos : int = 0
Tags
access

private

Return values
array<string|int, mixed>|bool

decodeTime()

BER-decode the time

private static decodeTime(string $content, int $tag) : string

Called by _decode_ber() and in the case of implicit tags asn1map().

Parameters
$content : string
$tag : int
Tags
access

private

Return values
string

encode_der()

ASN.1 Encode (Helper function)

private static encode_der(Element|string|array<string|int, mixed> $source, array<string|int, mixed> $mapping[, int $idx = null ][, array<string|int, mixed> $special = [] ]) : string
Parameters
$source : Element|string|array<string|int, mixed>
$mapping : array<string|int, mixed>
$idx : int = null
$special : array<string|int, mixed> = []
Tags
access

private

Return values
string

        
On this page

Search results