diff --git a/android/LICENSE_ASSETBRIDGE b/android/LICENSE_ASSETBRIDGE deleted file mode 100644 index 09d4125bc..000000000 --- a/android/LICENSE_ASSETBRIDGE +++ /dev/null @@ -1,23 +0,0 @@ -Copyright (c) 2013 Steve Havelka - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the "Software"), to deal in the Software without -restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. - diff --git a/android/java/lib/src/main/java/com/arieslabs/assetbridge/Assetbridge.java b/android/java/lib/src/main/java/com/arieslabs/assetbridge/Assetbridge.java deleted file mode 100644 index 357f053f6..000000000 --- a/android/java/lib/src/main/java/com/arieslabs/assetbridge/Assetbridge.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.arieslabs.assetbridge; - -import android.content.Context; -import android.content.res.AssetManager; -import android.util.Log; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; - -public class Assetbridge { - - private static final String TAG = "Assetbridge"; - - /*static { - System.loadLibrary("assetbridge"); - }*/ - - // unpack - public static void unpack(Context c) { - - try { - // first let's get the data directory - String datadir = c.getFilesDir().getAbsolutePath(); - Log.v(TAG, "Extracting assets to " + datadir); - - // now we need the assetmanager - AssetManager am = c.getAssets(); - - // only extract what we need - ArrayList assets = new ArrayList(); - assets.add("ca-bundle.crt"); - //assets.add("styles"); - - // iterate on the files... - for(String asset : assets) { - delete(datadir + "/" + asset); - copyAssetFolder(am, asset, datadir + "/" + asset); - } - - // last, set the ASSETDIR environment variable for the C - // parts of the procee - //setassetdir(c.getCacheDir().getPath()); - - } catch (IOException e) { - Log.e("Assetbridge", "Can't unpack assets from APK", e); - } - - } - - public static void delete(String dest) { - File destfh = new File(dest); - if (destfh.exists()) { - destfh.delete(); - } - } - - - public static void copyAssetFolder(AssetManager am, String src, String dest) - throws IOException { - Log.v(TAG, "Copying " + src); - - InputStream srcIS = null; - File destfh; - - // this is the only way we can tell if this is a file or a - // folder - we have to open the asset, and if the open fails, - // it's a folder... - boolean isDir = false; - try { - srcIS = am.open(src); - } catch (FileNotFoundException e) { - isDir = true; - } - - Log.v(TAG, src + " was " + (isDir ? "a dir" : "a file")); - - // either way, we'll use the dest as a File - destfh = new File(dest); - - // and now, depending on .. - if(isDir) { - - // If the directory doesn't yet exist, create it - if( !destfh.exists() ) { - if (!destfh.mkdir()) { - throw new IOException("Failed to mkdir"); - } - } - - // list the assets in the directory... - String assets[] = am.list(src); - - // and copy them all using same. - for(String asset : assets) { - copyAssetFolder(am, src + "/" + asset, dest + "/" + asset); - } - - } else { - - int count, buffer_len = 2048; - byte[] data = new byte[buffer_len]; - - // copy the file from the assets subsystem to the filesystem - FileOutputStream destOS = new FileOutputStream(destfh); - - //copy the file content in bytes - while( (count = srcIS.read(data, 0, buffer_len)) != -1) { - destOS.write(data, 0, count); - } - - // and close the two files - srcIS.close(); - destOS.close(); - - } - } - - // the native method to set the environment variable - //public static native void setassetdir(String s); -} diff --git a/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java b/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java index 48fb8840d..ce6496db1 100644 --- a/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java +++ b/android/java/lib/src/main/java/com/mapbox/mapboxgl/lib/MapView.java @@ -16,7 +16,6 @@ import android.net.NetworkInfo; import android.os.Bundle; import android.support.annotation.NonNull; import android.util.AttributeSet; -import android.util.DisplayMetrics; import android.util.Log; import android.view.GestureDetector; import android.view.InputDevice; @@ -31,7 +30,6 @@ import android.widget.ZoomButtonsController; import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector; import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector; -import com.arieslabs.assetbridge.Assetbridge; // Custom view that shows a Map // Based on SurfaceView as we use OpenGL ES to render @@ -143,9 +141,6 @@ public class MapView extends SurfaceView { String dataPath = context.getFilesDir().getAbsolutePath(); String apkPath = context.getPackageCodePath(); - // Extract the asset files - Assetbridge.unpack(context); - // Load the map style and API key //mStyleUrl = "https://mapbox.github.io/mapbox-gl-styles/styles/bright-v6.json"; mStyleUrl = "asset://styles/styles/bright-v6.json"; diff --git a/gyp/mbgl-android.gypi b/gyp/mbgl-android.gypi index 7b9c6faa0..8f35ddea9 100644 --- a/gyp/mbgl-android.gypi +++ b/gyp/mbgl-android.gypi @@ -12,6 +12,7 @@ '<@(curl_cflags)', '<@(nu_cflags)', '<@(zip_cflags)', + '<@(openssl_cflags)', '-I<(boost_root)/include', ], 'cflags': [ diff --git a/platform/android/asset_request_baton.cpp b/platform/android/asset_request_baton.cpp index c4e0a1799..30819e506 100644 --- a/platform/android/asset_request_baton.cpp +++ b/platform/android/asset_request_baton.cpp @@ -89,25 +89,7 @@ void AssetRequestBaton::run(uv_async_t *async) { cleanup(async); return; } -/* - if (stat.size > std::numeric_limits::max()) { - // File is too large for us to open this way because uv_buf's only support unsigned - // ints as maximum size. - if (ptr->request) { - ptr->request->response = std::unique_ptr(new Response); - ptr->request->response->code = UV_EFBIG; - ptr->request->response->message = uv_strerror(UV_EFBIG); - ptr->request->notify(); - } - zip_fclose(ptr->apk_file); - ptr->apk_file = nullptr; - zip_close(ptr->apk); - ptr->apk = nullptr; - cleanup(async); - return; - } -*/ - //const unsigned int size = static_cast(stat.size); + const std::unique_ptr body = boost::make_unique(stat.size); if (zip_fread(ptr->apk_file, reinterpret_cast(body.get()), stat.size) == -1 || diff --git a/platform/default/http_request_baton_curl.cpp b/platform/default/http_request_baton_curl.cpp index fb66215c0..23029bc5e 100644 --- a/platform/default/http_request_baton_curl.cpp +++ b/platform/default/http_request_baton_curl.cpp @@ -6,6 +6,9 @@ #ifdef __ANDROID__ #include + #include + #include + #include #endif #include @@ -408,6 +411,103 @@ size_t curl_header_cb(char * const buffer, const size_t size, const size_t nmemb return length; } +// This function is called to load the CA bundle +// from http://curl.haxx.se/libcurl/c/cacertinmem.html +#ifdef __ANDROID__ +static CURLcode sslctx_function(CURL */*curl*/, void *sslctx, void */*parm*/) { + + int error = 0; + struct zip *apk = zip_open(mbgl::android::apk_path.c_str(), 0, &error); + if (apk == nullptr) { + return CURLE_SSL_CACERT_BADFILE; + } + + struct zip_file *apk_file = zip_fopen(apk, "assets/ca-bundle.crt", ZIP_FL_NOCASE); + if (apk_file == nullptr) { + zip_close(apk); + apk = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + struct zip_stat stat; + if (zip_stat(apk, "assets/ca-bundle.crt", ZIP_FL_NOCASE, &stat) != 0) { + zip_fclose(apk_file); + apk_file = nullptr; + zip_close(apk); + apk = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + if (stat.size > std::numeric_limits::max()) { + zip_fclose(apk_file); + apk_file = nullptr; + zip_close(apk); + apk = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + const std::unique_ptr pem = boost::make_unique(stat.size); + + if (zip_fread(apk_file, reinterpret_cast(pem.get()), stat.size) == -1) { + zip_fclose(apk_file); + apk_file = nullptr; + zip_close(apk); + apk = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + // get a pointer to the X509 certificate store (which may be empty!) + X509_STORE *store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx); + if (store == nullptr) { + return CURLE_SSL_CACERT_BADFILE; + } + + // get a BIO + BIO *bio = BIO_new_mem_buf(pem.get(), static_cast(stat.size)); + if (bio == nullptr) { + store = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + // use it to read the PEM formatted certificate from memory into an X509 + // structure that SSL can use + X509 *cert = nullptr; + while (PEM_read_bio_X509(bio, &cert, 0, nullptr) != nullptr) { + if (cert == nullptr) { + BIO_free(bio); + bio = nullptr; + store = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + // add our certificate to this store + if (X509_STORE_add_cert(store, cert) == 0) { + X509_free(cert); + cert = nullptr; + BIO_free(bio); + bio = nullptr; + store = nullptr; + return CURLE_SSL_CACERT_BADFILE; + } + + X509_free(cert); + cert = nullptr; + } + + // decrease reference counts + BIO_free(bio); + bio = nullptr; + + zip_fclose(apk_file); + apk_file = nullptr; + zip_close(apk); + apk = nullptr; + + // all set to go + return CURLE_OK; +} +#endif + // This function must run in the CURL thread. void start_request(void *const ptr) { assert(uv_thread_self() == thread_id); @@ -437,15 +537,14 @@ void start_request(void *const ptr) { context->baton->response = std::unique_ptr(new Response()); } -#ifndef __ANDROID__ - std::string ca_path = "ca-bundle.crt"; -#else - std::string ca_path = mbgl::android::data_path + "/ca-bundle.crt"; -#endif - // Carry on the shared pointer in the private information of the CURL handle. curl_easy_setopt(context->handle, CURLOPT_PRIVATE, context); - curl_easy_setopt(context->handle, CURLOPT_CAINFO, ca_path.c_str()); +#ifndef __ANDROID__ + curl_easy_setopt(context->handle, CURLOPT_CAINFO, "ca-bundle.crt") +#else + curl_easy_setopt(context->handle, CURLOPT_SSLCERTTYPE, "PEM"); + curl_easy_setopt(context->handle, CURLOPT_SSL_CTX_FUNCTION, sslctx_function); +#endif curl_easy_setopt(context->handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(context->handle, CURLOPT_URL, context->baton->path.c_str()); curl_easy_setopt(context->handle, CURLOPT_WRITEFUNCTION, curl_write_cb);