/** * getFromDB() find one (or all) CSR(s) for a person in the database. * * @param uid $person limit the query to the person's common-name * @param String|null $pubHash the hash of the public key * @return CSR|False The CSR for the person * @access public */ static function getFromDB($uid, $pubHash) { $res = false; if (!isset($uid) || !isset($pubHash)) { return false; } $query = "SELECT * FROM csr_cache WHERE "; $query .= "auth_key=:auth_key AND "; $query .= "common_name=:common_name"; $data = array(); $data['auth_key'] = $pubHash; $data['common_name'] = $uid; try { $csr_res = MDB2Wrapper::execute($query, null, $data); if (count($csr_res) != 1) { return false; } } catch (DBStatementException $dbse) { Logger::log_event(LOG_WARNING, __FILE__ . ":" . __LINE__ . "cannot retrieve CSR from DB. Server said: " . $dbse->getMessage()); return false; } catch (DBQueryException $dbqe) { Logger::log_event(LOG_WARNING, __FILE__ . ":" . __LINE__ . "cannot retrieve CSR from DB. Server said: " . $dbse->getMessage()); return false; } $csr_type = $csr_res[0]['type']; if ($csr_type == CSR_PKCS10::getCSRType()) { $csr = new CSR_PKCS10($csr_res[0]['csr']); } else { if ($csr_type == CSR_SPKAC::getCSRType()) { $csr = new CSR_SPKAC($csr_res[0]['csr']); } else { throw new CryptoElementException("Unsupported CSR type " . $csr_type . "!"); } } $csr->setUploadedDate($csr_res[0]['uploaded_date']); $csr->setUploadedFromIP(Output::formatIP($csr_res[0]['from_ip'], true)); if ($csr->getAuthToken() !== $pubHash) { Logger::log_event(LOG_ALERT, "Found CSR in database with hash {$pubHash} but " . "this does not correspond to pubkey. Corrupted db?"); return false; } return $csr; }