@@ -30,59 +30,59 @@ |
||
30 | 30 | * @package OC\Security\CSRF\TokenStorage |
31 | 31 | */ |
32 | 32 | class SessionStorage { |
33 | - /** @var ISession */ |
|
34 | - private $session; |
|
33 | + /** @var ISession */ |
|
34 | + private $session; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param ISession $session |
|
38 | - */ |
|
39 | - public function __construct(ISession $session) { |
|
40 | - $this->session = $session; |
|
41 | - } |
|
36 | + /** |
|
37 | + * @param ISession $session |
|
38 | + */ |
|
39 | + public function __construct(ISession $session) { |
|
40 | + $this->session = $session; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param ISession $session |
|
45 | - */ |
|
46 | - public function setSession(ISession $session) { |
|
47 | - $this->session = $session; |
|
48 | - } |
|
43 | + /** |
|
44 | + * @param ISession $session |
|
45 | + */ |
|
46 | + public function setSession(ISession $session) { |
|
47 | + $this->session = $session; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns the current token or throws an exception if none is found. |
|
52 | - * |
|
53 | - * @return string |
|
54 | - * @throws \Exception |
|
55 | - */ |
|
56 | - public function getToken() { |
|
57 | - $token = $this->session->get('requesttoken'); |
|
58 | - if(empty($token)) { |
|
59 | - throw new \Exception('Session does not contain a requesttoken'); |
|
60 | - } |
|
50 | + /** |
|
51 | + * Returns the current token or throws an exception if none is found. |
|
52 | + * |
|
53 | + * @return string |
|
54 | + * @throws \Exception |
|
55 | + */ |
|
56 | + public function getToken() { |
|
57 | + $token = $this->session->get('requesttoken'); |
|
58 | + if(empty($token)) { |
|
59 | + throw new \Exception('Session does not contain a requesttoken'); |
|
60 | + } |
|
61 | 61 | |
62 | - return $token; |
|
63 | - } |
|
62 | + return $token; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Set the valid current token to $value. |
|
67 | - * |
|
68 | - * @param string $value |
|
69 | - */ |
|
70 | - public function setToken($value) { |
|
71 | - $this->session->set('requesttoken', $value); |
|
72 | - } |
|
65 | + /** |
|
66 | + * Set the valid current token to $value. |
|
67 | + * |
|
68 | + * @param string $value |
|
69 | + */ |
|
70 | + public function setToken($value) { |
|
71 | + $this->session->set('requesttoken', $value); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Removes the current token. |
|
76 | - */ |
|
77 | - public function removeToken() { |
|
78 | - $this->session->remove('requesttoken'); |
|
79 | - } |
|
80 | - /** |
|
81 | - * Whether the storage has a storage. |
|
82 | - * |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function hasToken() { |
|
86 | - return $this->session->exists('requesttoken'); |
|
87 | - } |
|
74 | + /** |
|
75 | + * Removes the current token. |
|
76 | + */ |
|
77 | + public function removeToken() { |
|
78 | + $this->session->remove('requesttoken'); |
|
79 | + } |
|
80 | + /** |
|
81 | + * Whether the storage has a storage. |
|
82 | + * |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function hasToken() { |
|
86 | + return $this->session->exists('requesttoken'); |
|
87 | + } |
|
88 | 88 | } |
@@ -30,78 +30,78 @@ |
||
30 | 30 | * @package OC\Security\CSRF |
31 | 31 | */ |
32 | 32 | class CsrfTokenManager { |
33 | - /** @var CsrfTokenGenerator */ |
|
34 | - private $tokenGenerator; |
|
35 | - /** @var SessionStorage */ |
|
36 | - private $sessionStorage; |
|
37 | - /** @var CsrfToken|null */ |
|
38 | - private $csrfToken = null; |
|
33 | + /** @var CsrfTokenGenerator */ |
|
34 | + private $tokenGenerator; |
|
35 | + /** @var SessionStorage */ |
|
36 | + private $sessionStorage; |
|
37 | + /** @var CsrfToken|null */ |
|
38 | + private $csrfToken = null; |
|
39 | 39 | |
40 | - /** |
|
41 | - * @param CsrfTokenGenerator $tokenGenerator |
|
42 | - * @param SessionStorage $storageInterface |
|
43 | - */ |
|
44 | - public function __construct(CsrfTokenGenerator $tokenGenerator, |
|
45 | - SessionStorage $storageInterface) { |
|
46 | - $this->tokenGenerator = $tokenGenerator; |
|
47 | - $this->sessionStorage = $storageInterface; |
|
48 | - } |
|
40 | + /** |
|
41 | + * @param CsrfTokenGenerator $tokenGenerator |
|
42 | + * @param SessionStorage $storageInterface |
|
43 | + */ |
|
44 | + public function __construct(CsrfTokenGenerator $tokenGenerator, |
|
45 | + SessionStorage $storageInterface) { |
|
46 | + $this->tokenGenerator = $tokenGenerator; |
|
47 | + $this->sessionStorage = $storageInterface; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns the current CSRF token, if none set it will create a new one. |
|
52 | - * |
|
53 | - * @return CsrfToken |
|
54 | - */ |
|
55 | - public function getToken() { |
|
56 | - if(!is_null($this->csrfToken)) { |
|
57 | - return $this->csrfToken; |
|
58 | - } |
|
50 | + /** |
|
51 | + * Returns the current CSRF token, if none set it will create a new one. |
|
52 | + * |
|
53 | + * @return CsrfToken |
|
54 | + */ |
|
55 | + public function getToken() { |
|
56 | + if(!is_null($this->csrfToken)) { |
|
57 | + return $this->csrfToken; |
|
58 | + } |
|
59 | 59 | |
60 | - if($this->sessionStorage->hasToken()) { |
|
61 | - $value = $this->sessionStorage->getToken(); |
|
62 | - } else { |
|
63 | - $value = $this->tokenGenerator->generateToken(); |
|
64 | - $this->sessionStorage->setToken($value); |
|
65 | - } |
|
60 | + if($this->sessionStorage->hasToken()) { |
|
61 | + $value = $this->sessionStorage->getToken(); |
|
62 | + } else { |
|
63 | + $value = $this->tokenGenerator->generateToken(); |
|
64 | + $this->sessionStorage->setToken($value); |
|
65 | + } |
|
66 | 66 | |
67 | - $this->csrfToken = new CsrfToken($value); |
|
68 | - return $this->csrfToken; |
|
69 | - } |
|
67 | + $this->csrfToken = new CsrfToken($value); |
|
68 | + return $this->csrfToken; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Invalidates any current token and sets a new one. |
|
73 | - * |
|
74 | - * @return CsrfToken |
|
75 | - */ |
|
76 | - public function refreshToken() { |
|
77 | - $value = $this->tokenGenerator->generateToken(); |
|
78 | - $this->sessionStorage->setToken($value); |
|
79 | - $this->csrfToken = new CsrfToken($value); |
|
80 | - return $this->csrfToken; |
|
81 | - } |
|
71 | + /** |
|
72 | + * Invalidates any current token and sets a new one. |
|
73 | + * |
|
74 | + * @return CsrfToken |
|
75 | + */ |
|
76 | + public function refreshToken() { |
|
77 | + $value = $this->tokenGenerator->generateToken(); |
|
78 | + $this->sessionStorage->setToken($value); |
|
79 | + $this->csrfToken = new CsrfToken($value); |
|
80 | + return $this->csrfToken; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Remove the current token from the storage. |
|
85 | - */ |
|
86 | - public function removeToken() { |
|
87 | - $this->csrfToken = null; |
|
88 | - $this->sessionStorage->removeToken(); |
|
89 | - } |
|
83 | + /** |
|
84 | + * Remove the current token from the storage. |
|
85 | + */ |
|
86 | + public function removeToken() { |
|
87 | + $this->csrfToken = null; |
|
88 | + $this->sessionStorage->removeToken(); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Verifies whether the provided token is valid. |
|
93 | - * |
|
94 | - * @param CsrfToken $token |
|
95 | - * @return bool |
|
96 | - */ |
|
97 | - public function isTokenValid(CsrfToken $token) { |
|
98 | - if(!$this->sessionStorage->hasToken()) { |
|
99 | - return false; |
|
100 | - } |
|
91 | + /** |
|
92 | + * Verifies whether the provided token is valid. |
|
93 | + * |
|
94 | + * @param CsrfToken $token |
|
95 | + * @return bool |
|
96 | + */ |
|
97 | + public function isTokenValid(CsrfToken $token) { |
|
98 | + if(!$this->sessionStorage->hasToken()) { |
|
99 | + return false; |
|
100 | + } |
|
101 | 101 | |
102 | - return hash_equals( |
|
103 | - $this->sessionStorage->getToken(), |
|
104 | - $token->getDecryptedValue() |
|
105 | - ); |
|
106 | - } |
|
102 | + return hash_equals( |
|
103 | + $this->sessionStorage->getToken(), |
|
104 | + $token->getDecryptedValue() |
|
105 | + ); |
|
106 | + } |
|
107 | 107 | } |
@@ -31,23 +31,23 @@ |
||
31 | 31 | * @package OC\Security\CSRF |
32 | 32 | */ |
33 | 33 | class CsrfTokenGenerator { |
34 | - /** @var ISecureRandom */ |
|
35 | - private $random; |
|
34 | + /** @var ISecureRandom */ |
|
35 | + private $random; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param ISecureRandom $random |
|
39 | - */ |
|
40 | - public function __construct(ISecureRandom $random) { |
|
41 | - $this->random = $random; |
|
42 | - } |
|
37 | + /** |
|
38 | + * @param ISecureRandom $random |
|
39 | + */ |
|
40 | + public function __construct(ISecureRandom $random) { |
|
41 | + $this->random = $random; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Generate a new CSRF token. |
|
46 | - * |
|
47 | - * @param int $length Length of the token in characters. |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public function generateToken($length = 32) { |
|
51 | - return $this->random->generate($length); |
|
52 | - } |
|
44 | + /** |
|
45 | + * Generate a new CSRF token. |
|
46 | + * |
|
47 | + * @param int $length Length of the token in characters. |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public function generateToken($length = 32) { |
|
51 | + return $this->random->generate($length); |
|
52 | + } |
|
53 | 53 | } |
@@ -43,93 +43,93 @@ |
||
43 | 43 | * @package OC\Security |
44 | 44 | */ |
45 | 45 | class Crypto implements ICrypto { |
46 | - /** @var AES $cipher */ |
|
47 | - private $cipher; |
|
48 | - /** @var int */ |
|
49 | - private $ivLength = 16; |
|
50 | - /** @var IConfig */ |
|
51 | - private $config; |
|
52 | - /** @var ISecureRandom */ |
|
53 | - private $random; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param IConfig $config |
|
57 | - * @param ISecureRandom $random |
|
58 | - */ |
|
59 | - function __construct(IConfig $config, ISecureRandom $random) { |
|
60 | - $this->cipher = new AES(); |
|
61 | - $this->config = $config; |
|
62 | - $this->random = $random; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param string $message The message to authenticate |
|
67 | - * @param string $password Password to use (defaults to `secret` in config.php) |
|
68 | - * @return string Calculated HMAC |
|
69 | - */ |
|
70 | - public function calculateHMAC($message, $password = '') { |
|
71 | - if($password === '') { |
|
72 | - $password = $this->config->getSystemValue('secret'); |
|
73 | - } |
|
74 | - |
|
75 | - // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
|
76 | - $password = hash('sha512', $password . 'a'); |
|
77 | - |
|
78 | - $hash = new Hash('sha512'); |
|
79 | - $hash->setKey($password); |
|
80 | - return $hash->hash($message); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
85 | - * @param string $plaintext |
|
86 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
87 | - * @return string Authenticated ciphertext |
|
88 | - */ |
|
89 | - public function encrypt($plaintext, $password = '') { |
|
90 | - if($password === '') { |
|
91 | - $password = $this->config->getSystemValue('secret'); |
|
92 | - } |
|
93 | - $this->cipher->setPassword($password); |
|
94 | - |
|
95 | - $iv = $this->random->generate($this->ivLength); |
|
96 | - $this->cipher->setIV($iv); |
|
97 | - |
|
98 | - $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); |
|
99 | - $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); |
|
100 | - |
|
101 | - return $ciphertext.'|'.$iv.'|'.$hmac; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
106 | - * @param string $authenticatedCiphertext |
|
107 | - * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
108 | - * @return string plaintext |
|
109 | - * @throws \Exception If the HMAC does not match |
|
110 | - */ |
|
111 | - public function decrypt($authenticatedCiphertext, $password = '') { |
|
112 | - if($password === '') { |
|
113 | - $password = $this->config->getSystemValue('secret'); |
|
114 | - } |
|
115 | - $this->cipher->setPassword($password); |
|
116 | - |
|
117 | - $parts = explode('|', $authenticatedCiphertext); |
|
118 | - if(sizeof($parts) !== 3) { |
|
119 | - throw new \Exception('Authenticated ciphertext could not be decoded.'); |
|
120 | - } |
|
121 | - |
|
122 | - $ciphertext = hex2bin($parts[0]); |
|
123 | - $iv = $parts[1]; |
|
124 | - $hmac = hex2bin($parts[2]); |
|
125 | - |
|
126 | - $this->cipher->setIV($iv); |
|
127 | - |
|
128 | - if(!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
129 | - throw new \Exception('HMAC does not match.'); |
|
130 | - } |
|
131 | - |
|
132 | - return $this->cipher->decrypt($ciphertext); |
|
133 | - } |
|
46 | + /** @var AES $cipher */ |
|
47 | + private $cipher; |
|
48 | + /** @var int */ |
|
49 | + private $ivLength = 16; |
|
50 | + /** @var IConfig */ |
|
51 | + private $config; |
|
52 | + /** @var ISecureRandom */ |
|
53 | + private $random; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param IConfig $config |
|
57 | + * @param ISecureRandom $random |
|
58 | + */ |
|
59 | + function __construct(IConfig $config, ISecureRandom $random) { |
|
60 | + $this->cipher = new AES(); |
|
61 | + $this->config = $config; |
|
62 | + $this->random = $random; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param string $message The message to authenticate |
|
67 | + * @param string $password Password to use (defaults to `secret` in config.php) |
|
68 | + * @return string Calculated HMAC |
|
69 | + */ |
|
70 | + public function calculateHMAC($message, $password = '') { |
|
71 | + if($password === '') { |
|
72 | + $password = $this->config->getSystemValue('secret'); |
|
73 | + } |
|
74 | + |
|
75 | + // Append an "a" behind the password and hash it to prevent reusing the same password as for encryption |
|
76 | + $password = hash('sha512', $password . 'a'); |
|
77 | + |
|
78 | + $hash = new Hash('sha512'); |
|
79 | + $hash->setKey($password); |
|
80 | + return $hash->hash($message); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Encrypts a value and adds an HMAC (Encrypt-Then-MAC) |
|
85 | + * @param string $plaintext |
|
86 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
87 | + * @return string Authenticated ciphertext |
|
88 | + */ |
|
89 | + public function encrypt($plaintext, $password = '') { |
|
90 | + if($password === '') { |
|
91 | + $password = $this->config->getSystemValue('secret'); |
|
92 | + } |
|
93 | + $this->cipher->setPassword($password); |
|
94 | + |
|
95 | + $iv = $this->random->generate($this->ivLength); |
|
96 | + $this->cipher->setIV($iv); |
|
97 | + |
|
98 | + $ciphertext = bin2hex($this->cipher->encrypt($plaintext)); |
|
99 | + $hmac = bin2hex($this->calculateHMAC($ciphertext.$iv, $password)); |
|
100 | + |
|
101 | + return $ciphertext.'|'.$iv.'|'.$hmac; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Decrypts a value and verifies the HMAC (Encrypt-Then-Mac) |
|
106 | + * @param string $authenticatedCiphertext |
|
107 | + * @param string $password Password to encrypt, if not specified the secret from config.php will be taken |
|
108 | + * @return string plaintext |
|
109 | + * @throws \Exception If the HMAC does not match |
|
110 | + */ |
|
111 | + public function decrypt($authenticatedCiphertext, $password = '') { |
|
112 | + if($password === '') { |
|
113 | + $password = $this->config->getSystemValue('secret'); |
|
114 | + } |
|
115 | + $this->cipher->setPassword($password); |
|
116 | + |
|
117 | + $parts = explode('|', $authenticatedCiphertext); |
|
118 | + if(sizeof($parts) !== 3) { |
|
119 | + throw new \Exception('Authenticated ciphertext could not be decoded.'); |
|
120 | + } |
|
121 | + |
|
122 | + $ciphertext = hex2bin($parts[0]); |
|
123 | + $iv = $parts[1]; |
|
124 | + $hmac = hex2bin($parts[2]); |
|
125 | + |
|
126 | + $this->cipher->setIV($iv); |
|
127 | + |
|
128 | + if(!hash_equals($this->calculateHMAC($parts[0].$parts[1], $password), $hmac)) { |
|
129 | + throw new \Exception('HMAC does not match.'); |
|
130 | + } |
|
131 | + |
|
132 | + return $this->cipher->decrypt($ciphertext); |
|
133 | + } |
|
134 | 134 | |
135 | 135 | } |
@@ -37,51 +37,51 @@ |
||
37 | 37 | * @package OC\Security |
38 | 38 | */ |
39 | 39 | class SecureRandom implements ISecureRandom { |
40 | - /** |
|
41 | - * Convenience method to get a low strength random number generator. |
|
42 | - * |
|
43 | - * Low Strength should be used anywhere that random strings are needed |
|
44 | - * in a non-cryptographical setting. They are not strong enough to be |
|
45 | - * used as keys or salts. They are however useful for one-time use tokens. |
|
46 | - * |
|
47 | - * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
48 | - * @return $this |
|
49 | - */ |
|
50 | - public function getLowStrengthGenerator() { |
|
51 | - return $this; |
|
52 | - } |
|
40 | + /** |
|
41 | + * Convenience method to get a low strength random number generator. |
|
42 | + * |
|
43 | + * Low Strength should be used anywhere that random strings are needed |
|
44 | + * in a non-cryptographical setting. They are not strong enough to be |
|
45 | + * used as keys or salts. They are however useful for one-time use tokens. |
|
46 | + * |
|
47 | + * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
48 | + * @return $this |
|
49 | + */ |
|
50 | + public function getLowStrengthGenerator() { |
|
51 | + return $this; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Convenience method to get a medium strength random number generator. |
|
56 | - * |
|
57 | - * Medium Strength should be used for most needs of a cryptographic nature. |
|
58 | - * They are strong enough to be used as keys and salts. However, they do |
|
59 | - * take some time and resources to generate, so they should not be over-used |
|
60 | - * |
|
61 | - * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
62 | - * @return $this |
|
63 | - */ |
|
64 | - public function getMediumStrengthGenerator() { |
|
65 | - return $this; |
|
66 | - } |
|
54 | + /** |
|
55 | + * Convenience method to get a medium strength random number generator. |
|
56 | + * |
|
57 | + * Medium Strength should be used for most needs of a cryptographic nature. |
|
58 | + * They are strong enough to be used as keys and salts. However, they do |
|
59 | + * take some time and resources to generate, so they should not be over-used |
|
60 | + * |
|
61 | + * @deprecated 9.0.0 Use \OC\Security\SecureRandom::generate directly or random_bytes() / random_int() |
|
62 | + * @return $this |
|
63 | + */ |
|
64 | + public function getMediumStrengthGenerator() { |
|
65 | + return $this; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Generate a random string of specified length. |
|
70 | - * @param int $length The length of the generated string |
|
71 | - * @param string $characters An optional list of characters to use if no character list is |
|
72 | - * specified all valid base64 characters are used. |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function generate($length, |
|
76 | - $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') { |
|
77 | - $maxCharIndex = strlen($characters) - 1; |
|
78 | - $randomString = ''; |
|
68 | + /** |
|
69 | + * Generate a random string of specified length. |
|
70 | + * @param int $length The length of the generated string |
|
71 | + * @param string $characters An optional list of characters to use if no character list is |
|
72 | + * specified all valid base64 characters are used. |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function generate($length, |
|
76 | + $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') { |
|
77 | + $maxCharIndex = strlen($characters) - 1; |
|
78 | + $randomString = ''; |
|
79 | 79 | |
80 | - while($length > 0) { |
|
81 | - $randomNumber = \random_int(0, $maxCharIndex); |
|
82 | - $randomString .= $characters[$randomNumber]; |
|
83 | - $length--; |
|
84 | - } |
|
85 | - return $randomString; |
|
86 | - } |
|
80 | + while($length > 0) { |
|
81 | + $randomNumber = \random_int(0, $maxCharIndex); |
|
82 | + $randomString .= $characters[$randomNumber]; |
|
83 | + $length--; |
|
84 | + } |
|
85 | + return $randomString; |
|
86 | + } |
|
87 | 87 | } |
@@ -27,104 +27,104 @@ |
||
27 | 27 | use OCP\ICertificate; |
28 | 28 | |
29 | 29 | class Certificate implements ICertificate { |
30 | - protected $name; |
|
31 | - |
|
32 | - protected $commonName; |
|
33 | - |
|
34 | - protected $organization; |
|
35 | - |
|
36 | - protected $serial; |
|
37 | - |
|
38 | - protected $issueDate; |
|
39 | - |
|
40 | - protected $expireDate; |
|
41 | - |
|
42 | - protected $issuerName; |
|
43 | - |
|
44 | - protected $issuerOrganization; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param string $data base64 encoded certificate |
|
48 | - * @param string $name |
|
49 | - * @throws \Exception If the certificate could not get parsed |
|
50 | - */ |
|
51 | - public function __construct($data, $name) { |
|
52 | - $this->name = $name; |
|
53 | - $gmt = new \DateTimeZone('GMT'); |
|
54 | - |
|
55 | - // If string starts with "file://" ignore the certificate |
|
56 | - $query = 'file://'; |
|
57 | - if(strtolower(substr($data, 0, strlen($query))) === $query) { |
|
58 | - throw new \Exception('Certificate could not get parsed.'); |
|
59 | - } |
|
60 | - |
|
61 | - $info = openssl_x509_parse($data); |
|
62 | - if(!is_array($info)) { |
|
63 | - throw new \Exception('Certificate could not get parsed.'); |
|
64 | - } |
|
65 | - |
|
66 | - $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
67 | - $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
68 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
69 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
70 | - $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
71 | - $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getName() { |
|
78 | - return $this->name; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return string|null |
|
83 | - */ |
|
84 | - public function getCommonName() { |
|
85 | - return $this->commonName; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function getOrganization() { |
|
92 | - return $this->organization; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @return \DateTime |
|
97 | - */ |
|
98 | - public function getIssueDate() { |
|
99 | - return $this->issueDate; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @return \DateTime |
|
104 | - */ |
|
105 | - public function getExpireDate() { |
|
106 | - return $this->expireDate; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return bool |
|
111 | - */ |
|
112 | - public function isExpired() { |
|
113 | - $now = new \DateTime(); |
|
114 | - return $this->issueDate > $now or $now > $this->expireDate; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @return string|null |
|
119 | - */ |
|
120 | - public function getIssuerName() { |
|
121 | - return $this->issuerName; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @return string|null |
|
126 | - */ |
|
127 | - public function getIssuerOrganization() { |
|
128 | - return $this->issuerOrganization; |
|
129 | - } |
|
30 | + protected $name; |
|
31 | + |
|
32 | + protected $commonName; |
|
33 | + |
|
34 | + protected $organization; |
|
35 | + |
|
36 | + protected $serial; |
|
37 | + |
|
38 | + protected $issueDate; |
|
39 | + |
|
40 | + protected $expireDate; |
|
41 | + |
|
42 | + protected $issuerName; |
|
43 | + |
|
44 | + protected $issuerOrganization; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param string $data base64 encoded certificate |
|
48 | + * @param string $name |
|
49 | + * @throws \Exception If the certificate could not get parsed |
|
50 | + */ |
|
51 | + public function __construct($data, $name) { |
|
52 | + $this->name = $name; |
|
53 | + $gmt = new \DateTimeZone('GMT'); |
|
54 | + |
|
55 | + // If string starts with "file://" ignore the certificate |
|
56 | + $query = 'file://'; |
|
57 | + if(strtolower(substr($data, 0, strlen($query))) === $query) { |
|
58 | + throw new \Exception('Certificate could not get parsed.'); |
|
59 | + } |
|
60 | + |
|
61 | + $info = openssl_x509_parse($data); |
|
62 | + if(!is_array($info)) { |
|
63 | + throw new \Exception('Certificate could not get parsed.'); |
|
64 | + } |
|
65 | + |
|
66 | + $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
67 | + $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
68 | + $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
69 | + $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
70 | + $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
71 | + $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getName() { |
|
78 | + return $this->name; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return string|null |
|
83 | + */ |
|
84 | + public function getCommonName() { |
|
85 | + return $this->commonName; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function getOrganization() { |
|
92 | + return $this->organization; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @return \DateTime |
|
97 | + */ |
|
98 | + public function getIssueDate() { |
|
99 | + return $this->issueDate; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @return \DateTime |
|
104 | + */ |
|
105 | + public function getExpireDate() { |
|
106 | + return $this->expireDate; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return bool |
|
111 | + */ |
|
112 | + public function isExpired() { |
|
113 | + $now = new \DateTime(); |
|
114 | + return $this->issueDate > $now or $now > $this->expireDate; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @return string|null |
|
119 | + */ |
|
120 | + public function getIssuerName() { |
|
121 | + return $this->issuerName; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @return string|null |
|
126 | + */ |
|
127 | + public function getIssuerOrganization() { |
|
128 | + return $this->issuerOrganization; |
|
129 | + } |
|
130 | 130 | } |
@@ -26,76 +26,76 @@ |
||
26 | 26 | use OCP\IUserManager; |
27 | 27 | |
28 | 28 | class Signer { |
29 | - /** @var Manager */ |
|
30 | - private $keyManager; |
|
31 | - /** @var ITimeFactory */ |
|
32 | - private $timeFactory; |
|
33 | - /** @var IUserManager */ |
|
34 | - private $userManager; |
|
29 | + /** @var Manager */ |
|
30 | + private $keyManager; |
|
31 | + /** @var ITimeFactory */ |
|
32 | + private $timeFactory; |
|
33 | + /** @var IUserManager */ |
|
34 | + private $userManager; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @param Manager $keyManager |
|
38 | - * @param ITimeFactory $timeFactory |
|
39 | - * @param IUserManager $userManager |
|
40 | - */ |
|
41 | - public function __construct(Manager $keyManager, |
|
42 | - ITimeFactory $timeFactory, |
|
43 | - IUserManager $userManager) { |
|
44 | - $this->keyManager = $keyManager; |
|
45 | - $this->timeFactory = $timeFactory; |
|
46 | - $this->userManager = $userManager; |
|
47 | - } |
|
36 | + /** |
|
37 | + * @param Manager $keyManager |
|
38 | + * @param ITimeFactory $timeFactory |
|
39 | + * @param IUserManager $userManager |
|
40 | + */ |
|
41 | + public function __construct(Manager $keyManager, |
|
42 | + ITimeFactory $timeFactory, |
|
43 | + IUserManager $userManager) { |
|
44 | + $this->keyManager = $keyManager; |
|
45 | + $this->timeFactory = $timeFactory; |
|
46 | + $this->userManager = $userManager; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Returns a signed blob for $data |
|
51 | - * |
|
52 | - * @param string $type |
|
53 | - * @param array $data |
|
54 | - * @param IUser $user |
|
55 | - * @return array ['message', 'signature'] |
|
56 | - */ |
|
57 | - public function sign($type, array $data, IUser $user) { |
|
58 | - $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
59 | - $data = [ |
|
60 | - 'data' => $data, |
|
61 | - 'type' => $type, |
|
62 | - 'signer' => $user->getCloudId(), |
|
63 | - 'timestamp' => $this->timeFactory->getTime(), |
|
64 | - ]; |
|
65 | - openssl_sign(json_encode($data), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
49 | + /** |
|
50 | + * Returns a signed blob for $data |
|
51 | + * |
|
52 | + * @param string $type |
|
53 | + * @param array $data |
|
54 | + * @param IUser $user |
|
55 | + * @return array ['message', 'signature'] |
|
56 | + */ |
|
57 | + public function sign($type, array $data, IUser $user) { |
|
58 | + $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
59 | + $data = [ |
|
60 | + 'data' => $data, |
|
61 | + 'type' => $type, |
|
62 | + 'signer' => $user->getCloudId(), |
|
63 | + 'timestamp' => $this->timeFactory->getTime(), |
|
64 | + ]; |
|
65 | + openssl_sign(json_encode($data), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
66 | 66 | |
67 | - return [ |
|
68 | - 'message' => $data, |
|
69 | - 'signature' => base64_encode($signature), |
|
70 | - ]; |
|
71 | - } |
|
67 | + return [ |
|
68 | + 'message' => $data, |
|
69 | + 'signature' => base64_encode($signature), |
|
70 | + ]; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Whether the data is signed properly |
|
75 | - * |
|
76 | - * @param array $data |
|
77 | - * @return bool |
|
78 | - */ |
|
79 | - public function verify(array $data) { |
|
80 | - if(isset($data['message']) |
|
81 | - && isset($data['signature']) |
|
82 | - && isset($data['message']['signer']) |
|
83 | - ) { |
|
84 | - $location = strrpos($data['message']['signer'], '@'); |
|
85 | - $userId = substr($data['message']['signer'], 0, $location); |
|
73 | + /** |
|
74 | + * Whether the data is signed properly |
|
75 | + * |
|
76 | + * @param array $data |
|
77 | + * @return bool |
|
78 | + */ |
|
79 | + public function verify(array $data) { |
|
80 | + if(isset($data['message']) |
|
81 | + && isset($data['signature']) |
|
82 | + && isset($data['message']['signer']) |
|
83 | + ) { |
|
84 | + $location = strrpos($data['message']['signer'], '@'); |
|
85 | + $userId = substr($data['message']['signer'], 0, $location); |
|
86 | 86 | |
87 | - $user = $this->userManager->get($userId); |
|
88 | - if($user !== null) { |
|
89 | - $key = $this->keyManager->getKey($user); |
|
90 | - return (bool)openssl_verify( |
|
91 | - json_encode($data['message']), |
|
92 | - base64_decode($data['signature']), |
|
93 | - $key->getPublic(), |
|
94 | - OPENSSL_ALGO_SHA512 |
|
95 | - ); |
|
96 | - } |
|
97 | - } |
|
87 | + $user = $this->userManager->get($userId); |
|
88 | + if($user !== null) { |
|
89 | + $key = $this->keyManager->getKey($user); |
|
90 | + return (bool)openssl_verify( |
|
91 | + json_encode($data['message']), |
|
92 | + base64_decode($data['signature']), |
|
93 | + $key->getPublic(), |
|
94 | + OPENSSL_ALGO_SHA512 |
|
95 | + ); |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | - return false; |
|
100 | - } |
|
99 | + return false; |
|
100 | + } |
|
101 | 101 | } |
@@ -22,25 +22,25 @@ |
||
22 | 22 | namespace OC\Security\IdentityProof; |
23 | 23 | |
24 | 24 | class Key { |
25 | - /** @var string */ |
|
26 | - private $publicKey; |
|
27 | - /** @var string */ |
|
28 | - private $privateKey; |
|
25 | + /** @var string */ |
|
26 | + private $publicKey; |
|
27 | + /** @var string */ |
|
28 | + private $privateKey; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $publicKey |
|
32 | - * @param string $privateKey |
|
33 | - */ |
|
34 | - public function __construct($publicKey, $privateKey) { |
|
35 | - $this->publicKey = $publicKey; |
|
36 | - $this->privateKey = $privateKey; |
|
37 | - } |
|
30 | + /** |
|
31 | + * @param string $publicKey |
|
32 | + * @param string $privateKey |
|
33 | + */ |
|
34 | + public function __construct($publicKey, $privateKey) { |
|
35 | + $this->publicKey = $publicKey; |
|
36 | + $this->privateKey = $privateKey; |
|
37 | + } |
|
38 | 38 | |
39 | - public function getPrivate() { |
|
40 | - return $this->privateKey; |
|
41 | - } |
|
39 | + public function getPrivate() { |
|
40 | + return $this->privateKey; |
|
41 | + } |
|
42 | 42 | |
43 | - public function getPublic() { |
|
44 | - return $this->publicKey; |
|
45 | - } |
|
43 | + public function getPublic() { |
|
44 | + return $this->publicKey; |
|
45 | + } |
|
46 | 46 | } |
@@ -26,83 +26,83 @@ |
||
26 | 26 | use OCP\Security\ICrypto; |
27 | 27 | |
28 | 28 | class Manager { |
29 | - /** @var IAppData */ |
|
30 | - private $appData; |
|
31 | - /** @var ICrypto */ |
|
32 | - private $crypto; |
|
29 | + /** @var IAppData */ |
|
30 | + private $appData; |
|
31 | + /** @var ICrypto */ |
|
32 | + private $crypto; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @param IAppData $appData |
|
36 | - * @param ICrypto $crypto |
|
37 | - */ |
|
38 | - public function __construct(IAppData $appData, |
|
39 | - ICrypto $crypto) { |
|
40 | - $this->appData = $appData; |
|
41 | - $this->crypto = $crypto; |
|
42 | - } |
|
34 | + /** |
|
35 | + * @param IAppData $appData |
|
36 | + * @param ICrypto $crypto |
|
37 | + */ |
|
38 | + public function __construct(IAppData $appData, |
|
39 | + ICrypto $crypto) { |
|
40 | + $this->appData = $appData; |
|
41 | + $this->crypto = $crypto; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Calls the openssl functions to generate a public and private key. |
|
46 | - * In a separate function for unit testing purposes. |
|
47 | - * |
|
48 | - * @return array [$publicKey, $privateKey] |
|
49 | - */ |
|
50 | - protected function generateKeyPair() { |
|
51 | - $config = [ |
|
52 | - 'digest_alg' => 'sha512', |
|
53 | - 'private_key_bits' => 2048, |
|
54 | - ]; |
|
44 | + /** |
|
45 | + * Calls the openssl functions to generate a public and private key. |
|
46 | + * In a separate function for unit testing purposes. |
|
47 | + * |
|
48 | + * @return array [$publicKey, $privateKey] |
|
49 | + */ |
|
50 | + protected function generateKeyPair() { |
|
51 | + $config = [ |
|
52 | + 'digest_alg' => 'sha512', |
|
53 | + 'private_key_bits' => 2048, |
|
54 | + ]; |
|
55 | 55 | |
56 | - // Generate new key |
|
57 | - $res = openssl_pkey_new($config); |
|
58 | - openssl_pkey_export($res, $privateKey); |
|
56 | + // Generate new key |
|
57 | + $res = openssl_pkey_new($config); |
|
58 | + openssl_pkey_export($res, $privateKey); |
|
59 | 59 | |
60 | - // Extract the public key from $res to $pubKey |
|
61 | - $publicKey = openssl_pkey_get_details($res); |
|
62 | - $publicKey = $publicKey['key']; |
|
60 | + // Extract the public key from $res to $pubKey |
|
61 | + $publicKey = openssl_pkey_get_details($res); |
|
62 | + $publicKey = $publicKey['key']; |
|
63 | 63 | |
64 | - return [$publicKey, $privateKey]; |
|
65 | - } |
|
64 | + return [$publicKey, $privateKey]; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Generate a key for $user |
|
69 | - * Note: If a key already exists it will be overwritten |
|
70 | - * |
|
71 | - * @param IUser $user |
|
72 | - * @return Key |
|
73 | - */ |
|
74 | - protected function generateKey(IUser $user) { |
|
75 | - list($publicKey, $privateKey) = $this->generateKeyPair(); |
|
67 | + /** |
|
68 | + * Generate a key for $user |
|
69 | + * Note: If a key already exists it will be overwritten |
|
70 | + * |
|
71 | + * @param IUser $user |
|
72 | + * @return Key |
|
73 | + */ |
|
74 | + protected function generateKey(IUser $user) { |
|
75 | + list($publicKey, $privateKey) = $this->generateKeyPair(); |
|
76 | 76 | |
77 | - // Write the private and public key to the disk |
|
78 | - try { |
|
79 | - $this->appData->newFolder($user->getUID()); |
|
80 | - } catch (\Exception $e) {} |
|
81 | - $folder = $this->appData->getFolder($user->getUID()); |
|
82 | - $folder->newFile('private') |
|
83 | - ->putContent($this->crypto->encrypt($privateKey)); |
|
84 | - $folder->newFile('public') |
|
85 | - ->putContent($publicKey); |
|
77 | + // Write the private and public key to the disk |
|
78 | + try { |
|
79 | + $this->appData->newFolder($user->getUID()); |
|
80 | + } catch (\Exception $e) {} |
|
81 | + $folder = $this->appData->getFolder($user->getUID()); |
|
82 | + $folder->newFile('private') |
|
83 | + ->putContent($this->crypto->encrypt($privateKey)); |
|
84 | + $folder->newFile('public') |
|
85 | + ->putContent($publicKey); |
|
86 | 86 | |
87 | - return new Key($publicKey, $privateKey); |
|
88 | - } |
|
87 | + return new Key($publicKey, $privateKey); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Get public and private key for $user |
|
92 | - * |
|
93 | - * @param IUser $user |
|
94 | - * @return Key |
|
95 | - */ |
|
96 | - public function getKey(IUser $user) { |
|
97 | - try { |
|
98 | - $folder = $this->appData->getFolder($user->getUID()); |
|
99 | - $privateKey = $this->crypto->decrypt( |
|
100 | - $folder->getFile('private')->getContent() |
|
101 | - ); |
|
102 | - $publicKey = $folder->getFile('public')->getContent(); |
|
103 | - return new Key($publicKey, $privateKey); |
|
104 | - } catch (\Exception $e) { |
|
105 | - return $this->generateKey($user); |
|
106 | - } |
|
107 | - } |
|
90 | + /** |
|
91 | + * Get public and private key for $user |
|
92 | + * |
|
93 | + * @param IUser $user |
|
94 | + * @return Key |
|
95 | + */ |
|
96 | + public function getKey(IUser $user) { |
|
97 | + try { |
|
98 | + $folder = $this->appData->getFolder($user->getUID()); |
|
99 | + $privateKey = $this->crypto->decrypt( |
|
100 | + $folder->getFile('private')->getContent() |
|
101 | + ); |
|
102 | + $publicKey = $folder->getFile('public')->getContent(); |
|
103 | + return new Key($publicKey, $privateKey); |
|
104 | + } catch (\Exception $e) { |
|
105 | + return $this->generateKey($user); |
|
106 | + } |
|
107 | + } |
|
108 | 108 | } |