function user_key_info($uid) { global $db; // User key info $query = $db->query("SELECT fid4 FROM mybb_userfields WHERE ufid='{$uid}'"); $userPubkey = $db->fetch_field($query, "fid4"); if ($userPubkey == "") { return false; } // Set up keyring $keyring = ".trkeys"; putenv("GNUPGHOME={$keyring}"); // Import key $gpg = new gnupg(); $gpg->seterrormode(gnupg::ERROR_WARNING); $wkey = $gpg->import($userPubkey); if (isset($wkey['fingerprint'])) { $keystatus = "OK"; $key_fingerprint = $wkey['fingerprint']; } else { $keystatus = "Brak"; $key_fingerprint = $lanag->na; } return ["key" => $userPubkey, "status" => $keystatus, "fingerprint" => $key_fingerprint]; }
/** * new gnupg object * * @return object */ function new_gnupg() { /** @noinspection PhpUndefinedClassInspection */ $gnupg = new gnupg(); putenv('GNUPGHOME='.GNUPGHOME); if (DEBUG) { /** @noinspection PhpUndefinedMethodInspection PhpUndefinedConstantInspection */ $gnupg->seterrormode(GNUPG_ERROR_WARNING); } return $gnupg; }
$randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } if ($userPubkey != "" && $userPubkey != "None") { // GPG login $rawChallenge = generateString(GPG_CHALLENGE_SIZE); $_SESSION['LOGIN_ST2_RAW_CHALLENGE'] = $rawChallenge; $_SESSION['LOGIN_ST2_LOGINDATA'] = serialize($loginhandler); putenv('GNUPGHOME=/tmp'); // Encrypt challenge using user's public key $gpg = new gnupg(); // Set error mode to exception $gpg->seterrormode(gnupg::ERROR_WARNING); // Import user's pubkey $gpgImportInfo = $gpg->import($userPubkey); if ($gpgImportInfo == false || $gpgImportInfo['fingerprint'] == "") { error($lang->error_invalidgpg); } // Add encryption key $gpgAddKey = $gpg->addencryptkey($gpgImportInfo['fingerprint']); $encryptedChallenge = $gpg->encrypt($rawChallenge); $plugins->add_hook("member_do_login_end", "add_gpg_vars"); function add_gpg_vars() { global $encryptedChallenge, $rawChallenge, $redirectUrl, $mybb; $redirectUrl = $mybb->input['url']; } $plugins->run_hooks("member_do_login_end");
/** * GnuPG decrypt and verify a message using the recipient private key * Returns an array in the format: array (0 => $message, 1 => $signatures) * http://devzone.zend.com/article/3753-Using-GnuPG-with-PHP * NOTE: GnuPG must be installed and configured with PHP. * The recipient must be in your private key ring * @param string $recipient Recipient Indentity (e.g. email address) * @param string $recipientKey Recipient Secret Key * @param string $message Message to decrypt * @return array */ public static function _verifyGnuPG($recipient, $recipientKey, $message) { // Create new GnuPG object $gpg = new \gnupg(); // Set error mode $gpg->seterrormode(\gnupg::ERROR_EXCEPTION); // Add the recipient decryption key $gpg->adddecryptkey($recipient, $recipientKey); // Set decrpyted string $decrypted = ''; // Set decrypted and verification data $return[1] = $gpg->decryptverify($message, $decrypted); // For each signature foreach ($return[1] as $key => &$signature) { // Get further user data $signature['user'] = $gpg->keyinfo($signature['fingerprint']); } // Add decrypted data to return array $return[0] = $decrypted; // Return decryption data return $return; }
* @license OXPL */ global $m; CCanDo::checkAdmin(); $module = CValue::get("module"); $file = isset($_FILES['import']) ? $_FILES['import'] : null; $fingerprint = $keydata = null; if ($file) { $keydata = file_get_contents($file['tmp_name']); if ($module) { $path = CAppUI::conf("{$module} gnupg_path"); } $gpg = new gnupg(); if ($module && $path) { putenv("HOME={$path}"); } $gpg->seterrormode(gnupg::ERROR_EXCEPTION); try { $info = $gpg->import($keydata); } catch (Exception $e) { mbTrace($e->getMessage()); } if (array_key_exists("fingerprint", $info)) { $fingerprint = $info['fingerprint']; } } // Création du template $smarty = new CSmartyDP(); $smarty->assign("fingerprint", $fingerprint); $smarty->assign("keydata", $keydata); $smarty->display("ajax_import_key.tpl");
public function encrypt_and_sign_message($recipient_key_id, $plaintext, $signer_key_id, $passphrase) { $this->set_env(); try { $gpg = new gnupg(); // throw exception if error occurs $gpg->seterrormode(gnupg::ERROR_EXCEPTION); $gpg->addencryptkey($recipient_key_id); $gpg->addsignkey($signer_key_id, $passphrase); $cipher_text = $gpg->encryptsign($plaintext); $this->restore_env(); return $cipher_text; } catch (Exception $e) { // restore the envelope $this->restore_env(); // re-throw the exception throw $e; } }