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

Commit 0c108c9f authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Remove clearkey hidl code"

parents 4736f153 5cee4673
Loading
Loading
Loading
Loading
+0 −86
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "hidl_ClearkeyDecryptor"
#include <utils/Log.h>

#include <openssl/aes.h>

#include "AesCtrDecryptor.h"
#include "ClearKeyTypes.h"

namespace android {
namespace hardware {
namespace drm {
namespace V1_4 {
namespace clearkey {

using ::android::hardware::drm::V1_0::SubSample;
using ::android::hardware::drm::V1_0::Status;

static const size_t kBlockBitCount = kBlockSize * 8;

Status AesCtrDecryptor::decrypt(
        const std::vector<uint8_t>& key,
        const Iv iv, const uint8_t* source,
        uint8_t* destination,
        const std::vector<SubSample> subSamples,
        size_t numSubSamples,
        size_t* bytesDecryptedOut) {
    uint32_t blockOffset = 0;
    uint8_t previousEncryptedCounter[kBlockSize];
    memset(previousEncryptedCounter, 0, kBlockSize);

    if (key.size() != kBlockSize || (sizeof(Iv) / sizeof(uint8_t)) != kBlockSize) {
        android_errorWriteLog(0x534e4554, "63982768");
        return Status::ERROR_DRM_DECRYPT;
    }

    size_t offset = 0;
    AES_KEY opensslKey;
    AES_set_encrypt_key(key.data(), kBlockBitCount, &opensslKey);
    Iv opensslIv;
    memcpy(opensslIv, iv, sizeof(opensslIv));

    for (size_t i = 0; i < numSubSamples; ++i) {
        const SubSample& subSample = subSamples[i];

        if (subSample.numBytesOfClearData > 0) {
            memcpy(destination + offset, source + offset,
                    subSample.numBytesOfClearData);
            offset += subSample.numBytesOfClearData;
        }

        if (subSample.numBytesOfEncryptedData > 0) {
            AES_ctr128_encrypt(source + offset, destination + offset,
                    subSample.numBytesOfEncryptedData, &opensslKey,
                    opensslIv, previousEncryptedCounter,
                    &blockOffset);
            offset += subSample.numBytesOfEncryptedData;
        }
    }

    *bytesDecryptedOut = offset;
    return Status::OK;
}

} // namespace clearkey
} // namespace V1_4
} // namespace drm
} // namespace hardware
} // namespace android
+0 −167
Original line number Diff line number Diff line
//
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// *** THIS PACKAGE HAS SPECIAL LICENSING CONDITIONS.  PLEASE
//     CONSULT THE OWNERS AND opensource-licensing@google.com BEFORE
//     DEPENDING ON IT IN YOUR PROJECT. ***
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_av_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    //   legacy_by_exception_only (by exception only)
    default_applicable_licenses: ["frameworks_av_license"],
}

cc_defaults {
    name: "clearkey_service_defaults",
    vendor: true,

    srcs: [
        "AesCtrDecryptor.cpp",
        "Base64.cpp",
        "Buffer.cpp",
        "CreatePluginFactories.cpp",
        "CryptoFactory.cpp",
        "CryptoPlugin.cpp",
        "DeviceFiles.cpp",
        "DrmFactory.cpp",
        "DrmPlugin.cpp",
        "InitDataParser.cpp",
        "JsonWebKey.cpp",
        "MemoryFileSystem.cpp",
        "Session.cpp",
        "SessionLibrary.cpp",
    ],

    relative_install_path: "hw",

    cflags: ["-Wall", "-Werror", "-Wthread-safety"],

    shared_libs: [
        "android.hardware.drm@1.0",
        "android.hardware.drm@1.1",
        "android.hardware.drm@1.2",
        "android.hardware.drm@1.3",
        "android.hardware.drm@1.4",
        "libbase",
        "libbinder",
        "libcrypto",
        "libhidlbase",
        "libhidlmemory",
        "liblog",
        "libprotobuf-cpp-lite",
        "libutils",
    ],

    static_libs: [
        "libclearkeycommon",
        "libclearkeydevicefiles-protos",
        "libjsmn",
    ],

    local_include_dirs: ["include"],

    export_static_lib_headers: ["libjsmn"],

    sanitize: {
        integer_overflow: true,
    },
}
cc_library_static {
    name: "libclearkeydevicefiles-protos",
    vendor: true,

    proto: {
        export_proto_headers: true,
        type: "lite",
    },
    srcs: ["protos/DeviceFiles.proto"],
}

