1b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker/*
2b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * Copyright (C) 2016 The Android Open Source Project
3b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker *
4b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * Licensed under the Apache License, Version 2.0 (the "License");
5b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * you may not use this file except in compliance with the License.
6b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * You may obtain a copy of the License at
7b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker *
8b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker *      http://www.apache.org/licenses/LICENSE-2.0
9b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker *
10b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * Unless required by applicable law or agreed to in writing, software
11b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * distributed under the License is distributed on an "AS IS" BASIS,
12b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * See the License for the specific language governing permissions and
14b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker * limitations under the License.
15b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker */
16972a3e329276e9880e4977d8dfeb4ec85edaa863Jeff Tinker#define LOG_TAG "android.hardware.drm@1.0-impl"
17b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker
18b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker#include "CryptoFactory.h"
193eb7df74b8eae5c902039d1b76402350715a0944Steven Moreland#include <log/log.h>
20b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker#include "CryptoPlugin.h"
21fcde9ff3a08a2c569f0ec90fa3487ecb9d34444dJohn W. Bruce#include "LegacyPluginPath.h"
22b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker#include "TypeConvert.h"
23b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker
24b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinkernamespace android {
25b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinkernamespace hardware {
26b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinkernamespace drm {
27b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinkernamespace V1_0 {
28b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinkernamespace implementation {
29b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker
309a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker    CryptoFactory::CryptoFactory() :
31fcde9ff3a08a2c569f0ec90fa3487ecb9d34444dJohn W. Bruce        loader(getDrmPluginPath(), "createCryptoFactory") {
32b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker    }
33b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker
349a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker    // Methods from ::android::hardware::drm::V1_0::ICryptoFactory follow.
359a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker    Return<bool> CryptoFactory::isCryptoSchemeSupported(
369a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker            const hidl_array<uint8_t, 16>& uuid) {
379a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker        for (size_t i = 0; i < loader.factoryCount(); i++) {
389a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker            if (loader.getFactory(i)->isCryptoSchemeSupported(uuid.data())) {
399a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                return true;
409a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker            }
41b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker        }
429a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker        return false;
43b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker    }
44b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker
459a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker    Return<void> CryptoFactory::createPlugin(const hidl_array<uint8_t, 16>& uuid,
469a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker            const hidl_vec<uint8_t>& initData, createPlugin_cb _hidl_cb) {
479a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker        for (size_t i = 0; i < loader.factoryCount(); i++) {
489a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker            if (loader.getFactory(i)->isCryptoSchemeSupported(uuid.data())) {
499a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                android::CryptoPlugin *legacyPlugin = NULL;
509a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                status_t status = loader.getFactory(i)->createPlugin(uuid.data(),
519a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                        initData.data(), initData.size(), &legacyPlugin);
529a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                CryptoPlugin *newPlugin = NULL;
539a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                if (legacyPlugin == NULL) {
549a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                    ALOGE("Crypto legacy HAL: failed to create crypto plugin");
559a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                } else {
569a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                    newPlugin = new CryptoPlugin(legacyPlugin);
579a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                }
589a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                _hidl_cb(toStatus(status), newPlugin);
599a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker                return Void();
609a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker            }
61fbf365037014e81711611384aeee9469590210f6Jeff Tinker        }
629a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker        _hidl_cb(Status::ERROR_DRM_CANNOT_HANDLE, NULL);
639a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker        return Void();
64b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker    }
65fbf365037014e81711611384aeee9469590210f6Jeff Tinker
669a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker    ICryptoFactory* HIDL_FETCH_ICryptoFactory(const char* /* name */) {
679a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker        return new CryptoFactory();
689a33bb2b25aaa1ff0fc05b0b4d73f7c2af55ac92Jeff Tinker    }
69b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker
70b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker}  // namespace implementation
71b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker}  // namespace V1_0
72b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker}  // namespace drm
73b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker}  // namespace hardware
74b075caa3b95c0c980e6118fb78878b87ce21a6a3Jeff Tinker}  // namespace android
75