PublicKeyTokenProvider.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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\TokenPasswordExpiredException;
  32. use OC\Authentication\Exceptions\PasswordlessTokenException;
  33. use OC\Authentication\Exceptions\WipeTokenException;
  34. use OCP\AppFramework\Db\TTransactional;
  35. use OCP\Cache\CappedMemoryCache;
  36. use OCP\AppFramework\Db\DoesNotExistException;
  37. use OCP\AppFramework\Utility\ITimeFactory;
  38. use OCP\IConfig;
  39. use OCP\IDBConnection;
  40. use OCP\Security\ICrypto;
  41. use Psr\Log\LoggerInterface;
  42. class PublicKeyTokenProvider implements IProvider {
  43. use TTransactional;
  44. /** @var PublicKeyTokenMapper */
  45. private $mapper;
  46. /** @var ICrypto */
  47. private $crypto;
  48. /** @var IConfig */
  49. private $config;
  50. private IDBConnection $db;
  51. /** @var LoggerInterface */
  52. private $logger;
  53. /** @var ITimeFactory */
  54. private $time;
  55. /** @var CappedMemoryCache */
  56. private $cache;
  57. public function __construct(PublicKeyTokenMapper $mapper,
  58. ICrypto $crypto,
  59. IConfig $config,
  60. IDBConnection $db,
  61. LoggerInterface $logger,
  62. ITimeFactory $time) {
  63. $this->mapper = $mapper;
  64. $this->crypto = $crypto;
  65. $this->config = $config;
  66. $this->db = $db;
  67. $this->logger = $logger;
  68. $this->time = $time;
  69. $this->cache = new CappedMemoryCache();
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function generateToken(string $token,
  75. string $uid,
  76. string $loginName,
  77. ?string $password,
  78. string $name,
  79. int $type = IToken::TEMPORARY_TOKEN,
  80. int $remember = IToken::DO_NOT_REMEMBER): IToken {
  81. if (mb_strlen($name) > 128) {
  82. $name = mb_substr($name, 0, 120) . '…';
  83. }
  84. $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember);
  85. $this->mapper->insert($dbToken);
  86. // Add the token to the cache
  87. $this->cache[$dbToken->getToken()] = $dbToken;
  88. return $dbToken;
  89. }
  90. public function getToken(string $tokenId): IToken {
  91. $tokenHash = $this->hashToken($tokenId);
  92. if (isset($this->cache[$tokenHash])) {
  93. if ($this->cache[$tokenHash] instanceof DoesNotExistException) {
  94. $ex = $this->cache[$tokenHash];
  95. throw new InvalidTokenException("Token does not exist: " . $ex->getMessage(), 0, $ex);
  96. }
  97. $token = $this->cache[$tokenHash];
  98. } else {
  99. try {
  100. $token = $this->mapper->getToken($this->hashToken($tokenId));
  101. $this->cache[$token->getToken()] = $token;
  102. } catch (DoesNotExistException $ex) {
  103. try {
  104. $token = $this->mapper->getToken($this->hashTokenWithEmptySecret($tokenId));
  105. $this->cache[$token->getToken()] = $token;
  106. $this->rotate($token, $tokenId, $tokenId);
  107. } catch (DoesNotExistException $ex2) {
  108. $this->cache[$tokenHash] = $ex2;
  109. throw new InvalidTokenException("Token does not exist: " . $ex->getMessage(), 0, $ex);
  110. }
  111. }
  112. }
  113. if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) {
  114. throw new ExpiredTokenException($token);
  115. }
  116. if ($token->getType() === IToken::WIPE_TOKEN) {
  117. throw new WipeTokenException($token);
  118. }
  119. if ($token->getPasswordInvalid() === true) {
  120. //The password is invalid we should throw an TokenPasswordExpiredException
  121. throw new TokenPasswordExpiredException($token);
  122. }
  123. return $token;
  124. }
  125. public function getTokenById(int $tokenId): IToken {
  126. try {
  127. $token = $this->mapper->getTokenById($tokenId);
  128. } catch (DoesNotExistException $ex) {
  129. throw new InvalidTokenException("Token with ID $tokenId does not exist: " . $ex->getMessage(), 0, $ex);
  130. }
  131. if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) {
  132. throw new ExpiredTokenException($token);
  133. }
  134. if ($token->getType() === IToken::WIPE_TOKEN) {
  135. throw new WipeTokenException($token);
  136. }
  137. if ($token->getPasswordInvalid() === true) {
  138. //The password is invalid we should throw an TokenPasswordExpiredException
  139. throw new TokenPasswordExpiredException($token);
  140. }
  141. return $token;
  142. }
  143. public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
  144. $this->cache->clear();
  145. return $this->atomic(function () use ($oldSessionId, $sessionId) {
  146. $token = $this->getToken($oldSessionId);
  147. if (!($token instanceof PublicKeyToken)) {
  148. throw new InvalidTokenException("Invalid token type");
  149. }
  150. $password = null;
  151. if (!is_null($token->getPassword())) {
  152. $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
  153. $password = $this->decryptPassword($token->getPassword(), $privateKey);
  154. }
  155. $newToken = $this->generateToken(
  156. $sessionId,
  157. $token->getUID(),
  158. $token->getLoginName(),
  159. $password,
  160. $token->getName(),
  161. IToken::TEMPORARY_TOKEN,
  162. $token->getRemember()
  163. );
  164. $this->mapper->delete($token);
  165. return $newToken;
  166. }, $this->db);
  167. }
  168. public function invalidateToken(string $token) {
  169. $this->cache->clear();
  170. $this->mapper->invalidate($this->hashToken($token));
  171. $this->mapper->invalidate($this->hashTokenWithEmptySecret($token));
  172. }
  173. public function invalidateTokenById(string $uid, int $id) {
  174. $this->cache->clear();
  175. $this->mapper->deleteById($uid, $id);
  176. }
  177. public function invalidateOldTokens() {
  178. $this->cache->clear();
  179. $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24);
  180. $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']);
  181. $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER);
  182. $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
  183. $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']);
  184. $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER);
  185. }
  186. public function updateToken(IToken $token) {
  187. $this->cache->clear();
  188. if (!($token instanceof PublicKeyToken)) {
  189. throw new InvalidTokenException("Invalid token type");
  190. }
  191. $this->mapper->update($token);
  192. }
  193. public function updateTokenActivity(IToken $token) {
  194. $this->cache->clear();
  195. if (!($token instanceof PublicKeyToken)) {
  196. throw new InvalidTokenException("Invalid token type");
  197. }
  198. $activityInterval = $this->config->getSystemValueInt('token_auth_activity_update', 60);
  199. $activityInterval = min(max($activityInterval, 0), 300);
  200. /** @var PublicKeyToken $token */
  201. $now = $this->time->getTime();
  202. if ($token->getLastActivity() < ($now - $activityInterval)) {
  203. $token->setLastActivity($now);
  204. $this->mapper->updateActivity($token, $now);
  205. }
  206. }
  207. public function getTokenByUser(string $uid): array {
  208. return $this->mapper->getTokenByUser($uid);
  209. }
  210. public function getPassword(IToken $savedToken, string $tokenId): string {
  211. if (!($savedToken instanceof PublicKeyToken)) {
  212. throw new InvalidTokenException("Invalid token type");
  213. }
  214. if ($savedToken->getPassword() === null) {
  215. throw new PasswordlessTokenException();
  216. }
  217. // Decrypt private key with tokenId
  218. $privateKey = $this->decrypt($savedToken->getPrivateKey(), $tokenId);
  219. // Decrypt password with private key
  220. return $this->decryptPassword($savedToken->getPassword(), $privateKey);
  221. }
  222. public function setPassword(IToken $token, string $tokenId, string $password) {
  223. $this->cache->clear();
  224. if (!($token instanceof PublicKeyToken)) {
  225. throw new InvalidTokenException("Invalid token type");
  226. }
  227. // When changing passwords all temp tokens are deleted
  228. $this->mapper->deleteTempToken($token);
  229. // Update the password for all tokens
  230. $tokens = $this->mapper->getTokenByUser($token->getUID());
  231. foreach ($tokens as $t) {
  232. $publicKey = $t->getPublicKey();
  233. $t->setPassword($this->encryptPassword($password, $publicKey));
  234. $this->updateToken($t);
  235. }
  236. }
  237. public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
  238. $this->cache->clear();
  239. if (!($token instanceof PublicKeyToken)) {
  240. throw new InvalidTokenException("Invalid token type");
  241. }
  242. // Decrypt private key with oldTokenId
  243. $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId);
  244. // Encrypt with the new token
  245. $token->setPrivateKey($this->encrypt($privateKey, $newTokenId));
  246. $token->setToken($this->hashToken($newTokenId));
  247. $this->updateToken($token);
  248. return $token;
  249. }
  250. private function encrypt(string $plaintext, string $token): string {
  251. $secret = $this->config->getSystemValue('secret');
  252. return $this->crypto->encrypt($plaintext, $token . $secret);
  253. }
  254. /**
  255. * @throws InvalidTokenException
  256. */
  257. private function decrypt(string $cipherText, string $token): string {
  258. $secret = $this->config->getSystemValue('secret');
  259. try {
  260. return $this->crypto->decrypt($cipherText, $token . $secret);
  261. } catch (\Exception $ex) {
  262. // Retry with empty secret as a fallback for instances where the secret might not have been set by accident
  263. try {
  264. return $this->crypto->decrypt($cipherText, $token);
  265. } catch (\Exception $ex2) {
  266. // Delete the invalid token
  267. $this->invalidateToken($token);
  268. throw new InvalidTokenException("Could not decrypt token password: " . $ex->getMessage(), 0, $ex2);
  269. }
  270. }
  271. }
  272. private function encryptPassword(string $password, string $publicKey): string {
  273. openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING);
  274. $encryptedPassword = base64_encode($encryptedPassword);
  275. return $encryptedPassword;
  276. }
  277. private function decryptPassword(string $encryptedPassword, string $privateKey): string {
  278. $encryptedPassword = base64_decode($encryptedPassword);
  279. openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);
  280. return $password;
  281. }
  282. private function hashToken(string $token): string {
  283. $secret = $this->config->getSystemValue('secret');
  284. return hash('sha512', $token . $secret);
  285. }
  286. /**
  287. * @deprecated Fallback for instances where the secret might not have been set by accident
  288. */
  289. private function hashTokenWithEmptySecret(string $token): string {
  290. return hash('sha512', $token);
  291. }
  292. /**
  293. * @throws \RuntimeException when OpenSSL reports a problem
  294. */
  295. private function newToken(string $token,
  296. string $uid,
  297. string $loginName,
  298. $password,
  299. string $name,
  300. int $type,
  301. int $remember): PublicKeyToken {
  302. $dbToken = new PublicKeyToken();
  303. $dbToken->setUid($uid);
  304. $dbToken->setLoginName($loginName);
  305. $config = array_merge([
  306. 'digest_alg' => 'sha512',
  307. 'private_key_bits' => $password !== null && strlen($password) > 250 ? 4096 : 2048,
  308. ], $this->config->getSystemValue('openssl', []));
  309. // Generate new key
  310. $res = openssl_pkey_new($config);
  311. if ($res === false) {
  312. $this->logOpensslError();
  313. throw new \RuntimeException('OpenSSL reported a problem');
  314. }
  315. if (openssl_pkey_export($res, $privateKey, null, $config) === false) {
  316. $this->logOpensslError();
  317. throw new \RuntimeException('OpenSSL reported a problem');
  318. }
  319. // Extract the public key from $res to $pubKey
  320. $publicKey = openssl_pkey_get_details($res);
  321. $publicKey = $publicKey['key'];
  322. $dbToken->setPublicKey($publicKey);
  323. $dbToken->setPrivateKey($this->encrypt($privateKey, $token));
  324. if (!is_null($password) && $this->config->getSystemValueBool('auth.storeCryptedPassword', true)) {
  325. if (strlen($password) > 469) {
  326. 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');
  327. }
  328. $dbToken->setPassword($this->encryptPassword($password, $publicKey));
  329. }
  330. $dbToken->setName($name);
  331. $dbToken->setToken($this->hashToken($token));
  332. $dbToken->setType($type);
  333. $dbToken->setRemember($remember);
  334. $dbToken->setLastActivity($this->time->getTime());
  335. $dbToken->setLastCheck($this->time->getTime());
  336. $dbToken->setVersion(PublicKeyToken::VERSION);
  337. return $dbToken;
  338. }
  339. public function markPasswordInvalid(IToken $token, string $tokenId) {
  340. $this->cache->clear();
  341. if (!($token instanceof PublicKeyToken)) {
  342. throw new InvalidTokenException("Invalid token type");
  343. }
  344. $token->setPasswordInvalid(true);
  345. $this->mapper->update($token);
  346. }
  347. public function updatePasswords(string $uid, string $password) {
  348. $this->cache->clear();
  349. // prevent setting an empty pw as result of pw-less-login
  350. if ($password === '' || !$this->config->getSystemValueBool('auth.storeCryptedPassword', true)) {
  351. return;
  352. }
  353. // Update the password for all tokens
  354. $tokens = $this->mapper->getTokenByUser($uid);
  355. foreach ($tokens as $t) {
  356. $publicKey = $t->getPublicKey();
  357. $encryptedPassword = $this->encryptPassword($password, $publicKey);
  358. if ($t->getPassword() !== $encryptedPassword) {
  359. $t->setPassword($encryptedPassword);
  360. $t->setPasswordInvalid(false);
  361. $this->updateToken($t);
  362. }
  363. }
  364. }
  365. private function logOpensslError() {
  366. $errors = [];
  367. while ($error = openssl_error_string()) {
  368. $errors[] = $error;
  369. }
  370. $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
  371. }
  372. }