forked from robrichards/xmlseclibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlsec-decrypt.phpt
executable file
·101 lines (88 loc) · 2.5 KB
/
xmlsec-decrypt.phpt
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
--TEST--
Basic Decryption
--FILE--
<?php
require(dirname(__FILE__) . '/../xmlseclibs.php');
use RobRichards\XMLSecLibs\XMLSecEnc;
/* When we need to locate our own key based on something like a key name */
function locateLocalKey($objKey) {
/* In this example the key is identified by filename */
$filename = $objKey->name;
if (! empty($filename)) {
$objKey->loadKey(dirname(__FILE__) . "/$filename", TRUE);
} else {
$objKey->loadKey(dirname(__FILE__) . "/privkey.pem", TRUE);
}
}
$arTests = array('AOESP_SHA1'=>'oaep_sha1-res.xml', 'AES128-GCM'=>'aes128-gcm-res.xml',
'AES192-GCM'=>'aes192-gcm-res.xml', 'AES256-GCM'=>'aes256-gcm-res.xml');
$doc = new DOMDocument();
foreach ($arTests AS $testName=>$testFile) {
$output = NULL;
print "$testName: ";
// Skip AES tests is PHP < 7.1.0
if ((substr($testName, 0, 3) === "AES") && (version_compare(PHP_VERSION, '7.1.0') < 0)) {
print "Passed\n";
continue;
}
$doc->load(dirname(__FILE__) . "/$testFile");
try {
$objenc = new XMLSecEnc();
$encData = $objenc->locateEncryptedData($doc);
if (! $encData) {
throw new Exception("Cannot locate Encrypted Data");
}
$objenc->setNode($encData);
$objenc->type = $encData->getAttribute("Type");
if (! $objKey = $objenc->locateKey()) {
throw new Exception("We know the secret key, but not the algorithm");
}
$key = NULL;
if ($objKeyInfo = $objenc->locateKeyInfo($objKey)) {
if ($objKeyInfo->isEncrypted) {
$objencKey = $objKeyInfo->encryptedCtx;
locateLocalKey($objKeyInfo);
$key = $objencKey->decryptKey($objKeyInfo);
}
}
if (! $objKey->key && empty($key)) {
locateLocalKey($objKey);
}
if (empty($objKey->key)) {
$objKey->loadKey($key);
}
$token = NULL;
if ($decrypt = $objenc->decryptNode($objKey, TRUE)) {
$output = NULL;
if ($decrypt instanceof DOMNode) {
if ($decrypt instanceof DOMDocument) {
$output = $decrypt->saveXML();
} else {
$output = $decrypt->ownerDocument->saveXML();
}
} else {
$output = $decrypt;
}
}
} catch (Exception $e) {
var_dump($e);
}
$outfile = dirname(__FILE__) . "/basic-doc.xml";
$res = NULL;
if (file_exists($outfile)) {
$resDoc = new DOMDocument();
$resDoc->load($outfile);
$res = $resDoc->saveXML();
if ($output == $res) {
print "Passed\n";
continue;
}
}
print "Failed\n";
}
?>
--EXPECTF--
AOESP_SHA1: Passed
AES128-GCM: Passed
AES192-GCM: Passed
AES256-GCM: Passed