1 /*
2  * Copyright (C) 2017 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 "DrmHal"
19 
20 #include <mediadrm/DrmHal.h>
21 #include <mediadrm/DrmHalAidl.h>
22 #include <mediadrm/DrmHalHidl.h>
23 #include <mediadrm/DrmStatus.h>
24 #include <mediadrm/DrmUtils.h>
25 
26 namespace android {
27 
DrmHal()28 DrmHal::DrmHal() {
29     mDrmHalHidl = sp<DrmHalHidl>::make();
30     mDrmHalAidl = sp<DrmHalAidl>::make();
31 }
32 
~DrmHal()33 DrmHal::~DrmHal() {}
34 
initCheck() const35 DrmStatus DrmHal::initCheck() const {
36     if (mDrmHalAidl->initCheck() == OK || mDrmHalHidl->initCheck() == OK) return DrmStatus(OK);
37     if (mDrmHalAidl->initCheck() == NO_INIT || mDrmHalHidl->initCheck() == NO_INIT)
38         return DrmStatus(NO_INIT);
39     return mDrmHalHidl->initCheck();
40 }
41 
isCryptoSchemeSupported(const uint8_t uuid[16],const String8 & mimeType,DrmPlugin::SecurityLevel securityLevel,bool * result)42 DrmStatus DrmHal::isCryptoSchemeSupported(const uint8_t uuid[16], const String8& mimeType,
43                                           DrmPlugin::SecurityLevel securityLevel, bool* result) {
44     DrmStatus statusResult =
45             mDrmHalAidl->isCryptoSchemeSupported(uuid, mimeType, securityLevel, result);
46     if (*result) return statusResult;
47     return mDrmHalHidl->isCryptoSchemeSupported(uuid, mimeType, securityLevel, result);
48 }
49 
createPlugin(const uint8_t uuid[16],const String8 & appPackageName)50 DrmStatus DrmHal::createPlugin(const uint8_t uuid[16], const String8& appPackageName) {
51     return mDrmHalAidl->createPlugin(uuid, appPackageName) == OK
52                    ? DrmStatus(OK)
53                    : mDrmHalHidl->createPlugin(uuid, appPackageName);
54 }
55 
destroyPlugin()56 DrmStatus DrmHal::destroyPlugin() {
57     DrmStatus statusResult = mDrmHalAidl->destroyPlugin();
58     DrmStatus statusResultHidl = mDrmHalHidl->destroyPlugin();
59     if (statusResult != OK) return statusResult;
60     return statusResultHidl;
61 }
62 
openSession(DrmPlugin::SecurityLevel securityLevel,Vector<uint8_t> & sessionId)63 DrmStatus DrmHal::openSession(DrmPlugin::SecurityLevel securityLevel, Vector<uint8_t>& sessionId) {
64     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->openSession(securityLevel, sessionId);
65     return mDrmHalHidl->openSession(securityLevel, sessionId);
66 }
67 
closeSession(Vector<uint8_t> const & sessionId)68 DrmStatus DrmHal::closeSession(Vector<uint8_t> const& sessionId) {
69     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->closeSession(sessionId);
70     return mDrmHalHidl->closeSession(sessionId);
71 }
72 
getKeyRequest(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & initData,String8 const & mimeType,DrmPlugin::KeyType keyType,KeyedVector<String8,String8> const & optionalParameters,Vector<uint8_t> & request,String8 & defaultUrl,DrmPlugin::KeyRequestType * keyRequestType)73 DrmStatus DrmHal::getKeyRequest(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& initData,
74                                 String8 const& mimeType, DrmPlugin::KeyType keyType,
75                                 KeyedVector<String8, String8> const& optionalParameters,
76                                 Vector<uint8_t>& request, String8& defaultUrl,
77                                 DrmPlugin::KeyRequestType* keyRequestType) {
78     if (mDrmHalAidl->initCheck() == OK)
79         return mDrmHalAidl->getKeyRequest(sessionId, initData, mimeType, keyType,
80                                           optionalParameters, request, defaultUrl, keyRequestType);
81     return mDrmHalHidl->getKeyRequest(sessionId, initData, mimeType, keyType, optionalParameters,
82                                       request, defaultUrl, keyRequestType);
83 }
84 
provideKeyResponse(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & response,Vector<uint8_t> & keySetId)85 DrmStatus DrmHal::provideKeyResponse(Vector<uint8_t> const& sessionId,
86                                      Vector<uint8_t> const& response, Vector<uint8_t>& keySetId) {
87     if (mDrmHalAidl->initCheck() == OK)
88         return mDrmHalAidl->provideKeyResponse(sessionId, response, keySetId);
89     return mDrmHalHidl->provideKeyResponse(sessionId, response, keySetId);
90 }
91 
removeKeys(Vector<uint8_t> const & keySetId)92 DrmStatus DrmHal::removeKeys(Vector<uint8_t> const& keySetId) {
93     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->removeKeys(keySetId);
94     return mDrmHalHidl->removeKeys(keySetId);
95 }
96 
restoreKeys(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & keySetId)97 DrmStatus DrmHal::restoreKeys(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keySetId) {
98     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->restoreKeys(sessionId, keySetId);
99     return mDrmHalHidl->restoreKeys(sessionId, keySetId);
100 }
101 
queryKeyStatus(Vector<uint8_t> const & sessionId,KeyedVector<String8,String8> & infoMap) const102 DrmStatus DrmHal::queryKeyStatus(Vector<uint8_t> const& sessionId,
103                                  KeyedVector<String8, String8>& infoMap) const {
104     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->queryKeyStatus(sessionId, infoMap);
105     return mDrmHalHidl->queryKeyStatus(sessionId, infoMap);
106 }
107 
getProvisionRequest(String8 const & certType,String8 const & certAuthority,Vector<uint8_t> & request,String8 & defaultUrl)108 DrmStatus DrmHal::getProvisionRequest(String8 const& certType, String8 const& certAuthority,
109                                       Vector<uint8_t>& request, String8& defaultUrl) {
110     if (mDrmHalAidl->initCheck() == OK)
111         return mDrmHalAidl->getProvisionRequest(certType, certAuthority, request, defaultUrl);
112     return mDrmHalHidl->getProvisionRequest(certType, certAuthority, request, defaultUrl);
113 }
114 
provideProvisionResponse(Vector<uint8_t> const & response,Vector<uint8_t> & certificate,Vector<uint8_t> & wrappedKey)115 DrmStatus DrmHal::provideProvisionResponse(Vector<uint8_t> const& response,
116                                            Vector<uint8_t>& certificate,
117                                            Vector<uint8_t>& wrappedKey) {
118     if (mDrmHalAidl->initCheck() == OK)
119         return mDrmHalAidl->provideProvisionResponse(response, certificate, wrappedKey);
120     return mDrmHalHidl->provideProvisionResponse(response, certificate, wrappedKey);
121 }
122 
getSecureStops(List<Vector<uint8_t>> & secureStops)123 DrmStatus DrmHal::getSecureStops(List<Vector<uint8_t>>& secureStops) {
124     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getSecureStops(secureStops);
125     return mDrmHalHidl->getSecureStops(secureStops);
126 }
127 
getSecureStopIds(List<Vector<uint8_t>> & secureStopIds)128 DrmStatus DrmHal::getSecureStopIds(List<Vector<uint8_t>>& secureStopIds) {
129     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getSecureStopIds(secureStopIds);
130     return mDrmHalHidl->getSecureStopIds(secureStopIds);
131 }
132 
getSecureStop(Vector<uint8_t> const & ssid,Vector<uint8_t> & secureStop)133 DrmStatus DrmHal::getSecureStop(Vector<uint8_t> const& ssid, Vector<uint8_t>& secureStop) {
134     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getSecureStop(ssid, secureStop);
135     return mDrmHalHidl->getSecureStop(ssid, secureStop);
136 }
137 
releaseSecureStops(Vector<uint8_t> const & ssRelease)138 DrmStatus DrmHal::releaseSecureStops(Vector<uint8_t> const& ssRelease) {
139     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->releaseSecureStops(ssRelease);
140     return mDrmHalHidl->releaseSecureStops(ssRelease);
141 }
142 
removeSecureStop(Vector<uint8_t> const & ssid)143 DrmStatus DrmHal::removeSecureStop(Vector<uint8_t> const& ssid) {
144     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->removeSecureStop(ssid);
145     return mDrmHalHidl->removeSecureStop(ssid);
146 }
147 
removeAllSecureStops()148 DrmStatus DrmHal::removeAllSecureStops() {
149     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->removeAllSecureStops();
150     return mDrmHalHidl->removeAllSecureStops();
151 }
152 
getHdcpLevels(DrmPlugin::HdcpLevel * connectedLevel,DrmPlugin::HdcpLevel * maxLevel) const153 DrmStatus DrmHal::getHdcpLevels(DrmPlugin::HdcpLevel* connectedLevel,
154                                 DrmPlugin::HdcpLevel* maxLevel) const {
155     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getHdcpLevels(connectedLevel, maxLevel);
156     return mDrmHalHidl->getHdcpLevels(connectedLevel, maxLevel);
157 }
158 
getNumberOfSessions(uint32_t * currentSessions,uint32_t * maxSessions) const159 DrmStatus DrmHal::getNumberOfSessions(uint32_t* currentSessions, uint32_t* maxSessions) const {
160     if (mDrmHalAidl->initCheck() == OK)
161         return mDrmHalAidl->getNumberOfSessions(currentSessions, maxSessions);
162     return mDrmHalHidl->getNumberOfSessions(currentSessions, maxSessions);
163 }
164 
getSecurityLevel(Vector<uint8_t> const & sessionId,DrmPlugin::SecurityLevel * level) const165 DrmStatus DrmHal::getSecurityLevel(Vector<uint8_t> const& sessionId,
166                                    DrmPlugin::SecurityLevel* level) const {
167     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getSecurityLevel(sessionId, level);
168     return mDrmHalHidl->getSecurityLevel(sessionId, level);
169 }
170 
getOfflineLicenseKeySetIds(List<Vector<uint8_t>> & keySetIds) const171 DrmStatus DrmHal::getOfflineLicenseKeySetIds(List<Vector<uint8_t>>& keySetIds) const {
172     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getOfflineLicenseKeySetIds(keySetIds);
173     return mDrmHalHidl->getOfflineLicenseKeySetIds(keySetIds);
174 }
175 
removeOfflineLicense(Vector<uint8_t> const & keySetId)176 DrmStatus DrmHal::removeOfflineLicense(Vector<uint8_t> const& keySetId) {
177     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->removeOfflineLicense(keySetId);
178     return mDrmHalHidl->removeOfflineLicense(keySetId);
179 }
180 
getOfflineLicenseState(Vector<uint8_t> const & keySetId,DrmPlugin::OfflineLicenseState * licenseState) const181 DrmStatus DrmHal::getOfflineLicenseState(Vector<uint8_t> const& keySetId,
182                                          DrmPlugin::OfflineLicenseState* licenseState) const {
183     if (mDrmHalAidl->initCheck() == OK)
184         return mDrmHalAidl->getOfflineLicenseState(keySetId, licenseState);
185     return mDrmHalHidl->getOfflineLicenseState(keySetId, licenseState);
186 }
187 
getPropertyString(String8 const & name,String8 & value) const188 DrmStatus DrmHal::getPropertyString(String8 const& name, String8& value) const {
189     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getPropertyString(name, value);
190     return mDrmHalHidl->getPropertyString(name, value);
191 }
192 
getPropertyByteArray(String8 const & name,Vector<uint8_t> & value) const193 DrmStatus DrmHal::getPropertyByteArray(String8 const& name, Vector<uint8_t>& value) const {
194     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getPropertyByteArray(name, value);
195     return mDrmHalHidl->getPropertyByteArray(name, value);
196 }
197 
setPropertyString(String8 const & name,String8 const & value) const198 DrmStatus DrmHal::setPropertyString(String8 const& name, String8 const& value) const {
199     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->setPropertyString(name, value);
200     return mDrmHalHidl->setPropertyString(name, value);
201 }
202 
setPropertyByteArray(String8 const & name,Vector<uint8_t> const & value) const203 DrmStatus DrmHal::setPropertyByteArray(String8 const& name, Vector<uint8_t> const& value) const {
204     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->setPropertyByteArray(name, value);
205     return mDrmHalHidl->setPropertyByteArray(name, value);
206 }
207 
getMetrics(const sp<IDrmMetricsConsumer> & consumer)208 DrmStatus DrmHal::getMetrics(const sp<IDrmMetricsConsumer>& consumer) {
209     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getMetrics(consumer);
210     return mDrmHalHidl->getMetrics(consumer);
211 }
212 
setCipherAlgorithm(Vector<uint8_t> const & sessionId,String8 const & algorithm)213 DrmStatus DrmHal::setCipherAlgorithm(Vector<uint8_t> const& sessionId, String8 const& algorithm) {
214     if (mDrmHalAidl->initCheck() == OK)
215         return mDrmHalAidl->setCipherAlgorithm(sessionId, algorithm);
216     return mDrmHalHidl->setCipherAlgorithm(sessionId, algorithm);
217 }
218 
setMacAlgorithm(Vector<uint8_t> const & sessionId,String8 const & algorithm)219 DrmStatus DrmHal::setMacAlgorithm(Vector<uint8_t> const& sessionId, String8 const& algorithm) {
220     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->setMacAlgorithm(sessionId, algorithm);
221     return mDrmHalHidl->setMacAlgorithm(sessionId, algorithm);
222 }
223 
encrypt(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & keyId,Vector<uint8_t> const & input,Vector<uint8_t> const & iv,Vector<uint8_t> & output)224 DrmStatus DrmHal::encrypt(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
225                           Vector<uint8_t> const& input, Vector<uint8_t> const& iv,
226                           Vector<uint8_t>& output) {
227     if (mDrmHalAidl->initCheck() == OK)
228         return mDrmHalAidl->encrypt(sessionId, keyId, input, iv, output);
229     return mDrmHalHidl->encrypt(sessionId, keyId, input, iv, output);
230 }
231 
decrypt(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & keyId,Vector<uint8_t> const & input,Vector<uint8_t> const & iv,Vector<uint8_t> & output)232 DrmStatus DrmHal::decrypt(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
233                           Vector<uint8_t> const& input, Vector<uint8_t> const& iv,
234                           Vector<uint8_t>& output) {
235     if (mDrmHalAidl->initCheck() == OK)
236         return mDrmHalAidl->decrypt(sessionId, keyId, input, iv, output);
237     return mDrmHalHidl->decrypt(sessionId, keyId, input, iv, output);
238 }
239 
sign(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & keyId,Vector<uint8_t> const & message,Vector<uint8_t> & signature)240 DrmStatus DrmHal::sign(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
241                        Vector<uint8_t> const& message, Vector<uint8_t>& signature) {
242     if (mDrmHalAidl->initCheck() == OK)
243         return mDrmHalAidl->sign(sessionId, keyId, message, signature);
244     return mDrmHalHidl->sign(sessionId, keyId, message, signature);
245 }
246 
verify(Vector<uint8_t> const & sessionId,Vector<uint8_t> const & keyId,Vector<uint8_t> const & message,Vector<uint8_t> const & signature,bool & match)247 DrmStatus DrmHal::verify(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
248                          Vector<uint8_t> const& message, Vector<uint8_t> const& signature,
249                          bool& match) {
250     if (mDrmHalAidl->initCheck() == OK)
251         return mDrmHalAidl->verify(sessionId, keyId, message, signature, match);
252     return mDrmHalHidl->verify(sessionId, keyId, message, signature, match);
253 }
254 
signRSA(Vector<uint8_t> const & sessionId,String8 const & algorithm,Vector<uint8_t> const & message,Vector<uint8_t> const & wrappedKey,Vector<uint8_t> & signature)255 DrmStatus DrmHal::signRSA(Vector<uint8_t> const& sessionId, String8 const& algorithm,
256                           Vector<uint8_t> const& message, Vector<uint8_t> const& wrappedKey,
257                           Vector<uint8_t>& signature) {
258     if (mDrmHalAidl->initCheck() == OK)
259         return mDrmHalAidl->signRSA(sessionId, algorithm, message, wrappedKey, signature);
260     return mDrmHalHidl->signRSA(sessionId, algorithm, message, wrappedKey, signature);
261 }
262 
setListener(const sp<IDrmClient> & listener)263 DrmStatus DrmHal::setListener(const sp<IDrmClient>& listener) {
264     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->setListener(listener);
265     return mDrmHalHidl->setListener(listener);
266 }
267 
requiresSecureDecoder(const char * mime,bool * required) const268 DrmStatus DrmHal::requiresSecureDecoder(const char* mime, bool* required) const {
269     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->requiresSecureDecoder(mime, required);
270     return mDrmHalHidl->requiresSecureDecoder(mime, required);
271 }
272 
requiresSecureDecoder(const char * mime,DrmPlugin::SecurityLevel securityLevel,bool * required) const273 DrmStatus DrmHal::requiresSecureDecoder(const char* mime, DrmPlugin::SecurityLevel securityLevel,
274                                         bool* required) const {
275     if (mDrmHalAidl->initCheck() == OK)
276         return mDrmHalAidl->requiresSecureDecoder(mime, securityLevel, required);
277     return mDrmHalHidl->requiresSecureDecoder(mime, securityLevel, required);
278 }
279 
setPlaybackId(Vector<uint8_t> const & sessionId,const char * playbackId)280 DrmStatus DrmHal::setPlaybackId(Vector<uint8_t> const& sessionId, const char* playbackId) {
281     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->setPlaybackId(sessionId, playbackId);
282     return mDrmHalHidl->setPlaybackId(sessionId, playbackId);
283 }
284 
getLogMessages(Vector<drm::V1_4::LogMessage> & logs) const285 DrmStatus DrmHal::getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const {
286     if (mDrmHalAidl->initCheck() == OK) return mDrmHalAidl->getLogMessages(logs);
287     return mDrmHalHidl->getLogMessages(logs);
288 }
289 
getSupportedSchemes(std::vector<uint8_t> & schemes) const290 DrmStatus DrmHal::getSupportedSchemes(std::vector<uint8_t>& schemes) const {
291     status_t statusResult;
292     statusResult = mDrmHalAidl->getSupportedSchemes(schemes);
293     if (statusResult == OK) return statusResult;
294     return mDrmHalHidl->getSupportedSchemes(schemes);
295 }
296 
297 }  // namespace android
298