-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnssec_key2ds.php
More file actions
115 lines (108 loc) · 3.67 KB
/
Copy pathdnssec_key2ds.php
File metadata and controls
115 lines (108 loc) · 3.67 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
112
113
114
115
<?php
/**
* Calculate ds-rdata from dnskey-rdata
* For additional information please refer to RFC 5910: http://www.ietf.org/rfc/rfc5910.txt
*
* @param string owner, the coanonical name of the owner (e.g. example.com.)
* @param int flags, the flags of the dnskey (only 256 or 257)
* @param int protocol, the protocol of the dnskey (only 3)
* @param int algoritm, the algorithm of the dnskey (only 3, 5, 6, 7, 8, 10, 12, 13 or 14)
* @param string publickey, the full publickey base64 encoded (care, no spaces allowed)
*
* @return array, on success
* Array (
* [owner] => $owner
* [keytag] => $keytag
* [algorithm] => $algorithm
* [digest] => Array (
* [] => Array (
* [type] => 1
* [hash] => $digest_sha1
* ),
* [] => Array (
* [type] => 2
* [hash] => $digest_sha256
* )
* )
* )
* @return int < 0, on failure
* -1, unsupported owner
* -2, unsupported flags
* -3, unsupported protocol
* -4, unsupported algorithm
* -5, unsupported publickey
*/
function dnssec_key2ds($owner, $flags, $protocol, $algorithm, $publickey) {
// define paramenter check variants
$regex_owner = '/^[a-z0-9\-]+\.[a-z]+\.$/';
$allowed_flags = array(256, 257);
$allowed_protocol = array(3);
$allowed_algorithm = array(3,5,6,7,8,10,12,13,14);
$regex_publickey = '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/';
// do parameter checks and break if failed
if(!preg_match($regex_owner, $owner)) return -1;
if(!in_array($flags, $allowed_flags)) return -2;
if(!in_array($protocol, $allowed_protocol)) return -3;
if(!in_array($algorithm, $allowed_algorithm)) return -4;
if(!preg_match($regex_publickey, $publickey)) return -5;
// calculate hex of parameters
$owner_hex = '';
$parts = explode(".", substr($owner, 0, -1));
foreach ($parts as $part) {
$len = dechex(strlen($part));
$owner_hex .= str_repeat('0', 2 - strlen($len)).$len;
$part = str_split($part);
for ($i = 0; $i < count($part); $i++) {
$byte = strtoupper(dechex(ord($part[$i])));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$owner_hex .= $byte;
}
}
$owner_hex .= '00';
$flags_hex = sprintf("%04d", dechex($flags));
$protocol_hex = sprintf("%02d", dechex($protocol));
$algorithm_hex = sprintf("%02d", dechex($algorithm));
$publickey_hex = bin2hex(base64_decode($publickey));
// calculate keytag using algorithm defined in rfc
$string = hex2bin($flags_hex.$protocol_hex.$algorithm_hex.$publickey_hex);
$sum = 0;
for($i = 0; $i < strlen($string); $i++) {
$b = ord($string[$i]);
$sum += ($i & 1) ? $b : $b << 8;
}
$keytag = 0xffff & ($sum + ($sum >> 16));
// calculate digest using rfc specified hashing algorithms
$string = hex2bin($owner_hex.$flags_hex.$protocol_hex.$algorithm_hex.$publickey_hex);
$digest_sha1 = strtoupper(sha1($string));
$digest_sha256 = strtoupper(hash('sha256', $string));
// return results and also copied parameters
return array(
//'debug' => array($owner_hex, $flags_hex, $protocol_hex, $algorithm_hex, $publickey_hex),
'owner' => $owner,
'keytag' => $keytag,
'algorithm' => $algorithm,
'digest' => array(
array(
'type' => 1,
'hash' => $digest_sha1
),
array(
'type' => 2,
'hash' => $digest_sha256
)
)
);
}
/**
* hex2bin compatibility function (missing in PHP < 5.4)
*/
if (!function_exists('hex2bin')) {
function hex2bin($str) {
$sbin = "";
$len = strlen($str);
for ($i=0; $i<$len; $i+=2 ) {
$sbin .= pack("H*", substr($str, $i, 2));
}
return $sbin;
}
}