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