-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.php
263 lines (215 loc) · 5.94 KB
/
funcs.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* Copyright (C) 2015 - 2022 Green Screens Ltd.
*
* PHP lib to create Green Screens Web Terminal encrypted URL
*
* Encryption lib from http://phpseclib.sourceforge.net/
*
* cURL program used to call remote Green Screens Terminal Service
* to retrieve RSA enryption key.
*
* cURL PHP support can be inalled with
* sudo apt-get install php5-curl
*
*/
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
set_include_path(get_include_path() . PATH_SEPARATOR . 'otplib');
require_once "lib/otphp.php";
require_once "Crypt/RSA.php";
require_once "Crypt/AES.php";
function getOTP($key = "0000000000000000")
{
$totp = new \OTPHP\TOTP($key);
return $totp->now();
}
/*
* cURL program caller
*
* In case of errors try to instal it on machine with
* sudo apt-get install php5-curl
*/
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/*
* Retrieve RSA from remote service with cURL
* Parse to JSON object
*
* {"succss": true, "ts" : 34532452354325, "key" : "PKCS12 ecded public key", "build":20220101}
*
* $obj->{'key'};
* $obj->{'ts'};
*/
function getRSA($url)
{
$json = CallAPI("GET", $url, false);
$obj = json_decode($json);
if ($obj->{'success'} != true) {
print $json;
}
return $obj;
}
/*
* Encrypt data with RSA public key
*/
function rsa_encrypt($string, $public_key)
{
//Create an instance of the RSA cypher and load the key into it
$cipher = new Crypt_RSA();
$cipher->loadKey($public_key);
//Set the encryption mode
$cipher->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//Return the encrypted version
return base64_encode($cipher->encrypt($string));
}
/*
* Make 128 bit random string for AES key and IV
*/
function makeid($len) {
$ALPHANUM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$text = "";
$poslen = strlen($ALPHANUM) - 1;
$i = $len;
$pos = 0;
while ($i-- > 0 ) {
$pos = rand(0, $poslen);
$text = $text . $ALPHANUM[$pos];
}
return $text;
}
/*
* Convert parameters to JSON structure
*/
function getJson($uuid = "", $host = "", $user = "user", $password = "", $program = "", $menu = "", $lib = "", $exp = 0, $displayName = "", $otp = 0, $api = "")
{
$json_data = array('uuid' => $uuid,
'host' => $host,
'user' => $user,
'displayName' => $displayName,
'password' => $password,
'program' => $program,
'menu' => $menu,
'lib' => $lib,
'exp' => $exp,
'otp' => $otp,
'api' => $api
);
return json_encode($json_data);
}
/*
* Encrypt login data and convert to JSON url string for 5250 terminal
*/
function encrypt($service = "", $uuid = "", $host = "", $user = "user", $password = "", $program = "", $menu = "", $lib = "", $exp = 0, $displayName = "", $otp = 0)
{
// login params
$params = getJson($uuid, $host, $user, $password, $program, $menu, $lib, $exp, $displayName, $otp);
return encryptJson($service, $params);
}
/**
* Encrypt JSON object for Green Screens 5250 Web Terminal
*/
function encryptJson($service = "", $jsonObj)
{
// retrieve JSON object with RSA and ts values
$rsaObj = getRSA($service . "/services/auth");
$rsaKey = $rsaObj->{'key'};
// gs server build
$build = $rsaObj->{'build'};
// prepare AES
$aesKey = makeid(16);
$aesIV = makeid(16);
$cipher = new Crypt_AES(CRYPT_AES_MODE_CTR);
$cipher->setKeyLength(128);
$cipher->setKey($aesKey);
$cipher->setIV($aesIV);
// encrypt params and convert to hex data
$encrypted = $cipher->encrypt($jsonObj);
$hex = bin2hex($encrypted);
// encrypt aes with rsa
$aesRsa = $aesIV . $aesKey;
$aesRsaEncrypted = rsa_encrypt($aesRsa, $rsaKey);
$json_data = array('d' => $hex,
'k' => $aesRsaEncrypted,
'v' => 0,
'b' => $build);
return $json_data;
}
/**
* Convert JSON object to url string
*/
function jsonToURLService($url = "", $json)
{
$build = $json->{'build'};
unset($json['build']);
$params = jsonToURL($json);
// add support for build greater or equal to GS 20220725
if ( $build >= 20220725 ) {
return $url . "/terminal?" . $params;
}
return $url . "/lite?" . $params;
}
/*
* Iterate over JSON object properties and create query string
*/
function jsonToURL($json)
{
$parm = "";
$obj = $json;
foreach ($json as $key => $value) {
$parm = $parm . $key . "=" . $value . "&";
}
return $parm;
}
/*
* Get Java/Javascript timestap in future
*/
function getTimestamp($future)
{
$date = new DateTime();
// php timestamp to java/javascript format * future date
$timestamp = $date->getTimestamp() * 1000 * $future;
return $timestamp;
}
/*
* Get Java/Javascript timestap in future in hours
*/
function futureHours($hours)
{
// 24h, 60, 60s
$future = $hours * 60 * 60;
return getTimestamp($future);
}
/*
* Get Java/Javascript timestap in future in hours
*/
function futureDays($days)
{
// 5day, 24h, 60, 60s
$future = $days * 24 * 60 * 60;
return getTimestamp($future);
}
?>