blob: 63341e06f7c860a4e9326e23a867214e0153628b [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
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
Takeshi Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshi27ed8ad2010-07-29 10:12:27 +090018#define LOG_TAG "DrmManagerService(Native)"
19#include <utils/Log.h>
20
Takeshi Aimi34738462010-11-16 13:56:11 +090021#include <private/android_filesystem_config.h>
James Dong8635b7b2011-03-14 17:01:38 -070022#include <media/MemoryLeakTrackUtil.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090023
aimitakeshi27ed8ad2010-07-29 10:12:27 +090024#include <errno.h>
25#include <utils/threads.h>
26#include <binder/IServiceManager.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090027#include <binder/IPCThreadState.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090028#include <sys/stat.h>
29#include "DrmManagerService.h"
30#include "DrmManager.h"
31
32using namespace android;
33
Takeshi Aimi34738462010-11-16 13:56:11 +090034static Vector<uid_t> trustedUids;
35
36static bool isProtectedCallAllowed() {
Jeff Tinkerb7100942014-06-17 16:45:46 -070037 // 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;
Takeshi Aimi34738462010-11-16 13:56:11 +090049}
50
aimitakeshi27ed8ad2010-07-29 10:12:27 +090051void DrmManagerService::instantiate() {
Steve Block3856b092011-10-20 11:56:00 +010052 ALOGV("instantiate");
Takeshi Aimie943f842010-10-08 23:05:49 +090053 defaultServiceManager()->addService(String16("drm.drmManager"), new DrmManagerService());
Takeshi Aimi34738462010-11-16 13:56:11 +090054
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 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +090063}
64
Takeshi Aimie943f842010-10-08 23:05:49 +090065DrmManagerService::DrmManagerService() :
66 mDrmManager(NULL) {
Steve Block3856b092011-10-20 11:56:00 +010067 ALOGV("created");
aimitakeshi27ed8ad2010-07-29 10:12:27 +090068 mDrmManager = new DrmManager();
Takeshi Aimie943f842010-10-08 23:05:49 +090069 mDrmManager->loadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090070}
71
72DrmManagerService::~DrmManagerService() {
Steve Block3856b092011-10-20 11:56:00 +010073 ALOGV("Destroyed");
Takeshi Aimie943f842010-10-08 23:05:49 +090074 mDrmManager->unloadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090075 delete mDrmManager; mDrmManager = NULL;
76}
77
Gloria Wang8f001512011-07-21 15:10:22 -070078int DrmManagerService::addUniqueId(bool isNative) {
79 return mDrmManager->addUniqueId(isNative);
Takeshi Aimi2272ee22010-09-20 23:40:41 +090080}
81
82void DrmManagerService::removeUniqueId(int uniqueId) {
83 mDrmManager->removeUniqueId(uniqueId);
84}
85
Takeshi Aimie943f842010-10-08 23:05:49 +090086void DrmManagerService::addClient(int uniqueId) {
87 mDrmManager->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090088}
89
Takeshi Aimie943f842010-10-08 23:05:49 +090090void DrmManagerService::removeClient(int uniqueId) {
91 mDrmManager->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090092}
93
94status_t DrmManagerService::setDrmServiceListener(
95 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Steve Block3856b092011-10-20 11:56:00 +010096 ALOGV("Entering setDrmServiceListener");
aimitakeshi27ed8ad2010-07-29 10:12:27 +090097 mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
98 return DRM_NO_ERROR;
99}
100
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900101DrmConstraints* DrmManagerService::getConstraints(
102 int uniqueId, const String8* path, const int action) {
Steve Block3856b092011-10-20 11:56:00 +0100103 ALOGV("Entering getConstraints from content");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900104 return mDrmManager->getConstraints(uniqueId, path, action);
105}
106
Takeshi Aimi34738462010-11-16 13:56:11 +0900107DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
Steve Block3856b092011-10-20 11:56:00 +0100108 ALOGV("Entering getMetadata from content");
Takeshi Aimi34738462010-11-16 13:56:11 +0900109 return mDrmManager->getMetadata(uniqueId, path);
110}
111
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900112bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100113 ALOGV("Entering canHandle");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900114 return mDrmManager->canHandle(uniqueId, path, mimeType);
115}
116
117DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100118 ALOGV("Entering processDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900119 return mDrmManager->processDrmInfo(uniqueId, drmInfo);
120}
121
122DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Steve Block3856b092011-10-20 11:56:00 +0100123 ALOGV("Entering acquireDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900124 return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
125}
126
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900127status_t DrmManagerService::saveRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900128 int uniqueId, const DrmRights& drmRights,
129 const String8& rightsPath, const String8& contentPath) {
Steve Block3856b092011-10-20 11:56:00 +0100130 ALOGV("Entering saveRights");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900131 return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
132}
133
James Dongbf5b3b22012-07-30 17:57:39 -0700134String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
Steve Block3856b092011-10-20 11:56:00 +0100135 ALOGV("Entering getOriginalMimeType");
James Dongbf5b3b22012-07-30 17:57:39 -0700136 return mDrmManager->getOriginalMimeType(uniqueId, path, fd);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900137}
138
139int DrmManagerService::getDrmObjectType(
140 int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100141 ALOGV("Entering getDrmObjectType");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900142 return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
143}
144
145int DrmManagerService::checkRightsStatus(
146 int uniqueId, const String8& path, int action) {
Steve Block3856b092011-10-20 11:56:00 +0100147 ALOGV("Entering checkRightsStatus");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900148 return mDrmManager->checkRightsStatus(uniqueId, path, action);
149}
150
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900151status_t DrmManagerService::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900152 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Steve Block3856b092011-10-20 11:56:00 +0100153 ALOGV("Entering consumeRights");
James Dong328745b2012-02-28 13:55:55 -0800154 if (!isProtectedCallAllowed()) {
155 return DRM_ERROR_NO_PERMISSION;
156 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900157 return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900158}
159
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900160status_t DrmManagerService::setPlaybackStatus(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800161 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Steve Block3856b092011-10-20 11:56:00 +0100162 ALOGV("Entering setPlaybackStatus");
James Dong328745b2012-02-28 13:55:55 -0800163 if (!isProtectedCallAllowed()) {
164 return DRM_ERROR_NO_PERMISSION;
165 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900166 return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900167}
168
169bool DrmManagerService::validateAction(
170 int uniqueId, const String8& path,
171 int action, const ActionDescription& description) {
Steve Block3856b092011-10-20 11:56:00 +0100172 ALOGV("Entering validateAction");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900173 return mDrmManager->validateAction(uniqueId, path, action, description);
174}
175
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900176status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
Steve Block3856b092011-10-20 11:56:00 +0100177 ALOGV("Entering removeRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900178 return mDrmManager->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900179}
180
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900181status_t DrmManagerService::removeAllRights(int uniqueId) {
Steve Block3856b092011-10-20 11:56:00 +0100182 ALOGV("Entering removeAllRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900183 return mDrmManager->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900184}
185
186int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100187 ALOGV("Entering openConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900188 return mDrmManager->openConvertSession(uniqueId, mimeType);
189}
190
191DrmConvertedStatus* DrmManagerService::convertData(
192 int uniqueId, int convertId, const DrmBuffer* inputData) {
Steve Block3856b092011-10-20 11:56:00 +0100193 ALOGV("Entering convertData");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900194 return mDrmManager->convertData(uniqueId, convertId, inputData);
195}
196
197DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
Steve Block3856b092011-10-20 11:56:00 +0100198 ALOGV("Entering closeConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900199 return mDrmManager->closeConvertSession(uniqueId, convertId);
200}
201
202status_t DrmManagerService::getAllSupportInfo(
203 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Steve Block3856b092011-10-20 11:56:00 +0100204 ALOGV("Entering getAllSupportInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900205 return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
206}
207
208DecryptHandle* DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800209 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100210 ALOGV("Entering DrmManagerService::openDecryptSession");
Takeshi Aimi34738462010-11-16 13:56:11 +0900211 if (isProtectedCallAllowed()) {
James Dong9d2f3862012-01-10 08:24:37 -0800212 return mDrmManager->openDecryptSession(uniqueId, fd, offset, length, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900213 }
214
215 return NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900216}
217
Takeshi Aimie943f842010-10-08 23:05:49 +0900218DecryptHandle* DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800219 int uniqueId, const char* uri, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("Entering DrmManagerService::openDecryptSession with uri");
Takeshi Aimi34738462010-11-16 13:56:11 +0900221 if (isProtectedCallAllowed()) {
James Dong9d2f3862012-01-10 08:24:37 -0800222 return mDrmManager->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900223 }
224
225 return NULL;
Takeshi Aimie943f842010-10-08 23:05:49 +0900226}
227
Kei Takahashicba7b322012-01-18 17:10:19 +0900228DecryptHandle* 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
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900238status_t DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Steve Block3856b092011-10-20 11:56:00 +0100239 ALOGV("Entering closeDecryptSession");
James Dong328745b2012-02-28 13:55:55 -0800240 if (!isProtectedCallAllowed()) {
241 return DRM_ERROR_NO_PERMISSION;
242 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900243 return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900244}
245
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900246status_t DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900247 int decryptUnitId, const DrmBuffer* headerInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100248 ALOGV("Entering initializeDecryptUnit");
James Dong328745b2012-02-28 13:55:55 -0800249 if (!isProtectedCallAllowed()) {
250 return DRM_ERROR_NO_PERMISSION;
251 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900252 return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900253}
254
255status_t DrmManagerService::decrypt(
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900256 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
257 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
Steve Block3856b092011-10-20 11:56:00 +0100258 ALOGV("Entering decrypt");
James Dong328745b2012-02-28 13:55:55 -0800259 if (!isProtectedCallAllowed()) {
260 return DRM_ERROR_NO_PERMISSION;
261 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900262 return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900263}
264
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900265status_t DrmManagerService::finalizeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900266 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Steve Block3856b092011-10-20 11:56:00 +0100267 ALOGV("Entering finalizeDecryptUnit");
James Dong328745b2012-02-28 13:55:55 -0800268 if (!isProtectedCallAllowed()) {
269 return DRM_ERROR_NO_PERMISSION;
270 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900271 return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900272}
273
274ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800275 void* buffer, ssize_t numBytes, off64_t offset) {
Steve Block3856b092011-10-20 11:56:00 +0100276 ALOGV("Entering pread");
James Dong328745b2012-02-28 13:55:55 -0800277 if (!isProtectedCallAllowed()) {
278 return DRM_ERROR_NO_PERMISSION;
279 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900280 return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
281}
282
James Dong8635b7b2011-03-14 17:01:38 -0700283status_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