bdc89ec702420a355fff9879d7bce240848c61d3
[atutor.git] / mods / ldap / include / lib / rsa.inc.php
1 <?php
2 /*
3 Library which implements basic work with RSA encryption + decryption
4 also using authoring hash-cookie
5
6 maintainer smal (Serhiy Voyt)
7 smalgroup@gmail.com
8
9 Before use read carefully readme.txt and define varaibles below
10
11 This script is a part of ATutor LDAP-authoring module and it's required to right work all LDAP-authoring in ATutor. RSA
12
13 Before use this script you must check your system to folowing:
14 1.In your system must be installed OpenSSL package - required to generate private key.
15 2.In PHP must be enabled OpenSSL Functions - required to rsa.inc.php (see phpinfo() to check this)
16
17
18 */
19
20 define('EXP', 10001); 
21 define('PUBLIC_KEY', 'B1CBE3B5456CDF6D5A85F32715415A0F85ADAB289B7AD21CA2B925BD28231994B72856093C46D2A67CF8136CBDCF430C0EF7990403DAF4830CE4633D98A16703');
22 define('PRIVATE_KEY', AT_INCLUDE_PATH.'/lib/pk.pem');
23 define('TTL', 120);
24
25 function auth_cookie() {
26         
27         global $db;
28         
29         $hash = md5(mt_rand());
30         $time = time();
31
32         $sql    = "INSERT INTO ".TABLE_PREFIX."auth_cookie VALUES(0, '$hash', $time)";
33         $result = mysql_query($sql, $db);
34         
35         $id = mysql_insert_id();
36         
37         $auth_cookie = "|".$hash;
38         return $auth_cookie;
39 }
40
41
42 function rsa_decode($key,$enc_str){
43         
44         if ($fp = fopen($key, 'r')){
45                 $priv_key = fread($fp, 8192);
46                 fclose($fp);
47         }else{
48                  return false;
49                  exit;
50         }
51
52         if (!$keyh = openssl_get_privatekey($priv_key)) {
53                 return false;
54                 exit;
55         }
56         
57         $pub_key = openssl_pkey_get_public($key);
58         echo ($pub_key);
59
60         if (openssl_private_decrypt(base64_decode($enc_str), $decoded_string, $keyh)){
61                 return $decoded_string; 
62         }else{
63                  return false;
64                  exit;
65         }
66         
67         
68 }
69
70 function clear_auth_cookie(){
71         
72         global $db;
73         
74         $cur_time = time() - TTL;
75         $sql = "DELETE FROM ".TABLE_PREFIX."auth_cookie WHERE ttl < ".$cur_time;
76         mysql_query($sql,$db);
77         
78 }
79
80
81 function check_valid_login($decoded_auth){
82
83         global $db;
84         
85
86         list($password, $hash) = explode("|", $decoded_auth);
87         
88         $sql = "SELECT ttl FROM ".TABLE_PREFIX."auth_cookie WHERE hash ='$hash'";
89         
90         if ($result = mysql_query($sql, $db)){
91                 if ($row = mysql_fetch_array($result)){
92                         $ttl_valid = time() - $row['ttl'];
93                         if ($ttl_valid < TTL) {
94                                 return $password;
95                         }else{
96                                 return false;
97                         }
98                         
99                 }       
100         }
101         
102                         
103 }
104
105 ?>