openid.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * OpenID utility functions. Taken from Drupal 6 code (from dries)
  5. * @package chamilo.auth.openid
  6. */
  7. /**
  8. * Code
  9. */
  10. // Diffie-Hellman Key Exchange Default Value.
  11. define('OPENID_DH_DEFAULT_MOD', '155172898181473697471232257763715539915724801' .
  12. '966915404479707795314057629378541917580651227423698188993727816152646631' .
  13. '438561595825688188889951272158842675419950341258706556549803580104870537' .
  14. '681476726513255747040765857479291291572334510643245094715007229621094194' .
  15. '349783925984760375594985848253359305585439638443');
  16. // Constants for Diffie-Hellman key exchange computations.
  17. define('OPENID_DH_DEFAULT_GEN', '2');
  18. define('OPENID_SHA1_BLOCKSIZE', 64);
  19. define('OPENID_RAND_SOURCE', '/dev/urandom');
  20. // OpenID namespace URLs
  21. define('OPENID_NS_2_0', 'http://specs.openid.net/auth/2.0');
  22. define('OPENID_NS_1_1', 'http://openid.net/signon/1.1');
  23. define('OPENID_NS_1_0', 'http://openid.net/signon/1.0');
  24. /**
  25. * Performs an HTTP 302 redirect (for the 1.x protocol).
  26. * This function should be deprecated for 1.8.6.2 needs documentation
  27. */
  28. function openid_redirect_http($url, $message) {
  29. $query = array();
  30. foreach ($message as $key => $val) {
  31. $query[] = $key . '=' . urlencode($val);
  32. }
  33. $sep = (strpos($url, '?') === FALSE) ? '?' : '&';
  34. header('Location: ' . $url . $sep . implode('&', $query), TRUE, 302);
  35. //exit;
  36. }
  37. /**
  38. * Creates a js auto-submit redirect for (for the 2.x protocol)
  39. * This function should be deprecated for 1.8.6.2 needs documentation
  40. */
  41. function openid_redirect($url, $message) {
  42. $output = '<html><head><title>' . get_lang('OpenIDRedirect') . "</title></head>\n<body>";
  43. $output .= '<form method="post" action="' . $url . '" id="openid-redirect-form">';
  44. foreach ($message as $key => $value) {
  45. $output .='<input type="hidden" name="' . $key . '" value="' . $value . '">';
  46. }
  47. $output .= '<noscript><input type="submit" name="submit" value="' . get_lang('Send') . '"/></noscript>';
  48. $output .= '</form>';
  49. $output .= '<script type="text/javascript">document.getElementById("openid-redirect-form").submit();</script>';
  50. $output .= "</body></html>";
  51. return $output;
  52. }
  53. /**
  54. * Determine if the given identifier is an XRI ID.
  55. */
  56. function _openid_is_xri($identifier) {
  57. $firstchar = substr($identifier, 0, 1);
  58. if ($firstchar == "@" || $firstchar == "=")
  59. return TRUE;
  60. if (stristr($identifier, 'xri://') !== FALSE) {
  61. return TRUE;
  62. }
  63. return FALSE;
  64. }
  65. /**
  66. * Normalize the given identifier as per spec.
  67. */
  68. function _openid_normalize($identifier) {
  69. if (_openid_is_xri($identifier)) {
  70. return _openid_normalize_xri($identifier);
  71. } else {
  72. return _openid_normalize_url($identifier);
  73. }
  74. }
  75. function _openid_normalize_xri($xri) {
  76. $normalized_xri = $xri;
  77. if (stristr($xri, 'xri://') !== FALSE) {
  78. $normalized_xri = substr($xri, 6);
  79. }
  80. return $normalized_xri;
  81. }
  82. function _openid_normalize_url($url) {
  83. $normalized_url = $url;
  84. if (stristr($url, '://') === FALSE) {
  85. $normalized_url = 'http://' . $url;
  86. }
  87. if (substr_count($normalized_url, '/') < 3) {
  88. $normalized_url .= '/';
  89. }
  90. return $normalized_url;
  91. }
  92. /**
  93. * Create a serialized message packet as per spec: $key:$value\n .
  94. */
  95. function _openid_create_message($data) {
  96. $serialized = '';
  97. foreach ($data as $key => $value) {
  98. if ((strpos($key, ':') !== FALSE) || (strpos($key, "\n") !== FALSE) || (strpos($value, "\n") !== FALSE)) {
  99. return null;
  100. }
  101. $serialized .= "$key:$value\n";
  102. }
  103. return $serialized;
  104. }
  105. /**
  106. * Encode a message from _openid_create_message for HTTP Post
  107. */
  108. function _openid_encode_message($message) {
  109. $encoded_message = '';
  110. $items = explode("\n", $message);
  111. foreach ($items as $item) {
  112. $parts = explode(':', $item, 2);
  113. if (count($parts) == 2) {
  114. if ($encoded_message != '') {
  115. $encoded_message .= '&';
  116. }
  117. $encoded_message .= rawurlencode(trim($parts[0])) . '=' . rawurlencode(trim($parts[1]));
  118. }
  119. }
  120. return $encoded_message;
  121. }
  122. /**
  123. * Convert a direct communication message
  124. * into an associative array.
  125. */
  126. function _openid_parse_message($message) {
  127. $parsed_message = array();
  128. $items = explode("\n", $message);
  129. foreach ($items as $item) {
  130. $parts = explode(':', $item, 2);
  131. if (count($parts) == 2) {
  132. $parsed_message[$parts[0]] = $parts[1];
  133. }
  134. }
  135. return $parsed_message;
  136. }
  137. /**
  138. * Return a nonce value - formatted per OpenID spec.
  139. */
  140. function _openid_nonce() {
  141. // YYYY-MM-DDThh:mm:ssTZD UTC, plus some optional extra unique chars
  142. return gmstrftime('%Y-%m-%dT%H:%M:%S%Z') .
  143. chr(mt_rand(0, 25) + 65) .
  144. chr(mt_rand(0, 25) + 65) .
  145. chr(mt_rand(0, 25) + 65) .
  146. chr(mt_rand(0, 25) + 65);
  147. }
  148. /**
  149. * Pull the href attribute out of an html link element.
  150. */
  151. function _openid_link_href($rel, $html) {
  152. $rel = preg_quote($rel);
  153. preg_match('|<link\s+rel=["\'](.*)' . $rel . '(.*)["\'](.*)/?>|iU', $html, $matches);
  154. if (isset($matches[3])) {
  155. preg_match('|href=["\']([^"]+)["\']|iU', $matches[0], $href);
  156. return trim($href[1]);
  157. }
  158. return FALSE;
  159. }
  160. /**
  161. * Pull the http-equiv attribute out of an html meta element
  162. */
  163. function _openid_meta_httpequiv($equiv, $html) {
  164. preg_match('|<meta\s+http-equiv=["\']' . $equiv . '["\'](.*)/?>|iU', $html, $matches);
  165. if (isset($matches[1])) {
  166. preg_match('|content=["\']([^"]+)["\']|iU', $matches[1], $content);
  167. return $content[1];
  168. }
  169. return FALSE;
  170. }
  171. /**
  172. * Sign certain keys in a message
  173. * @param $association - object loaded from openid_association or openid_server_association table
  174. * - important fields are ->assoc_type and ->mac_key
  175. * @param $message_array - array of entire message about to be sent
  176. * @param $keys_to_sign - keys in the message to include in signature (without
  177. * 'openid.' appended)
  178. */
  179. function _openid_signature($association, $message_array, $keys_to_sign) {
  180. $signature = '';
  181. $sign_data = array();
  182. foreach ($keys_to_sign as $key) {
  183. if (isset($message_array['openid.' . $key])) {
  184. $sign_data[$key] = $message_array['openid.' . $key];
  185. }
  186. }
  187. $message = _openid_create_message($sign_data);
  188. $secret = base64_decode($association->mac_key);
  189. $signature = _openid_hmac($secret, $message);
  190. return base64_encode($signature);
  191. }
  192. function _openid_hmac($key, $text) {
  193. if (strlen($key) > OPENID_SHA1_BLOCKSIZE) {
  194. $key = _openid_sha1($key, true);
  195. }
  196. $key = str_pad($key, OPENID_SHA1_BLOCKSIZE, chr(0x00));
  197. $ipad = str_repeat(chr(0x36), OPENID_SHA1_BLOCKSIZE);
  198. $opad = str_repeat(chr(0x5c), OPENID_SHA1_BLOCKSIZE);
  199. $hash1 = _openid_sha1(($key ^ $ipad) . $text, true);
  200. $hmac = _openid_sha1(($key ^ $opad) . $hash1, true);
  201. return $hmac;
  202. }
  203. function _openid_sha1($text) {
  204. $hex = sha1($text);
  205. $raw = '';
  206. for ($i = 0; $i < 40; $i += 2) {
  207. $hexcode = substr($hex, $i, 2);
  208. $charcode = (int) base_convert($hexcode, 16, 10);
  209. $raw .= chr($charcode);
  210. }
  211. return $raw;
  212. }
  213. function _openid_dh_base64_to_long($str) {
  214. $b64 = base64_decode($str);
  215. return _openid_dh_binary_to_long($b64);
  216. }
  217. function _openid_dh_long_to_base64($str) {
  218. return base64_encode(_openid_dh_long_to_binary($str));
  219. }
  220. function _openid_dh_binary_to_long($str) {
  221. $bytes = array_merge(unpack('C*', $str));
  222. $n = 0;
  223. foreach ($bytes as $byte) {
  224. $n = bcmul($n, pow(2, 8));
  225. $n = bcadd($n, $byte);
  226. }
  227. return $n;
  228. }
  229. function _openid_dh_long_to_binary($long) {
  230. $cmp = bccomp($long, 0);
  231. if ($cmp < 0) {
  232. return FALSE;
  233. }
  234. if ($cmp == 0) {
  235. return "\x00";
  236. }
  237. $bytes = array();
  238. while (bccomp($long, 0) > 0) {
  239. array_unshift($bytes, bcmod($long, 256));
  240. $long = bcdiv($long, pow(2, 8));
  241. }
  242. if ($bytes && ($bytes[0] > 127)) {
  243. array_unshift($bytes, 0);
  244. }
  245. $string = '';
  246. foreach ($bytes as $byte) {
  247. $string .= pack('C', $byte);
  248. }
  249. return $string;
  250. }
  251. function _openid_dh_xorsecret($shared, $secret) {
  252. $dh_shared_str = _openid_dh_long_to_binary($shared);
  253. $sha1_dh_shared = _openid_sha1($dh_shared_str);
  254. $xsecret = "";
  255. for ($i = 0; $i < strlen($secret); $i++) {
  256. $xsecret .= chr(ord($secret[$i]) ^ ord($sha1_dh_shared[$i]));
  257. }
  258. return $xsecret;
  259. }
  260. function _openid_dh_rand($stop) {
  261. static $duplicate_cache = array();
  262. // Used as the key for the duplicate cache
  263. $rbytes = _openid_dh_long_to_binary($stop);
  264. if (array_key_exists($rbytes, $duplicate_cache)) {
  265. list($duplicate, $nbytes) = $duplicate_cache[$rbytes];
  266. } else {
  267. if ($rbytes[0] == "\x00") {
  268. $nbytes = strlen($rbytes) - 1;
  269. } else {
  270. $nbytes = strlen($rbytes);
  271. }
  272. $mxrand = bcpow(256, $nbytes);
  273. // If we get a number less than this, then it is in the
  274. // duplicated range.
  275. $duplicate = bcmod($mxrand, $stop);
  276. if (count($duplicate_cache) > 10) {
  277. $duplicate_cache = array();
  278. }
  279. $duplicate_cache[$rbytes] = array($duplicate, $nbytes);
  280. }
  281. do {
  282. $bytes = "\x00" . _openid_get_bytes($nbytes);
  283. $n = _openid_dh_binary_to_long($bytes);
  284. // Keep looping if this value is in the low duplicated range.
  285. } while (bccomp($n, $duplicate) < 0);
  286. return bcmod($n, $stop);
  287. }
  288. function _openid_get_bytes($num_bytes) {
  289. static $f = null;
  290. $bytes = '';
  291. if (!isset($f)) {
  292. $f = @fopen(OPENID_RAND_SOURCE, "r");
  293. }
  294. if (!$f) {
  295. // pseudorandom used
  296. $bytes = '';
  297. for ($i = 0; $i < $num_bytes; $i += 4) {
  298. $bytes .= pack('L', mt_rand());
  299. }
  300. $bytes = substr($bytes, 0, $num_bytes);
  301. } else {
  302. $bytes = fread($f, $num_bytes);
  303. }
  304. return $bytes;
  305. }
  306. /**
  307. * Fix PHP's habit of replacing '.' by '_' in posted data.
  308. */
  309. function _openid_fix_post(&$post) {
  310. //$extensions = module_invoke_all('openid', 'extension');
  311. foreach ($post as $key => $value) {
  312. if (strpos($key, 'openid_') === 0) {
  313. $fixed_key = str_replace('openid_', 'openid.', $key);
  314. $fixed_key = str_replace('openid.ns_', 'openid.ns.', $fixed_key);
  315. $fixed_key = str_replace('openid.sreg_', 'openid.sreg.', $fixed_key);
  316. //foreach ($extensions as $ext) {
  317. // $fixed_key = str_replace('openid.'.$ext.'_', 'openid.'.$ext.'.', $fixed_key);
  318. //}
  319. unset($post[$key]);
  320. $post[$fixed_key] = $value;
  321. }
  322. }
  323. }
  324. /**
  325. * Provide bcpowmod support for PHP4.
  326. */
  327. if (!function_exists('bcpowmod')) {
  328. function bcpowmod($base, $exp, $mod) {
  329. $square = bcmod($base, $mod);
  330. $result = 1;
  331. while (bccomp($exp, 0) > 0) {
  332. if (bcmod($exp, 2)) {
  333. $result = bcmod(bcmul($result, $square), $mod);
  334. }
  335. $square = bcmod(bcmul($square, $square), $mod);
  336. $exp = bcdiv($exp, 2);
  337. }
  338. return $result;
  339. }
  340. }