-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtsa_1.php
139 lines (127 loc) · 4.57 KB
/
tsa_1.php
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
// Last change at 20:50 Sore Minggu 01 Maret 2009
// Last change at 14:30 Sore Senin 23 Maret 2009 req not based of content-type header,
// but based on req structure
// Last change at 21:30 Sore Rabu 08 Juli 2009
// Last change at Minggu 14 Mei 2023 21:05:45 Sore
define("TSA_NAME", "TSA1");
$crlAttached = false;
if(array_key_exists('crls', $TSA)) {
foreach($TSA['crls'] as $crlnum=>$crl) {
$parseCrl = getcrldate($crl);
$diffTime = difftime($parseCrl['next']);
if($diffTime < 0) {
tsalog("CRL \"$crlnum\" expired!", 'w');
}
$crlAttached .= bin2hex($crl);
}
$crlAttached = explicit('1',
$crlAttached
);
}
if(!$signerCertId = x509_get_pubkeys($TSA['signer'])) {
tsalog("x509_get_pubkeys failed\nOn ".__FILE__."(".__LINE__.")", 'e');
exit;
}
$TimeStamp = gmdate('ymdHis');
$contentHash = sha1(hex2bin($PARSED_REQ['contents']['content']));
$authenticatedAttributes = seq(
OBJ_pkcs9_contentType.
set(
OBJ_pkcs7_data
)
).
seq(
OBJ_pkcs9_signingTime.
set(
utctime($TimeStamp)
)
).
seq(
OBJ_pkcs9_messageDigest.
set(
oct($contentHash)
)
);
$authenticatedAttributes_hash = sha1(
hex2bin(
set(
$authenticatedAttributes
)
)
);
$to_encrypt = seq(
seq(
OBJ_sha1.
OBJ_null
).
oct($authenticatedAttributes_hash)
);
if(!@openssl_private_encrypt(hex2bin($to_encrypt), $crypted, $TSA['signer'])) {
tsalog("Failed to signing\n".__FILE__."(".__LINE__.")", 'e');
exit;
}
$extraCerts = false;
if(array_key_exists('extracerts', $TSA)) {
foreach($TSA['extracerts'] as $extCrt) {
$extraCerts .= get_cert($extCrt);
}
}
$res = seq(
OBJ_pkcs7_signed.
explicit('0',
seq(
int('1').
set(
seq(
OBJ_sha1.
OBJ_null
)
).
$PARSED_REQ['contents']['hex'].
explicit('0',
bin2hex(get_cert($TSA['signer'])).
bin2hex($extraCerts)
).
$crlAttached.
set(
seq(
int('1').
seq(
$signerCertId['issuerName'].
int($signerCertId['serialNumber'])
).
seq(
OBJ_sha1.
OBJ_null
).
explicit('0',
$authenticatedAttributes
).
seq(
OBJ_rsaEncryption.
OBJ_null
).
oct(
bin2hex($crypted)
)
)
)
)
)
);
$response = hex2bin($res);
$respOut = base64_encode($response);
header('Server: Hda');
header('Content-Length: '.strlen($respOut));
header('Content-Type: application/timestamp-reply');
echo $respOut;
tsalogfile($respOut, 'resp1.der');
// $h = fopen(getcwd().'/lastReq1.der', 'w');
// fwrite($h, $req);
// fclose($h);
// $h = fopen(getcwd().'/lastResp1.der', 'w');
// fwrite($h, chunk_split($respOut));
// fclose($h);
tsalog("Response Successfull. Hash alg:".TSA_HASHALGORITHM, 'i');
?>