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 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: 41 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: 54 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 67 status_t provideKeyResponse(Vector<uint8_t> const &sessionId, 68 Vector<uint8_t> const &response, 69 Vector<uint8_t> &keySetId); 70 71 status_t removeKeys(Vector<uint8_t> const &keySetId); 72 73 status_t restoreKeys(Vector<uint8_t> const &sessionId, 74 Vector<uint8_t> const &keySetId); 75 76 status_t queryKeyStatus(Vector<uint8_t> const &sessionId, 77 KeyedVector<String8, String8> &infoMap) const; 78 79 status_t getProvisionRequest(Vector<uint8_t> &request, 80 String8 &defaultUrl); 81 82 status_t provideProvisionResponse(Vector<uint8_t> const &response); 83 84 status_t getSecureStops(List<Vector<uint8_t> > &secureStops); 85 status_t releaseSecureStops(Vector<uint8_t> const &ssRelease); 86 87 status_t getPropertyString(String8 const &name, String8 &value ) const; 88 status_t getPropertyByteArray(String8 const &name, 89 Vector<uint8_t> &value ) const; 90 91 status_t setPropertyString(String8 const &name, 92 String8 const &value ); 93 status_t setPropertyByteArray(String8 const &name, 94 Vector<uint8_t> const &value ); 95 96 status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId, 97 String8 const &algorithm); 98 99 status_t setMacAlgorithm(Vector<uint8_t> const &sessionId, 100 String8 const &algorithm); 101 102 status_t encrypt(Vector<uint8_t> const &sessionId, 103 Vector<uint8_t> const &keyId, 104 Vector<uint8_t> const &input, 105 Vector<uint8_t> const &iv, 106 Vector<uint8_t> &output); 107 108 status_t decrypt(Vector<uint8_t> const &sessionId, 109 Vector<uint8_t> const &keyId, 110 Vector<uint8_t> const &input, 111 Vector<uint8_t> const &iv, 112 Vector<uint8_t> &output); 113 114 status_t sign(Vector<uint8_t> const &sessionId, 115 Vector<uint8_t> const &keyId, 116 Vector<uint8_t> const &message, 117 Vector<uint8_t> &signature); 118 119 status_t verify(Vector<uint8_t> const &sessionId, 120 Vector<uint8_t> const &keyId, 121 Vector<uint8_t> const &message, 122 Vector<uint8_t> const &signature, 123 bool &match); 124 125 private: 126 String8 vectorToString(Vector<uint8_t> const &vector) const; 127 String8 arrayToString(uint8_t const *array, size_t len) const; 128 String8 stringMapToString(KeyedVector<String8, String8> map) const; 129 130 SortedVector<Vector<uint8_t> > mSessions; 131 SortedVector<Vector<uint8_t> > mKeySets; 132 133 static const ssize_t kNotFound = -1; 134 ssize_t findSession(Vector<uint8_t> const &sessionId) const; 135 ssize_t findKeySet(Vector<uint8_t> const &keySetId) const; 136 137 Mutex mLock; 138 KeyedVector<String8, String8> mStringProperties; 139 KeyedVector<String8, Vector<uint8_t> > mByteArrayProperties; 140 }; 141 142 143 class MockCryptoPlugin : public CryptoPlugin { 144 145 bool requiresSecureDecoderComponent(const char *mime) const; 146 147 ssize_t decrypt(bool secure, 148 const uint8_t key[16], const uint8_t iv[16], 149 Mode mode, const void *srcPtr, 150 const SubSample *subSamples, size_t numSubSamples, 151 void *dstPtr, AString *errorDetailMsg); 152 private: 153 String8 subSamplesToString(CryptoPlugin::SubSample const *subSamples, size_t numSubSamples) const; 154 String8 arrayToString(uint8_t const *array, size_t len) const; 155 }; 156 }; 157