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