InputStreamBinder.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Nextcloud SingleSignOn
  3. *
  4. * @author David Luhmer
  5. * Copyright (C) 2019 David Luhmer
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * More information here: https://github.com/abeluck/android-streams-ipc
  21. */
  22. package com.nextcloud.android.sso;
  23. import android.accounts.Account;
  24. import android.accounts.AuthenticatorException;
  25. import android.accounts.OperationCanceledException;
  26. import android.content.Context;
  27. import android.content.SharedPreferences;
  28. import android.net.Uri;
  29. import android.os.Binder;
  30. import android.os.ParcelFileDescriptor;
  31. import android.text.TextUtils;
  32. import android.util.Log;
  33. import com.nextcloud.android.sso.aidl.IInputStreamService;
  34. import com.nextcloud.android.sso.aidl.NextcloudRequest;
  35. import com.nextcloud.android.sso.aidl.ParcelFileDescriptorUtil;
  36. import com.nextcloud.client.account.UserAccountManager;
  37. import com.owncloud.android.lib.common.OwnCloudAccount;
  38. import com.owncloud.android.lib.common.OwnCloudClient;
  39. import com.owncloud.android.lib.common.OwnCloudClientManager;
  40. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  41. import com.owncloud.android.lib.common.utils.Log_OC;
  42. import com.owncloud.android.utils.EncryptionUtils;
  43. import org.apache.commons.httpclient.HttpConnection;
  44. import org.apache.commons.httpclient.HttpMethodBase;
  45. import org.apache.commons.httpclient.HttpState;
  46. import org.apache.commons.httpclient.NameValuePair;
  47. import org.apache.commons.httpclient.methods.DeleteMethod;
  48. import org.apache.commons.httpclient.methods.GetMethod;
  49. import org.apache.commons.httpclient.methods.HeadMethod;
  50. import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
  51. import org.apache.commons.httpclient.methods.PostMethod;
  52. import org.apache.commons.httpclient.methods.PutMethod;
  53. import org.apache.commons.httpclient.methods.RequestEntity;
  54. import org.apache.commons.httpclient.methods.StringRequestEntity;
  55. import org.apache.jackrabbit.webdav.DavConstants;
  56. import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
  57. import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
  58. import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
  59. import java.io.BufferedReader;
  60. import java.io.ByteArrayInputStream;
  61. import java.io.ByteArrayOutputStream;
  62. import java.io.IOException;
  63. import java.io.InputStream;
  64. import java.io.InputStreamReader;
  65. import java.io.ObjectInputStream;
  66. import java.io.ObjectOutputStream;
  67. import java.io.Serializable;
  68. import java.util.Collection;
  69. import java.util.List;
  70. import java.util.Map;
  71. import androidx.annotation.VisibleForTesting;
  72. import static com.nextcloud.android.sso.Constants.DELIMITER;
  73. import static com.nextcloud.android.sso.Constants.EXCEPTION_ACCOUNT_NOT_FOUND;
  74. import static com.nextcloud.android.sso.Constants.EXCEPTION_HTTP_REQUEST_FAILED;
  75. import static com.nextcloud.android.sso.Constants.EXCEPTION_INVALID_REQUEST_URL;
  76. import static com.nextcloud.android.sso.Constants.EXCEPTION_INVALID_TOKEN;
  77. import static com.nextcloud.android.sso.Constants.EXCEPTION_UNSUPPORTED_METHOD;
  78. import static com.nextcloud.android.sso.Constants.SSO_SHARED_PREFERENCE;
  79. /**
  80. * Stream binder to pass usable InputStreams across the process boundary in Android.
  81. */
  82. public class InputStreamBinder extends IInputStreamService.Stub {
  83. private final static String TAG = "InputStreamBinder";
  84. private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
  85. private static final String CHARSET_UTF8 = "UTF-8";
  86. private static final int HTTP_STATUS_CODE_OK = 200;
  87. private static final int HTTP_STATUS_CODE_MULTIPLE_CHOICES = 300;
  88. private static final char PATH_SEPARATOR = '/';
  89. private static final int ZERO_LENGTH = 0;
  90. private Context context;
  91. private UserAccountManager accountManager;
  92. public InputStreamBinder(Context context, UserAccountManager accountManager) {
  93. this.context = context;
  94. this.accountManager = accountManager;
  95. }
  96. public ParcelFileDescriptor performNextcloudRequestV2(ParcelFileDescriptor input) {
  97. return performNextcloudRequestAndBodyStreamV2(input, null);
  98. }
  99. public ParcelFileDescriptor performNextcloudRequestAndBodyStreamV2(
  100. ParcelFileDescriptor input,
  101. ParcelFileDescriptor requestBodyParcelFileDescriptor) {
  102. // read the input
  103. final InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
  104. final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
  105. new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
  106. Exception exception = null;
  107. Response response = new Response();
  108. try {
  109. // Start request and catch exceptions
  110. NextcloudRequest request = deserializeObjectAndCloseStream(is);
  111. response = processRequestV2(request, requestBodyInputStream);
  112. } catch (Exception e) {
  113. Log_OC.e(TAG, "Error during Nextcloud request", e);
  114. exception = e;
  115. }
  116. try {
  117. // Write exception to the stream followed by the actual network stream
  118. InputStream exceptionStream = serializeObjectToInputStreamV2(exception, response.getPlainHeadersString());
  119. InputStream resultStream = new java.io.SequenceInputStream(exceptionStream, response.getBody());
  120. return ParcelFileDescriptorUtil.pipeFrom(resultStream,
  121. thread -> Log.d(TAG, "Done sending result"),
  122. response.getMethod());
  123. } catch (IOException e) {
  124. Log_OC.e(TAG, "Error while sending response back to client app", e);
  125. }
  126. return null;
  127. }
  128. public ParcelFileDescriptor performNextcloudRequest(ParcelFileDescriptor input) {
  129. return performNextcloudRequestAndBodyStream(input, null);
  130. }
  131. public ParcelFileDescriptor performNextcloudRequestAndBodyStream(
  132. ParcelFileDescriptor input,
  133. ParcelFileDescriptor requestBodyParcelFileDescriptor) {
  134. // read the input
  135. final InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
  136. final InputStream requestBodyInputStream = requestBodyParcelFileDescriptor != null ?
  137. new ParcelFileDescriptor.AutoCloseInputStream(requestBodyParcelFileDescriptor) : null;
  138. Exception exception = null;
  139. HttpMethodBase httpMethod = null;
  140. InputStream httpStream = new InputStream() {
  141. @Override
  142. public int read() {
  143. return ZERO_LENGTH;
  144. }
  145. };
  146. try {
  147. // Start request and catch exceptions
  148. NextcloudRequest request = deserializeObjectAndCloseStream(is);
  149. httpMethod = processRequest(request, requestBodyInputStream);
  150. httpStream = httpMethod.getResponseBodyAsStream();
  151. } catch (Exception e) {
  152. Log_OC.e(TAG, "Error during Nextcloud request", e);
  153. exception = e;
  154. }
  155. try {
  156. // Write exception to the stream followed by the actual network stream
  157. InputStream exceptionStream = serializeObjectToInputStream(exception);
  158. InputStream resultStream;
  159. if (httpStream != null) {
  160. resultStream = new java.io.SequenceInputStream(exceptionStream, httpStream);
  161. } else {
  162. resultStream = exceptionStream;
  163. }
  164. return ParcelFileDescriptorUtil.pipeFrom(resultStream,
  165. thread -> Log.d(TAG, "Done sending result"),
  166. httpMethod);
  167. } catch (IOException e) {
  168. Log_OC.e(TAG, "Error while sending response back to client app", e);
  169. }
  170. return null;
  171. }
  172. private ByteArrayInputStream serializeObjectToInputStreamV2(Exception exception, String headers) {
  173. byte[] baosByteArray = new byte[0];
  174. try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  175. ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  176. oos.writeObject(exception);
  177. oos.writeObject(headers);
  178. oos.flush();
  179. oos.close();
  180. baosByteArray = baos.toByteArray();
  181. } catch (IOException e) {
  182. Log_OC.e(TAG, "Error while sending response back to client app", e);
  183. }
  184. return new ByteArrayInputStream(baosByteArray);
  185. }
  186. private <T extends Serializable> ByteArrayInputStream serializeObjectToInputStream(T obj) throws IOException {
  187. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  188. ObjectOutputStream oos = new ObjectOutputStream(baos);
  189. oos.writeObject(obj);
  190. oos.flush();
  191. oos.close();
  192. return new ByteArrayInputStream(baos.toByteArray());
  193. }
  194. private <T extends Serializable> T deserializeObjectAndCloseStream(InputStream is) throws IOException,
  195. ClassNotFoundException {
  196. ObjectInputStream ois = new ObjectInputStream(is);
  197. T result = (T) ois.readObject();
  198. is.close();
  199. ois.close();
  200. return result;
  201. }
  202. public class NCPropFindMethod extends PropFindMethod {
  203. NCPropFindMethod(String uri, int propfindType, int depth) throws IOException {
  204. super(uri, propfindType, new DavPropertyNameSet(), depth);
  205. }
  206. @Override
  207. protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
  208. // Do not process the response body here. Instead pass it on to client app.
  209. }
  210. }
  211. private HttpMethodBase buildMethod(NextcloudRequest request, Uri baseUri, InputStream requestBodyInputStream)
  212. throws IOException {
  213. String requestUrl = baseUri + request.getUrl();
  214. HttpMethodBase method;
  215. switch (request.getMethod()) {
  216. case "GET":
  217. method = new GetMethod(requestUrl);
  218. break;
  219. case "POST":
  220. method = new PostMethod(requestUrl);
  221. if (requestBodyInputStream != null) {
  222. RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
  223. ((PostMethod) method).setRequestEntity(requestEntity);
  224. } else if (request.getRequestBody() != null) {
  225. StringRequestEntity requestEntity = new StringRequestEntity(
  226. request.getRequestBody(),
  227. CONTENT_TYPE_APPLICATION_JSON,
  228. CHARSET_UTF8);
  229. ((PostMethod) method).setRequestEntity(requestEntity);
  230. }
  231. break;
  232. case "PATCH":
  233. method = new PatchMethod(requestUrl);
  234. if (requestBodyInputStream != null) {
  235. RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
  236. ((PatchMethod) method).setRequestEntity(requestEntity);
  237. } else if (request.getRequestBody() != null) {
  238. StringRequestEntity requestEntity = new StringRequestEntity(
  239. request.getRequestBody(),
  240. CONTENT_TYPE_APPLICATION_JSON,
  241. CHARSET_UTF8);
  242. ((PatchMethod) method).setRequestEntity(requestEntity);
  243. }
  244. break;
  245. case "PUT":
  246. method = new PutMethod(requestUrl);
  247. if (requestBodyInputStream != null) {
  248. RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
  249. ((PutMethod) method).setRequestEntity(requestEntity);
  250. } else if (request.getRequestBody() != null) {
  251. StringRequestEntity requestEntity = new StringRequestEntity(
  252. request.getRequestBody(),
  253. CONTENT_TYPE_APPLICATION_JSON,
  254. CHARSET_UTF8);
  255. ((PutMethod) method).setRequestEntity(requestEntity);
  256. }
  257. break;
  258. case "DELETE":
  259. method = new DeleteMethod(requestUrl);
  260. break;
  261. case "PROPFIND":
  262. method = new NCPropFindMethod(requestUrl, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
  263. if (request.getRequestBody() != null) {
  264. //text/xml; charset=UTF-8 is taken from XmlRequestEntity... Should be application/xml
  265. StringRequestEntity requestEntity = new StringRequestEntity(
  266. request.getRequestBody(),
  267. "text/xml; charset=UTF-8",
  268. CHARSET_UTF8);
  269. ((PropFindMethod) method).setRequestEntity(requestEntity);
  270. }
  271. break;
  272. case "MKCOL":
  273. method = new MkColMethod(requestUrl);
  274. break;
  275. case "HEAD":
  276. method = new HeadMethod(requestUrl);
  277. break;
  278. default:
  279. throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);
  280. }
  281. return method;
  282. }
  283. private HttpMethodBase processRequest(final NextcloudRequest request, final InputStream requestBodyInputStream)
  284. throws UnsupportedOperationException,
  285. com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
  286. OperationCanceledException, AuthenticatorException, IOException {
  287. Account account = accountManager.getAccountByName(request.getAccountName());
  288. if (account == null) {
  289. throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
  290. }
  291. // Validate token
  292. if (!isValid(request)) {
  293. throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
  294. }
  295. // Validate URL
  296. if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
  297. throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL,
  298. new IllegalStateException("URL need to start with a /"));
  299. }
  300. OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
  301. OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
  302. OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
  303. HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
  304. if (request.getParameterV2() != null && !request.getParameterV2().isEmpty()) {
  305. method.setQueryString(convertListToNVP(request.getParameterV2()));
  306. } else {
  307. method.setQueryString(convertMapToNVP(request.getParameter()));
  308. }
  309. method.addRequestHeader("OCS-APIREQUEST", "true");
  310. for (Map.Entry<String, List<String>> header : request.getHeader().entrySet()) {
  311. // https://stackoverflow.com/a/3097052
  312. method.addRequestHeader(header.getKey(), TextUtils.join(",", header.getValue()));
  313. if ("OCS-APIREQUEST".equalsIgnoreCase(header.getKey())) {
  314. throw new IllegalStateException(
  315. "The 'OCS-APIREQUEST' header will be automatically added by the Nextcloud SSO Library. " +
  316. "Please remove the header before making a request");
  317. }
  318. }
  319. client.setFollowRedirects(request.isFollowRedirects());
  320. int status = client.executeMethod(method);
  321. // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
  322. if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
  323. return method;
  324. } else {
  325. InputStream inputStream = method.getResponseBodyAsStream();
  326. String total = "No response body";
  327. // If response body is available
  328. if (inputStream != null) {
  329. total = inputStreamToString(inputStream);
  330. Log_OC.e(TAG, total);
  331. }
  332. method.releaseConnection();
  333. throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED,
  334. new IllegalStateException(String.valueOf(status),
  335. new IllegalStateException(total)));
  336. }
  337. }
  338. private Response processRequestV2(final NextcloudRequest request, final InputStream requestBodyInputStream)
  339. throws UnsupportedOperationException,
  340. com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
  341. OperationCanceledException, AuthenticatorException, IOException {
  342. Account account = accountManager.getAccountByName(request.getAccountName());
  343. if (account == null) {
  344. throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
  345. }
  346. // Validate token
  347. if (!isValid(request)) {
  348. throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
  349. }
  350. // Validate URL
  351. if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
  352. throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL,
  353. new IllegalStateException("URL need to start with a /"));
  354. }
  355. OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
  356. OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
  357. OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);
  358. HttpMethodBase method = buildMethod(request, client.getBaseUri(), requestBodyInputStream);
  359. if (request.getParameterV2() != null && !request.getParameterV2().isEmpty()) {
  360. method.setQueryString(convertListToNVP(request.getParameterV2()));
  361. } else {
  362. method.setQueryString(convertMapToNVP(request.getParameter()));
  363. }
  364. method.addRequestHeader("OCS-APIREQUEST", "true");
  365. for (Map.Entry<String, List<String>> header : request.getHeader().entrySet()) {
  366. // https://stackoverflow.com/a/3097052
  367. method.addRequestHeader(header.getKey(), TextUtils.join(",", header.getValue()));
  368. if ("OCS-APIREQUEST".equalsIgnoreCase(header.getKey())) {
  369. throw new IllegalStateException(
  370. "The 'OCS-APIREQUEST' header will be automatically added by the Nextcloud SSO Library. " +
  371. "Please remove the header before making a request");
  372. }
  373. }
  374. client.setFollowRedirects(request.isFollowRedirects());
  375. int status = client.executeMethod(method);
  376. // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
  377. if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
  378. return new Response(method);
  379. } else {
  380. InputStream inputStream = method.getResponseBodyAsStream();
  381. String total = "No response body";
  382. // If response body is available
  383. if (inputStream != null) {
  384. total = inputStreamToString(inputStream);
  385. Log_OC.e(TAG, total);
  386. }
  387. method.releaseConnection();
  388. throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED,
  389. new IllegalStateException(String.valueOf(status),
  390. new IllegalStateException(total)));
  391. }
  392. }
  393. private boolean isValid(NextcloudRequest request) {
  394. String callingPackageName = context.getPackageManager().getNameForUid(Binder.getCallingUid());
  395. SharedPreferences sharedPreferences = context.getSharedPreferences(SSO_SHARED_PREFERENCE,
  396. Context.MODE_PRIVATE);
  397. String hash = sharedPreferences.getString(callingPackageName + DELIMITER + request.getAccountName(), "");
  398. return validateToken(hash, request.getToken());
  399. }
  400. private boolean validateToken(String hash, String token) {
  401. if (hash.isEmpty() || !hash.contains("$")) {
  402. throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
  403. }
  404. String salt = hash.split("\\$")[1]; // TODO extract "$"
  405. String newHash = EncryptionUtils.generateSHA512(token, salt);
  406. // As discussed with Lukas R. at the Nextcloud Conf 2018, always compare whole strings
  407. // and don't exit prematurely if the string does not match anymore to prevent timing-attacks
  408. return isEqual(hash.getBytes(), newHash.getBytes());
  409. }
  410. // Taken from http://codahale.com/a-lesson-in-timing-attacks/
  411. private static boolean isEqual(byte[] a, byte[] b) {
  412. if (a.length != b.length) {
  413. return false;
  414. }
  415. int result = 0;
  416. for (int i = 0; i < a.length; i++) {
  417. result |= a[i] ^ b[i];
  418. }
  419. return result == 0;
  420. }
  421. private static String inputStreamToString(InputStream inputStream) {
  422. try {
  423. StringBuilder total = new StringBuilder();
  424. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  425. String line = reader.readLine();
  426. while (line != null) {
  427. total.append(line).append('\n');
  428. line = reader.readLine();
  429. }
  430. return total.toString();
  431. } catch (Exception e) {
  432. return e.getMessage();
  433. }
  434. }
  435. @VisibleForTesting
  436. public static NameValuePair[] convertMapToNVP(Map<String, String> map) {
  437. NameValuePair[] nvp = new NameValuePair[map.size()];
  438. int i = 0;
  439. for (String key : map.keySet()) {
  440. nvp[i] = new NameValuePair(key, map.get(key));
  441. i++;
  442. }
  443. return nvp;
  444. }
  445. @VisibleForTesting
  446. public static NameValuePair[] convertListToNVP(Collection<QueryParam> list) {
  447. NameValuePair[] nvp = new NameValuePair[list.size()];
  448. int i = 0;
  449. for (QueryParam pair : list) {
  450. nvp[i] = new NameValuePair(pair.key, pair.value);
  451. i++;
  452. }
  453. return nvp;
  454. }
  455. }