cc_library {
    name: "libclearkeyhidl",
    defaults: ["clearkey_service_defaults"],
}

cc_binary {
    name: "android.hardware.drm@1.2-service.clearkey",
    defaults: ["clearkey_service_defaults"],
    srcs: ["service.cpp"],
    init_rc: ["android.hardware.drm@1.2-service.clearkey.rc"],
    vintf_fragments: ["manifest_android.hardware.drm@1.2-service.clearkey.xml"],
}

cc_binary {
    name: "android.hardware.drm@1.2-service-lazy.clearkey",
    overrides: ["android.hardware.drm@1.2-service.clearkey"],
    defaults: ["clearkey_service_defaults"],
    srcs: ["serviceLazy.cpp"],
    init_rc: ["android.hardware.drm@1.2-service-lazy.clearkey.rc"],
    vintf_fragments: ["manifest_android.hardware.drm@1.2-service.clearkey.xml"],
}

cc_binary {
    name: "android.hardware.drm@1.4-service.clearkey",
    defaults: ["clearkey_service_defaults"],
    srcs: ["service.cpp"],
    init_rc: ["android.hardware.drm@1.4-service.clearkey.rc"],
    vintf_fragments: ["manifest_android.hardware.drm@1.4-service.clearkey.xml"],
}

cc_binary {
    name: "android.hardware.drm@1.4-service-lazy.clearkey",
    overrides: ["android.hardware.drm@1.4-service.clearkey"],
    defaults: ["clearkey_service_defaults"],
    srcs: ["serviceLazy.cpp"],
    init_rc: ["android.hardware.drm@1.4-service-lazy.clearkey.rc"],
    vintf_fragments: ["manifest_android.hardware.drm@1.4-service.clearkey.xml"],
}

cc_fuzz {
    name: "clearkeyV1.4_fuzzer",
    vendor: true,
    srcs: [
        "fuzzer/clearkeyV1.4_fuzzer.cpp",
    ],
    static_libs: [
        "libclearkeyhidl",
        "libclearkeycommon",
        "libclearkeydevicefiles-protos",
        "libjsmn",
        "libprotobuf-cpp-lite",
    ],
    shared_libs: [
        "android.hidl.allocator@1.0",
        "android.hardware.drm@1.0",
        "android.hardware.drm@1.1",
        "android.hardware.drm@1.2",
        "android.hardware.drm@1.3",
        "android.hardware.drm@1.4",
        "libcrypto",
        "libhidlbase",
        "libhidlmemory",
        "liblog",
        "libutils",
    ],
    fuzz_config: {
        cc: [
            "android-media-fuzzing-reports@google.com",
        ],
        componentid: 155276,
    },
}
+0 −175
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Base64.h"

#include <string>

