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