RSA.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Lib\Auth\PublicKey;
  25. use OCA\Files_External\Lib\Auth\AuthMechanism;
  26. use OCA\Files_External\Lib\DefinitionParameter;
  27. use OCA\Files_External\Lib\StorageConfig;
  28. use OCP\IConfig;
  29. use OCP\IL10N;
  30. use OCP\IUser;
  31. use phpseclib\Crypt\RSA as RSACrypt;
  32. /**
  33. * RSA public key authentication
  34. */
  35. class RSA extends AuthMechanism {
  36. /** @var IConfig */
  37. private $config;
  38. public function __construct(IL10N $l, IConfig $config) {
  39. $this->config = $config;
  40. $this
  41. ->setIdentifier('publickey::rsa')
  42. ->setScheme(self::SCHEME_PUBLICKEY)
  43. ->setText($l->t('RSA public key'))
  44. ->addParameters([
  45. new DefinitionParameter('user', $l->t('Login')),
  46. new DefinitionParameter('public_key', $l->t('Public key')),
  47. (new DefinitionParameter('private_key', 'private_key'))
  48. ->setType(DefinitionParameter::VALUE_HIDDEN),
  49. ])
  50. ->addCustomJs('public_key')
  51. ;
  52. }
  53. /**
  54. * @return void
  55. */
  56. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  57. $auth = new RSACrypt();
  58. $auth->setPassword($this->config->getSystemValue('secret', ''));
  59. if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
  60. // Add fallback routine for a time where secret was not enforced to be exists
  61. $auth->setPassword('');
  62. if (!$auth->loadKey($storage->getBackendOption('private_key'))) {
  63. throw new \RuntimeException('unable to load private key');
  64. }
  65. }
  66. $storage->setBackendOption('public_key_auth', $auth);
  67. }
  68. /**
  69. * Generate a keypair
  70. *
  71. * @param int $keyLenth
  72. * @return array ['privatekey' => $privateKey, 'publickey' => $publicKey]
  73. */
  74. public function createKey($keyLength) {
  75. $rsa = new RSACrypt();
  76. $rsa->setPublicKeyFormat(RSACrypt::PUBLIC_FORMAT_OPENSSH);
  77. $rsa->setPassword($this->config->getSystemValue('secret', ''));
  78. if ($keyLength !== 1024 && $keyLength !== 2048 && $keyLength !== 4096) {
  79. $keyLength = 1024;
  80. }
  81. return $rsa->createKey($keyLength);
  82. }
  83. }