From bb229f729114bcd20b861e8b118fd6c805b96b73 Mon Sep 17 00:00:00 2001
From: Bjoern Schiessle <schiessle@owncloud.com>
Date: Thu, 26 Jul 2012 17:19:55 +0200
Subject: [PATCH] write private/public key from the client to the server

---
 apps/files_encryption/lib/keymanager.php | 31 ++++++++++---
 lib/ocs.php                              | 58 +++++++++++++++---------
 2 files changed, 62 insertions(+), 27 deletions(-)

diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php
index f48047a692..0bef3b7492 100644
--- a/apps/files_encryption/lib/keymanager.php
+++ b/apps/files_encryption/lib/keymanager.php
@@ -73,8 +73,16 @@ class Keymanager {
 	 * @return bool true/false
 	 */
 	public static function setPrivateKey($user, $key) {
-		$view = new \OC_FilesystemView('/'.$user.'/files_encryption/');
-		return $view->file_put_contents($user.'.private.key', $key);
+
+		\OC_FileProxy::$enabled = false;
+		
+		$view = new \OC_FilesystemView('/'.$user.'/files_encryption');
+		if (!$view->file_exists('')) $view->mkdir('');
+		$result = $view->file_put_contents($user.'.private.key', $key);
+		
+		\OC_FileProxy::$enabled = true;
+		
+		return $result;
 	}
 	
 	
@@ -86,8 +94,16 @@ class Keymanager {
 	 * @return bool true/false
 	 */
 	public static function setPublicKey($user, $key) {
-		$view = new \OC_FilesystemView('/public-keys/');
-		return $view->file_put_contents($user.'.public.key', $key);
+		
+		\OC_FileProxy::$enabled = false;
+		
+		$view = new \OC_FilesystemView('/public-keys');
+		if (!$view->file_exists('')) $view->mkdir('');
+		$result = $view->file_put_contents($user.'.public.key', $key);
+		
+		\OC_FileProxy::$enabled = true;
+		
+		return $result;
 	}
 	
 	/**
@@ -103,10 +119,13 @@ class Keymanager {
 		\OC_FileProxy::$enabled = false;
 		
 		$view = new \OC_FilesystemView( '/' . $userId . '/' . 'files_encryption' );
-		
-		return $view->file_put_contents( '/' . $path . '.key', $key );
+		$path_parts = pathinfo($path);
+		if (!$view->file_exists($path_parts['dirname'])) $view->mkdir($path_parts['dirname']);
+		$result = $view->file_put_contents( '/' . $path . '.key', $key );
 		
 		\OC_FileProxy::$enabled = true;	
+		
+		return $result;
 	}
 	
 }
\ No newline at end of file
diff --git a/lib/ocs.php b/lib/ocs.php
index 9d30b062bc..5349053ad2 100644
--- a/lib/ocs.php
+++ b/lib/ocs.php
@@ -173,10 +173,20 @@ class OC_OCS {
 			$user=$ex[$paracount-3];
 			OC_OCS::publicKeyGet($format,$user);
 
+		//keysetpublic
+		}elseif(($method=='post') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'publickey')){
+				$user=$ex[$paracount-3];
+				$key = self::readData('post', 'key', 'string');
+				OC_OCS::publicKeySet($format,$user, $key);
+		
 		// keygetprivate 
 		}elseif(($method=='get') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'privatekey')){
 			$user=$ex[$paracount-3];
 			OC_OCS::privateKeyGet($format,$user);
+		}elseif(($method=='post') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'privatekey')){
+				$user=$ex[$paracount-3];
+				$key = self::readData('post', 'key', 'string');
+				OC_OCS::privateKeySet($format,$user, $key);
 
 
 // add more calls here
@@ -678,20 +688,23 @@ class OC_OCS {
          * @param string $key
          * @return string xml/json
          */
-        private static function publicKeySet($format, $user, $key) {
+        private static function publicKeySet($format, $user, $key) {
         	$login=OC_OCS::checkpassword();
-        	if($login == $user) {
-        		if(OC_User::userExists($user)){
-        			//TODO: SET public key
-        			echo self::generateXml('', 'ok', 100, 'Public key uploaded');
-        		}else{
-        			echo self::generateXml('', 'fail', 300, 'User does not exist');
+        	if(($login==$user)) {
+        		if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        			if (($key = OCA_Encryption\Keymanager::setPublicKey($user, $key))) {
+        				echo self::generateXml('', 'ok', 100, '');
+        			} else {
+        				echo self::generateXml('', 'fail', 404, 'could not add your public key to the key storage');
+        			}
+        		} else {
+        			echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
         		}
         	}else{
         		echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
         	}
         }
-
+        	
         /**
         * get the private key of a user
         * @param string $format
@@ -725,19 +738,22 @@ class OC_OCS {
 		 * @param string $key
 		 * @return string xml/json
 		 */
-		private static function privateKeySet($format, $user, $key) {
-			$login=OC_OCS::checkpassword();
-			if($login == $user) {
-				if(OC_User::userExists($user)){
-					//TODO: SET private key
-					echo self::generateXml('', 'ok', 100, 'Private key uploaded');
-				}else{
-					echo self::generateXml('', 'fail', 300, 'User does not exist');
-				}
-			}else{
-				echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
-			}
-		}
+        private static function privateKeySet($format, $user, $key) {
+        	$login=OC_OCS::checkpassword();
+        	if(($login==$user)) {
+        		if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        			if (($key = OCA_Encryption\Keymanager::setPrivateKey($user, $key))) {
+        				echo self::generateXml('', 'ok', 100, '');
+        			} else {
+        				echo self::generateXml('', 'fail', 404, 'could not add your private key to the key storage');
+        			}
+        		} else {
+        			echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
+        		}
+        	}else{
+        		echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
+        	}
+        }
 
 		/**
 		 * get the encryption key of a file
-- 
GitLab