Home | History | Annotate | Download | only in libmediadrm
      1 /*
      2  * Copyright (C) 2013 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 "IDrm"
     19 #include <utils/Log.h>
     20 
     21 #include <binder/Parcel.h>
     22 #include <media/stagefright/MediaErrors.h>
     23 #include <media/stagefright/foundation/ADebug.h>
     24 #include <media/stagefright/foundation/AString.h>
     25 #include <mediadrm/IDrm.h>
     26 
     27 namespace android {
     28 
     29 enum {
     30     INIT_CHECK = IBinder::FIRST_CALL_TRANSACTION,
     31     IS_CRYPTO_SUPPORTED,
     32     CREATE_PLUGIN,
     33     DESTROY_PLUGIN,
     34     OPEN_SESSION,
     35     CLOSE_SESSION,
     36     GET_KEY_REQUEST,
     37     PROVIDE_KEY_RESPONSE,
     38     REMOVE_KEYS,
     39     RESTORE_KEYS,
     40     QUERY_KEY_STATUS,
     41     GET_PROVISION_REQUEST,
     42     PROVIDE_PROVISION_RESPONSE,
     43     GET_SECURE_STOPS,
     44     RELEASE_SECURE_STOPS,
     45     GET_PROPERTY_STRING,
     46     GET_PROPERTY_BYTE_ARRAY,
     47     SET_PROPERTY_STRING,
     48     SET_PROPERTY_BYTE_ARRAY,
     49     GET_METRICS,
     50     SET_CIPHER_ALGORITHM,
     51     SET_MAC_ALGORITHM,
     52     ENCRYPT,
     53     DECRYPT,
     54     SIGN,
     55     SIGN_RSA,
     56     VERIFY,
     57     SET_LISTENER,
     58     GET_SECURE_STOP,
     59     REMOVE_ALL_SECURE_STOPS,
     60     GET_HDCP_LEVELS,
     61     GET_NUMBER_OF_SESSIONS,
     62     GET_SECURITY_LEVEL,
     63     REMOVE_SECURE_STOP,
     64     GET_SECURE_STOP_IDS,
     65     GET_OFFLINE_LICENSE_KEYSET_IDS,
     66     REMOVE_OFFLINE_LICENSE,
     67     GET_OFFLINE_LICENSE_STATE
     68 };
     69 
     70 struct BpDrm : public BpInterface<IDrm> {
     71     explicit BpDrm(const sp<IBinder> &impl)
     72         : BpInterface<IDrm>(impl) {
     73     }
     74 
     75     virtual status_t initCheck() const {
     76         Parcel data, reply;
     77         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
     78         status_t status = remote()->transact(INIT_CHECK, data, &reply);
     79         if (status != OK) {
     80             return status;
     81         }
     82 
     83         return reply.readInt32();
     84     }
     85 
     86     virtual status_t isCryptoSchemeSupported(const uint8_t uuid[16], const String8 &mimeType,
     87             DrmPlugin::SecurityLevel level, bool *isSupported) {
     88         Parcel data, reply;
     89         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
     90         data.write(uuid, 16);
     91         data.writeString8(mimeType);
     92         data.writeInt32(level);
     93 
     94         status_t status = remote()->transact(IS_CRYPTO_SUPPORTED, data, &reply);
     95         if (status != OK) {
     96             ALOGE("isCryptoSchemeSupported: binder call failed: %d", status);
     97             return status;
     98         }
     99         *isSupported = static_cast<bool>(reply.readInt32());
    100 
    101         return reply.readInt32();
    102     }
    103 
    104     virtual status_t createPlugin(const uint8_t uuid[16],
    105                                   const String8& appPackageName) {
    106         Parcel data, reply;
    107         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    108         data.write(uuid, 16);
    109         data.writeString8(appPackageName);
    110         status_t status = remote()->transact(CREATE_PLUGIN, data, &reply);
    111         if (status != OK) {
    112             ALOGE("createPlugin: binder call failed: %d", status);
    113             return status;
    114         }
    115 
    116         return reply.readInt32();
    117     }
    118 
    119     virtual status_t destroyPlugin() {
    120         Parcel data, reply;
    121         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    122         status_t status = remote()->transact(DESTROY_PLUGIN, data, &reply);
    123         if (status != OK) {
    124             return status;
    125         }
    126 
    127         return reply.readInt32();
    128     }
    129 
    130     virtual status_t openSession(DrmPlugin::SecurityLevel level,
    131             Vector<uint8_t> &sessionId) {
    132         Parcel data, reply;
    133         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    134         data.writeInt32(level);
    135 
    136         status_t status = remote()->transact(OPEN_SESSION, data, &reply);
    137         if (status != OK) {
    138             return status;
    139         }
    140         readVector(reply, sessionId);
    141 
    142         return reply.readInt32();
    143     }
    144 
    145     virtual status_t closeSession(Vector<uint8_t> const &sessionId) {
    146         Parcel data, reply;
    147         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    148 
    149         writeVector(data, sessionId);
    150         status_t status = remote()->transact(CLOSE_SESSION, data, &reply);
    151         if (status != OK) {
    152             return status;
    153         }
    154 
    155         return reply.readInt32();
    156     }
    157 
    158     virtual status_t
    159         getKeyRequest(Vector<uint8_t> const &sessionId,
    160                       Vector<uint8_t> const &initData,
    161                       String8 const &mimeType, DrmPlugin::KeyType keyType,
    162                       KeyedVector<String8, String8> const &optionalParameters,
    163                       Vector<uint8_t> &request, String8 &defaultUrl,
    164                       DrmPlugin::KeyRequestType *keyRequestType) {
    165         Parcel data, reply;
    166         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    167 
    168         writeVector(data, sessionId);
    169         writeVector(data, initData);
    170         data.writeString8(mimeType);
    171         data.writeInt32((uint32_t)keyType);
    172 
    173         data.writeInt32(optionalParameters.size());
    174         for (size_t i = 0; i < optionalParameters.size(); ++i) {
    175             data.writeString8(optionalParameters.keyAt(i));
    176             data.writeString8(optionalParameters.valueAt(i));
    177         }
    178 
    179         status_t status = remote()->transact(GET_KEY_REQUEST, data, &reply);
    180         if (status != OK) {
    181             return status;
    182         }
    183 
    184         readVector(reply, request);
    185         defaultUrl = reply.readString8();
    186         *keyRequestType = static_cast<DrmPlugin::KeyRequestType>(reply.readInt32());
    187 
    188         return reply.readInt32();
    189     }
    190 
    191     virtual status_t provideKeyResponse(Vector<uint8_t> const &sessionId,
    192                                         Vector<uint8_t> const &response,
    193                                         Vector<uint8_t> &keySetId) {
    194         Parcel data, reply;
    195         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    196         writeVector(data, sessionId);
    197         writeVector(data, response);
    198 
    199         status_t status = remote()->transact(PROVIDE_KEY_RESPONSE, data, &reply);
    200         if (status != OK) {
    201             return status;
    202         }
    203 
    204         readVector(reply, keySetId);
    205 
    206         return reply.readInt32();
    207     }
    208 
    209     virtual status_t removeKeys(Vector<uint8_t> const &keySetId) {
    210         Parcel data, reply;
    211         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    212 
    213         writeVector(data, keySetId);
    214         status_t status = remote()->transact(REMOVE_KEYS, data, &reply);
    215         if (status != OK) {
    216             return status;
    217         }
    218 
    219         return reply.readInt32();
    220     }
    221 
    222     virtual status_t restoreKeys(Vector<uint8_t> const &sessionId,
    223                                  Vector<uint8_t> const &keySetId) {
    224         Parcel data, reply;
    225         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    226 
    227         writeVector(data, sessionId);
    228         writeVector(data, keySetId);
    229         status_t status = remote()->transact(RESTORE_KEYS, data, &reply);
    230         if (status != OK) {
    231             return status;
    232         }
    233 
    234         return reply.readInt32();
    235     }
    236 
    237     virtual status_t queryKeyStatus(Vector<uint8_t> const &sessionId,
    238                                         KeyedVector<String8, String8> &infoMap) const {
    239         Parcel data, reply;
    240         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    241 
    242         writeVector(data, sessionId);
    243         status_t status = remote()->transact(QUERY_KEY_STATUS, data, &reply);
    244         if (status != OK) {
    245             return status;
    246         }
    247 
    248         infoMap.clear();
    249         size_t count = reply.readInt32();
    250         for (size_t i = 0; i < count; i++) {
    251             String8 key = reply.readString8();
    252             String8 value = reply.readString8();
    253             infoMap.add(key, value);
    254         }
    255         return reply.readInt32();
    256     }
    257 
    258     virtual status_t getProvisionRequest(String8 const &certType,
    259                                          String8 const &certAuthority,
    260                                          Vector<uint8_t> &request,
    261                                          String8 &defaultUrl) {
    262         Parcel data, reply;
    263         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    264 
    265         data.writeString8(certType);
    266         data.writeString8(certAuthority);
    267         status_t status = remote()->transact(GET_PROVISION_REQUEST, data, &reply);
    268         if (status != OK) {
    269             return status;
    270         }
    271 
    272         readVector(reply, request);
    273         defaultUrl = reply.readString8();
    274 
    275         return reply.readInt32();
    276     }
    277 
    278     virtual status_t provideProvisionResponse(Vector<uint8_t> const &response,
    279                                               Vector<uint8_t> &certificate,
    280                                               Vector<uint8_t> &wrappedKey) {
    281         Parcel data, reply;
    282         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    283 
    284         writeVector(data, response);
    285         status_t status = remote()->transact(PROVIDE_PROVISION_RESPONSE, data, &reply);
    286         if (status != OK) {
    287             return status;
    288         }
    289 
    290         readVector(reply, certificate);
    291         readVector(reply, wrappedKey);
    292 
    293         return reply.readInt32();
    294     }
    295 
    296     virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops) {
    297         Parcel data, reply;
    298         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    299 
    300         status_t status = remote()->transact(GET_SECURE_STOPS, data, &reply);
    301         if (status != OK) {
    302             return status;
    303         }
    304 
    305         secureStops.clear();
    306         uint32_t count = reply.readInt32();
    307         for (size_t i = 0; i < count; i++) {
    308             Vector<uint8_t> secureStop;
    309             readVector(reply, secureStop);
    310             secureStops.push_back(secureStop);
    311         }
    312         return reply.readInt32();
    313     }
    314 
    315     virtual status_t getSecureStopIds(List<Vector<uint8_t> > &secureStopIds) {
    316         Parcel data, reply;
    317         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    318 
    319         status_t status = remote()->transact(GET_SECURE_STOP_IDS, data, &reply);
    320         if (status != OK) {
    321             return status;
    322         }
    323 
    324         secureStopIds.clear();
    325         uint32_t count = reply.readInt32();
    326         for (size_t i = 0; i < count; i++) {
    327             Vector<uint8_t> secureStopId;
    328             readVector(reply, secureStopId);
    329             secureStopIds.push_back(secureStopId);
    330         }
    331         return reply.readInt32();
    332     }
    333 
    334     virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop) {
    335         Parcel data, reply;
    336         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    337 
    338         writeVector(data, ssid);
    339         status_t status = remote()->transact(GET_SECURE_STOP, data, &reply);
    340         if (status != OK) {
    341             return status;
    342         }
    343 
    344         readVector(reply, secureStop);
    345         return reply.readInt32();
    346     }
    347 
    348     virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease) {
    349         Parcel data, reply;
    350         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    351 
    352         writeVector(data, ssRelease);
    353         status_t status = remote()->transact(RELEASE_SECURE_STOPS, data, &reply);
    354         if (status != OK) {
    355             return status;
    356         }
    357 
    358         return reply.readInt32();
    359     }
    360 
    361     virtual status_t removeSecureStop(Vector<uint8_t> const &ssid) {
    362         Parcel data, reply;
    363         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    364 
    365         writeVector(data, ssid);
    366         status_t status = remote()->transact(REMOVE_SECURE_STOP, data, &reply);
    367         if (status != OK) {
    368             return status;
    369         }
    370 
    371         return reply.readInt32();
    372     }
    373 
    374     virtual status_t removeAllSecureStops() {
    375         Parcel data, reply;
    376         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    377 
    378         status_t status = remote()->transact(REMOVE_ALL_SECURE_STOPS, data, &reply);
    379         if (status != OK) {
    380             return status;
    381         }
    382 
    383         return reply.readInt32();
    384     }
    385 
    386     virtual status_t getOfflineLicenseKeySetIds(List<Vector<uint8_t> > &keySetIds) const {
    387         Parcel data, reply;
    388         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    389 
    390         status_t status = remote()->transact(GET_OFFLINE_LICENSE_KEYSET_IDS, data, &reply);
    391         if (status != OK) {
    392             return status;
    393         }
    394 
    395         keySetIds.clear();
    396         uint32_t count = reply.readInt32();
    397         for (size_t i = 0; i < count; i++) {
    398             Vector<uint8_t> keySetId;
    399             readVector(reply, keySetId);
    400             keySetIds.push_back(keySetId);
    401         }
    402         return reply.readInt32();
    403     }
    404 
    405     virtual status_t removeOfflineLicense(Vector<uint8_t> const &keySetId) {
    406         Parcel data, reply;
    407         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    408 
    409         writeVector(data, keySetId);
    410         status_t status = remote()->transact(REMOVE_OFFLINE_LICENSE, data, &reply);
    411         if (status != OK) {
    412             return status;
    413         }
    414         return reply.readInt32();
    415     }
    416 
    417     virtual status_t getOfflineLicenseState(Vector<uint8_t> const &keySetId,
    418             DrmPlugin::OfflineLicenseState *licenseState) const {
    419         Parcel data, reply;
    420         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    421 
    422         writeVector(data, keySetId);
    423         status_t status = remote()->transact(GET_OFFLINE_LICENSE_STATE, data, &reply);
    424         if (status != OK) {
    425             *licenseState = DrmPlugin::OfflineLicenseState::kOfflineLicenseStateUnknown;
    426             return status;
    427         }
    428         *licenseState = static_cast<DrmPlugin::OfflineLicenseState>(reply.readInt32());
    429         return reply.readInt32();
    430     }
    431 
    432     virtual status_t getPropertyString(String8 const &name, String8 &value) const {
    433         Parcel data, reply;
    434         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    435 
    436         data.writeString8(name);
    437         status_t status = remote()->transact(GET_PROPERTY_STRING, data, &reply);
    438         if (status != OK) {
    439             return status;
    440         }
    441 
    442         value = reply.readString8();
    443         return reply.readInt32();
    444     }
    445 
    446     virtual status_t getHdcpLevels(DrmPlugin::HdcpLevel *connected,
    447             DrmPlugin::HdcpLevel *max) const {
    448         Parcel data, reply;
    449 
    450         if (connected == NULL || max == NULL) {
    451             return BAD_VALUE;
    452         }
    453 
    454         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    455 
    456         status_t status = remote()->transact(GET_HDCP_LEVELS, data, &reply);
    457         if (status != OK) {
    458             return status;
    459         }
    460 
    461         *connected = static_cast<DrmPlugin::HdcpLevel>(reply.readInt32());
    462         *max = static_cast<DrmPlugin::HdcpLevel>(reply.readInt32());
    463         return reply.readInt32();
    464     }
    465 
    466     virtual status_t getNumberOfSessions(uint32_t *open, uint32_t *max) const {
    467         Parcel data, reply;
    468 
    469         if (open == NULL || max == NULL) {
    470             return BAD_VALUE;
    471         }
    472 
    473         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    474 
    475         status_t status = remote()->transact(GET_NUMBER_OF_SESSIONS, data, &reply);
    476         if (status != OK) {
    477             return status;
    478         }
    479 
    480         *open = reply.readInt32();
    481         *max = reply.readInt32();
    482         return reply.readInt32();
    483     }
    484 
    485     virtual status_t getSecurityLevel(Vector<uint8_t> const &sessionId,
    486             DrmPlugin::SecurityLevel *level) const {
    487         Parcel data, reply;
    488 
    489         if (level == NULL) {
    490             return BAD_VALUE;
    491         }
    492 
    493         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    494 
    495         writeVector(data, sessionId);
    496         status_t status = remote()->transact(GET_SECURITY_LEVEL, data, &reply);
    497         if (status != OK) {
    498             return status;
    499         }
    500 
    501         *level = static_cast<DrmPlugin::SecurityLevel>(reply.readInt32());
    502         return reply.readInt32();
    503     }
    504 
    505     virtual status_t getPropertyByteArray(String8 const &name, Vector<uint8_t> &value) const {
    506         Parcel data, reply;
    507         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    508 
    509         data.writeString8(name);
    510         status_t status = remote()->transact(GET_PROPERTY_BYTE_ARRAY, data, &reply);
    511         if (status != OK) {
    512             return status;
    513         }
    514 
    515         readVector(reply, value);
    516         return reply.readInt32();
    517     }
    518 
    519     virtual status_t setPropertyString(String8 const &name, String8 const &value) const {
    520         Parcel data, reply;
    521         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    522 
    523         data.writeString8(name);
    524         data.writeString8(value);
    525         status_t status = remote()->transact(SET_PROPERTY_STRING, data, &reply);
    526         if (status != OK) {
    527             return status;
    528         }
    529 
    530         return reply.readInt32();
    531     }
    532 
    533     virtual status_t setPropertyByteArray(String8 const &name,
    534                                           Vector<uint8_t> const &value) const {
    535         Parcel data, reply;
    536         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    537 
    538         data.writeString8(name);
    539         writeVector(data, value);
    540         status_t status = remote()->transact(SET_PROPERTY_BYTE_ARRAY, data, &reply);
    541         if (status != OK) {
    542             return status;
    543         }
    544 
    545         return reply.readInt32();
    546     }
    547 
    548     virtual status_t getMetrics(os::PersistableBundle *metrics) {
    549         if (metrics == NULL) {
    550             return BAD_VALUE;
    551         }
    552         Parcel data, reply;
    553         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    554 
    555         status_t status = remote()->transact(GET_METRICS, data, &reply);
    556         if (status != OK) {
    557             return status;
    558         }
    559         // The reply data is ordered as
    560         // 1) 32 bit integer reply followed by
    561         // 2) Serialized PersistableBundle containing metrics.
    562         status_t reply_status;
    563         if (reply.readInt32(&reply_status) != OK
    564            || reply_status != OK) {
    565           ALOGE("Failed to read getMetrics response code from parcel. %d",
    566                 reply_status);
    567           return reply_status;
    568         }
    569 
    570         status = metrics->readFromParcel(&reply);
    571         if (status != OK) {
    572             ALOGE("Failed to read metrics from parcel. %d", status);
    573             return status;
    574         }
    575         return reply_status;
    576     }
    577 
    578     virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
    579                                         String8 const &algorithm) {
    580         Parcel data, reply;
    581         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    582 
    583         writeVector(data, sessionId);
    584         data.writeString8(algorithm);
    585         status_t status = remote()->transact(SET_CIPHER_ALGORITHM, data, &reply);
    586         if (status != OK) {
    587             return status;
    588         }
    589         return reply.readInt32();
    590     }
    591 
    592     virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
    593                                      String8 const &algorithm) {
    594         Parcel data, reply;
    595         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    596 
    597         writeVector(data, sessionId);
    598         data.writeString8(algorithm);
    599         status_t status = remote()->transact(SET_MAC_ALGORITHM, data, &reply);
    600         if (status != OK) {
    601             return status;
    602         }
    603         return reply.readInt32();
    604     }
    605 
    606     virtual status_t encrypt(Vector<uint8_t> const &sessionId,
    607                              Vector<uint8_t> const &keyId,
    608                              Vector<uint8_t> const &input,
    609                              Vector<uint8_t> const &iv,
    610                              Vector<uint8_t> &output) {
    611         Parcel data, reply;
    612         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    613 
    614         writeVector(data, sessionId);
    615         writeVector(data, keyId);
    616         writeVector(data, input);
    617         writeVector(data, iv);
    618 
    619         status_t status = remote()->transact(ENCRYPT, data, &reply);
    620         if (status != OK) {
    621             return status;
    622         }
    623         readVector(reply, output);
    624 
    625         return reply.readInt32();
    626     }
    627 
    628     virtual status_t decrypt(Vector<uint8_t> const &sessionId,
    629                              Vector<uint8_t> const &keyId,
    630                              Vector<uint8_t> const &input,
    631                              Vector<uint8_t> const &iv,
    632                              Vector<uint8_t> &output) {
    633         Parcel data, reply;
    634         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    635 
    636         writeVector(data, sessionId);
    637         writeVector(data, keyId);
    638         writeVector(data, input);
    639         writeVector(data, iv);
    640 
    641         status_t status = remote()->transact(DECRYPT, data, &reply);
    642         if (status != OK) {
    643             return status;
    644         }
    645         readVector(reply, output);
    646 
    647         return reply.readInt32();
    648     }
    649 
    650     virtual status_t sign(Vector<uint8_t> const &sessionId,
    651                           Vector<uint8_t> const &keyId,
    652                           Vector<uint8_t> const &message,
    653                           Vector<uint8_t> &signature) {
    654         Parcel data, reply;
    655         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    656 
    657         writeVector(data, sessionId);
    658         writeVector(data, keyId);
    659         writeVector(data, message);
    660 
    661         status_t status = remote()->transact(SIGN, data, &reply);
    662         if (status != OK) {
    663             return status;
    664         }
    665         readVector(reply, signature);
    666 
    667         return reply.readInt32();
    668     }
    669 
    670     virtual status_t verify(Vector<uint8_t> const &sessionId,
    671                             Vector<uint8_t> const &keyId,
    672                             Vector<uint8_t> const &message,
    673                             Vector<uint8_t> const &signature,
    674                             bool &match) {
    675         Parcel data, reply;
    676         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    677 
    678         writeVector(data, sessionId);
    679         writeVector(data, keyId);
    680         writeVector(data, message);
    681         writeVector(data, signature);
    682 
    683         status_t status = remote()->transact(VERIFY, data, &reply);
    684         if (status != OK) {
    685             return status;
    686         }
    687         match = (bool)reply.readInt32();
    688         return reply.readInt32();
    689     }
    690 
    691     virtual status_t signRSA(Vector<uint8_t> const &sessionId,
    692                              String8 const &algorithm,
    693                              Vector<uint8_t> const &message,
    694                              Vector<uint8_t> const &wrappedKey,
    695                              Vector<uint8_t> &signature) {
    696         Parcel data, reply;
    697         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    698 
    699         writeVector(data, sessionId);
    700         data.writeString8(algorithm);
    701         writeVector(data, message);
    702         writeVector(data, wrappedKey);
    703 
    704         status_t status = remote()->transact(SIGN_RSA, data, &reply);
    705         if (status != OK) {
    706             return status;
    707         }
    708         readVector(reply, signature);
    709 
    710         return reply.readInt32();
    711     }
    712 
    713     virtual status_t setListener(const sp<IDrmClient>& listener) {
    714         Parcel data, reply;
    715         data.writeInterfaceToken(IDrm::getInterfaceDescriptor());
    716         data.writeStrongBinder(IInterface::asBinder(listener));
    717         status_t status = remote()->transact(SET_LISTENER, data, &reply);
    718         if (status != OK) {
    719             return status;
    720         }
    721         return reply.readInt32();
    722     }
    723 
    724 private:
    725     void readVector(Parcel &reply, Vector<uint8_t> &vector) const {
    726         uint32_t size = reply.readInt32();
    727         vector.insertAt((size_t)0, size);
    728         reply.read(vector.editArray(), size);
    729     }
    730 
    731     void writeVector(Parcel &data, Vector<uint8_t> const &vector) const {
    732         data.writeInt32(vector.size());
    733         data.write(vector.array(), vector.size());
    734     }
    735 
    736     DISALLOW_EVIL_CONSTRUCTORS(BpDrm);
    737 };
    738 
    739 IMPLEMENT_META_INTERFACE(Drm, "android.drm.IDrm");
    740 
    741 ////////////////////////////////////////////////////////////////////////////////
    742 
    743 void BnDrm::readVector(const Parcel &data, Vector<uint8_t> &vector) const {
    744     uint32_t size = data.readInt32();
    745     if (vector.insertAt((size_t)0, size) < 0) {
    746         vector.clear();
    747     }
    748     if (data.read(vector.editArray(), size) != NO_ERROR) {
    749         vector.clear();
    750         android_errorWriteWithInfoLog(0x534e4554, "62872384", -1, NULL, 0);
    751     }
    752 }
    753 
    754 void BnDrm::writeVector(Parcel *reply, Vector<uint8_t> const &vector) const {
    755     reply->writeInt32(vector.size());
    756     reply->write(vector.array(), vector.size());
    757 }
    758 
    759 status_t BnDrm::onTransact(
    760     uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
    761     switch (code) {
    762         case INIT_CHECK:
    763         {
    764             CHECK_INTERFACE(IDrm, data, reply);
    765             reply->writeInt32(initCheck());
    766             return OK;
    767         }
    768 
    769         case IS_CRYPTO_SUPPORTED:
    770         {
    771             CHECK_INTERFACE(IDrm, data, reply);
    772             uint8_t uuid[16];
    773             data.read(uuid, sizeof(uuid));
    774             String8 mimeType = data.readString8();
    775             DrmPlugin::SecurityLevel level =
    776                     static_cast<DrmPlugin::SecurityLevel>(data.readInt32());
    777             bool isSupported = false;
    778             status_t result = isCryptoSchemeSupported(uuid, mimeType, level, &isSupported);
    779             reply->writeInt32(isSupported);
    780             reply->writeInt32(result);
    781             return OK;
    782         }
    783 
    784         case CREATE_PLUGIN:
    785         {
    786             CHECK_INTERFACE(IDrm, data, reply);
    787             uint8_t uuid[16];
    788             data.read(uuid, sizeof(uuid));
    789             String8 appPackageName = data.readString8();
    790             reply->writeInt32(createPlugin(uuid, appPackageName));
    791             return OK;
    792         }
    793 
    794         case DESTROY_PLUGIN:
    795         {
    796             CHECK_INTERFACE(IDrm, data, reply);
    797             reply->writeInt32(destroyPlugin());
    798             return OK;
    799         }
    800 
    801         case OPEN_SESSION:
    802         {
    803             CHECK_INTERFACE(IDrm, data, reply);
    804             DrmPlugin::SecurityLevel level =
    805                     static_cast<DrmPlugin::SecurityLevel>(data.readInt32());
    806             Vector<uint8_t> sessionId;
    807             status_t result = openSession(level, sessionId);
    808             writeVector(reply, sessionId);
    809             reply->writeInt32(result);
    810             return OK;
    811         }
    812 
    813         case CLOSE_SESSION:
    814         {
    815             CHECK_INTERFACE(IDrm, data, reply);
    816             Vector<uint8_t> sessionId;
    817             readVector(data, sessionId);
    818             reply->writeInt32(closeSession(sessionId));
    819             return OK;
    820         }
    821 
    822         case GET_KEY_REQUEST:
    823         {
    824             CHECK_INTERFACE(IDrm, data, reply);
    825             Vector<uint8_t> sessionId, initData;
    826 
    827             readVector(data, sessionId);
    828             readVector(data, initData);
    829             String8 mimeType = data.readString8();
    830             DrmPlugin::KeyType keyType = (DrmPlugin::KeyType)data.readInt32();
    831 
    832             KeyedVector<String8, String8> optionalParameters;
    833             uint32_t count = data.readInt32();
    834             for (size_t i = 0; i < count; ++i) {
    835                 String8 key, value;
    836                 key = data.readString8();
    837                 value = data.readString8();
    838                 optionalParameters.add(key, value);
    839             }
    840 
    841             Vector<uint8_t> request;
    842             String8 defaultUrl;
    843             DrmPlugin::KeyRequestType keyRequestType = DrmPlugin::kKeyRequestType_Unknown;
    844 
    845             status_t result = getKeyRequest(sessionId, initData, mimeType,
    846                     keyType, optionalParameters, request, defaultUrl,
    847                     &keyRequestType);
    848 
    849             writeVector(reply, request);
    850             reply->writeString8(defaultUrl);
    851             reply->writeInt32(static_cast<int32_t>(keyRequestType));
    852             reply->writeInt32(result);
    853             return OK;
    854         }
    855 
    856         case PROVIDE_KEY_RESPONSE:
    857         {
    858             CHECK_INTERFACE(IDrm, data, reply);
    859             Vector<uint8_t> sessionId, response, keySetId;
    860             readVector(data, sessionId);
    861             readVector(data, response);
    862             uint32_t result = provideKeyResponse(sessionId, response, keySetId);
    863             writeVector(reply, keySetId);
    864             reply->writeInt32(result);
    865             return OK;
    866         }
    867 
    868         case REMOVE_KEYS:
    869         {
    870             CHECK_INTERFACE(IDrm, data, reply);
    871             Vector<uint8_t> keySetId;
    872             readVector(data, keySetId);
    873             reply->writeInt32(removeKeys(keySetId));
    874             return OK;
    875         }
    876 
    877         case RESTORE_KEYS:
    878         {
    879             CHECK_INTERFACE(IDrm, data, reply);
    880             Vector<uint8_t> sessionId, keySetId;
    881             readVector(data, sessionId);
    882             readVector(data, keySetId);
    883             reply->writeInt32(restoreKeys(sessionId, keySetId));
    884             return OK;
    885         }
    886 
    887         case QUERY_KEY_STATUS:
    888         {
    889             CHECK_INTERFACE(IDrm, data, reply);
    890             Vector<uint8_t> sessionId;
    891             readVector(data, sessionId);
    892             KeyedVector<String8, String8> infoMap;
    893             status_t result = queryKeyStatus(sessionId, infoMap);
    894             size_t count = infoMap.size();
    895             reply->writeInt32(count);
    896             for (size_t i = 0; i < count; ++i) {
    897                 reply->writeString8(infoMap.keyAt(i));
    898                 reply->writeString8(infoMap.valueAt(i));
    899             }
    900             reply->writeInt32(result);
    901             return OK;
    902         }
    903 
    904         case GET_PROVISION_REQUEST:
    905         {
    906             CHECK_INTERFACE(IDrm, data, reply);
    907             String8 certType = data.readString8();
    908             String8 certAuthority = data.readString8();
    909 
    910             Vector<uint8_t> request;
    911             String8 defaultUrl;
    912             status_t result = getProvisionRequest(certType, certAuthority,
    913                                                   request, defaultUrl);
    914             writeVector(reply, request);
    915             reply->writeString8(defaultUrl);
    916             reply->writeInt32(result);
    917             return OK;
    918         }
    919 
    920         case PROVIDE_PROVISION_RESPONSE:
    921         {
    922             CHECK_INTERFACE(IDrm, data, reply);
    923             Vector<uint8_t> response;
    924             Vector<uint8_t> certificate;
    925             Vector<uint8_t> wrappedKey;
    926             readVector(data, response);
    927             status_t result = provideProvisionResponse(response, certificate, wrappedKey);
    928             writeVector(reply, certificate);
    929             writeVector(reply, wrappedKey);
    930             reply->writeInt32(result);
    931             return OK;
    932         }
    933 
    934         case GET_SECURE_STOPS:
    935         {
    936             CHECK_INTERFACE(IDrm, data, reply);
    937             List<Vector<uint8_t> > secureStops;
    938             status_t result = getSecureStops(secureStops);
    939             size_t count = secureStops.size();
    940             reply->writeInt32(count);
    941             List<Vector<uint8_t> >::iterator iter = secureStops.begin();
    942             while(iter != secureStops.end()) {
    943                 size_t size = iter->size();
    944                 reply->writeInt32(size);
    945                 reply->write(iter->array(), iter->size());
    946                 iter++;
    947             }
    948             reply->writeInt32(result);
    949             return OK;
    950         }
    951 
    952         case GET_SECURE_STOP_IDS:
    953         {
    954             CHECK_INTERFACE(IDrm, data, reply);
    955             List<Vector<uint8_t> > secureStopIds;
    956             status_t result = getSecureStopIds(secureStopIds);
    957             size_t count = secureStopIds.size();
    958             reply->writeInt32(count);
    959             List<Vector<uint8_t> >::iterator iter = secureStopIds.begin();
    960             while(iter != secureStopIds.end()) {
    961                 size_t size = iter->size();
    962                 reply->writeInt32(size);
    963                 reply->write(iter->array(), iter->size());
    964                 iter++;
    965             }
    966             reply->writeInt32(result);
    967             return OK;
    968         }
    969 
    970         case GET_SECURE_STOP:
    971         {
    972             CHECK_INTERFACE(IDrm, data, reply);
    973             Vector<uint8_t> ssid, secureStop;
    974             readVector(data, ssid);
    975             status_t result = getSecureStop(ssid, secureStop);
    976             writeVector(reply, secureStop);
    977             reply->writeInt32(result);
    978             return OK;
    979         }
    980 
    981         case RELEASE_SECURE_STOPS:
    982         {
    983             CHECK_INTERFACE(IDrm, data, reply);
    984             Vector<uint8_t> ssRelease;
    985             readVector(data, ssRelease);
    986             reply->writeInt32(releaseSecureStops(ssRelease));
    987             return OK;
    988         }
    989 
    990         case REMOVE_SECURE_STOP:
    991         {
    992             CHECK_INTERFACE(IDrm, data, reply);
    993             Vector<uint8_t> ssid;
    994             readVector(data, ssid);
    995             reply->writeInt32(removeSecureStop(ssid));
    996             return OK;
    997         }
    998 
    999         case REMOVE_ALL_SECURE_STOPS:
   1000         {
   1001             CHECK_INTERFACE(IDrm, data, reply);
   1002             reply->writeInt32(removeAllSecureStops());
   1003             return OK;
   1004         }
   1005 
   1006         case GET_HDCP_LEVELS:
   1007         {
   1008             CHECK_INTERFACE(IDrm, data, reply);
   1009             DrmPlugin::HdcpLevel connected = DrmPlugin::kHdcpLevelUnknown;
   1010             DrmPlugin::HdcpLevel max = DrmPlugin::kHdcpLevelUnknown;
   1011             status_t result = getHdcpLevels(&connected, &max);
   1012             reply->writeInt32(connected);
   1013             reply->writeInt32(max);
   1014             reply->writeInt32(result);
   1015             return OK;
   1016         }
   1017 
   1018         case GET_NUMBER_OF_SESSIONS:
   1019         {
   1020             CHECK_INTERFACE(IDrm, data, reply);
   1021             uint32_t open = 0, max = 0;
   1022             status_t result = getNumberOfSessions(&open, &max);
   1023             reply->writeInt32(open);
   1024             reply->writeInt32(max);
   1025             reply->writeInt32(result);
   1026             return OK;
   1027         }
   1028 
   1029         case GET_SECURITY_LEVEL:
   1030         {
   1031             CHECK_INTERFACE(IDrm, data, reply);
   1032             Vector<uint8_t> sessionId;
   1033             readVector(data, sessionId);
   1034             DrmPlugin::SecurityLevel level = DrmPlugin::kSecurityLevelUnknown;
   1035             status_t result = getSecurityLevel(sessionId, &level);
   1036             reply->writeInt32(level);
   1037             reply->writeInt32(result);
   1038             return OK;
   1039         }
   1040 
   1041         case GET_OFFLINE_LICENSE_KEYSET_IDS:
   1042         {
   1043             CHECK_INTERFACE(IDrm, data, reply);
   1044             List<Vector<uint8_t> > keySetIds;
   1045             status_t result = getOfflineLicenseKeySetIds(keySetIds);
   1046             size_t count = keySetIds.size();
   1047             reply->writeInt32(count);
   1048             List<Vector<uint8_t> >::iterator iter = keySetIds.begin();
   1049             while(iter != keySetIds.end()) {
   1050                 size_t size = iter->size();
   1051                 reply->writeInt32(size);
   1052                 reply->write(iter->array(), iter->size());
   1053                 iter++;
   1054             }
   1055             reply->writeInt32(result);
   1056             return OK;
   1057         }
   1058 
   1059         case REMOVE_OFFLINE_LICENSE:
   1060         {
   1061             CHECK_INTERFACE(IDrm, data, reply);
   1062             Vector<uint8_t> keySetId;
   1063             readVector(data, keySetId);
   1064             reply->writeInt32(removeOfflineLicense(keySetId));
   1065             return OK;
   1066         }
   1067 
   1068         case GET_OFFLINE_LICENSE_STATE:
   1069         {
   1070             CHECK_INTERFACE(IDrm, data, reply);
   1071             Vector<uint8_t> keySetId;
   1072             readVector(data, keySetId);
   1073             DrmPlugin::OfflineLicenseState state;
   1074             status_t result = getOfflineLicenseState(keySetId, &state);
   1075             reply->writeInt32(static_cast<DrmPlugin::OfflineLicenseState>(state));
   1076             reply->writeInt32(result);
   1077             return OK;
   1078         }
   1079 
   1080         case GET_PROPERTY_STRING:
   1081         {
   1082             CHECK_INTERFACE(IDrm, data, reply);
   1083             String8 name = data.readString8();
   1084             String8 value;
   1085             status_t result = getPropertyString(name, value);
   1086             reply->writeString8(value);
   1087             reply->writeInt32(result);
   1088             return OK;
   1089         }
   1090 
   1091         case GET_PROPERTY_BYTE_ARRAY:
   1092         {
   1093             CHECK_INTERFACE(IDrm, data, reply);
   1094             String8 name = data.readString8();
   1095             Vector<uint8_t> value;
   1096             status_t result = getPropertyByteArray(name, value);
   1097             writeVector(reply, value);
   1098             reply->writeInt32(result);
   1099             return OK;
   1100         }
   1101 
   1102         case SET_PROPERTY_STRING:
   1103         {
   1104             CHECK_INTERFACE(IDrm, data, reply);
   1105             String8 name = data.readString8();
   1106             String8 value = data.readString8();
   1107             reply->writeInt32(setPropertyString(name, value));
   1108             return OK;
   1109         }
   1110 
   1111         case SET_PROPERTY_BYTE_ARRAY:
   1112         {
   1113             CHECK_INTERFACE(IDrm, data, reply);
   1114             String8 name = data.readString8();
   1115             Vector<uint8_t> value;
   1116             readVector(data, value);
   1117             reply->writeInt32(setPropertyByteArray(name, value));
   1118             return OK;
   1119         }
   1120 
   1121         case GET_METRICS:
   1122         {
   1123             CHECK_INTERFACE(IDrm, data, reply);
   1124 
   1125             os::PersistableBundle metrics;
   1126             status_t result = getMetrics(&metrics);
   1127             // The reply data is ordered as
   1128             // 1) 32 bit integer reply followed by
   1129             // 2) Serialized PersistableBundle containing metrics.
   1130             // Only write the metrics if the getMetrics result was
   1131             // OK and we successfully added the status to reply.
   1132             status_t parcel_result = reply->writeInt32(result);
   1133             if (result == OK && parcel_result == OK) {
   1134                 parcel_result = metrics.writeToParcel(reply);
   1135             }
   1136             return parcel_result;
   1137         }
   1138 
   1139         case SET_CIPHER_ALGORITHM:
   1140         {
   1141             CHECK_INTERFACE(IDrm, data, reply);
   1142             Vector<uint8_t> sessionId;
   1143             readVector(data, sessionId);
   1144             String8 algorithm = data.readString8();
   1145             reply->writeInt32(setCipherAlgorithm(sessionId, algorithm));
   1146             return OK;
   1147         }
   1148 
   1149         case SET_MAC_ALGORITHM:
   1150         {
   1151             CHECK_INTERFACE(IDrm, data, reply);
   1152             Vector<uint8_t> sessionId;
   1153             readVector(data, sessionId);
   1154             String8 algorithm = data.readString8();
   1155             reply->writeInt32(setMacAlgorithm(sessionId, algorithm));
   1156             return OK;
   1157         }
   1158 
   1159         case ENCRYPT:
   1160         {
   1161             CHECK_INTERFACE(IDrm, data, reply);
   1162             Vector<uint8_t> sessionId, keyId, input, iv, output;
   1163             readVector(data, sessionId);
   1164             readVector(data, keyId);
   1165             readVector(data, input);
   1166             readVector(data, iv);
   1167             uint32_t result = encrypt(sessionId, keyId, input, iv, output);
   1168             writeVector(reply, output);
   1169             reply->writeInt32(result);
   1170             return OK;
   1171         }
   1172 
   1173         case DECRYPT:
   1174         {
   1175             CHECK_INTERFACE(IDrm, data, reply);
   1176             Vector<uint8_t> sessionId, keyId, input, iv, output;
   1177             readVector(data, sessionId);
   1178             readVector(data, keyId);
   1179             readVector(data, input);
   1180             readVector(data, iv);
   1181             uint32_t result = decrypt(sessionId, keyId, input, iv, output);
   1182             writeVector(reply, output);
   1183             reply->writeInt32(result);
   1184             return OK;
   1185         }
   1186 
   1187         case SIGN:
   1188         {
   1189             CHECK_INTERFACE(IDrm, data, reply);
   1190             Vector<uint8_t> sessionId, keyId, message, signature;
   1191             readVector(data, sessionId);
   1192             readVector(data, keyId);
   1193             readVector(data, message);
   1194             uint32_t result = sign(sessionId, keyId, message, signature);
   1195             writeVector(reply, signature);
   1196             reply->writeInt32(result);
   1197             return OK;
   1198         }
   1199 
   1200         case VERIFY:
   1201         {
   1202             CHECK_INTERFACE(IDrm, data, reply);
   1203             Vector<uint8_t> sessionId, keyId, message, signature;
   1204             readVector(data, sessionId);
   1205             readVector(data, keyId);
   1206             readVector(data, message);
   1207             readVector(data, signature);
   1208             bool match = false;
   1209             uint32_t result = verify(sessionId, keyId, message, signature, match);
   1210             reply->writeInt32(match);
   1211             reply->writeInt32(result);
   1212             return OK;
   1213         }
   1214 
   1215         case SIGN_RSA:
   1216         {
   1217             CHECK_INTERFACE(IDrm, data, reply);
   1218             Vector<uint8_t> sessionId, message, wrappedKey, signature;
   1219             readVector(data, sessionId);
   1220             String8 algorithm = data.readString8();
   1221             readVector(data, message);
   1222             readVector(data, wrappedKey);
   1223             uint32_t result = signRSA(sessionId, algorithm, message, wrappedKey, signature);
   1224             writeVector(reply, signature);
   1225             reply->writeInt32(result);
   1226             return OK;
   1227         }
   1228 
   1229     case SET_LISTENER: {
   1230         CHECK_INTERFACE(IDrm, data, reply);
   1231         sp<IDrmClient> listener =
   1232             interface_cast<IDrmClient>(data.readStrongBinder());
   1233         reply->writeInt32(setListener(listener));
   1234         return NO_ERROR;
   1235     } break;
   1236 
   1237     default:
   1238         return BBinder::onTransact(code, data, reply, flags);
   1239     }
   1240 }
   1241 
   1242 }  // namespace android
   1243