-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtcpdf_asn1.min.php
More file actions
111 lines (106 loc) · 2.96 KB
/
tcpdf_asn1.min.php
File metadata and controls
111 lines (106 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* @file
* This is a PHP function for parsing and creating ASN.1 syntax for handle minimum timeStamp request and response.
* @author Hida
* @version 1.0.1
* Last Update : 17/05/2023
*/
function asn1_first($hex) {
$asn1_Id = substr($hex, 0, 2);
$header = substr($hex, 2, 2);
if($asn1_Id == 'bf') {
if(hexdec($header) > 128) {
$headerLength = hexdec(substr($hex, 6, 2));
$reduced = 8; // the string reduced by id & headerLength
$expNum = (128*(hexdec($header)-128))+hexdec(substr($hex, 4, 2));
$header2 = substr($hex, 4, 2);
if(hexdec($header2) >= 128) {
$headerLength = hexdec(substr($hex, 8, 2));
$reduced = 10;
$expNum = (16384*(hexdec($header)-128))+(128*(hexdec($header2)-128))+hexdec(substr($hex, 6, 2));
}
} else {
$headerLength = hexdec(substr($hex, 4, 2));
$reduced = 6;
$expNum = hexdec(substr($hex, 2, 2));
}
$asn1_Id = "EXP:"."$expNum";
} else {
//echo "$header==";
if($header == '83') {
$headerLength = hexdec(substr($hex, 4, 6));
$reduced = 10;
} elseif ($header == '82') {
$headerLength = hexdec(substr($hex, 4, 4));
$reduced = 8;
} elseif ($header == '81') {
$headerLength = hexdec(substr($hex, 4, 2));
$reduced = 6;
} else {
$l=0;
$l = hexdec(substr($hex, 2, 2));
$headerLength = $l;
$reduced = 4;
//echo "$headerLength --".substr($hex, 2, 2)."--<br>";
}
}
$str_remains = substr($hex, $reduced+($headerLength*2));
$content = substr($hex, $reduced, $headerLength*2);
$return['res'] = array($asn1_Id, $content); // array 0=>iD(sequence be 30, integer be 02, etc) 1=>contents of id
$return['rem'] = $str_remains; // the remain string returned
if($str_remains == '' && $content == '') { // if remains string was empty & contents also empty, function return FALSE
$return = false;
}
return $return;
}
function asn1parse($hex) {
//$return =false;
while(asn1_first($hex) != false) { // while asn1_first() still return string
$r = asn1_first($hex);
$return[] = array($r['res'][0],$r['res'][1]);
$hex = $r['rem']; // $hex now be result of asn1_first()
}
if(!is_array(@$return)) {
return false;
}
return $return;
}
function asn1_header($str) {
$len = strlen($str)/2;
$ret = dechex($len);
if(strlen($ret)%2 != 0) {
$ret = "0$ret";
}
$headerLength = strlen($ret)/2;
if($len > 127) {
$ret = "8".$headerLength.$ret;
}
return $ret;
}
function SEQ($hex) {
$ret = "30".asn1_header($hex).$hex;
return $ret;
}
function OCT($hex) {
$ret = "04".asn1_header($hex).$hex;
return $ret;
}
function INT($int) {
if(strlen($int)%2 != 0) {
$int = "0$int";
}
$int = "$int";
$ret = "02".asn1_header($int).$int;
return $ret;
}
function SET($hex) {
$ret = "31".asn1_header($hex).$hex;
return $ret;
}
//function EXPLICIT($num="0", $hex) {
function EXPLICIT($num, $hex) {
$ret = "a$num".asn1_header($hex).$hex;
return $ret;
}
?>