Mercurial > hg > marc_php
comparison marccore.php @ 4:c642254dc9ee draft default tip
Fixed transfer chain generation and construction of empty updates, some small improvements in tools
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Sat, 22 Nov 2014 18:18:52 +0100 |
parents | 5c8c4fa95803 |
children |
comparison
equal
deleted
inserted
replaced
3:5c8c4fa95803 | 4:c642254dc9ee |
---|---|
97 if (!self::CanImport($upd, $current)) throw new Exception('Can not update resource'); | 97 if (!self::CanImport($upd, $current)) throw new Exception('Can not update resource'); |
98 } | 98 } |
99 if (isset($upd['transfer']) && (strlen($upd['transfer']) != 0 && strlen($upd['transfer']) != NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES)) throw new Exception('Transfer recipient key is not valid'); | 99 if (isset($upd['transfer']) && (strlen($upd['transfer']) != 0 && strlen($upd['transfer']) != NACL_CRYPTO_SIGN_ed25519_PUBLICKEYBYTES)) throw new Exception('Transfer recipient key is not valid'); |
100 if ($current) { | 100 if ($current) { |
101 unset($upd['transferchain']); | 101 unset($upd['transferchain']); |
102 if (isset($current['transferchain']) && ($chain = self::Decode($current['transferchain'])) && $chain->Verify() && $chain->serial >= time() - 365*24*60*60 && isset($chain->transfer) && ($chain->transfer == $upd['key'] || ($upd['key'] == $current['key'] && !strlen($chain->transfer)))) { | 102 if (isset($current['transferchain']) && ($chain = self::Decode($current['transferchain'])) && $chain->Verify() && ($current['key'] == $upd['key'] || ($chain->key == $current['key'] && $chain->serial == $current['serial']))) { |
103 $upd['transferchain'] = $current['transferchain']; | 103 $chain = $chain; |
104 } elseif (isset($current['transfer']) && isset($current['updatemessage']) && $current['serial'] >= time() - 365*24*60*60 && isset($current['transfer']) && ($current['transfer'] == $upd['key'] || !strlen($current['transfer']))) { | 104 } elseif (isset($current['updatemessage']) && $current['key'] != $upd['key']) { |
105 $upd['transferchain'] = $current['updatemessage']; | 105 $chain = $current; |
106 } else { | |
107 $chain = NULL; | |
106 } | 108 } |
107 } | 109 while ($chain && $chain->key == $upd['key']) $chain = isset($chain->transferchain) ? self::Decode($chain->transferchain) : NULL; |
108 if (isset($upd['transfer'])) { | 110 if ($chain && $chain->Verify() && $chain->serial >= time() - 365*24*60*60) $upd['transferchain'] = $chain->updatemessage; |
109 if (isset($upd['transferchain'])) { | 111 } |
110 $chain = self::Decode($upd['transferchain']); | 112 if (isset($upd['transfer']) && isset($upd['value']) && !is_null($upd['value'])) { |
111 while ($chain && $chain->key == $upd['key']) $chain = ($chain->Verify() && $chain->serial >= time() - 365*24*60*60 && isset($chain->transferchain)) ? self::Decode($chain->transferchain) : NULL; | 113 $chain = array('label' => $upd['label'], 'serial' => $upd['serial'], 'key' => $upd['key'], 'transfer' => $upd['transfer']); |
112 if ($chain && $chain->Verify() && $chain->serial >= time() - 365*24*60*60) $upd['transferchain'] = $chain->updatemessage; else unset($upd['transferchain']); | 114 if (isset($upd['expiration'])) $chain['expiration'] = $upd['expiration']; |
113 } | 115 if (isset($upd['transferchain'])) $chain['transferchain'] = $upd['transferchain']; |
114 if (isset($upd['value']) && !is_null($upd['value'])) { | 116 $chain = self::Create($chain, $seckey); |
115 $chain = array('label' => $upd['label'], 'serial' => $upd['serial'], 'key' => $upd['key'], 'transfer' => $upd['transfer']); | 117 if ($chain && strlen($chain->updatemessage) <= 0xffff) $upd['transferchain'] = $chain->updatemessage; |
116 if (isset($upd['expiration'])) $chain['expiration'] = $upd['expiration']; | |
117 if (isset($upd['transferchain'])) $chain['transferchain'] = $upd['transferchain']; | |
118 $chain = self::Create($chain, $seckey); | |
119 if ($chain && strlen($chain->updatemessage) <= 0xffff) $upd['transferchain'] = $chain->updatemessage; | |
120 } | |
121 } | 118 } |
122 $data = marc_encode_int32be($upd['serial']); | 119 $data = marc_encode_int32be($upd['serial']); |
123 $data .= chr(strlen($upd['label'])).$upd['label']; | 120 $data .= chr(strlen($upd['label'])).$upd['label']; |
124 $value = array(); | 121 $value = array(); |
125 if (isset($upd->_extensions)) foreach ($upd->_extensions as $identifier => $item) $value[$identifier] = $item; | 122 if (isset($upd->_extensions)) foreach ($upd->_extensions as $identifier => $item) $value[$identifier] = $item; |
131 foreach ($value as $identifier => $item) { | 128 foreach ($value as $identifier => $item) { |
132 $item = (string)$item; | 129 $item = (string)$item; |
133 if (strlen($item) > 0xffff) throw new Exception('Extension data too big'); | 130 if (strlen($item) > 0xffff) throw new Exception('Extension data too big'); |
134 $data .= chr($identifier).marc_encode_int16be(strlen($item)).$item; | 131 $data .= chr($identifier).marc_encode_int16be(strlen($item)).$item; |
135 } | 132 } |
136 if (isset($upd['value'])) $data .= self::EncodeValue($upd['value']); | 133 $data .= self::EncodeValue(isset($upd['value']) ? $upd['value'] : NULL); |
137 $data = nacl_crypto_sign_ed25519($data, $seckey); | 134 $data = nacl_crypto_sign_ed25519($data, $seckey); |
138 if (!strlen($data)) throw new Exception('Failed to sign data'); | 135 if (!strlen($data)) throw new Exception('Failed to sign data'); |
139 if (!strlen(nacl_crypto_sign_ed25519_open($data, $upd['key']))) throw new Exception('Key pair is not valid'); | 136 if (!strlen(nacl_crypto_sign_ed25519_open($data, $upd['key']))) throw new Exception('Key pair is not valid'); |
140 $data = chr(2).$upd['key'].$data; | 137 $data = chr(2).$upd['key'].$data; |
141 return self::Decode($data); | 138 return self::Decode($data); |