| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 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 "NuPlayerDrm" |
| 19 | |
| 20 | #include "NuPlayerDrm.h" |
| 21 | |
| 22 | #include <binder/IServiceManager.h> |
| Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 23 | #include <mediadrm/IMediaDrmService.h> |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | // static helpers - internal |
| 30 | |
| 31 | sp<IDrm> NuPlayerDrm::CreateDrm(status_t *pstatus) |
| 32 | { |
| 33 | status_t &status = *pstatus; |
| 34 | sp<IServiceManager> sm = defaultServiceManager(); |
| 35 | sp<IBinder> binder = sm->getService(String16("media.drm")); |
| 36 | ALOGV("CreateDrm binder %p", (binder != NULL ? binder.get() : 0)); |
| 37 | |
| 38 | sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder); |
| 39 | if (service == NULL) { |
| 40 | ALOGE("CreateDrm failed at IMediaDrmService"); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | sp<IDrm> drm = service->makeDrm(); |
| 45 | if (drm == NULL) { |
| 46 | ALOGE("CreateDrm failed at makeDrm"); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | // this is before plugin creation so NO_INIT is fine |
| 51 | status = drm->initCheck(); |
| 52 | if (status != OK && status != NO_INIT) { |
| 53 | ALOGE("CreateDrm failed drm->initCheck(): %d", status); |
| 54 | return NULL; |
| 55 | } |
| 56 | return drm; |
| 57 | } |
| 58 | |
| 59 | sp<ICrypto> NuPlayerDrm::createCrypto(status_t *pstatus) |
| 60 | { |
| 61 | status_t &status = *pstatus; |
| 62 | sp<IServiceManager> sm = defaultServiceManager(); |
| 63 | sp<IBinder> binder = sm->getService(String16("media.drm")); |
| 64 | |
| 65 | sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder); |
| 66 | if (service == NULL) { |
| 67 | status = UNKNOWN_ERROR; |
| 68 | ALOGE("CreateCrypto failed at IMediaDrmService"); |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | sp<ICrypto> crypto = service->makeCrypto(); |
| 73 | if (crypto == NULL) { |
| 74 | status = UNKNOWN_ERROR; |
| 75 | ALOGE("createCrypto failed"); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | // this is before plugin creation so NO_INIT is fine |
| 80 | status = crypto->initCheck(); |
| 81 | if (status != OK && status != NO_INIT) { |
| 82 | ALOGE("createCrypto failed crypto->initCheck(): %d", status); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | return crypto; |
| 87 | } |
| 88 | |
| 89 | Vector<DrmUUID> NuPlayerDrm::parsePSSH(const void *pssh, size_t psshsize) |
| 90 | { |
| 91 | Vector<DrmUUID> drmSchemes, empty; |
| 92 | const int DATALEN_SIZE = 4; |
| 93 | |
| 94 | // the format of the buffer is 1 or more of: |
| 95 | // { |
| 96 | // 16 byte uuid |
| 97 | // 4 byte data length N |
| 98 | // N bytes of data |
| 99 | // } |
| 100 | // Determine the number of entries in the source data. |
| 101 | // Since we got the data from stagefright, we trust it is valid and properly formatted. |
| 102 | |
| 103 | const uint8_t *data = (const uint8_t*)pssh; |
| 104 | size_t len = psshsize; |
| 105 | size_t numentries = 0; |
| 106 | while (len > 0) { |
| 107 | if (len < DrmUUID::UUID_SIZE) { |
| 108 | ALOGE("ParsePSSH: invalid PSSH data"); |
| 109 | return empty; |
| 110 | } |
| 111 | |
| 112 | const uint8_t *uuidPtr = data; |
| 113 | |
| 114 | // skip uuid |
| 115 | data += DrmUUID::UUID_SIZE; |
| 116 | len -= DrmUUID::UUID_SIZE; |
| 117 | |
| 118 | // get data length |
| 119 | if (len < DATALEN_SIZE) { |
| 120 | ALOGE("ParsePSSH: invalid PSSH data"); |
| 121 | return empty; |
| 122 | } |
| 123 | |
| 124 | uint32_t datalen = *((uint32_t*)data); |
| 125 | data += DATALEN_SIZE; |
| 126 | len -= DATALEN_SIZE; |
| 127 | |
| 128 | if (len < datalen) { |
| 129 | ALOGE("ParsePSSH: invalid PSSH data"); |
| 130 | return empty; |
| 131 | } |
| 132 | |
| 133 | // skip the data |
| 134 | data += datalen; |
| 135 | len -= datalen; |
| 136 | |
| 137 | DrmUUID _uuid(uuidPtr); |
| 138 | drmSchemes.add(_uuid); |
| 139 | |
| 140 | ALOGV("ParsePSSH[%zu]: %s: %s", numentries, |
| 141 | _uuid.toHexString().string(), |
| 142 | DrmUUID::arrayToHex(data, datalen).string() |
| 143 | ); |
| 144 | |
| 145 | numentries++; |
| 146 | } |
| 147 | |
| 148 | return drmSchemes; |
| 149 | } |
| 150 | |
| 151 | Vector<DrmUUID> NuPlayerDrm::getSupportedDrmSchemes(const void *pssh, size_t psshsize) |
| 152 | { |
| 153 | Vector<DrmUUID> psshDRMs = parsePSSH(pssh, psshsize); |
| 154 | |
| 155 | Vector<DrmUUID> supportedDRMs; |
| 156 | // temporary DRM object for crypto Scheme enquiry (without creating a plugin) |
| 157 | status_t status = OK; |
| 158 | sp<IDrm> drm = CreateDrm(&status); |
| 159 | if (drm != NULL) { |
| 160 | for (size_t i = 0; i < psshDRMs.size(); i++) { |
| 161 | DrmUUID uuid = psshDRMs[i]; |
| Jeff Tinker | 99dbfa8 | 2019-01-17 17:27:06 -0800 | [diff] [blame] | 162 | if (drm->isCryptoSchemeSupported(uuid.ptr(), String8(), |
| 163 | DrmPlugin::kSecurityLevelUnknown)) |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 164 | supportedDRMs.add(uuid); |
| 165 | } |
| 166 | |
| 167 | drm.clear(); |
| 168 | } else { |
| 169 | ALOGE("getSupportedDrmSchemes: Can't create Drm obj: %d", status); |
| 170 | } |
| 171 | |
| 172 | ALOGV("getSupportedDrmSchemes: psshDRMs: %zu supportedDRMs: %zu", |
| 173 | psshDRMs.size(), supportedDRMs.size()); |
| 174 | |
| 175 | return supportedDRMs; |
| 176 | } |
| 177 | |
| 178 | // static helpers - public |
| 179 | |
| 180 | sp<ICrypto> NuPlayerDrm::createCryptoAndPlugin(const uint8_t uuid[16], |
| 181 | const Vector<uint8_t> &drmSessionId, status_t &status) |
| 182 | { |
| 183 | // Extra check |
| 184 | if (drmSessionId.isEmpty()) { |
| 185 | status = INVALID_OPERATION; |
| 186 | ALOGE("createCryptoAndPlugin: Failed. Empty drmSessionId. status: %d", status); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | status = OK; |
| 191 | sp<ICrypto> crypto = createCrypto(&status); |
| 192 | if (crypto == NULL) { |
| 193 | ALOGE("createCryptoAndPlugin: createCrypto failed. status: %d", status); |
| 194 | return NULL; |
| 195 | } |
| 196 | ALOGV("createCryptoAndPlugin: createCrypto succeeded"); |
| 197 | |
| 198 | status = crypto->createPlugin(uuid, drmSessionId.array(), drmSessionId.size()); |
| 199 | if (status != OK) { |
| 200 | ALOGE("createCryptoAndPlugin: createCryptoPlugin failed. status: %d", status); |
| 201 | // crypto will clean itself when leaving the current scope |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | return crypto; |
| 206 | } |
| 207 | |
| 208 | // Parcel has only private copy constructor so passing it in rather than returning |
| Hassan Shojania | dd4ce18 | 2017-04-20 15:25:39 -0700 | [diff] [blame] | 209 | void NuPlayerDrm::retrieveDrmInfo(const void *pssh, size_t psshsize, Parcel *parcel) |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 210 | { |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 211 | // 1) PSSH bytes |
| 212 | parcel->writeUint32(psshsize); |
| 213 | parcel->writeByteArray(psshsize, (const uint8_t*)pssh); |
| 214 | |
| 215 | ALOGV("retrieveDrmInfo: MEDIA_DRM_INFO PSSH: size: %zu %s", psshsize, |
| 216 | DrmUUID::arrayToHex((uint8_t*)pssh, psshsize).string()); |
| 217 | |
| 218 | // 2) supportedDRMs |
| 219 | Vector<DrmUUID> supportedDRMs = getSupportedDrmSchemes(pssh, psshsize); |
| 220 | parcel->writeUint32(supportedDRMs.size()); |
| 221 | for (size_t i = 0; i < supportedDRMs.size(); i++) { |
| 222 | DrmUUID uuid = supportedDRMs[i]; |
| 223 | parcel->writeByteArray(DrmUUID::UUID_SIZE, uuid.ptr()); |
| 224 | |
| 225 | ALOGV("retrieveDrmInfo: MEDIA_DRM_INFO supportedScheme[%zu] %s", i, |
| 226 | uuid.toHexString().string()); |
| 227 | } |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | //////////////////////////////////////////////////////////////////////////////////////////// |
| 231 | /// Helpers for NuPlayerDecoder |
| 232 | //////////////////////////////////////////////////////////////////////////////////////////// |
| 233 | |
| 234 | NuPlayerDrm::CryptoInfo *NuPlayerDrm::makeCryptoInfo( |
| 235 | int numSubSamples, |
| 236 | uint8_t key[kBlockSize], |
| 237 | uint8_t iv[kBlockSize], |
| 238 | CryptoPlugin::Mode mode, |
| 239 | size_t *clearbytes, |
| 240 | size_t *encryptedbytes) |
| 241 | { |
| 242 | // size needed to store all the crypto data |
| Marco Nelissen | 62e83c9 | 2018-07-31 15:25:58 -0700 | [diff] [blame] | 243 | size_t cryptosize; |
| 244 | // sizeof(CryptoInfo) + sizeof(CryptoPlugin::SubSample) * numSubSamples; |
| 245 | if (__builtin_mul_overflow(sizeof(CryptoPlugin::SubSample), numSubSamples, &cryptosize) || |
| 246 | __builtin_add_overflow(cryptosize, sizeof(CryptoInfo), &cryptosize)) { |
| 247 | ALOGE("crypto size overflow"); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 251 | CryptoInfo *ret = (CryptoInfo*) malloc(cryptosize); |
| 252 | if (ret == NULL) { |
| 253 | ALOGE("couldn't allocate %zu bytes", cryptosize); |
| 254 | return NULL; |
| 255 | } |
| 256 | ret->numSubSamples = numSubSamples; |
| 257 | memcpy(ret->key, key, kBlockSize); |
| 258 | memcpy(ret->iv, iv, kBlockSize); |
| 259 | ret->mode = mode; |
| 260 | ret->pattern.mEncryptBlocks = 0; |
| 261 | ret->pattern.mSkipBlocks = 0; |
| 262 | ret->subSamples = (CryptoPlugin::SubSample*)(ret + 1); |
| 263 | CryptoPlugin::SubSample *subSamples = ret->subSamples; |
| 264 | |
| 265 | for (int i = 0; i < numSubSamples; i++) { |
| 266 | subSamples[i].mNumBytesOfClearData = (clearbytes == NULL) ? 0 : clearbytes[i]; |
| 267 | subSamples[i].mNumBytesOfEncryptedData = (encryptedbytes == NULL) ? |
| 268 | 0 : |
| 269 | encryptedbytes[i]; |
| 270 | } |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 275 | NuPlayerDrm::CryptoInfo *NuPlayerDrm::getSampleCryptoInfo(MetaDataBase &meta) |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 276 | { |
| 277 | uint32_t type; |
| 278 | const void *crypteddata; |
| 279 | size_t cryptedsize; |
| 280 | |
| Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 281 | if (!meta.findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) { |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | size_t numSubSamples = cryptedsize / sizeof(size_t); |
| 285 | |
| 286 | if (numSubSamples <= 0) { |
| 287 | ALOGE("getSampleCryptoInfo INVALID numSubSamples: %zu", numSubSamples); |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | const void *cleardata; |
| 292 | size_t clearsize; |
| Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 293 | if (meta.findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) { |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 294 | if (clearsize != cryptedsize) { |
| 295 | // The two must be of the same length. |
| 296 | ALOGE("getSampleCryptoInfo mismatch cryptedsize: %zu != clearsize: %zu", |
| 297 | cryptedsize, clearsize); |
| 298 | return NULL; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | const void *key; |
| 303 | size_t keysize; |
| Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 304 | if (meta.findData(kKeyCryptoKey, &type, &key, &keysize)) { |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 305 | if (keysize != kBlockSize) { |
| Hassan Shojania | e00e392 | 2017-02-15 20:24:41 -0800 | [diff] [blame] | 306 | ALOGE("getSampleCryptoInfo Keys must be %d bytes in length: %zu", |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 307 | kBlockSize, keysize); |
| 308 | // Keys must be 16 bytes in length. |
| 309 | return NULL; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | const void *iv; |
| 314 | size_t ivsize; |
| Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 315 | if (meta.findData(kKeyCryptoIV, &type, &iv, &ivsize)) { |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 316 | if (ivsize != kBlockSize) { |
| Hassan Shojania | e00e392 | 2017-02-15 20:24:41 -0800 | [diff] [blame] | 317 | ALOGE("getSampleCryptoInfo IV must be %d bytes in length: %zu", |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 318 | kBlockSize, ivsize); |
| 319 | // IVs must be 16 bytes in length. |
| 320 | return NULL; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | int32_t mode; |
| Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 325 | if (!meta.findInt32(kKeyCryptoMode, &mode)) { |
| Hassan Shojania | cefac14 | 2017-02-06 21:02:02 -0800 | [diff] [blame] | 326 | mode = CryptoPlugin::kMode_AES_CTR; |
| 327 | } |
| 328 | |
| 329 | return makeCryptoInfo(numSubSamples, |
| 330 | (uint8_t*) key, |
| 331 | (uint8_t*) iv, |
| 332 | (CryptoPlugin::Mode)mode, |
| 333 | (size_t*) cleardata, |
| 334 | (size_t*) crypteddata); |
| 335 | } |
| 336 | |
| 337 | } // namespace android |
| 338 | |