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 #include <utils/Mutex.h> 18 19 #include "drm/DrmAPI.h" 20 #include "hardware/CryptoAPI.h" 21 22 extern "C" { 23 android::DrmFactory *createDrmFactory(); 24 android::CryptoFactory *createCryptoFactory(); 25 } 26 27 namespace android { 28 29 class MockDrmFactory : public DrmFactory { 30 public: 31 MockDrmFactory() {} 32 virtual ~MockDrmFactory() {} 33 34 bool isCryptoSchemeSupported(const uint8_t uuid[16]); 35 status_t createDrmPlugin(const uint8_t uuid[16], DrmPlugin **plugin); 36 }; 37 38 class MockCryptoFactory : public CryptoFactory { 39 public: 40 MockCryptoFactory() {} 41 virtual ~MockCryptoFactory() {} 42 43 bool isCryptoSchemeSupported(const uint8_t uuid[16]) const; 44 status_t createPlugin( 45 const uint8_t uuid[16], const void *data, size_t size, 46 CryptoPlugin **plugin); 47 }; 48 49 50 51 class MockDrmPlugin : public DrmPlugin { 52 public: 53 MockDrmPlugin() {} 54 virtual ~MockDrmPlugin() {} 55 56 // from DrmPlugin 57 status_t openSession(Vector<uint8_t> &sessionId); 58 status_t closeSession(Vector<uint8_t> const &sessionId); 59 60 status_t getKeyRequest(Vector<uint8_t> const &sessionId, 61 Vector<uint8_t> const &initData, 62 String8 const &mimeType, KeyType keyType, 63 KeyedVector<String8, String8> const &optionalParameters, 64 Vector<uint8_t> &request, String8 &defaultUrl); 65 66 status_t provideKeyResponse(Vector<uint8_t> const &sessionId, 67 Vector<uint8_t> const &response, 68 Vector<uint8_t> &keySetId); 69 70 status_t removeKeys(Vector<uint8_t> const &keySetId); 71 72 status_t restoreKeys(Vector<uint8_t> const &sessionId, 73 Vector<uint8_t> const &keySetId); 74 75 status_t queryKeyStatus(Vector<uint8_t> const &sessionId, 76 KeyedVector<String8, String8> &infoMap) const; 77 78 status_t getProvisionRequest(Vector<uint8_t> &request, 79 String8 &defaultUrl); 80 81 status_t provideProvisionResponse(Vector<uint8_t> const &response); 82 83 status_t getSecureStops(List<Vector<uint8_t> > &secureStops); 84 status_t releaseSecureStops(Vector<uint8_t> const &ssRelease); 85 86 status_t getPropertyString(String8 const &name, String8 &value ) const; 87 status_t getPropertyByteArray(String8 const &name, 88 Vector<uint8_t> &value ) const; 89 90 status_t setPropertyString(String8 const &name, 91 String8 const &value ); 92 status_t setPropertyByteArray(String8 const &name, 93 Vector<uint8_t> const &value ); 94 95 status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId, 96 String8 const &algorithm); 97 98 status_t setMacAlgorithm(Vector<uint8_t> const &sessionId, 99 String8 const &algorithm); 100 101 status_t encrypt(Vector<uint8_t> const &sessionId, 102 Vector<uint8_t> const &keyId, 103 Vector<uint8_t> const &input, 104 Vector<uint8_t> const &iv, 105 Vector<uint8_t> &output); 106 107 status_t decrypt(Vector<uint8_t> const &sessionId, 108 Vector<uint8_t> const &keyId, 109 Vector<uint8_t> const &input, 110 Vector<uint8_t> const &iv, 111 Vector<uint8_t> &output); 112 113 status_t sign(Vector<uint8_t> const &sessionId, 114 Vector<uint8_t> const &keyId, 115 Vector<uint8_t> const &message, 116 Vector<uint8_t> &signature); 117 118 status_t verify(Vector<uint8_t> const &sessionId, 119 Vector<uint8_t> const &keyId, 120 Vector<uint8_t> const &message, 121 Vector<uint8_t> const &signature, 122 bool &match); 123 124 private: 125 String8 vectorToString(Vector<uint8_t> const &vector) const; 126 String8 arrayToString(uint8_t const *array, size_t len) const; 127 String8 stringMapToString(KeyedVector<String8, String8> map) const; 128 129 SortedVector<Vector<uint8_t> > mSessions; 130 SortedVector<Vector<uint8_t> > mKeySets; 131 132 static const ssize_t kNotFound = -1; 133 ssize_t findSession(Vector<uint8_t> const &sessionId) const; 134 ssize_t findKeySet(Vector<uint8_t> const &keySetId) const; 135 136 Mutex mLock; 137 KeyedVector<String8, String8> mStringProperties; 138 KeyedVector<String8, Vector<uint8_t> > mByteArrayProperties; 139 }; 140 141 142 class MockCryptoPlugin : public CryptoPlugin { 143 144 bool requiresSecureDecoderComponent(const char *mime) const; 145 146 ssize_t decrypt(bool secure, 147 const uint8_t key[16], const uint8_t iv[16], 148 Mode mode, const void *srcPtr, 149 const SubSample *subSamples, size_t numSubSamples, 150 void *dstPtr, AString *errorDetailMsg); 151 private: 152 String8 subSamplesToString(CryptoPlugin::SubSample const *subSamples, size_t numSubSamples) const; 153 String8 arrayToString(uint8_t const *array, size_t len) const; 154 }; 155 }; 156