CryptoWrapper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Phil Davis <phil.davis@inf.org>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Session;
  26. use OCP\IConfig;
  27. use OCP\IRequest;
  28. use OCP\ISession;
  29. use OCP\Security\ICrypto;
  30. use OCP\Security\ISecureRandom;
  31. /**
  32. * Class CryptoWrapper provides some rough basic level of additional security by
  33. * storing the session data in an encrypted form.
  34. *
  35. * The content of the session is encrypted using another cookie sent by the browser.
  36. * One should note that an adversary with access to the source code or the system
  37. * memory is still able to read the original session ID from the users' request.
  38. * This thus can not be considered a strong security measure one should consider
  39. * it as an additional small security obfuscation layer to comply with compliance
  40. * guidelines.
  41. *
  42. * TODO: Remove this in a future release with an approach such as
  43. * https://github.com/owncloud/core/pull/17866
  44. *
  45. * @package OC\Session
  46. */
  47. class CryptoWrapper {
  48. const COOKIE_NAME = 'oc_sessionPassphrase';
  49. /** @var IConfig */
  50. protected $config;
  51. /** @var ISession */
  52. protected $session;
  53. /** @var ICrypto */
  54. protected $crypto;
  55. /** @var ISecureRandom */
  56. protected $random;
  57. /** @var string */
  58. protected $passphrase;
  59. /**
  60. * @param IConfig $config
  61. * @param ICrypto $crypto
  62. * @param ISecureRandom $random
  63. * @param IRequest $request
  64. */
  65. public function __construct(IConfig $config,
  66. ICrypto $crypto,
  67. ISecureRandom $random,
  68. IRequest $request) {
  69. $this->crypto = $crypto;
  70. $this->config = $config;
  71. $this->random = $random;
  72. if (!is_null($request->getCookie(self::COOKIE_NAME))) {
  73. $this->passphrase = $request->getCookie(self::COOKIE_NAME);
  74. } else {
  75. $this->passphrase = $this->random->generate(128);
  76. $secureCookie = $request->getServerProtocol() === 'https';
  77. // FIXME: Required for CI
  78. if (!defined('PHPUNIT_RUN')) {
  79. $webRoot = \OC::$WEBROOT;
  80. if($webRoot === '') {
  81. $webRoot = '/';
  82. }
  83. setcookie(self::COOKIE_NAME, $this->passphrase, 0, $webRoot, '', $secureCookie, true);
  84. }
  85. }
  86. }
  87. /**
  88. * @param ISession $session
  89. * @return ISession
  90. */
  91. public function wrapSession(ISession $session) {
  92. if (!($session instanceof CryptoSessionData)) {
  93. return new CryptoSessionData($session, $this->crypto, $this->passphrase);
  94. }
  95. return $session;
  96. }
  97. }