PublicKeyTokenProvider.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Authentication\Token;
  24. use OC\Authentication\Exceptions\ExpiredTokenException;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Exceptions\PasswordlessTokenException;
  27. use OC\Authentication\Exceptions\WipeTokenException;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IConfig;
  31. use OCP\ILogger;
  32. use OCP\Security\ICrypto;
  33. class PublicKeyTokenProvider implements IProvider {
  34. /** @var PublicKeyTokenMapper */
  35. private $mapper;
  36. /** @var ICrypto */
  37. private $crypto;
  38. /** @var IConfig */
  39. private $config;
  40. /** @var ILogger $logger */
  41. private $logger;
  42. /** @var ITimeFactory $time */
  43. private $time;
  44. public function __construct(PublicKeyTokenMapper $mapper,
  45. ICrypto $crypto,
  46. IConfig $config,
  47. ILogger $logger,
  48. ITimeFactory $time) {
  49. $this->mapper = $mapper;
  50. $this->crypto = $crypto;
  51. $this->config = $config;
  52. $this->logger = $logger;
  53. $this->time = $time;
  54. }
  55. public function generateToken(string $token,
  56. string $uid,
  57. string $loginName,
  58. $password,
  59. string $name,
  60. int $type = IToken::TEMPORARY_TOKEN,
  61. int $remember = IToken::DO_NOT_REMEMBER): IToken {
  62. $dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember);
  63. $this->mapper->insert($dbToken);
  64. return $dbToken;
  65. }
  66. public function getToken(string $tokenId): IToken {
  67. try {
  68. $token = $this->mapper->getToken($this->hashToken($tokenId));
  69. } catch (DoesNotExistException $ex) {
  70. throw new InvalidTokenException();
  71. }
  72. if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) {
  73. throw new ExpiredTokenException($token);
  74. }
  75. if ($token->getType() === IToken::WIPE_TOKEN) {
  76. throw new WipeTokenException($token);
  77. }
  78. return $token;
  79. }
  80. public function getTokenById(int $tokenId): IToken {
  81. try {
  82. $token = $this->mapper->getTokenById($tokenId);
  83. } catch (DoesNotExistException $ex) {
  84. throw new InvalidTokenException();
  85. }
  86. if ((int)$token->getExpires() !== 0 && $token->getExpires() < $this->time->getTime()) {
  87. throw new ExpiredTokenException($token);
  88. }
  89. if ($token->getType() === IToken::WIPE_TOKEN) {
  90. throw new WipeTokenException($token);
  91. }
  92. return $token;
  93. }
  94. public function renewSessionToken(string $oldSessionId, string $sessionId) {
  95. $token = $this->getToken($oldSessionId);
  96. if (!($token instanceof PublicKeyToken)) {
  97. throw new InvalidTokenException();
  98. }
  99. $password = null;
  100. if (!is_null($token->getPassword())) {
  101. $privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
  102. $password = $this->decryptPassword($token->getPassword(), $privateKey);
  103. }
  104. $this->generateToken(
  105. $sessionId,
  106. $token->getUID(),
  107. $token->getLoginName(),
  108. $password,
  109. $token->getName(),
  110. IToken::TEMPORARY_TOKEN,
  111. $token->getRemember()
  112. );
  113. $this->mapper->delete($token);
  114. }
  115. public function invalidateToken(string $token) {
  116. $this->mapper->invalidate($this->hashToken($token));
  117. }
  118. public function invalidateTokenById(string $uid, int $id) {
  119. $this->mapper->deleteById($uid, $id);
  120. }
  121. public function invalidateOldTokens() {
  122. $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24);
  123. $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']);
  124. $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER);
  125. $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
  126. $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']);
  127. $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER);
  128. }
  129. public function updateToken(IToken $token) {
  130. if (!($token instanceof PublicKeyToken)) {
  131. throw new InvalidTokenException();
  132. }
  133. $this->mapper->update($token);
  134. }
  135. public function updateTokenActivity(IToken $token) {
  136. if (!($token instanceof PublicKeyToken)) {
  137. throw new InvalidTokenException();
  138. }
  139. /** @var DefaultToken $token */
  140. $now = $this->time->getTime();
  141. if ($token->getLastActivity() < ($now - 60)) {
  142. // Update token only once per minute
  143. $token->setLastActivity($now);
  144. $this->mapper->update($token);
  145. }
  146. }
  147. public function getTokenByUser(string $uid): array {
  148. return $this->mapper->getTokenByUser($uid);
  149. }
  150. public function getPassword(IToken $token, string $tokenId): string {
  151. if (!($token instanceof PublicKeyToken)) {
  152. throw new InvalidTokenException();
  153. }
  154. if ($token->getPassword() === null) {
  155. throw new PasswordlessTokenException();
  156. }
  157. // Decrypt private key with tokenId
  158. $privateKey = $this->decrypt($token->getPrivateKey(), $tokenId);
  159. // Decrypt password with private key
  160. return $this->decryptPassword($token->getPassword(), $privateKey);
  161. }
  162. public function setPassword(IToken $token, string $tokenId, string $password) {
  163. if (!($token instanceof PublicKeyToken)) {
  164. throw new InvalidTokenException();
  165. }
  166. // When changing passwords all temp tokens are deleted
  167. $this->mapper->deleteTempToken($token);
  168. // Update the password for all tokens
  169. $tokens = $this->mapper->getTokenByUser($token->getUID());
  170. foreach ($tokens as $t) {
  171. $publicKey = $t->getPublicKey();
  172. $t->setPassword($this->encryptPassword($password, $publicKey));
  173. $this->updateToken($t);
  174. }
  175. }
  176. public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
  177. if (!($token instanceof PublicKeyToken)) {
  178. throw new InvalidTokenException();
  179. }
  180. // Decrypt private key with oldTokenId
  181. $privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId);
  182. // Encrypt with the new token
  183. $token->setPrivateKey($this->encrypt($privateKey, $newTokenId));
  184. $token->setToken($this->hashToken($newTokenId));
  185. $this->updateToken($token);
  186. return $token;
  187. }
  188. private function encrypt(string $plaintext, string $token): string {
  189. $secret = $this->config->getSystemValue('secret');
  190. return $this->crypto->encrypt($plaintext, $token . $secret);
  191. }
  192. /**
  193. * @throws InvalidTokenException
  194. */
  195. private function decrypt(string $cipherText, string $token): string {
  196. $secret = $this->config->getSystemValue('secret');
  197. try {
  198. return $this->crypto->decrypt($cipherText, $token . $secret);
  199. } catch (\Exception $ex) {
  200. // Delete the invalid token
  201. $this->invalidateToken($token);
  202. throw new InvalidTokenException();
  203. }
  204. }
  205. private function encryptPassword(string $password, string $publicKey): string {
  206. openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING);
  207. $encryptedPassword = base64_encode($encryptedPassword);
  208. return $encryptedPassword;
  209. }
  210. private function decryptPassword(string $encryptedPassword, string $privateKey): string {
  211. $encryptedPassword = base64_decode($encryptedPassword);
  212. openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);
  213. return $password;
  214. }
  215. private function hashToken(string $token): string {
  216. $secret = $this->config->getSystemValue('secret');
  217. return hash('sha512', $token . $secret);
  218. }
  219. /**
  220. * Convert a DefaultToken to a publicKeyToken
  221. * This will also be updated directly in the Database
  222. */
  223. public function convertToken(DefaultToken $defaultToken, string $token, $password): PublicKeyToken {
  224. $pkToken = $this->newToken(
  225. $token,
  226. $defaultToken->getUID(),
  227. $defaultToken->getLoginName(),
  228. $password,
  229. $defaultToken->getName(),
  230. $defaultToken->getType(),
  231. $defaultToken->getRemember()
  232. );
  233. $pkToken->setExpires($defaultToken->getExpires());
  234. $pkToken->setId($defaultToken->getId());
  235. return $this->mapper->update($pkToken);
  236. }
  237. private function newToken(string $token,
  238. string $uid,
  239. string $loginName,
  240. $password,
  241. string $name,
  242. int $type,
  243. int $remember): PublicKeyToken {
  244. $dbToken = new PublicKeyToken();
  245. $dbToken->setUid($uid);
  246. $dbToken->setLoginName($loginName);
  247. $config = array_merge([
  248. 'digest_alg' => 'sha512',
  249. 'private_key_bits' => 2048,
  250. ], $this->config->getSystemValue('openssl', []));
  251. // Generate new key
  252. $res = openssl_pkey_new($config);
  253. if ($res === false) {
  254. $this->logOpensslError();
  255. }
  256. openssl_pkey_export($res, $privateKey);
  257. // Extract the public key from $res to $pubKey
  258. $publicKey = openssl_pkey_get_details($res);
  259. $publicKey = $publicKey['key'];
  260. $dbToken->setPublicKey($publicKey);
  261. $dbToken->setPrivateKey($this->encrypt($privateKey, $token));
  262. if (!is_null($password)) {
  263. $dbToken->setPassword($this->encryptPassword($password, $publicKey));
  264. }
  265. $dbToken->setName($name);
  266. $dbToken->setToken($this->hashToken($token));
  267. $dbToken->setType($type);
  268. $dbToken->setRemember($remember);
  269. $dbToken->setLastActivity($this->time->getTime());
  270. $dbToken->setLastCheck($this->time->getTime());
  271. $dbToken->setVersion(PublicKeyToken::VERSION);
  272. return $dbToken;
  273. }
  274. public function markPasswordInvalid(IToken $token, string $tokenId) {
  275. if (!($token instanceof PublicKeyToken)) {
  276. throw new InvalidTokenException();
  277. }
  278. $token->setPasswordInvalid(true);
  279. $this->mapper->update($token);
  280. }
  281. public function updatePasswords(string $uid, string $password) {
  282. if (!$this->mapper->hasExpiredTokens($uid)) {
  283. // Nothing to do here
  284. return;
  285. }
  286. // Update the password for all tokens
  287. $tokens = $this->mapper->getTokenByUser($uid);
  288. foreach ($tokens as $t) {
  289. $publicKey = $t->getPublicKey();
  290. $t->setPassword($this->encryptPassword($password, $publicKey));
  291. $this->updateToken($t);
  292. }
  293. }
  294. private function logOpensslError() {
  295. $errors = [];
  296. while ($error = openssl_error_string()) {
  297. $errors[] = $error;
  298. }
  299. $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors));
  300. }
  301. }