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