From d039f11905658f2642d84f4054abde0c3b920ea8 Mon Sep 17 00:00:00 2001
From: Bjoern Schiessle <schiessle@owncloud.com>
Date: Wed, 15 Aug 2012 13:18:11 +0200
Subject: [PATCH] provide ocs calls and keymanager functions to get/set both
 keys (private, public) of a user together

---
 apps/files_encryption/lib/keymanager.php | 27 ++++++++++++
 lib/ocs.php                              | 53 ++++++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php
index 1ffeff99288..ea6e4872d4b 100644
--- a/apps/files_encryption/lib/keymanager.php
+++ b/apps/files_encryption/lib/keymanager.php
@@ -55,6 +55,20 @@ class Keymanager {
 		
 	}
 	
+	/**
+	 * @brief retrieve both keys from a user (private and public)
+	 *
+	 * @return string private key or false
+	 */
+	public static function getUserKeys() {
+	
+	return array(
+			'privatekey' => self::getPrivateKey(),
+			'publickey' => self::getPublicKey(),
+			);
+	
+	}
+	
 	/**
 	 * @brief retrieve a list of the public key from all users with access to the file
 	 *
@@ -145,6 +159,19 @@ class Keymanager {
 		
 	}
 	
+	/**
+	 * @brief store private keys from the user
+	 *
+	 * @param string privatekey
+	 * @param string publickey
+	 * @return bool true/false
+	 */
+	public static function setUserKeys($privatekey, $publickey) {
+	
+		return (self::setPrivateKey($privatekey) && self::setPublicKey($publickey));
+	
+	}
+	
 	
 	/**
 	 * @brief store public key of the user
diff --git a/lib/ocs.php b/lib/ocs.php
index 5d4e19c0c4a..423e1752da6 100644
--- a/lib/ocs.php
+++ b/lib/ocs.php
@@ -187,6 +187,16 @@ class OC_OCS {
 				$key = self::readData('post', 'key', 'string');
 				OC_OCS::privateKeySet($format, $key);
 			
+		// keygetuser
+		}elseif(($method=='get') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='cloud') and ($ex[$paracount-2] == 'userkeys')){
+			OC_OCS::userKeysGet($format);
+			
+		//keysetuser
+		}elseif(($method=='post') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='cloud') and ($ex[$paracount-2] == 'userkeys')){
+			$privatekey = self::readData('post', 'privatekey', 'string');
+			$publickey = self::readData('post', 'publickey', 'string');
+			OC_OCS::userKeysSet($format, $privatekey, $publickey);
+			
 		// keygetfiles
 		}elseif(($method=='get') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'file') and ($ex[$paracount-2] == 'filekey')){
 			$file = urldecode($ex[$paracount-3]);
@@ -744,6 +754,49 @@ class OC_OCS {
         	}
         }
 
+        /**
+         * get both user keys (private and public)
+         * @param string $format
+         * @return string xml/json
+         */
+        private static function userKeysGet($format) {
+        	$login=OC_OCS::checkpassword();
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode() === 'client') {
+        		$keys = OCA_Encryption\Keymanager::getUserKeys();
+        		if ($keys['privatekey'] && $keys['publickey']) {
+        			$xml=array();
+        			$xml['privatekey']=$keys['privatekey'];
+        			$xml['publickey']=$keys['publickey'];
+        			$txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
+        			echo($txt);
+        		} else {
+        			echo self::generateXml('', 'fail', 404, 'Keys not found on the server');
+        		}
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled');
+        	}
+        }
+        
+        /**
+         * set both user keys (private and public)
+         * @param string $format
+         * @param string $privatekey
+         * @param string @publickey
+         * @return string xml/json
+         */
+        private static function userKeysSet($format, $privatekey, $publickey) {
+        	$login=OC_OCS::checkpassword();
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode() === 'client') {
+        		if (($key = OCA_Encryption\Keymanager::setUserKeys($privatekey, $publickey))) {
+        			echo self::generateXml('', 'ok', 100, '');
+        		} else {
+        			echo self::generateXml('', 'fail', 404, 'could not add your keys to the key storage');
+        		}
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled');
+        	}
+        }
+        
 		/**
 		 * get the encryption key of a file
 		 * @param string $format
-- 
GitLab