Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10fe9434 authored by Robert Shih's avatar Robert Shih
Browse files

DrmUtils: helpers to create hidl CryptoFactories/Plugins

Bug: 134787536
Test: testPocCVE_2017_13253
Change-Id: I3d71b249bd55d4895b5201d470f37817bcb5903b
parent e6cbccae
Loading
Loading
Loading
Loading
+83 −0
Original line number Original line Diff line number Diff line
@@ -17,6 +17,15 @@
//#define LOG_NDEBUG 0
//#define LOG_NDEBUG 0
#define LOG_TAG "DrmUtils"
#define LOG_TAG "DrmUtils"


#include <android/hardware/drm/1.0/ICryptoFactory.h>
#include <android/hardware/drm/1.0/ICryptoPlugin.h>
#include <android/hardware/drm/1.1/ICryptoFactory.h>
#include <android/hardware/drm/1.2/ICryptoFactory.h>
#include <android/hidl/manager/1.0/IServiceManager.h>
#include <hidl/HidlSupport.h>

#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/String16.h>
#include <utils/String16.h>
#include <binder/IInterface.h>
#include <binder/IInterface.h>
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>
@@ -29,6 +38,12 @@
#include <mediadrm/IDrm.h>
#include <mediadrm/IDrm.h>
#include <mediadrm/IMediaDrmService.h>
#include <mediadrm/IMediaDrmService.h>


using HServiceManager = ::android::hidl::manager::V1_0::IServiceManager;
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using namespace ::android::hardware::drm;

namespace android {
namespace android {
namespace DrmUtils {
namespace DrmUtils {


@@ -67,6 +82,57 @@ sp<Iface> MakeObject(status_t *pstatus) {
        return new Hal();
        return new Hal();
    }
    }
}
}

template <typename Hal, typename V>
void MakeCryptoFactories(const uint8_t uuid[16], V &cryptoFactories) {
    sp<HServiceManager> serviceManager = HServiceManager::getService();
    if (serviceManager == nullptr) {
        ALOGE("Failed to get service manager");
        exit(-1);
    }

    serviceManager->listByInterface(Hal::descriptor, [&](const hidl_vec<hidl_string> &registered) {
        for (const auto &instance : registered) {
            auto factory = Hal::getService(instance);
            if (factory != nullptr) {
                ALOGI("found %s %s", Hal::descriptor, instance.c_str());
                if (factory->isCryptoSchemeSupported(uuid)) {
                    cryptoFactories.push_back(factory);
                }
            }
        }
    });
}

hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) {
    hidl_vec<uint8_t> vec(size);
    if (ptr != nullptr) {
        memcpy(vec.data(), ptr, size);
    }
    return vec;
}

hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) {
    if (ptr == nullptr) {
        return hidl_array<uint8_t, 16>();
    }
    return hidl_array<uint8_t, 16>(ptr);
}

sp<::V1_0::ICryptoPlugin> MakeCryptoPlugin(const sp<::V1_0::ICryptoFactory> &factory,
                                           const uint8_t uuid[16], const void *initData,
                                           size_t initDataSize) {
    sp<::V1_0::ICryptoPlugin> plugin;
    factory->createPlugin(toHidlArray16(uuid), toHidlVec(initData, initDataSize),
                          [&](::V1_0::Status status, const sp<::V1_0::ICryptoPlugin> &hPlugin) {
                              if (status != ::V1_0::Status::OK) {
                                  return;
                              }
                              plugin = hPlugin;
                          });
    return plugin;
}

} // namespace
} // namespace


bool UseDrmService() {
bool UseDrmService() {
@@ -81,5 +147,22 @@ sp<ICrypto> MakeCrypto(status_t *pstatus) {
    return MakeObject<ICrypto, CryptoHal>(pstatus);
    return MakeObject<ICrypto, CryptoHal>(pstatus);
}
}


std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]) {
    std::vector<sp<::V1_0::ICryptoFactory>> cryptoFactories;
    MakeCryptoFactories<::V1_0::ICryptoFactory>(uuid, cryptoFactories);
    MakeCryptoFactories<::V1_1::ICryptoFactory>(uuid, cryptoFactories);
    MakeCryptoFactories<::V1_2::ICryptoFactory>(uuid, cryptoFactories);
    return cryptoFactories;
}

std::vector<sp<ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16], const void *initData,
                                                 size_t initDataSize) {
    std::vector<sp<ICryptoPlugin>> plugins;
    for (const auto &factory : MakeCryptoFactories(uuid)) {
        plugins.push_back(MakeCryptoPlugin(factory, uuid, initData, initDataSize));
    }
    return plugins;
}

}  // namespace DrmUtils
}  // namespace DrmUtils
}  // namespace android
}  // namespace android
+9 −0
Original line number Original line Diff line number Diff line
@@ -17,9 +17,13 @@
#ifndef ANDROID_DRMUTILS_H
#ifndef ANDROID_DRMUTILS_H
#define ANDROID_DRMUTILS_H
#define ANDROID_DRMUTILS_H


#include <android/hardware/drm/1.0/ICryptoFactory.h>
#include <utils/Errors.h>  // for status_t
#include <utils/Errors.h>  // for status_t
#include <utils/StrongPointer.h>
#include <utils/StrongPointer.h>
#include <binder/Parcel.h>
#include <binder/Parcel.h>
#include <vector>

using namespace ::android::hardware::drm;


namespace android {
namespace android {


@@ -77,6 +81,11 @@ void WriteKeysChange(
    obj.writeInt32(hasNewUsableKey);
    obj.writeInt32(hasNewUsableKey);
}
}


std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);

std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
                                                         const void *initData, size_t initDataSize);

} // namespace DrmUtils
} // namespace DrmUtils


} // namespace android
} // namespace android