Jeff Tinker | b075caa | 2016-12-06 23:15:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
Jeff Tinker | 972a3e3 | 2017-01-23 14:02:50 -0800 | [diff] [blame] | 16 | #define LOG_TAG "android.hardware.drm@1.0-impl" |
Jeff Tinker | b075caa | 2016-12-06 23:15:20 -0800 | [diff] [blame] | 17 | |
| 18 | #include <utils/KeyedVector.h> |
| 19 | #include <utils/String8.h> |
| 20 | |
| 21 | #include "DrmPlugin.h" |
| 22 | #include "TypeConvert.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace drm { |
Jeff Tinker | b075caa | 2016-12-06 23:15:20 -0800 | [diff] [blame] | 27 | namespace V1_0 { |
| 28 | namespace implementation { |
| 29 | |
Jeff Tinker | da002fe | 2017-01-19 14:41:11 -0800 | [diff] [blame] | 30 | // Methods from ::android::hardware::drm::V1_0::IDrmPlugin follow. |
Jeff Tinker | b075caa | 2016-12-06 23:15:20 -0800 | [diff] [blame] | 31 | |
| 32 | Return<void> DrmPlugin::openSession(openSession_cb _hidl_cb) { |
Jeff Tinker | b075caa | 2016-12-06 23:15:20 -0800 | [diff] [blame] | 33 | Vector<uint8_t> legacySessionId; |
| 34 | status_t status = mLegacyPlugin->openSession(legacySessionId); |
| 35 | _hidl_cb(toStatus(status), toHidlVec(legacySessionId)); |
| 36 | return Void(); |
| 37 | } |
| 38 | |
| 39 | Return<Status> DrmPlugin::closeSession(const hidl_vec<uint8_t>& sessionId) { |
| 40 | return toStatus(mLegacyPlugin->closeSession(toVector(sessionId))); |
| 41 | } |
| 42 | |
| 43 | Return<void> DrmPlugin::getKeyRequest(const hidl_vec<uint8_t>& scope, |
| 44 | const hidl_vec<uint8_t>& initData, const hidl_string& mimeType, |
| 45 | KeyType keyType, const hidl_vec<KeyValue>& optionalParameters, |
| 46 | getKeyRequest_cb _hidl_cb) { |
| 47 | |
| 48 | status_t status = android::OK; |
| 49 | |
| 50 | android::DrmPlugin::KeyType legacyKeyType; |
| 51 | switch(keyType) { |
| 52 | case KeyType::OFFLINE: |
| 53 | legacyKeyType = android::DrmPlugin::kKeyType_Offline; |
| 54 | break; |
| 55 | case KeyType::STREAMING: |
| 56 | legacyKeyType = android::DrmPlugin::kKeyType_Streaming; |
| 57 | break; |
| 58 | case KeyType::RELEASE: |
| 59 | legacyKeyType = android::DrmPlugin::kKeyType_Release; |
| 60 | break; |
| 61 | default: |
| 62 | status = android::BAD_VALUE; |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | Vector<uint8_t> legacyRequest; |
| 67 | KeyRequestType requestType = KeyRequestType::UNKNOWN; |
| 68 | String8 defaultUrl; |
| 69 | |
| 70 | if (status == android::OK) { |
| 71 | android::KeyedVector<String8, String8> legacyOptionalParameters; |
| 72 | for (size_t i = 0; i < optionalParameters.size(); i++) { |
| 73 | legacyOptionalParameters.add(String8(optionalParameters[i].key), |
| 74 | String8(optionalParameters[i].value)); |
| 75 | } |
| 76 | |
| 77 | android::DrmPlugin::KeyRequestType legacyRequestType = |
| 78 | android::DrmPlugin::kKeyRequestType_Unknown; |
| 79 | |
| 80 | status_t status = mLegacyPlugin->getKeyRequest(toVector(scope), |
| 81 | toVector(initData), String8(mimeType), legacyKeyType, |
| 82 | legacyOptionalParameters, legacyRequest, defaultUrl, |
| 83 | &legacyRequestType); |
| 84 | |
| 85 | switch(legacyRequestType) { |
| 86 | case android::DrmPlugin::kKeyRequestType_Initial: |
| 87 | requestType = KeyRequestType::INITIAL; |
| 88 | break; |
| 89 | case android::DrmPlugin::kKeyRequestType_Renewal: |
| 90 | requestType = KeyRequestType::RENEWAL; |
| 91 | break; |
| 92 | case android::DrmPlugin::kKeyRequestType_Release: |
| 93 | requestType = KeyRequestType::RELEASE; |
| 94 | break; |
| 95 | case android::DrmPlugin::kKeyRequestType_Unknown: |
| 96 | status = android::BAD_VALUE; |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | _hidl_cb(toStatus(status), toHidlVec(legacyRequest), requestType, |
| 101 | defaultUrl.string()); |
| 102 | return Void(); |
| 103 | } |
| 104 | |
| 105 | Return<void> DrmPlugin::provideKeyResponse(const hidl_vec<uint8_t>& scope, |
| 106 | const hidl_vec<uint8_t>& response, provideKeyResponse_cb _hidl_cb) { |
| 107 | |
| 108 | Vector<uint8_t> keySetId; |
| 109 | status_t status = mLegacyPlugin->provideKeyResponse(toVector(scope), |
| 110 | toVector(response), keySetId); |
| 111 | _hidl_cb(toStatus(status), toHidlVec(keySetId)); |
| 112 | return Void(); |
| 113 | } |
| 114 | |
| 115 | Return<Status> DrmPlugin::removeKeys(const hidl_vec<uint8_t>& sessionId) { |
| 116 | return toStatus(mLegacyPlugin->removeKeys(toVector(sessionId))); |
| 117 | } |
| 118 | |
| 119 | Return<Status> DrmPlugin::restoreKeys(const hidl_vec<uint8_t>& sessionId, |
| 120 | const hidl_vec<uint8_t>& keySetId) { |
| 121 | status_t legacyStatus = mLegacyPlugin->restoreKeys(toVector(sessionId), |
| 122 | toVector(keySetId)); |
| 123 | return toStatus(legacyStatus); |
| 124 | } |
| 125 | |
| 126 | Return<void> DrmPlugin::queryKeyStatus(const hidl_vec<uint8_t>& sessionId, |
| 127 | queryKeyStatus_cb _hidl_cb) { |
| 128 | |
| 129 | android::KeyedVector<String8, String8> legacyInfoMap; |
| 130 | status_t status = mLegacyPlugin->queryKeyStatus(toVector(sessionId), |
| 131 | legacyInfoMap); |
| 132 | |
| 133 | Vector<KeyValue> infoMapVec; |
| 134 | for (size_t i = 0; i < legacyInfoMap.size(); i++) { |
| 135 | KeyValue keyValuePair; |
| 136 | keyValuePair.key = String8(legacyInfoMap.keyAt(i)); |
| 137 | keyValuePair.value = String8(legacyInfoMap.valueAt(i)); |
| 138 | infoMapVec.push_back(keyValuePair); |
| 139 | } |
| 140 | _hidl_cb(toStatus(status), toHidlVec(infoMapVec)); |
| 141 | return Void(); |
| 142 | } |
| 143 | |
| 144 | Return<void> DrmPlugin::getProvisionRequest( |
| 145 | const hidl_string& certificateType, |
| 146 | const hidl_string& certificateAuthority, |
| 147 | getProvisionRequest_cb _hidl_cb) { |
| 148 | |
| 149 | Vector<uint8_t> legacyRequest; |
| 150 | String8 legacyDefaultUrl; |
| 151 | status_t status = mLegacyPlugin->getProvisionRequest( |
| 152 | String8(certificateType), String8(certificateAuthority), |
| 153 | legacyRequest, legacyDefaultUrl); |
| 154 | |
| 155 | _hidl_cb(toStatus(status), toHidlVec(legacyRequest), |
| 156 | hidl_string(legacyDefaultUrl)); |
| 157 | return Void(); |
| 158 | } |
| 159 | |
| 160 | Return<void> DrmPlugin::provideProvisionResponse( |
| 161 | const hidl_vec<uint8_t>& response, |
| 162 | provideProvisionResponse_cb _hidl_cb) { |
| 163 | |
| 164 | Vector<uint8_t> certificate; |
| 165 | Vector<uint8_t> wrappedKey; |
| 166 | |
| 167 | status_t legacyStatus = mLegacyPlugin->provideProvisionResponse( |
| 168 | toVector(response), certificate, wrappedKey); |
| 169 | |
| 170 | _hidl_cb(toStatus(legacyStatus), toHidlVec(certificate), |
| 171 | toHidlVec(wrappedKey)); |
| 172 | return Void(); |
| 173 | } |
| 174 | |
| 175 | Return<void> DrmPlugin::getSecureStops(getSecureStops_cb _hidl_cb) { |
| 176 | List<Vector<uint8_t> > legacySecureStops; |
| 177 | status_t status = mLegacyPlugin->getSecureStops(legacySecureStops); |
| 178 | |
| 179 | Vector<SecureStop> secureStopsVec; |
| 180 | List<Vector<uint8_t> >::iterator iter = legacySecureStops.begin(); |
| 181 | |
| 182 | while (iter != legacySecureStops.end()) { |
| 183 | SecureStop secureStop; |
| 184 | secureStop.opaqueData = toHidlVec(*iter++); |
| 185 | secureStopsVec.push_back(secureStop); |
| 186 | } |
| 187 | |
| 188 | _hidl_cb(toStatus(status), toHidlVec(secureStopsVec)); |
| 189 | return Void(); |
| 190 | } |
| 191 | |
| 192 | Return<void> DrmPlugin::getSecureStop(const hidl_vec<uint8_t>& secureStopId, |
| 193 | getSecureStop_cb _hidl_cb) { |
| 194 | |
| 195 | Vector<uint8_t> legacySecureStop; |
| 196 | status_t status = mLegacyPlugin->getSecureStop(toVector(secureStopId), |
| 197 | legacySecureStop); |
| 198 | |
| 199 | SecureStop secureStop; |
| 200 | secureStop.opaqueData = toHidlVec(legacySecureStop); |
| 201 | _hidl_cb(toStatus(status), secureStop); |
| 202 | return Void(); |
| 203 | } |
| 204 | |
| 205 | Return<Status> DrmPlugin::releaseAllSecureStops() { |
| 206 | return toStatus(mLegacyPlugin->releaseAllSecureStops()); |
| 207 | } |
| 208 | |
| 209 | Return<Status> DrmPlugin::releaseSecureStop( |
| 210 | const hidl_vec<uint8_t>& secureStopId) { |
| 211 | status_t legacyStatus = |
| 212 | mLegacyPlugin->releaseSecureStops(toVector(secureStopId)); |
| 213 | return toStatus(legacyStatus); |
| 214 | } |
| 215 | |
| 216 | Return<void> DrmPlugin::getPropertyString(const hidl_string& propertyName, |
| 217 | getPropertyString_cb _hidl_cb) { |
| 218 | String8 legacyValue; |
| 219 | status_t status = mLegacyPlugin->getPropertyString( |
| 220 | String8(propertyName), legacyValue); |
| 221 | _hidl_cb(toStatus(status), legacyValue.string()); |
| 222 | return Void(); |
| 223 | } |
| 224 | |
| 225 | Return<void> DrmPlugin::getPropertyByteArray(const hidl_string& propertyName, |
| 226 | getPropertyByteArray_cb _hidl_cb) { |
| 227 | Vector<uint8_t> legacyValue; |
| 228 | status_t status = mLegacyPlugin->getPropertyByteArray( |
| 229 | String8(propertyName), legacyValue); |
| 230 | _hidl_cb(toStatus(status), toHidlVec(legacyValue)); |
| 231 | return Void(); |
| 232 | } |
| 233 | |
| 234 | Return<Status> DrmPlugin::setPropertyString(const hidl_string& propertyName, |
| 235 | const hidl_string& value) { |
| 236 | status_t legacyStatus = |
| 237 | mLegacyPlugin->setPropertyString(String8(propertyName), |
| 238 | String8(value)); |
| 239 | return toStatus(legacyStatus); |
| 240 | } |
| 241 | |
| 242 | Return<Status> DrmPlugin::setPropertyByteArray( |
| 243 | const hidl_string& propertyName, const hidl_vec<uint8_t>& value) { |
| 244 | status_t legacyStatus = |
| 245 | mLegacyPlugin->setPropertyByteArray(String8(propertyName), |
| 246 | toVector(value)); |
| 247 | return toStatus(legacyStatus); |
| 248 | } |
| 249 | |
| 250 | Return<Status> DrmPlugin::setCipherAlgorithm( |
| 251 | const hidl_vec<uint8_t>& sessionId, const hidl_string& algorithm) { |
| 252 | status_t legacyStatus = |
| 253 | mLegacyPlugin->setCipherAlgorithm(toVector(sessionId), |
| 254 | String8(algorithm)); |
| 255 | return toStatus(legacyStatus); |
| 256 | } |
| 257 | |
| 258 | Return<Status> DrmPlugin::setMacAlgorithm( |
| 259 | const hidl_vec<uint8_t>& sessionId, const hidl_string& algorithm) { |
| 260 | status_t legacyStatus = |
| 261 | mLegacyPlugin->setMacAlgorithm(toVector(sessionId), |
| 262 | String8(algorithm)); |
| 263 | return toStatus(legacyStatus); |
| 264 | } |
| 265 | |
| 266 | Return<void> DrmPlugin::encrypt(const hidl_vec<uint8_t>& sessionId, |
| 267 | const hidl_vec<uint8_t>& keyId, const hidl_vec<uint8_t>& input, |
| 268 | const hidl_vec<uint8_t>& iv, encrypt_cb _hidl_cb) { |
| 269 | |
| 270 | Vector<uint8_t> legacyOutput; |
| 271 | status_t status = mLegacyPlugin->encrypt(toVector(sessionId), |
| 272 | toVector(keyId), toVector(input), toVector(iv), legacyOutput); |
| 273 | _hidl_cb(toStatus(status), toHidlVec(legacyOutput)); |
| 274 | return Void(); |
| 275 | } |
| 276 | |
| 277 | Return<void> DrmPlugin::decrypt(const hidl_vec<uint8_t>& sessionId, |
| 278 | const hidl_vec<uint8_t>& keyId, const hidl_vec<uint8_t>& input, |
| 279 | const hidl_vec<uint8_t>& iv, decrypt_cb _hidl_cb) { |
| 280 | |
| 281 | Vector<uint8_t> legacyOutput; |
| 282 | status_t status = mLegacyPlugin->decrypt(toVector(sessionId), |
| 283 | toVector(keyId), toVector(input), toVector(iv), legacyOutput); |
| 284 | _hidl_cb(toStatus(status), toHidlVec(legacyOutput)); |
| 285 | return Void(); |
| 286 | } |
| 287 | |
| 288 | Return<void> DrmPlugin::sign(const hidl_vec<uint8_t>& sessionId, |
| 289 | const hidl_vec<uint8_t>& keyId, const hidl_vec<uint8_t>& message, |
| 290 | sign_cb _hidl_cb) { |
| 291 | Vector<uint8_t> legacySignature; |
| 292 | status_t status = mLegacyPlugin->sign(toVector(sessionId), |
| 293 | toVector(keyId), toVector(message), legacySignature); |
| 294 | _hidl_cb(toStatus(status), toHidlVec(legacySignature)); |
| 295 | return Void(); |
| 296 | } |
| 297 | |
| 298 | Return<void> DrmPlugin::verify(const hidl_vec<uint8_t>& sessionId, |
| 299 | const hidl_vec<uint8_t>& keyId, const hidl_vec<uint8_t>& message, |
| 300 | const hidl_vec<uint8_t>& signature, verify_cb _hidl_cb) { |
| 301 | |
| 302 | bool match; |
| 303 | status_t status = mLegacyPlugin->verify(toVector(sessionId), |
| 304 | toVector(keyId), toVector(message), toVector(signature), |
| 305 | match); |
| 306 | _hidl_cb(toStatus(status), match); |
| 307 | return Void(); |
| 308 | } |
| 309 | |
| 310 | Return<void> DrmPlugin::signRSA(const hidl_vec<uint8_t>& sessionId, |
| 311 | const hidl_string& algorithm, const hidl_vec<uint8_t>& message, |
| 312 | const hidl_vec<uint8_t>& wrappedKey, signRSA_cb _hidl_cb) { |
| 313 | |
| 314 | Vector<uint8_t> legacySignature; |
| 315 | status_t status = mLegacyPlugin->signRSA(toVector(sessionId), |
| 316 | String8(algorithm), toVector(message), toVector(wrappedKey), |
| 317 | legacySignature); |
| 318 | _hidl_cb(toStatus(status), toHidlVec(legacySignature)); |
| 319 | return Void(); |
| 320 | } |
| 321 | |
| 322 | Return<void> DrmPlugin::setListener(const sp<IDrmPluginListener>& listener) { |
| 323 | mListener = listener; |
| 324 | return Void(); |
| 325 | } |
| 326 | |
| 327 | Return<void> DrmPlugin::sendEvent(EventType eventType, |
| 328 | const hidl_vec<uint8_t>& sessionId, const hidl_vec<uint8_t>& data) { |
| 329 | mListener->sendEvent(eventType, sessionId, data); |
| 330 | return Void(); |
| 331 | } |
| 332 | |
| 333 | Return<void> DrmPlugin::sendExpirationUpdate( |
| 334 | const hidl_vec<uint8_t>& sessionId, int64_t expiryTimeInMS) { |
| 335 | mListener->sendExpirationUpdate(sessionId, expiryTimeInMS); |
| 336 | return Void(); |
| 337 | } |
| 338 | |
| 339 | Return<void> DrmPlugin::sendKeysChange(const hidl_vec<uint8_t>& sessionId, |
| 340 | const hidl_vec<KeyStatus>& keyStatusList, bool hasNewUsableKey) { |
| 341 | mListener->sendKeysChange(sessionId, keyStatusList, hasNewUsableKey); |
| 342 | return Void(); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | // Methods from android::DrmPluginListener |
| 347 | |
| 348 | void DrmPlugin::sendEvent(android::DrmPlugin::EventType legacyEventType, |
| 349 | int /*unused*/, Vector<uint8_t> const *sessionId, |
| 350 | Vector<uint8_t> const *data) { |
| 351 | |
| 352 | EventType eventType; |
| 353 | bool sendEvent = true; |
| 354 | switch(legacyEventType) { |
| 355 | case android::DrmPlugin::kDrmPluginEventProvisionRequired: |
| 356 | eventType = EventType::PROVISION_REQUIRED; |
| 357 | break; |
| 358 | case android::DrmPlugin::kDrmPluginEventKeyNeeded: |
| 359 | eventType = EventType::KEY_NEEDED; |
| 360 | break; |
| 361 | case android::DrmPlugin::kDrmPluginEventKeyExpired: |
| 362 | eventType = EventType::KEY_EXPIRED; |
| 363 | break; |
| 364 | case android::DrmPlugin::kDrmPluginEventVendorDefined: |
| 365 | eventType = EventType::VENDOR_DEFINED; |
| 366 | break; |
| 367 | case android::DrmPlugin::kDrmPluginEventSessionReclaimed: |
| 368 | eventType = EventType::SESSION_RECLAIMED; |
| 369 | break; |
| 370 | default: |
| 371 | sendEvent = false; |
| 372 | break; |
| 373 | } |
| 374 | if (sendEvent) { |
| 375 | mListener->sendEvent(eventType, toHidlVec(*sessionId), |
| 376 | toHidlVec(*data)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void DrmPlugin::sendExpirationUpdate(Vector<uint8_t> const *sessionId, |
| 381 | int64_t expiryTimeInMS) { |
| 382 | mListener->sendExpirationUpdate(toHidlVec(*sessionId), expiryTimeInMS); |
| 383 | } |
| 384 | |
| 385 | void DrmPlugin::sendKeysChange(Vector<uint8_t> const *sessionId, |
| 386 | Vector<android::DrmPlugin::KeyStatus> const *legacyKeyStatusList, |
| 387 | bool hasNewUsableKey) { |
| 388 | |
| 389 | Vector<KeyStatus> keyStatusVec; |
| 390 | for (size_t i = 0; i < legacyKeyStatusList->size(); i++) { |
| 391 | const android::DrmPlugin::KeyStatus &legacyKeyStatus = |
| 392 | legacyKeyStatusList->itemAt(i); |
| 393 | |
| 394 | KeyStatus keyStatus; |
| 395 | |
| 396 | switch(legacyKeyStatus.mType) { |
| 397 | case android::DrmPlugin::kKeyStatusType_Usable: |
| 398 | keyStatus.type = KeyStatusType::USABLE; |
| 399 | break; |
| 400 | case android::DrmPlugin::kKeyStatusType_Expired: |
| 401 | keyStatus.type = KeyStatusType::EXPIRED; |
| 402 | break; |
| 403 | case android::DrmPlugin::kKeyStatusType_OutputNotAllowed: |
| 404 | keyStatus.type = KeyStatusType::OUTPUTNOTALLOWED; |
| 405 | break; |
| 406 | case android::DrmPlugin::kKeyStatusType_StatusPending: |
| 407 | keyStatus.type = KeyStatusType::STATUSPENDING; |
| 408 | break; |
| 409 | case android::DrmPlugin::kKeyStatusType_InternalError: |
| 410 | default: |
| 411 | keyStatus.type = KeyStatusType::INTERNALERROR; |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | keyStatus.keyId = toHidlVec(legacyKeyStatus.mKeyId); |
| 416 | keyStatusVec.push_back(keyStatus); |
| 417 | } |
| 418 | mListener->sendKeysChange(toHidlVec(*sessionId), |
| 419 | toHidlVec(keyStatusVec), hasNewUsableKey); |
| 420 | } |
| 421 | |
| 422 | } // namespace implementation |
| 423 | } // namespace V1_0 |
| 424 | } // namespace drm |
Jeff Tinker | b075caa | 2016-12-06 23:15:20 -0800 | [diff] [blame] | 425 | } // namespace hardware |
| 426 | } // namespace android |