PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/util/keys.php

https://bitbucket.org/rev22/timekoin
PHP | 85 lines | 66 code | 15 blank | 4 comment | 28 complexity | 49da05ec619c4c30587f13eeb0e6a6d7 MD5 | raw file
  1. <?PHP
  2. include '/usr/share/timekoin/public/configuration.php';
  3. global $keys_quiet;
  4. global $keys_cmdline;
  5. $keys_cmdline = ((isset($_GET['cmdline']) && ($_GET['cmdline'] == 1)));
  6. $keys_quiet = ((isset($_GET['quiet']) && ($_GET['quiet'] == 1)));
  7. function println($l) {
  8. global $keys_cmdline;
  9. if (!$keys_cmdline) { echo "<br/>"; };
  10. echo $l;
  11. if ($keys_cmdline) { echo "\n"; };
  12. }
  13. if(mysql_pconnect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD) == FALSE)
  14. {
  15. println("mySQL connect failed...");
  16. }
  17. if(mysql_select_db(MYSQL_DATABASE) == FALSE)
  18. {
  19. println("mySQL select database failed...");
  20. }
  21. if ((!isset($_GET['resetkeys'])) || ($_GET['resetkeys'] != 1)) {
  22. $sql = "SELECT field_data FROM my_keys WHERE field_name = 'server_private_key' LIMIT 1";
  23. $result = mysql_query($sql);
  24. if ($result === FALSE) {
  25. println("Could not access database.");
  26. exit;
  27. }
  28. $result = mysql_result($result,0,"field_data");
  29. $result = (($result !== "Private Key Goes Here") &&
  30. ($result !== ""));
  31. if ($result) {
  32. if (!$keys_quiet) { println("A private key is already present."); }
  33. # A previous key was set
  34. if ($keys_cmdline) {
  35. if (!$keys_quiet) { println("Use --reset to force resetting the key."); }
  36. } else {
  37. echo "<br/><form method=get><input type=checkbox id=resetkeys name=resetkeys ><label for=resetkeys>Reset private and public keys for Timekoin</label><input type=Submit id=submit name=submit value=\"Reset\"></form>";
  38. }
  39. exit;
  40. }
  41. }
  42. // Create the keypair
  43. $res = openssl_pkey_new(array(
  44. 'private_key_bits' => 1536,
  45. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  46. ));
  47. // Get private key
  48. openssl_pkey_export($res, $privateKey);
  49. // Get public key
  50. $pubKey=openssl_pkey_get_details($res);
  51. $pubKey=$pubKey["key"];
  52. $sql = "UPDATE `my_keys` SET `field_data` = '$privateKey' WHERE `my_keys`.`field_name` = 'server_private_key' LIMIT 1";
  53. if(mysql_query($sql) == TRUE && empty($privateKey) == FALSE)
  54. {
  55. println("Private Key created and stored in Database");
  56. }
  57. else
  58. {
  59. println("Private Key creation failed (OpenSSL Failure)...");
  60. }
  61. $sql = "UPDATE `my_keys` SET `field_data` = '$pubKey' WHERE `my_keys`.`field_name` = 'server_public_key' LIMIT 1";
  62. if(mysql_query($sql) == TRUE && empty($pubKey) == FALSE)
  63. {
  64. println("Public Key created and stored in Database");
  65. }
  66. else
  67. {
  68. println("Public Key creation failed (OpenSSL Failure)...");
  69. }
  70. ?>