InputStreamBinder.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Nextcloud SingleSignOn
  3. *
  4. * @author David Luhmer
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * More information here: https://github.com/abeluck/android-streams-ipc
  20. */
  21. package com.nextcloud.android.sso;
  22. import android.accounts.Account;
  23. import android.accounts.AuthenticatorException;
  24. import android.accounts.OperationCanceledException;
  25. import android.content.Context;
  26. import android.content.SharedPreferences;
  27. import android.os.Binder;
  28. import android.os.ParcelFileDescriptor;
  29. import android.util.Log;
  30. import com.nextcloud.android.sso.aidl.IInputStreamService;
  31. import com.nextcloud.android.sso.aidl.NextcloudRequest;
  32. import com.nextcloud.android.sso.aidl.ParcelFileDescriptorUtil;
  33. import com.owncloud.android.authentication.AccountUtils;
  34. import com.owncloud.android.lib.common.OwnCloudAccount;
  35. import com.owncloud.android.lib.common.OwnCloudClient;
  36. import com.owncloud.android.lib.common.OwnCloudClientManager;
  37. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  38. import com.owncloud.android.lib.common.utils.Log_OC;
  39. import com.owncloud.android.utils.EncryptionUtils;
  40. import org.apache.commons.httpclient.HttpMethodBase;
  41. import org.apache.commons.httpclient.NameValuePair;
  42. import org.apache.commons.httpclient.methods.DeleteMethod;
  43. import org.apache.commons.httpclient.methods.GetMethod;
  44. import org.apache.commons.httpclient.methods.PostMethod;
  45. import org.apache.commons.httpclient.methods.PutMethod;
  46. import org.apache.commons.httpclient.methods.StringRequestEntity;
  47. import java.io.BufferedReader;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.InputStreamReader;
  53. import java.io.ObjectInputStream;
  54. import java.io.ObjectOutputStream;
  55. import java.io.Serializable;
  56. import java.util.Map;
  57. import static com.nextcloud.android.sso.Constants.EXCEPTION_ACCOUNT_NOT_FOUND;
  58. import static com.nextcloud.android.sso.Constants.EXCEPTION_HTTP_REQUEST_FAILED;
  59. import static com.nextcloud.android.sso.Constants.EXCEPTION_INVALID_REQUEST_URL;
  60. import static com.nextcloud.android.sso.Constants.EXCEPTION_INVALID_TOKEN;
  61. import static com.nextcloud.android.sso.Constants.EXCEPTION_UNSUPPORTED_METHOD;
  62. import static com.nextcloud.android.sso.Constants.SSO_SHARED_PREFERENCE;
  63. /**
  64. * Stream binder to pass usable InputStreams across the process boundary in Android.
  65. */
  66. public class InputStreamBinder extends IInputStreamService.Stub {
  67. private final static String TAG = "InputStreamBinder";
  68. private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
  69. private static final String CHARSET_UTF8 = "UTF-8";
  70. private static final int HTTP_STATUS_CODE_OK = 200;
  71. private static final int HTTP_STATUS_CODE_MULTIPLE_CHOICES = 300;
  72. private static final char PATH_SEPARATOR = '/';
  73. private Context context;
  74. public InputStreamBinder(Context context) {
  75. this.context = context;
  76. }
  77. private NameValuePair[] convertMapToNVP(Map<String, String> map) {
  78. NameValuePair[] nvp = new NameValuePair[map.size()];
  79. int i = 0;
  80. for (String key : map.keySet()) {
  81. nvp[i] = new NameValuePair(key, map.get(key));
  82. i++;
  83. }
  84. return nvp;
  85. }
  86. public ParcelFileDescriptor performNextcloudRequest(ParcelFileDescriptor input) {
  87. // read the input
  88. final InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
  89. Exception exception = null;
  90. InputStream httpStream = new InputStream() {
  91. @Override
  92. public int read() {
  93. return 0;
  94. }
  95. };
  96. try {
  97. // Start request and catch exceptions
  98. NextcloudRequest request = deserializeObjectAndCloseStream(is);
  99. httpStream = processRequest(request);
  100. } catch (Exception e) {
  101. Log_OC.e(TAG, "Error during Nextcloud request", e);
  102. exception = e;
  103. }
  104. try {
  105. // Write exception to the stream followed by the actual network stream
  106. InputStream exceptionStream = serializeObjectToInputStream(exception);
  107. InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, httpStream);
  108. return ParcelFileDescriptorUtil.pipeFrom(resultStream, thread -> Log.d(TAG, "Done sending result"));
  109. } catch (IOException e) {
  110. Log_OC.e(TAG, "Error while sending response back to client app", e);
  111. }
  112. return null;
  113. }
  114. private <T extends Serializable> ByteArrayInputStream serializeObjectToInputStream(T obj) throws IOException {
  115. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  116. ObjectOutputStream oos = new ObjectOutputStream(baos);
  117. oos.writeObject(obj);
  118. oos.flush();
  119. oos.close();
  120. return new ByteArrayInputStream(baos.toByteArray());
  121. }
  122. private <T extends Serializable> T deserializeObjectAndCloseStream(InputStream is) throws IOException, ClassNotFoundException {
  123. ObjectInputStream ois = new ObjectInputStream(is);
  124. T result = (T) ois.readObject();
  125. is.close();
  126. ois.close();
  127. return result;
  128. }
  129. private InputStream processRequest(final NextcloudRequest request) throws UnsupportedOperationException, com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException, OperationCanceledException, AuthenticatorException, IOException {
  130. Account account = AccountUtils.getOwnCloudAccountByName(context, request.getAccountName()); // TODO handle case that account is not found!
  131. if(account == null) {
  132. throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
  133. }
  134. // Validate token
  135. if (!isValid(request)) {
  136. throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
  137. }
  138. // Validate URL
  139. if(request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
  140. throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL, new IllegalStateException("URL need to start with a /"));
  141. }
  142. OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
  143. OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
  144. OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
  145. String requestUrl = client.getBaseUri() + request.getUrl();
  146. HttpMethodBase method;
  147. switch (request.getMethod()) {
  148. case "GET":
  149. method = new GetMethod(requestUrl);
  150. break;
  151. case "POST":
  152. method = new PostMethod(requestUrl);
  153. if (request.getRequestBody() != null) {
  154. StringRequestEntity requestEntity = new StringRequestEntity(
  155. request.getRequestBody(),
  156. CONTENT_TYPE_APPLICATION_JSON,
  157. CHARSET_UTF8);
  158. ((PostMethod) method).setRequestEntity(requestEntity);
  159. }
  160. break;
  161. case "PUT":
  162. method = new PutMethod(requestUrl);
  163. if (request.getRequestBody() != null) {
  164. StringRequestEntity requestEntity = new StringRequestEntity(
  165. request.getRequestBody(),
  166. CONTENT_TYPE_APPLICATION_JSON,
  167. CHARSET_UTF8);
  168. ((PutMethod) method).setRequestEntity(requestEntity);
  169. }
  170. break;
  171. case "DELETE":
  172. method = new DeleteMethod(requestUrl);
  173. break;
  174. default:
  175. throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);
  176. }
  177. method.setQueryString(convertMapToNVP(request.getParameter()));
  178. method.addRequestHeader("OCS-APIREQUEST", "true");
  179. client.setFollowRedirects(request.isFollowRedirects());
  180. int status = client.executeMethod(method);
  181. // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
  182. if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
  183. return method.getResponseBodyAsStream();
  184. } else {
  185. StringBuilder total = new StringBuilder();
  186. InputStream inputStream = method.getResponseBodyAsStream();
  187. // If response body is available
  188. if(inputStream != null) {
  189. BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
  190. for (String line; (line = r.readLine()) != null; ) {
  191. total.append(line).append('\n');
  192. }
  193. Log_OC.e(TAG, total.toString());
  194. }
  195. throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED, new IllegalStateException(String.valueOf(status), new Throwable(total.toString())));
  196. }
  197. }
  198. private boolean isValid(NextcloudRequest request) {
  199. String callingPackageName = context.getPackageManager().getNameForUid(Binder.getCallingUid());
  200. SharedPreferences sharedPreferences = context.getSharedPreferences(SSO_SHARED_PREFERENCE,
  201. Context.MODE_PRIVATE);
  202. String hash = sharedPreferences.getString(callingPackageName, "");
  203. return validateToken(hash, request.getToken());
  204. }
  205. private boolean validateToken(String hash, String token) {
  206. if(hash.isEmpty() || !hash.contains("$")) {
  207. throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
  208. }
  209. String salt = hash.split("\\$")[1]; // TODO extract "$"
  210. String newHash = EncryptionUtils.generateSHA512(token, salt);
  211. // As discussed with Lukas R. at the Nextcloud Conf 2018, always compare whole strings
  212. // and don't exit prematurely if the string does not match anymore to prevent timing-attacks
  213. return isEqual(hash.getBytes(), newHash.getBytes());
  214. }
  215. // Taken from http://codahale.com/a-lesson-in-timing-attacks/
  216. private static boolean isEqual(byte[] a, byte[] b) {
  217. if (a.length != b.length) {
  218. return false;
  219. }
  220. int result = 0;
  221. for (int i = 0; i < a.length; i++) {
  222. result |= a[i] ^ b[i];
  223. }
  224. return result == 0;
  225. }
  226. }