namespace android {
namespace hardware {
namespace drm {
namespace V1_4 {
namespace clearkey {

sp<Buffer> decodeBase64(const std::string &s) {
    size_t n = s.size();

    if ((n % 4) != 0) {
        return nullptr;
    }

    size_t padding = 0;
    if (n >= 1 && s.c_str()[n - 1] == '=') {
        padding = 1;

        if (n >= 2 && s.c_str()[n - 2] == '=') {
            padding = 2;

            if (n >= 3 && s.c_str()[n - 3] == '=') {
                padding = 3;
            }
        }
    }

    // We divide first to avoid overflow. It's OK to do this because we
    // already made sure that n % 4 == 0.
    size_t outLen = (n / 4) * 3 - padding;

    sp<Buffer> buffer = new Buffer(outLen);
    uint8_t *out = buffer->data();
    if (out == nullptr || buffer->size() < outLen) {
        return nullptr;
    }

    size_t j = 0;
    uint32_t accum = 0;
    for (size_t i = 0; i < n; ++i) {
        char c = s.c_str()[i];
        unsigned value;
        if (c >= 'A' && c <= 'Z') {
            value = c - 'A';
        } else if (c >= 'a' && c <= 'z') {
            value = 26 + c - 'a';
        } else if (c >= '0' && c <= '9') {
            value = 52 + c - '0';
        } else if (c == '+' || c == '-') {
            value = 62;
        } else if (c == '/' || c == '_') {
            value = 63;
        } else if (c != '=') {
            return nullptr;
        } else {
            if (i < n - padding) {
                return nullptr;
            }

            value = 0;
        }

        accum = (accum << 6) | value;

        if (((i + 1) % 4) == 0) {
            if (j < outLen) { out[j++] = (accum >> 16); }
            if (j < outLen) { out[j++] = (accum >> 8) & 0xff; }
            if (j < outLen) { out[j++] = accum & 0xff; }

            accum = 0;
        }
    }

    return buffer;
}

static char encode6Bit(unsigned x) {
    if (x <= 25) {
        return 'A' + x;
    } else if (x <= 51) {
        return 'a' + x - 26;
    } else if (x <= 61) {
        return '0' + x - 52;
    } else if (x == 62) {
        return '+';
    } else {
        return '/';
    }
}

void encodeBase64(const void *_data, size_t size, std::string *out) {
    out->clear();

    const uint8_t *data = (const uint8_t *)_data;

    size_t i;
    for (i = 0; i < (size / 3) * 3; i += 3) {
        uint8_t x1 = data[i];
        uint8_t x2 = data[i + 1];
        uint8_t x3 = data[i + 2];

        out->push_back(encode6Bit(x1 >> 2));
        out->push_back(encode6Bit((x1 << 4 | x2 >> 4) & 0x3f));
        out->push_back(encode6Bit((x2 << 2 | x3 >> 6) & 0x3f));
        out->push_back(encode6Bit(x3 & 0x3f));
    }
    switch (size % 3) {
        case 0:
            break;
        case 2:
        {
            uint8_t x1 = data[i];
            uint8_t x2 = data[i + 1];
            out->push_back(encode6Bit(x1 >> 2));
            out->push_back(encode6Bit((x1 << 4 | x2 >> 4) & 0x3f));
            out->push_back(encode6Bit((x2 << 2) & 0x3f));
            out->push_back('=');
            break;
        }
        default:
        {
            uint8_t x1 = data[i];
            out->push_back(encode6Bit(x1 >> 2));
            out->push_back(encode6Bit((x1 << 4) & 0x3f));
            out->append("==");
            break;
        }
    }
}

void encodeBase64Url(const void *_data, size_t size, std::string *out) {
    encodeBase64(_data, size, out);

    if ((std::string::npos != out->find("+")) ||
            (std::string::npos != out->find("/"))) {
        size_t outLen = out->size();
        char *base64url = new char[outLen];
        for (size_t i = 0; i < outLen; ++i) {
            if (out->c_str()[i] == '+')
                base64url[i] = '-';
            else if (out->c_str()[i] == '/')
                base64url[i] = '_';
            else
                base64url[i] = out->c_str()[i];
        }

        out->assign(base64url, outLen);
        delete[] base64url;
    }
}

} // namespace clearkey
} // namespace V1_4
} // namespace drm
} // namespace hardware
} // namespace android
+0 −53
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Buffer.h"

#include <android/hardware/drm/1.0/types.h>

namespace android {
namespace hardware {
namespace drm {
namespace V1_4 {
namespace clearkey {

Buffer::Buffer(size_t capacity)
      : mRangeOffset(0),
      mOwnsData(true) {
    mData = malloc(capacity);
    if (mData == nullptr) {
        mCapacity = 0;
        mRangeLength = 0;
    } else {
        mCapacity = capacity;
        mRangeLength = capacity;
    }
}

Buffer::~Buffer() {
    if (mOwnsData) {
        if (mData != nullptr) {
            free(mData);
            mData = nullptr;
        }
    }
}

} // namespace clearkey
} // namespace V1_4
} // namespace drm
} // namespace hardware
} // namespace android
+0 −44
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CreatePluginFactories.h"

#include "CryptoFactory.h"
#include "DrmFactory.h"

namespace android {
namespace hardware {
namespace drm {
namespace V1_4 {
namespace clearkey {

extern "C" {

IDrmFactory* createDrmFactory() {
    return new DrmFactory();
}

ICryptoFactory* createCryptoFactory() {
    return new CryptoFactory();
}

} // extern "C"

}  // namespace clearkey
}  // namespace V1_4
}  // namespace drm
}  // namespace hardware
}  // namespace android
Loading