1/*
2 * Copyright (C) 2010 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 "DrmManagerService(Native)"
19#include <utils/Log.h>
20
21#include <private/android_filesystem_config.h>
22#include <media/MemoryLeakTrackUtil.h>
23
24#include <errno.h>
25#include <utils/threads.h>
26#include <binder/IServiceManager.h>
27#include <binder/IPCThreadState.h>
28#include <sys/stat.h>
29#include "DrmManagerService.h"
30#include "DrmManager.h"
31
32using namespace android;
33
34static Vector<uid_t> trustedUids;
35
36static bool isProtectedCallAllowed() {
37    return true;
38}
39
40void DrmManagerService::instantiate() {
41    ALOGV("instantiate");
42    defaultServiceManager()->addService(String16("drm.drmManager"), new DrmManagerService());
43
44    if (0 >= trustedUids.size()) {
45        // TODO
46        // Following implementation is just for reference.
47        // Each OEM manufacturer should implement/replace with their own solutions.
48
49        // Add trusted uids here
50        trustedUids.push(AID_MEDIA);
51    }
52}
53
54DrmManagerService::DrmManagerService() :
55        mDrmManager(NULL) {
56    ALOGV("created");
57    mDrmManager = new DrmManager();
58    mDrmManager->loadPlugIns();
59}
60
61DrmManagerService::~DrmManagerService() {
62    ALOGV("Destroyed");
63    mDrmManager->unloadPlugIns();
64    delete mDrmManager; mDrmManager = NULL;
65}
66
67int DrmManagerService::addUniqueId(bool isNative) {
68    return mDrmManager->addUniqueId(isNative);
69}
70
71void DrmManagerService::removeUniqueId(int uniqueId) {
72    mDrmManager->removeUniqueId(uniqueId);
73}
74
75void DrmManagerService::addClient(int uniqueId) {
76    mDrmManager->addClient(uniqueId);
77}
78
79void DrmManagerService::removeClient(int uniqueId) {
80    mDrmManager->removeClient(uniqueId);
81}
82
83status_t DrmManagerService::setDrmServiceListener(
84            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
85    ALOGV("Entering setDrmServiceListener");
86    mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
87    return DRM_NO_ERROR;
88}
89
90DrmConstraints* DrmManagerService::getConstraints(
91            int uniqueId, const String8* path, const int action) {
92    ALOGV("Entering getConstraints from content");
93    return mDrmManager->getConstraints(uniqueId, path, action);
94}
95
96DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
97    ALOGV("Entering getMetadata from content");
98    return mDrmManager->getMetadata(uniqueId, path);
99}
100
101bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
102    ALOGV("Entering canHandle");
103    return mDrmManager->canHandle(uniqueId, path, mimeType);
104}
105
106DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
107    ALOGV("Entering processDrmInfo");
108    return mDrmManager->processDrmInfo(uniqueId, drmInfo);
109}
110
111DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
112    ALOGV("Entering acquireDrmInfo");
113    return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
114}
115
116status_t DrmManagerService::saveRights(
117            int uniqueId, const DrmRights& drmRights,
118            const String8& rightsPath, const String8& contentPath) {
119    ALOGV("Entering saveRights");
120    return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
121}
122
123String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
124    ALOGV("Entering getOriginalMimeType");
125    return mDrmManager->getOriginalMimeType(uniqueId, path, fd);
126}
127
128int DrmManagerService::getDrmObjectType(
129           int uniqueId, const String8& path, const String8& mimeType) {
130    ALOGV("Entering getDrmObjectType");
131    return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
132}
133
134int DrmManagerService::checkRightsStatus(
135            int uniqueId, const String8& path, int action) {
136    ALOGV("Entering checkRightsStatus");
137    return mDrmManager->checkRightsStatus(uniqueId, path, action);
138}
139
140status_t DrmManagerService::consumeRights(
141            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
142    ALOGV("Entering consumeRights");
143    if (!isProtectedCallAllowed()) {
144        return DRM_ERROR_NO_PERMISSION;
145    }
146    return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
147}
148
149status_t DrmManagerService::setPlaybackStatus(
150            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
151    ALOGV("Entering setPlaybackStatus");
152    if (!isProtectedCallAllowed()) {
153        return DRM_ERROR_NO_PERMISSION;
154    }
155    return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
156}
157
158bool DrmManagerService::validateAction(
159            int uniqueId, const String8& path,
160            int action, const ActionDescription& description) {
161    ALOGV("Entering validateAction");
162    return mDrmManager->validateAction(uniqueId, path, action, description);
163}
164
165status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
166    ALOGV("Entering removeRights");
167    return mDrmManager->removeRights(uniqueId, path);
168}
169
170status_t DrmManagerService::removeAllRights(int uniqueId) {
171    ALOGV("Entering removeAllRights");
172    return mDrmManager->removeAllRights(uniqueId);
173}
174
175int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
176    ALOGV("Entering openConvertSession");
177    return mDrmManager->openConvertSession(uniqueId, mimeType);
178}
179
180DrmConvertedStatus* DrmManagerService::convertData(
181            int uniqueId, int convertId, const DrmBuffer* inputData) {
182    ALOGV("Entering convertData");
183    return mDrmManager->convertData(uniqueId, convertId, inputData);
184}
185
186DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
187    ALOGV("Entering closeConvertSession");
188    return mDrmManager->closeConvertSession(uniqueId, convertId);
189}
190
191status_t DrmManagerService::getAllSupportInfo(
192            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
193    ALOGV("Entering getAllSupportInfo");
194    return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
195}
196
197DecryptHandle* DrmManagerService::openDecryptSession(
198            int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
199    ALOGV("Entering DrmManagerService::openDecryptSession");
200    if (isProtectedCallAllowed()) {
201        return mDrmManager->openDecryptSession(uniqueId, fd, offset, length, mime);
202    }
203
204    return NULL;
205}
206
207DecryptHandle* DrmManagerService::openDecryptSession(
208            int uniqueId, const char* uri, const char* mime) {
209    ALOGV("Entering DrmManagerService::openDecryptSession with uri");
210    if (isProtectedCallAllowed()) {
211        return mDrmManager->openDecryptSession(uniqueId, uri, mime);
212    }
213
214    return NULL;
215}
216
217DecryptHandle* DrmManagerService::openDecryptSession(
218            int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
219    ALOGV("Entering DrmManagerService::openDecryptSession for streaming");
220    if (isProtectedCallAllowed()) {
221        return mDrmManager->openDecryptSession(uniqueId, buf, mimeType);
222    }
223
224    return NULL;
225}
226
227status_t DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
228    ALOGV("Entering closeDecryptSession");
229    if (!isProtectedCallAllowed()) {
230        return DRM_ERROR_NO_PERMISSION;
231    }
232    return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
233}
234
235status_t DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
236            int decryptUnitId, const DrmBuffer* headerInfo) {
237    ALOGV("Entering initializeDecryptUnit");
238    if (!isProtectedCallAllowed()) {
239        return DRM_ERROR_NO_PERMISSION;
240    }
241    return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
242}
243
244status_t DrmManagerService::decrypt(
245            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
246            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
247    ALOGV("Entering decrypt");
248    if (!isProtectedCallAllowed()) {
249        return DRM_ERROR_NO_PERMISSION;
250    }
251    return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
252}
253
254status_t DrmManagerService::finalizeDecryptUnit(
255            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
256    ALOGV("Entering finalizeDecryptUnit");
257    if (!isProtectedCallAllowed()) {
258        return DRM_ERROR_NO_PERMISSION;
259    }
260    return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
261}
262
263ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
264            void* buffer, ssize_t numBytes, off64_t offset) {
265    ALOGV("Entering pread");
266    if (!isProtectedCallAllowed()) {
267        return DRM_ERROR_NO_PERMISSION;
268    }
269    return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
270}
271
272status_t DrmManagerService::dump(int fd, const Vector<String16>& args)
273{
274    const size_t SIZE = 256;
275    char buffer[SIZE];
276    String8 result;
277    if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
278        snprintf(buffer, SIZE, "Permission Denial: "
279                "can't dump DrmManagerService from pid=%d, uid=%d\n",
280                IPCThreadState::self()->getCallingPid(),
281                IPCThreadState::self()->getCallingUid());
282        result.append(buffer);
283    } else {
284#if DRM_MEMORY_LEAK_TRACK
285        bool dumpMem = false;
286        for (size_t i = 0; i < args.size(); i++) {
287            if (args[i] == String16("-m")) {
288                dumpMem = true;
289            }
290        }
291        if (dumpMem) {
292            dumpMemoryAddresses(fd);
293        }
294#endif
295    }
296    write(fd, result.string(), result.size());
297    return NO_ERROR;
298}
299
300