PublicKeyTokenProvider.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OC\Authentication\Token;
  29. use OC\Authentication\Exceptions\ExpiredTokenException;
  30. use OC\Authentication\Exceptions\InvalidTokenException;
  31. use OC\Authentication\Exceptions\PasswordlessTokenException;
  32. use OC\Authentication\Exceptions\TokenPasswordExpiredException;
  33. use OC\Authentication\Exceptions\WipeTokenException;
  34. use OCP\AppFramework\Db\DoesNotExistException;
  35. use OCP\AppFramework\Db\TTransactional;
  36. use OCP\AppFramework\Utility\ITimeFactory;
  37. use OCP\Authentication\Token\IToken as OCPIToken;
  38. use OCP\Cache\CappedMemoryCache;
  39. use OCP\IConfig;
  40. use OCP\IDBConnection;
  41. use OCP\IUserManager;
  42. use OCP\Security\ICrypto;
  43. use OCP\Security\IHasher;
  44. use Psr\Log\LoggerInterface;
  45. class PublicKeyTokenProvider implements IProvider {
  46. public const TOKEN_MIN_LENGTH = 22;
  47. use TTransactional;
  48. /** @var PublicKeyTokenMapper */
  49. private $mapper;
  50. /** @var ICrypto */
  51. private $crypto;
  52. /** @var IConfig */
  53. private $config;
  54. private IDBConnection $db;
  55. /** @var LoggerInterface */
  56. private $logger;
  57. /** @var ITimeFactory */
  58. private $time;
  59. /** @var CappedMemoryCache */
  60. private $cache;
  61. private IHasher $hasher;
  62. public function __construct(PublicKeyTokenMapper $mapper,
  63. ICrypto $crypto,
  64. IConfig $config,
  65. IDBConnection $db,
  66. LoggerInterface $logger,
  67. ITimeFactory $time,
  68. IHasher $hasher) {
  69. $this->mapper = $mapper;
  70. $this->crypto = $crypto;
  71. $this->config = $config;
  72. $this->db = $db;
  73. $this->logger = $logger;
  74. $this->time = $time;
  75. $this->cache = new CappedMemoryCache();
  76. $this->hasher = $hasher;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function generateToken(string $token,
  82. string $uid,
  83. string $loginName,
  84. ?string $password,
  85. string $name,
  86. int $type = OCPIToken::TEMPORARY_TOKEN,
  87. int $remember = OCPIToken::DO_NOT_REMEMBER): OCPIToken {
  88. if (strlen($token) < self::TOKEN_MIN_LENGTH) {
  89. $exception = new InvalidTokenException('Token is too short, minimum of ' . self::TOKEN_MIN_LENGTH . ' characters is required, ' . strlen($token) . ' characters given');
  90. $this->logger->error('Invalid token provided when generating new token', ['exception' => $exception]);
  91. throw $exception;
  92. }
  93. if (mb_strlen($name) > 128) {
  94. $name = mb_substr($name, 0, 120) . '…';
  95. }
  96. // We need to check against one old token to see if there is a password
  97. // hash that we can reuse for detecting outdated passwords
  98. $randomOldToken = $this->mapper->getFirstTokenForUser($uid);
  99. $oldTokenMatches = $randomOldToken && $randomOldToken->getPasswordHash() && $password !== null && $this->hasher->verify(sha1($password) . $password, $randomOldToken->getPasswordHash());
  100. $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember);
  101. if ($oldTokenMatches) {
  102. $dbToken->setPasswordHash($randomOldToken->getPasswordHash());
  103. }
  104. $this->mapper->insert($dbToken);
  105. if (!$oldTokenMatches && $password !== null) {
  106. $this->updatePasswords($uid, $password);
  107. }
  108. // Add the token to the cache
  109. $this->cache[$dbToken->getToken()] = $dbToken;
  110. return $dbToken;
  111. }
  112. public function getToken(string $tokenId): OCPIToken {
  113. /**
  114. * Token length: 72
  115. * @see \OC\Core\Controller\ClientFlowLoginController::generateAppPassword
  116. * @see \OC\Core\Controller\AppPasswordController::getAppPassword
  117. * @see \OC\Core\Command\User\AddAppPassword::execute
  118. * @see \OC\Core\Service\LoginFlowV2Service::flowDone
  119. * @see \OCA\Talk\MatterbridgeManager::generatePassword
  120. * @see \OCA\Preferred_Providers\Controller\PasswordController::generateAppPassword
  121. * @see \OCA\GlobalSiteSelector\TokenHandler::generateAppPassword
  122. *
  123. * Token length: 22-256 - https://www.php.net/manual/en/session.configuration.php#ini.session.sid-length
  124. * @see \OC\User\Session::createSessionToken
  125. *
  126. * Token length: 29
  127. * @see \OCA\Settings\Controller\AuthSettingsController::generateRandomDeviceToken
  128. * @see \OCA\Registration\Service\RegistrationService::generateAppPassword
  129. */
  130. if (strlen($tokenId) < self::TOKEN_MIN_LENGTH) {
  131. throw new InvalidTokenException('Token is too short for a generated token, should be the password during basic auth');
  132. }
  133. $tokenHash = $this->hashToken($tokenId);
  134. if (isset($this->cache[$tokenHash])) {
  135. if ($this->cache[$tokenHash] instanceof DoesNotExistException) {
  136. $ex = $this->cache[$tokenHash];
  137. throw new InvalidTokenException("Token does not exist: " . $ex->getMessage(), 0, $ex);
  138. }
  139. $token = $this->cache[$tokenHash];
  140. } else {
  141. try {
  142. $token = $this->mapper->getToken($tokenHash);
  143. $this->cache[$token->getToken()] = $token;
  144. } catch (DoesNotExistException $ex) {
  145. try {
  146. $token = $this->mapper->getToken($this->hashTokenWithEmptySecret($tokenId));
  147. $this->cache[$token->getToken()] = $token;
  148. $this->rotate($token, $tokenId, $tokenId);
  149. } catch (DoesNotExistException $ex2) {
  150. $this->cache[$tokenHash] = $ex2;
  151. throw new InvalidTokenException("Token does not exist: " . $ex->getMessage(), 0, $ex);
  152. }
  153. }
  154. }
  155. if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) {
  156. throw new ExpiredTokenException($token);
  157. }
  158. if ($token->getType() === OCPIToken::WIPE_TOKEN) {
  159. throw new WipeTokenException($token);
  160. }
  161. if ($token->getPasswordInvalid() === true) {
  162. //The password is invalid we should throw an TokenPasswordExpiredException
  163. throw new TokenPasswordExpiredException($token);
  164. }
  165. return $token;
  166. }
  167. public function getTokenById(int $tokenId): OCPIToken {
  168. try {
  169. $token = $this->mapper->getTokenById($tokenId);
  170. } catch (DoesNotExistException $ex) {
  171. throw new InvalidTokenException("Token with ID $tokenId does not exist: " . $ex->getMessage(), 0, $ex);
  172. }
  173. if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) {
  174. throw new ExpiredTokenException($token);
  175. }
  176. if ($token->getType() === OCPIToken::WIPE_TOKEN) {
  177. throw new WipeTokenException($token);
  178. }
  179. if ($token->getPasswordInvalid() === true) {
  180. //The password is invalid we should throw an TokenPasswordExpiredException
  181. throw new TokenPasswordExpiredException($token);
  182. }
  183. return $token;
  184. }
  185. public function renewSessionToken(string $oldSessionId, string $sessionId): OCPIToken {
  186. $this->cache->clear();
  187. return $this->atomic(function () use ($oldSessionId, $sessionId) {
  188. $token = $this->getToken($oldSessionId);
  189. if (!($token instanceof PublicKeyToken)) {
  190. throw new InvalidTokenException("Invalid token type");
  191. }
  192. $password = null;
  193. if (!is_null($token->getPassword())) {
  194. $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
  195. $password = $this->decryptPassword($token->getPassword(), $privateKey);
  196. }
  197. $newToken = $this->generateToken(
  198. $sessionId,
  199. $token->getUID(),
  200. $token->getLoginName(),
  201. $password,
  202. $token->getName(),
  203. OCPIToken::TEMPORARY_TOKEN,
  204. $token->getRemember()
  205. );
  206. $this->mapper->delete($token);
  207. return $newToken;
  208. }, $this->db);
  209. }
  210. public function invalidateToken(string $token) {
  211. $this->cache->clear();
  212. $this->mapper->invalidate($this->hashToken($token));
  213. $this->mapper->invalidate($this->hashTokenWithEmptySecret($token));
  214. }
  215. public function invalidateTokenById(string $uid, int $id) {
  216. $this->cache->clear();
  217. $this->mapper->deleteById($uid, $id);
  218. }
  219. public function invalidateOldTokens() {
  220. $this->cache->clear();
  221. $olderThan = $this->time->getTime() - $this->config->getSystemValueInt('session_lifetime', 60 * 60 * 24);
  222. $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']);
  223. $this->mapper->invalidateOld($olderThan, OCPIToken::DO_NOT_REMEMBER);
  224. $rememberThreshold = $this->time->getTime() - $this->config->getSystemValueInt('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
  225. $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']);
  226. $this->mapper->invalidateOld($rememberThreshold, OCPIToken::REMEMBER);
  227. }
  228. public function invalidateLastUsedBefore(string $uid, int $before): void {
  229. $this->cache->clear();
  230. $this->mapper->invalidateLastUsedBefore($uid, $before);
  231. }
  232. public function updateToken(OCPIToken $token) {
  233. $this->cache->clear();
  234. if (!($token instanceof PublicKeyToken)) {
  235. throw new InvalidTokenException("Invalid token type");
  236. }
  237. $this->mapper->update($token);
  238. }
  239. public function updateTokenActivity(OCPIToken $token) {
  240. $this->cache->clear();
  241. if (!($token instanceof PublicKeyToken)) {
  242. throw new InvalidTokenException("Invalid token type");
  243. }
  244. $activityInterval = $this->config->getSystemValueInt('token_auth_activity_update', 60);
  245. $activityInterval = min(max($activityInterval, 0), 300);
  246. /** @var PublicKeyToken $token */
  247. $now = $this->time->getTime();
  248. if ($token->getLastActivity() < ($now - $activityInterval)) {
  249. $token->setLastActivity($now);
  250. $this->mapper->updateActivity($token, $now);
  251. }
  252. }
  253. public function getTokenByUser(string $uid): array {
  254. return $this->mapper->getTokenByUser($uid);
  255. }
  256. public function getPassword(OCPIToken $savedToken, string $tokenId): string {
  257. if (!($savedToken instanceof PublicKeyToken)) {
  258. throw new InvalidTokenException("Invalid token type");
  259. }
  260. if ($savedToken->getPassword() === null) {
  261. throw new PasswordlessTokenException();
  262. }
  263. // Decrypt private key with tokenId
  264. $privateKey = $this->decrypt($savedToken->getPrivateKey(), $tokenId);
  265. // Decrypt password with private key
  266. return $this->decryptPassword($savedToken->getPassword(), $privateKey);
  267. }
  268. public function setPassword(OCPIToken $token, string $tokenId, string $password) {
  269. $this->cache->clear();
  270. if (!($token instanceof PublicKeyToken)) {
  271. throw new InvalidTokenException("Invalid token type");
  272. }
  273. $this->atomic(function () use ($password, $token) {
  274. // When changing passwords all temp tokens are deleted
  275. $this->mapper->deleteTempToken($token);
  276. // Update the password for all tokens
  277. $tokens = $this->mapper->getTokenByUser($token->getUID());
  278. $hashedPassword = $this->hashPassword($password);
  279. foreach ($tokens as $t) {
  280. $publicKey = $t->getPublicKey();
  281. $t->setPassword($this->encryptPassword($password, $publicKey));
  282. $t->setPasswordHash($hashedPassword);
  283. $this->updateToken($t);
  284. }
  285. }, $this->db);
  286. }
  287. private function hashPassword(string $password): string {
  288. return $this->hasher->hash(sha1($password) . $password);
  289. }
  290. public function rotate(OCPIToken $token, string $oldTokenId, string $newTokenId): OCPIToken {
  291. $this->cache->clear();
  292. if (!($token instanceof PublicKeyToken)) {
  293. throw new InvalidTokenException("Invalid token type");
  294. }
  295. // Decrypt private key with oldTokenId
  296. $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId);
  297. // Encrypt with the new token
  298. $token->setPrivateKey($this->encrypt($privateKey, $newTokenId));
  299. $token->setToken($this->hashToken($newTokenId));
  300. $this->updateToken($token);
  301. return $token;
  302. }
  303. private function encrypt(string $plaintext, string $token): string {
  304. $secret = $this->config->getSystemValueString('secret');
  305. return $this->crypto->encrypt($plaintext, $token . $secret);
  306. }
  307. /**
  308. * @throws InvalidTokenException
  309. */
  310. private function decrypt(string $cipherText, string $token): string {
  311. $secret = $this->config->getSystemValueString('secret');
  312. try {
  313. return $this->crypto->decrypt($cipherText, $token . $secret);
  314. } catch (\Exception $ex) {
  315. // Retry with empty secret as a fallback for instances where the secret might not have been set by accident
  316. try {
  317. return $this->crypto->decrypt($cipherText, $token);
  318. } catch (\Exception $ex2) {
  319. // Delete the invalid token
  320. $this->invalidateToken($token);
  321. throw new InvalidTokenException("Could not decrypt token password: " . $ex->getMessage(), 0, $ex2);
  322. }
  323. }
  324. }
  325. private function encryptPassword(string $password, string $publicKey): string {
  326. openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING);
  327. $encryptedPassword = base64_encode($encryptedPassword);
  328. return $encryptedPassword;
  329. }
  330. private function decryptPassword(string $encryptedPassword, string $privateKey): string {
  331. $encryptedPassword = base64_decode($encryptedPassword);
  332. openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);
  333. return $password;
  334. }
  335. private function hashToken(string $token): string {
  336. $secret = $this->config->getSystemValueString('secret');
  337. return hash('sha512', $token . $secret);
  338. }
  339. /**
  340. * @deprecated Fallback for instances where the secret might not have been set by accident
  341. */
  342. private function hashTokenWithEmptySecret(string $token): string {
  343. return hash('sha512', $token);
  344. }
  345. /**
  346. * @throws \RuntimeException when OpenSSL reports a problem
  347. */
  348. private function newToken(string $token,
  349. string $uid,
  350. string $loginName,
  351. $password,
  352. string $name,
  353. int $type,
  354. int $remember): PublicKeyToken {
  355. $dbToken = new PublicKeyToken();
  356. $dbToken->setUid($uid);
  357. $dbToken->setLoginName($loginName);
  358. $config = array_merge([
  359. 'digest_alg' => 'sha512',
  360. 'private_key_bits' => $password !== null && strlen($password) > 250 ? 4096 : 2048,
  361. ], $this->config->getSystemValue('openssl', []));
  362. // Generate new key
  363. $res = openssl_pkey_new($config);
  364. if ($res === false) {
  365. $this->logOpensslError();
  366. throw new \RuntimeException('OpenSSL reported a problem');
  367. }
  368. if (openssl_pkey_export($res, $privateKey, null, $config) === false) {
  369. $this->logOpensslError();
  370. throw new \RuntimeException('OpenSSL reported a problem');
  371. }
  372. // Extract the public key from $res to $pubKey
  373. $publicKey = openssl_pkey_get_details($res);
  374. $publicKey = $publicKey['key'];
  375. $dbToken->setPublicKey($publicKey);
  376. $dbToken->setPrivateKey($this->encrypt($privateKey, $token));
  377. if (!is_null($password) && $this->config->getSystemValueBool('auth.storeCryptedPassword', true)) {
  378. if (strlen($password) > IUserManager::MAX_PASSWORD_LENGTH) {
  379. throw new \RuntimeException('Trying to save a password with more than 469 characters is not supported. If you want to use big passwords, disable the auth.storeCryptedPassword option in config.php');
  380. }
  381. $dbToken->setPassword($this->encryptPassword($password, $publicKey));
  382. $dbToken->setPasswordHash($this->hashPassword($password));
  383. }
  384. $dbToken->setName($name);
  385. $dbToken->setToken($this->hashToken($token));
  386. $dbToken->setType($type);
  387. $dbToken->setRemember($remember);
  388. $dbToken->setLastActivity($this->time->getTime());
  389. $dbToken->setLastCheck($this->time->getTime());
  390. $dbToken->setVersion(PublicKeyToken::VERSION);
  391. return $dbToken;
  392. }
  393. public function markPasswordInvalid(OCPIToken $token, string $tokenId) {
  394. $this->cache->clear();
  395. if (!($token instanceof PublicKeyToken)) {
  396. throw new InvalidTokenException("Invalid token type");
  397. }
  398. $token->setPasswordInvalid(true);
  399. $this->mapper->update($token);
  400. }
  401. public function updatePasswords(string $uid, string $password) {
  402. $this->cache->clear();
  403. // prevent setting an empty pw as result of pw-less-login
  404. if ($password === '' || !$this->config->getSystemValueBool('auth.storeCryptedPassword', true)) {
  405. return;
  406. }
  407. $this->atomic(function () use ($password, $uid) {
  408. // Update the password for all tokens
  409. $tokens = $this->mapper->getTokenByUser($uid);
  410. $newPasswordHash = null;
  411. /**
  412. * - true: The password hash could not be verified anymore
  413. * and the token needs to be updated with the newly encrypted password
  414. * - false: The hash could still be verified
  415. * - missing: The hash needs to be verified
  416. */
  417. $hashNeedsUpdate = [];
  418. foreach ($tokens as $t) {
  419. if (!isset($hashNeedsUpdate[$t->getPasswordHash()])) {
  420. if ($t->getPasswordHash() === null) {
  421. $hashNeedsUpdate[$t->getPasswordHash() ?: ''] = true;
  422. } elseif (!$this->hasher->verify(sha1($password) . $password, $t->getPasswordHash())) {
  423. $hashNeedsUpdate[$t->getPasswordHash() ?: ''] = true;
  424. } else {
  425. $hashNeedsUpdate[$t->getPasswordHash() ?: ''] = false;
  426. }
  427. }
  428. $needsUpdating = $hashNeedsUpdate[$t->getPasswordHash() ?: ''] ?? true;
  429. if ($needsUpdating) {
  430. if ($newPasswordHash === null) {
  431. $newPasswordHash = $this->hashPassword($password);
  432. }
  433. $publicKey = $t->getPublicKey();
  434. $t->setPassword($this->encryptPassword($password, $publicKey));
  435. $t->setPasswordHash($newPasswordHash);
  436. $t->setPasswordInvalid(false);
  437. $this->updateToken($t);
  438. }
  439. }
  440. // If password hashes are different we update them all to be equal so
  441. // that the next execution only needs to verify once
  442. if (count($hashNeedsUpdate) > 1) {
  443. $newPasswordHash = $this->hashPassword($password);
  444. $this->mapper->updateHashesForUser($uid, $newPasswordHash);
  445. }
  446. }, $this->db);
  447. }
  448. private function logOpensslError() {
  449. $errors = [];
  450. while ($error = openssl_error_string()) {
  451. $errors[] = $error;
  452. }
  453. $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
  454. }
  455. }