1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "ClearKeyCryptoPlugin"
19#include <utils/Log.h>
20
21#include <media/stagefright/MediaErrors.h>
22#include <utils/StrongPointer.h>
23
24#include "DrmPlugin.h"
25
26#include "Session.h"
27
28namespace clearkeydrm {
29
30using android::sp;
31
32status_t DrmPlugin::openSession(Vector<uint8_t>& sessionId) {
33    sp<Session> session = mSessionLibrary->createSession();
34    sessionId = session->sessionId();
35    return android::OK;
36}
37
38status_t DrmPlugin::closeSession(const Vector<uint8_t>& sessionId) {
39    sp<Session> session = mSessionLibrary->findSession(sessionId);
40    if (sessionId.size() == 0) {
41        return android::BAD_VALUE;
42    }
43    if (session.get()) {
44        mSessionLibrary->destroySession(session);
45        return android::OK;
46    }
47    return android::ERROR_DRM_SESSION_NOT_OPENED;
48}
49
50status_t DrmPlugin::getKeyRequest(
51        const Vector<uint8_t>& scope,
52        const Vector<uint8_t>& initData,
53        const String8& mimeType,
54        KeyType keyType,
55        const KeyedVector<String8, String8>& optionalParameters,
56        Vector<uint8_t>& request,
57        String8& defaultUrl,
58        DrmPlugin::KeyRequestType *keyRequestType) {
59    UNUSED(optionalParameters);
60    if (scope.size() == 0) {
61        return android::BAD_VALUE;
62    }
63    if (keyType != kKeyType_Streaming) {
64        return android::ERROR_DRM_CANNOT_HANDLE;
65    }
66    *keyRequestType = DrmPlugin::kKeyRequestType_Initial;
67    defaultUrl.clear();
68    sp<Session> session = mSessionLibrary->findSession(scope);
69    if (!session.get()) {
70        return android::ERROR_DRM_SESSION_NOT_OPENED;
71    }
72    return session->getKeyRequest(initData, mimeType, &request);
73}
74
75status_t DrmPlugin::provideKeyResponse(
76        const Vector<uint8_t>& scope,
77        const Vector<uint8_t>& response,
78        Vector<uint8_t>& keySetId) {
79    if (scope.size() == 0 || response.size() == 0) {
80        return android::BAD_VALUE;
81    }
82    sp<Session> session = mSessionLibrary->findSession(scope);
83    if (!session.get()) {
84        return android::ERROR_DRM_SESSION_NOT_OPENED;
85    }
86    status_t res = session->provideKeyResponse(response);
87    if (res == android::OK) {
88        // This is for testing AMediaDrm_setOnEventListener only.
89        sendEvent(kDrmPluginEventVendorDefined, 0, &scope, NULL);
90        keySetId.clear();
91    }
92    return res;
93}
94
95status_t DrmPlugin::getPropertyString(
96        const String8& name, String8& value) const {
97    if (name == "vendor") {
98        value = "Google";
99    } else if (name == "version") {
100        value = "1.0";
101    } else if (name == "description") {
102        value = "ClearKey CDM";
103    } else if (name == "algorithms") {
104        value = "";
105    } else if (name == "listenerTestSupport") {
106        value = "true";
107    } else {
108        ALOGE("App requested unknown string property %s", name.string());
109        return android::ERROR_DRM_CANNOT_HANDLE;
110    }
111    return android::OK;
112}
113
114}  // namespace clearkeydrm
115